Show / Hide Table of Contents

Class CodedInputStream

Reads and decodes protocol message fields.

Inheritance
System.Object
CodedInputStream
Implements
System.IDisposable
Inherited Members
System.Object.ToString()
System.Object.Equals(System.Object)
System.Object.Equals(System.Object, System.Object)
System.Object.ReferenceEquals(System.Object, System.Object)
System.Object.GetHashCode()
System.Object.GetType()
System.Object.MemberwiseClone()
Namespace: Google.Protobuf
Assembly: Google.Protobuf.dll
Syntax
public sealed class CodedInputStream : IDisposable
Remarks

This class is generally used by generated code to read appropriate primitives from the stream. It effectively encapsulates the lowest levels of protocol buffer format.

Repeated fields and map fields are not handled by this class; use RepeatedField<T> and MapField<TKey, TValue> to serialize such fields.

Constructors

CodedInputStream(Byte[])

Creates a new CodedInputStream reading data from the given byte array.

Declaration
public CodedInputStream(byte[] buffer)
Parameters
Type Name Description
System.Byte[] buffer

CodedInputStream(Byte[], Int32, Int32)

Creates a new CodedInputStream that reads from the given byte array slice.

Declaration
public CodedInputStream(byte[] buffer, int offset, int length)
Parameters
Type Name Description
System.Byte[] buffer
System.Int32 offset
System.Int32 length

CodedInputStream(Stream)

Creates a new CodedInputStream reading data from the given stream, which will be disposed when the returned object is disposed.

Declaration
public CodedInputStream(Stream input)
Parameters
Type Name Description
System.IO.Stream input

The stream to read from.

CodedInputStream(Stream, Boolean)

Creates a new CodedInputStream reading data from the given stream.

Declaration
public CodedInputStream(Stream input, bool leaveOpen)
Parameters
Type Name Description
System.IO.Stream input

The stream to read from.

System.Boolean leaveOpen

true to leave input open when the returned is disposed; false to dispose of the given stream when the returned object is disposed.

Properties

IsAtEnd

Returns true if the stream has reached the end of the input. This is the case if either the end of the underlying input source has been reached or the stream has reached a limit created using PushLimit.

Declaration
public bool IsAtEnd { get; }
Property Value
Type Description
System.Boolean

Position

Returns the current position in the input stream, or the position in the input buffer

Declaration
public long Position { get; }
Property Value
Type Description
System.Int64

RecursionLimit

Returns the recursion limit for this stream. This limit is applied whilst reading messages, to avoid maliciously-recursive data.

Declaration
public int RecursionLimit { get; }
Property Value
Type Description
System.Int32

The recursion limit for this stream.

Remarks

The default limit is 100.

SizeLimit

Returns the size limit for this stream.

Declaration
public int SizeLimit { get; }
Property Value
Type Description
System.Int32

The size limit.

Remarks

This limit is applied when reading from the underlying stream, as a sanity check. It is not applied when reading from a byte array data source without an underlying stream. The default value is Int32.MaxValue.

Methods

CreateWithLimits(Stream, Int32, Int32)

Creates a CodedInputStream with the specified size and recursion limits, reading from an input stream.

Declaration
public static CodedInputStream CreateWithLimits(Stream input, int sizeLimit, int recursionLimit)
Parameters
Type Name Description
System.IO.Stream input

The input stream to read from

System.Int32 sizeLimit

The total limit of data to read from the stream.

System.Int32 recursionLimit

The maximum recursion depth to allow while reading.

Returns
Type Description
CodedInputStream

A CodedInputStream reading from input with the specified size and recursion limits.

Remarks

This method exists separately from the constructor to reduce the number of constructor overloads. It is likely to be used considerably less frequently than the constructors, as the default limits are suitable for most use cases.

Dispose()

Disposes of this instance, potentially closing any underlying stream.

Declaration
public void Dispose()
Implements
System.IDisposable.Dispose()
Remarks

As there is no flushing to perform here, disposing of a CodedInputStream which was constructed with the leaveOpen option parameter set to true (or one which was constructed to read from a byte array) has no effect.

MaybeConsumeTag(UInt32)

Peeks at the next tag in the stream. If it matches tag, the tag is consumed and the method returns true; otherwise, the stream is left in the original position and the method returns false.

Declaration
public bool MaybeConsumeTag(uint tag)
Parameters
Type Name Description
System.UInt32 tag
Returns
Type Description
System.Boolean

PeekTag()

Peeks at the next field tag. This is like calling ReadTag(), but the tag is not consumed. (So a subsequent call to ReadTag() will return the same value.)

Declaration
public uint PeekTag()
Returns
Type Description
System.UInt32

ReadBool()

Reads a bool field from the stream.

Declaration
public bool ReadBool()
Returns
Type Description
System.Boolean

ReadBytes()

Reads a bytes field value from the stream.

Declaration
public ByteString ReadBytes()
Returns
Type Description
ByteString

ReadDouble()

Reads a double field from the stream.

Declaration
public double ReadDouble()
Returns
Type Description
System.Double

ReadEnum()

Reads an enum field value from the stream.

Declaration
public int ReadEnum()
Returns
Type Description
System.Int32

ReadFixed32()

Reads a fixed32 field from the stream.

Declaration
public uint ReadFixed32()
Returns
Type Description
System.UInt32

ReadFixed64()

Reads a fixed64 field from the stream.

Declaration
public ulong ReadFixed64()
Returns
Type Description
System.UInt64

ReadFloat()

Reads a float field from the stream.

Declaration
public float ReadFloat()
Returns
Type Description
System.Single

ReadGroup(IMessage)

Reads an embedded group field from the stream.

Declaration
public void ReadGroup(IMessage builder)
Parameters
Type Name Description
IMessage builder

ReadInt32()

Reads an int32 field from the stream.

Declaration
public int ReadInt32()
Returns
Type Description
System.Int32

ReadInt64()

Reads an int64 field from the stream.

Declaration
public long ReadInt64()
Returns
Type Description
System.Int64

ReadLength()

Reads a length for length-delimited data.

Declaration
public int ReadLength()
Returns
Type Description
System.Int32
Remarks

This is internally just reading a varint, but this method exists to make the calling code clearer.

ReadMessage(IMessage)

Reads an embedded message field value from the stream.

Declaration
public void ReadMessage(IMessage builder)
Parameters
Type Name Description
IMessage builder

ReadSFixed32()

Reads an sfixed32 field value from the stream.

Declaration
public int ReadSFixed32()
Returns
Type Description
System.Int32

ReadSFixed64()

Reads an sfixed64 field value from the stream.

Declaration
public long ReadSFixed64()
Returns
Type Description
System.Int64

ReadSInt32()

Reads an sint32 field value from the stream.

Declaration
public int ReadSInt32()
Returns
Type Description
System.Int32

ReadSInt64()

Reads an sint64 field value from the stream.

Declaration
public long ReadSInt64()
Returns
Type Description
System.Int64

ReadString()

Reads a string field from the stream.

Declaration
public string ReadString()
Returns
Type Description
System.String

ReadTag()

Reads a field tag, returning the tag of 0 for "end of stream".

Declaration
public uint ReadTag()
Returns
Type Description
System.UInt32

The next field tag, or 0 for end of stream. (0 is never a valid tag.)

Remarks

If this method returns 0, it doesn't necessarily mean the end of all the data in this CodedInputStream; it may be the end of the logical stream for an embedded message, for example.

ReadUInt32()

Reads a uint32 field value from the stream.

Declaration
public uint ReadUInt32()
Returns
Type Description
System.UInt32

ReadUInt64()

Reads a uint64 field from the stream.

Declaration
public ulong ReadUInt64()
Returns
Type Description
System.UInt64

SkipLastField()

Skips the data for the field with the tag we've just read. This should be called directly after ReadTag(), when the caller wishes to skip an unknown field.

Declaration
public void SkipLastField()
Remarks

This method throws InvalidProtocolBufferException if the last-read tag was an end-group tag. If a caller wishes to skip a group, they should skip the whole group, by calling this method after reading the start-group tag. This behavior allows callers to call this method on any field they don't understand, correctly resulting in an error if an end-group tag has not been paired with an earlier start-group tag.

Exceptions
Type Condition
InvalidProtocolBufferException

The last tag was an end-group tag

System.InvalidOperationException

The last read operation read to the end of the logical stream

Back to top