# Class CodedInputStream (3.27.1)

<button class="button button-with-icon cloud-select-dropdown__button " aria-haspopup="true">Version latest</button>

- [3.27.1 (latest)](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.CodedInputStream)
- [3.23.0](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/3.23.0/Google.Protobuf.CodedInputStream)
- [3.15.8](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/3.15.8/Google.Protobuf.CodedInputStream)

    public sealed class CodedInputStream : IDisposable

Reads and decodes protocol message fields.

## Inheritance

[object](https://learn.microsoft.com/dotnet/api/system.object) \> CodedInputStream

## Implements

[IDisposable](https://learn.microsoft.com/dotnet/api/system.idisposable)

## Inherited Members

[object.Equals(object)](https://learn.microsoft.com/dotnet/api/system.object.equals#system-object-equals(system-object)) [object.Equals(object, object)](https://learn.microsoft.com/dotnet/api/system.object.equals#system-object-equals(system-object-system-object)) [object.GetHashCode()](https://learn.microsoft.com/dotnet/api/system.object.gethashcode) [object.GetType()](https://learn.microsoft.com/dotnet/api/system.object.gettype) [object.ReferenceEquals(object, object)](https://learn.microsoft.com/dotnet/api/system.object.referenceequals) [object.ToString()](https://learn.microsoft.com/dotnet/api/system.object.tostring)

## Namespace

[Google.Protobuf](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf)

## Assembly

Google.Protobuf.dll

## 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\>](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.Collections.RepeatedField-1)
and [MapField\<TKey, TValue\>](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.Collections.MapField-2) to serialize such fields.

## Constructors

### CodedInputStream(byte\[\])

    public CodedInputStream(byte[] buffer)

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

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `buffer` | `https://learn.microsoft.com/dotnet/api/system.byte` |

### CodedInputStream(byte\[\], int, int)

    public CodedInputStream(byte[] buffer, int offset, int length)

Creates a new [CodedInputStream](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.CodedInputStream) that reads from the given byte array slice.

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `buffer` | `https://learn.microsoft.com/dotnet/api/system.byte` |
| `offset` | `https://learn.microsoft.com/dotnet/api/system.int32` |
| `length` | `https://learn.microsoft.com/dotnet/api/system.int32` |

### CodedInputStream(Stream)

    public CodedInputStream(Stream input)

Creates a new [CodedInputStream](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.CodedInputStream) reading data from the given stream, which will be disposed
when the returned object is disposed.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `input` | `https://learn.microsoft.com/dotnet/api/system.io.stream` The stream to read from. |

### CodedInputStream(Stream, bool)

    public CodedInputStream(Stream input, bool leaveOpen)

Creates a new [CodedInputStream](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.CodedInputStream) reading data from the given stream.

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `input` | `https://learn.microsoft.com/dotnet/api/system.io.stream` The stream to read from. |
| `leaveOpen` | `https://learn.microsoft.com/dotnet/api/system.boolean` `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

    public bool IsAtEnd { get; }

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.

| **Property Value** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.boolean` |   |

### Position

    public long Position { get; }

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

| **Property Value** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.int64` |   |

### RecursionLimit

    public int RecursionLimit { get; }

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

| **Property Value** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.int32` | The recursion limit for this stream. |

**Remarks** The default limit is 100.

### SizeLimit

    public int SizeLimit { get; }

Returns the size limit for this stream.

| **Property Value** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/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, int, int)

    public static CodedInputStream CreateWithLimits(Stream input, int sizeLimit, int recursionLimit)

Creates a [CodedInputStream](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.CodedInputStream) with the specified size and recursion limits, reading
from an input stream.

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `input` | `https://learn.microsoft.com/dotnet/api/system.io.stream` The input stream to read from |
| `sizeLimit` | `https://learn.microsoft.com/dotnet/api/system.int32` The total limit of data to read from the stream. |
| `recursionLimit` | `https://learn.microsoft.com/dotnet/api/system.int32` The maximum recursion depth to allow while reading. |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.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()

    public void Dispose()

Disposes of this instance, potentially closing any underlying stream.
**Remarks** As there is no flushing to perform here, disposing of a [CodedInputStream](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.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(uint)

    public bool MaybeConsumeTag(uint tag)

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`.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `tag` | `https://learn.microsoft.com/dotnet/api/system.uint32` |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.boolean` |   |

### PeekTag()

    public uint PeekTag()

Peeks at the next field tag. This is like calling [ReadTag()](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.CodedInputStream#Google_Protobuf_CodedInputStream_ReadTag), but the
tag is not consumed. (So a subsequent call to [ReadTag()](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.CodedInputStream#Google_Protobuf_CodedInputStream_ReadTag) will return the
same value.)

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.uint32` |   |

### ReadBool()

    public bool ReadBool()

Reads a bool field from the stream.

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.boolean` |   |

### ReadBytes()

    public ByteString ReadBytes()

Reads a bytes field value from the stream.

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.ByteString` |   |

### ReadDouble()

    public double ReadDouble()

Reads a double field from the stream.

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.double` |   |

### ReadEnum()

    public int ReadEnum()

Reads an enum field value from the stream.

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.int32` |   |

### ReadFixed32()

    public uint ReadFixed32()

Reads a fixed32 field from the stream.

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.uint32` |   |

### ReadFixed64()

    public ulong ReadFixed64()

Reads a fixed64 field from the stream.

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.uint64` |   |

### ReadFloat()

    public float ReadFloat()

Reads a float field from the stream.

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.single` |   |

### ReadGroup(IMessage)

    public void ReadGroup(IMessage builder)

Reads an embedded group field from the stream.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `builder` | `https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.IMessage` |

### ReadInt32()

    public int ReadInt32()

Reads an int32 field from the stream.

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.int32` |   |

### ReadInt64()

    public long ReadInt64()

Reads an int64 field from the stream.

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.int64` |   |

### ReadLength()

    public int ReadLength()

Reads a length for length-delimited data.

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.int32` |   |

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

### ReadMessage(IMessage)

    public void ReadMessage(IMessage builder)

Reads an embedded message field value from the stream.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `builder` | `https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.IMessage` |

### ReadRawMessage(IMessage)

    public void ReadRawMessage(IMessage message)

Reads a top-level message or a nested message after the limits for this message have been pushed.
(parser will proceed until the end of the current limit)
NOTE: this method needs to be public because it's invoked by the generated code - e.g. msg.MergeFrom(CodedInputStream input) method

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `message` | `https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.IMessage` |

### ReadSFixed32()

    public int ReadSFixed32()

Reads an sfixed32 field value from the stream.

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.int32` |   |

### ReadSFixed64()

    public long ReadSFixed64()

Reads an sfixed64 field value from the stream.

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.int64` |   |

### ReadSInt32()

    public int ReadSInt32()

Reads an sint32 field value from the stream.

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.int32` |   |

### ReadSInt64()

    public long ReadSInt64()

Reads an sint64 field value from the stream.

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.int64` |   |

### ReadString()

    public string ReadString()

Reads a string field from the stream.

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.string` |   |

### ReadTag()

    public uint ReadTag()

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

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/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()

    public uint ReadUInt32()

Reads a uint32 field value from the stream.

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.uint32` |   |

### ReadUInt64()

    public ulong ReadUInt64()

Reads a uint64 field from the stream.

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.uint64` |   |

### SkipLastField()

    public void SkipLastField()

Skips the data for the field with the tag we've just read.
This should be called directly after [ReadTag()](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.CodedInputStream#Google_Protobuf_CodedInputStream_ReadTag), when
the caller wishes to skip an unknown field.
**Remarks** This method throws [InvalidProtocolBufferException](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.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** | **Description** |
| `https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.InvalidProtocolBufferException` | The last tag was an end-group tag |
| `https://learn.microsoft.com/dotnet/api/system.invalidoperationexception` | The last read operation read to the end of the logical stream |