Class CodedOutputStream (3.15.8)

Stay organized with collections Save and categorize content based on your preferences.
public sealed class CodedOutputStream : IDisposable

Encodes and writes protocol message fields.

Inheritance

Object > CodedOutputStream

Namespace

Google.Protobuf

Assembly

Google.Protobuf.dll

Remarks

This class is generally used by generated code to write appropriate primitives to the stream. It effectively encapsulates the lowest levels of protocol buffer format. Unlike some other implementations, this does not include combined "write tag and value" methods. Generated code knows the exact byte representations of the tags they're going to write, so there's no need to re-encode them each time. Manually-written code calling this class should just call one of the WriteTag overloads before each value.

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

Constructors

CodedOutputStream(Stream)

public CodedOutputStream(Stream output)

Creates a new CodedOutputStream which write to the given stream, and disposes of that stream when the returned CodedOutputStream is disposed.

Parameter
NameDescription
outputStream

The stream to write to. It will be disposed when the returned CodedOutputStream is disposed.

CodedOutputStream(Stream, Boolean)

public CodedOutputStream(Stream output, bool leaveOpen)

Creates a new CodedOutputStream which write to the given stream.

Parameters
NameDescription
outputStream

The stream to write to.

leaveOpenBoolean

If true, output is left open when the returned CodedOutputStream is disposed; if false, the provided stream is disposed as well.

CodedOutputStream(Stream, Int32)

public CodedOutputStream(Stream output, int bufferSize)

Creates a new CodedOutputStream which write to the given stream and uses the specified buffer size.

Parameters
NameDescription
outputStream

The stream to write to. It will be disposed when the returned CodedOutputStream is disposed.

bufferSizeInt32

The size of buffer to use internally.

CodedOutputStream(Stream, Int32, Boolean)

public CodedOutputStream(Stream output, int bufferSize, bool leaveOpen)

Creates a new CodedOutputStream which write to the given stream and uses the specified buffer size.

Parameters
NameDescription
outputStream

The stream to write to.

bufferSizeInt32

The size of buffer to use internally.

leaveOpenBoolean

If true, output is left open when the returned CodedOutputStream is disposed; if false, the provided stream is disposed as well.

CodedOutputStream(Byte[])

public CodedOutputStream(byte[] flatArray)

Creates a new CodedOutputStream that writes directly to the given byte array. If more bytes are written than fit in the array, OutOfSpaceException will be thrown.

Parameter
NameDescription
flatArrayByte[]

Fields

DefaultBufferSize

public static readonly int DefaultBufferSize

The buffer size used by CreateInstance(Stream).

Field Value
TypeDescription
Int32

Properties

Position

public long Position { get; }

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

Property Value
TypeDescription
Int64

SpaceLeft

public int SpaceLeft { get; }

If writing to a flat array, returns the space left in the array. Otherwise, throws an InvalidOperationException.

Property Value
TypeDescription
Int32

Methods

CheckNoSpaceLeft()

public void CheckNoSpaceLeft()

Verifies that SpaceLeft returns zero. It's common to create a byte array that is exactly big enough to hold a message, then write to it with a CodedOutputStream. Calling CheckNoSpaceLeft after writing verifies that the message was actually as big as expected, which can help finding bugs.

ComputeBoolSize(Boolean)

public static int ComputeBoolSize(bool value)

Computes the number of bytes that would be needed to encode a bool field, including the tag.

Parameter
NameDescription
valueBoolean
Returns
TypeDescription
Int32

ComputeBytesSize(ByteString)

public static int ComputeBytesSize(ByteString value)

Computes the number of bytes that would be needed to encode a bytes field, including the tag.

Parameter
NameDescription
valueByteString
Returns
TypeDescription
Int32

ComputeDoubleSize(Double)

public static int ComputeDoubleSize(double value)

Computes the number of bytes that would be needed to encode a double field, including the tag.

Parameter
NameDescription
valueDouble
Returns
TypeDescription
Int32

ComputeEnumSize(Int32)

public static int ComputeEnumSize(int value)

Computes the number of bytes that would be needed to encode a enum field, including the tag. The caller is responsible for converting the enum value to its numeric value.

Parameter
NameDescription
valueInt32
Returns
TypeDescription
Int32

ComputeFixed32Size(UInt32)

public static int ComputeFixed32Size(uint value)

Computes the number of bytes that would be needed to encode a fixed32 field, including the tag.

Parameter
NameDescription
valueUInt32
Returns
TypeDescription
Int32

ComputeFixed64Size(UInt64)

public static int ComputeFixed64Size(ulong value)

Computes the number of bytes that would be needed to encode a fixed64 field, including the tag.

Parameter
NameDescription
valueUInt64
Returns
TypeDescription
Int32

ComputeFloatSize(Single)

public static int ComputeFloatSize(float value)

Computes the number of bytes that would be needed to encode a float field, including the tag.

Parameter
NameDescription
valueSingle
Returns
TypeDescription
Int32

ComputeGroupSize(IMessage)

public static int ComputeGroupSize(IMessage value)

Computes the number of bytes that would be needed to encode a group field, including the tag.

Parameter
NameDescription
valueIMessage
Returns
TypeDescription
Int32

ComputeInt32Size(Int32)

public static int ComputeInt32Size(int value)

Computes the number of bytes that would be needed to encode an int32 field, including the tag.

Parameter
NameDescription
valueInt32
Returns
TypeDescription
Int32

ComputeInt64Size(Int64)

public static int ComputeInt64Size(long value)

Computes the number of bytes that would be needed to encode an int64 field, including the tag.

Parameter
NameDescription
valueInt64
Returns
TypeDescription
Int32

ComputeLengthSize(Int32)

public static int ComputeLengthSize(int length)

Computes the number of bytes that would be needed to encode a length, as written by WriteLength(Int32).

Parameter
NameDescription
lengthInt32
Returns
TypeDescription
Int32

ComputeMessageSize(IMessage)

public static int ComputeMessageSize(IMessage value)

Computes the number of bytes that would be needed to encode an embedded message field, including the tag.

Parameter
NameDescription
valueIMessage
Returns
TypeDescription
Int32

ComputeRawVarint32Size(UInt32)

public static int ComputeRawVarint32Size(uint value)

Computes the number of bytes that would be needed to encode a varint.

Parameter
NameDescription
valueUInt32
Returns
TypeDescription
Int32

ComputeRawVarint64Size(UInt64)

public static int ComputeRawVarint64Size(ulong value)

Computes the number of bytes that would be needed to encode a varint.

Parameter
NameDescription
valueUInt64
Returns
TypeDescription
Int32

ComputeSFixed32Size(Int32)

public static int ComputeSFixed32Size(int value)

Computes the number of bytes that would be needed to encode an sfixed32 field, including the tag.

Parameter
NameDescription
valueInt32
Returns
TypeDescription
Int32

ComputeSFixed64Size(Int64)

public static int ComputeSFixed64Size(long value)

Computes the number of bytes that would be needed to encode an sfixed64 field, including the tag.

Parameter
NameDescription
valueInt64
Returns
TypeDescription
Int32

ComputeSInt32Size(Int32)

public static int ComputeSInt32Size(int value)

Computes the number of bytes that would be needed to encode an sint32 field, including the tag.

Parameter
NameDescription
valueInt32
Returns
TypeDescription
Int32

ComputeSInt64Size(Int64)

public static int ComputeSInt64Size(long value)

Computes the number of bytes that would be needed to encode an sint64 field, including the tag.

Parameter
NameDescription
valueInt64
Returns
TypeDescription
Int32

ComputeStringSize(String)

public static int ComputeStringSize(String value)

Computes the number of bytes that would be needed to encode a string field, including the tag.

Parameter
NameDescription
valueString
Returns
TypeDescription
Int32

ComputeTagSize(Int32)

public static int ComputeTagSize(int fieldNumber)

Computes the number of bytes that would be needed to encode a tag.

Parameter
NameDescription
fieldNumberInt32
Returns
TypeDescription
Int32

ComputeUInt32Size(UInt32)

public static int ComputeUInt32Size(uint value)

Computes the number of bytes that would be needed to encode a uint32 field, including the tag.

Parameter
NameDescription
valueUInt32
Returns
TypeDescription
Int32

ComputeUInt64Size(UInt64)

public static int ComputeUInt64Size(ulong value)

Computes the number of bytes that would be needed to encode a uint64 field, including the tag.

Parameter
NameDescription
valueUInt64
Returns
TypeDescription
Int32

Dispose()

public void Dispose()

Flushes any buffered data and optionally closes the underlying stream, if any.

Remarks

By default, any underlying stream is closed by this method. To configure this behaviour, use a constructor overload with a leaveOpen parameter. If this instance does not have an underlying stream, this method does nothing.

For the sake of efficiency, calling this method does not prevent future write calls - but if a later write ends up writing to a stream which has been disposed, that is likely to fail. It is recommend that you not call any other methods after this.

Flush()

public void Flush()

Flushes any buffered data to the underlying stream (if there is one).

WriteBool(Boolean)

public void WriteBool(bool value)

Writes a bool field value, without a tag, to the stream.

Parameter
NameDescription
valueBoolean

The value to write

WriteBytes(ByteString)

public void WriteBytes(ByteString value)

Write a byte string, without a tag, to the stream. The data is length-prefixed.

Parameter
NameDescription
valueByteString

The value to write

WriteDouble(Double)

public void WriteDouble(double value)

Writes a double field value, without a tag, to the stream.

Parameter
NameDescription
valueDouble

The value to write

WriteEnum(Int32)

public void WriteEnum(int value)

Writes an enum value, without a tag, to the stream.

Parameter
NameDescription
valueInt32

The value to write

WriteFixed32(UInt32)

public void WriteFixed32(uint value)

Writes a fixed32 field value, without a tag, to the stream.

Parameter
NameDescription
valueUInt32

The value to write

WriteFixed64(UInt64)

public void WriteFixed64(ulong value)

Writes a fixed64 field value, without a tag, to the stream.

Parameter
NameDescription
valueUInt64

The value to write

WriteFloat(Single)

public void WriteFloat(float value)

Writes a float field value, without a tag, to the stream.

Parameter
NameDescription
valueSingle

The value to write

WriteGroup(IMessage)

public void WriteGroup(IMessage value)

Writes a group, without a tag, to the stream.

Parameter
NameDescription
valueIMessage

The value to write

WriteInt32(Int32)

public void WriteInt32(int value)

Writes an int32 field value, without a tag, to the stream.

Parameter
NameDescription
valueInt32

The value to write

WriteInt64(Int64)

public void WriteInt64(long value)

Writes an int64 field value, without a tag, to the stream.

Parameter
NameDescription
valueInt64

The value to write

WriteLength(Int32)

public void WriteLength(int length)

Writes a length (in bytes) for length-delimited data.

Parameter
NameDescription
lengthInt32

Length value, in bytes.

Remarks

This method simply writes a rawint, but exists for clarity in calling code.

WriteMessage(IMessage)

public void WriteMessage(IMessage value)

Writes a message, without a tag, to the stream. The data is length-prefixed.

Parameter
NameDescription
valueIMessage

The value to write

WriteRawMessage(IMessage)

public void WriteRawMessage(IMessage value)

Writes a message, without a tag, to the stream. Only the message data is written, without a length-delimiter.

Parameter
NameDescription
valueIMessage

The value to write

WriteRawTag(Byte)

public void WriteRawTag(byte b1)

Writes the given single-byte tag directly to the stream.

Parameter
NameDescription
b1Byte

The encoded tag

WriteRawTag(Byte, Byte)

public void WriteRawTag(byte b1, byte b2)

Writes the given two-byte tag directly to the stream.

Parameters
NameDescription
b1Byte

The first byte of the encoded tag

b2Byte

The second byte of the encoded tag

WriteRawTag(Byte, Byte, Byte)

public void WriteRawTag(byte b1, byte b2, byte b3)

Writes the given three-byte tag directly to the stream.

Parameters
NameDescription
b1Byte

The first byte of the encoded tag

b2Byte

The second byte of the encoded tag

b3Byte

The third byte of the encoded tag

WriteRawTag(Byte, Byte, Byte, Byte)

public void WriteRawTag(byte b1, byte b2, byte b3, byte b4)

Writes the given four-byte tag directly to the stream.

Parameters
NameDescription
b1Byte

The first byte of the encoded tag

b2Byte

The second byte of the encoded tag

b3Byte

The third byte of the encoded tag

b4Byte

The fourth byte of the encoded tag

WriteRawTag(Byte, Byte, Byte, Byte, Byte)

public void WriteRawTag(byte b1, byte b2, byte b3, byte b4, byte b5)

Writes the given five-byte tag directly to the stream.

Parameters
NameDescription
b1Byte

The first byte of the encoded tag

b2Byte

The second byte of the encoded tag

b3Byte

The third byte of the encoded tag

b4Byte

The fourth byte of the encoded tag

b5Byte

The fifth byte of the encoded tag

WriteSFixed32(Int32)

public void WriteSFixed32(int value)

Writes an sfixed32 value, without a tag, to the stream.

Parameter
NameDescription
valueInt32

The value to write.

WriteSFixed64(Int64)

public void WriteSFixed64(long value)

Writes an sfixed64 value, without a tag, to the stream.

Parameter
NameDescription
valueInt64

The value to write

WriteSInt32(Int32)

public void WriteSInt32(int value)

Writes an sint32 value, without a tag, to the stream.

Parameter
NameDescription
valueInt32

The value to write

WriteSInt64(Int64)

public void WriteSInt64(long value)

Writes an sint64 value, without a tag, to the stream.

Parameter
NameDescription
valueInt64

The value to write

WriteString(String)

public void WriteString(string value)

Writes a string field value, without a tag, to the stream. The data is length-prefixed.

Parameter
NameDescription
valueString

The value to write

WriteTag(Int32, WireFormat.WireType)

public void WriteTag(int fieldNumber, WireFormat.WireType type)

Encodes and writes a tag.

Parameters
NameDescription
fieldNumberInt32

The number of the field to write the tag for

typeWireFormat.WireType

The wire format type of the tag to write

WriteTag(UInt32)

public void WriteTag(uint tag)

Writes an already-encoded tag.

Parameter
NameDescription
tagUInt32

The encoded tag

WriteUInt32(UInt32)

public void WriteUInt32(uint value)

Writes a uint32 value, without a tag, to the stream.

Parameter
NameDescription
valueUInt32

The value to write

WriteUInt64(UInt64)

public void WriteUInt64(ulong value)

Writes a uint64 field value, without a tag, to the stream.

Parameter
NameDescription
valueUInt64

The value to write