GSF.IO.BinaryStreamBase.TryReadString C# (CSharp) Method

TryReadString() public method

Reads a string from the provided stream. If the size of the string exceedes maxLength value is set to null and this function returns false.
This method can be used to limit the byte size returned. Since an untrusted source could claim that the length is int.MaxValue, this prevents allocating 2GB of RAM to store the result.
public TryReadString ( int maxLength, string &value ) : bool
maxLength int The maximum number of characters in the string.
value string an output
return bool
        public bool TryReadString(int maxLength, out string value)
        {
            byte[] data;
            if (!TryReadBytes(maxLength * 6, out data))
            {
                value = null;
                return false;
            }
            value = Utf8.GetString(data);
            if (value.Length > maxLength)
            {
                value = null;
                return false;
            }
            return true;
        }