System.IO.BinaryReader.Read7BitEncodedInt C# (CSharp) Method

Read7BitEncodedInt() protected method

protected Read7BitEncodedInt ( ) : int
return int
        internal protected int Read7BitEncodedInt()
        {
            // Read out an Int32 7 bits at a time.  The high bit
            // of the byte when on means to continue reading more bytes.
            int count = 0;
            int shift = 0;
            byte b;
            do
            {
                // Check for a corrupted stream.  Read a max of 5 bytes.
                // In a future version, add a DataFormatException.
                if (shift == 5 * 7)  // 5 bytes max per Int32, shift += 7
                {
                    throw new FormatException(SR.Format_Bad7BitInt32);
                }

                // ReadByte handles end of stream cases for us.
                b = ReadByte();
                count |= (b & 0x7F) << shift;
                shift += 7;
            } while ((b & 0x80) != 0);
            return count;
        }
    }

Usage Example

Example #1
0
        public static string ReadStringUTF8(this BinaryReader reader)
        {
            int byteCount = reader.Read7BitEncodedInt();

            byte[] bytes = reader.ReadBytesRequired(byteCount);
            return(Encoding.UTF8.GetString(bytes));
        }
All Usage Examples Of System.IO.BinaryReader::Read7BitEncodedInt