Class CodedOutputStream
Encodes and writes protocol message fields.
Inheritance
Implements
Inherited Members
Namespace: Google.Protobuf
Assembly: Google.Protobuf.dll
Syntax
public sealed class CodedOutputStream : IDisposable
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(Byte[])
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.
Declaration
public CodedOutputStream(byte[] flatArray)
Parameters
Type | Name | Description |
---|---|---|
System.Byte[] | flatArray |
CodedOutputStream(Stream)
Creates a new CodedOutputStream which write to the given stream, and disposes of that
stream when the returned CodedOutputStream
is disposed.
Declaration
public CodedOutputStream(Stream output)
Parameters
Type | Name | Description |
---|---|---|
System.IO.Stream | output | The stream to write to. It will be disposed when the returned |
CodedOutputStream(Stream, Boolean)
Creates a new CodedOutputStream which write to the given stream.
Declaration
public CodedOutputStream(Stream output, bool leaveOpen)
Parameters
Type | Name | Description |
---|---|---|
System.IO.Stream | output | The stream to write to. |
System.Boolean | leaveOpen | If |
CodedOutputStream(Stream, Int32)
Creates a new CodedOutputStream which write to the given stream and uses the specified buffer size.
Declaration
public CodedOutputStream(Stream output, int bufferSize)
Parameters
Type | Name | Description |
---|---|---|
System.IO.Stream | output | The stream to write to. It will be disposed when the returned |
System.Int32 | bufferSize | The size of buffer to use internally. |
CodedOutputStream(Stream, Int32, Boolean)
Creates a new CodedOutputStream which write to the given stream and uses the specified buffer size.
Declaration
public CodedOutputStream(Stream output, int bufferSize, bool leaveOpen)
Parameters
Type | Name | Description |
---|---|---|
System.IO.Stream | output | The stream to write to. |
System.Int32 | bufferSize | The size of buffer to use internally. |
System.Boolean | leaveOpen | If |
Fields
DefaultBufferSize
The buffer size used by CreateInstance(Stream).
Declaration
public static readonly int DefaultBufferSize
Field Value
Type | Description |
---|---|
System.Int32 |
Properties
Position
Returns the current position in the stream, or the position in the output buffer
Declaration
public long Position { get; }
Property Value
Type | Description |
---|---|
System.Int64 |
SpaceLeft
If writing to a flat array, returns the space left in the array. Otherwise, throws an InvalidOperationException.
Declaration
public int SpaceLeft { get; }
Property Value
Type | Description |
---|---|
System.Int32 |
Methods
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 bugs.
Declaration
public void CheckNoSpaceLeft()
ComputeBoolSize(Boolean)
Computes the number of bytes that would be needed to encode a bool field, including the tag.
Declaration
public static int ComputeBoolSize(bool value)
Parameters
Type | Name | Description |
---|---|---|
System.Boolean | value |
Returns
Type | Description |
---|---|
System.Int32 |
ComputeBytesSize(ByteString)
Computes the number of bytes that would be needed to encode a bytes field, including the tag.
Declaration
public static int ComputeBytesSize(ByteString value)
Parameters
Type | Name | Description |
---|---|---|
ByteString | value |
Returns
Type | Description |
---|---|
System.Int32 |
ComputeDoubleSize(Double)
Computes the number of bytes that would be needed to encode a double field, including the tag.
Declaration
public static int ComputeDoubleSize(double value)
Parameters
Type | Name | Description |
---|---|---|
System.Double | value |
Returns
Type | Description |
---|---|
System.Int32 |
ComputeEnumSize(Int32)
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.
Declaration
public static int ComputeEnumSize(int value)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | value |
Returns
Type | Description |
---|---|
System.Int32 |
ComputeFixed32Size(UInt32)
Computes the number of bytes that would be needed to encode a fixed32 field, including the tag.
Declaration
public static int ComputeFixed32Size(uint value)
Parameters
Type | Name | Description |
---|---|---|
System.UInt32 | value |
Returns
Type | Description |
---|---|
System.Int32 |
ComputeFixed64Size(UInt64)
Computes the number of bytes that would be needed to encode a fixed64 field, including the tag.
Declaration
public static int ComputeFixed64Size(ulong value)
Parameters
Type | Name | Description |
---|---|---|
System.UInt64 | value |
Returns
Type | Description |
---|---|
System.Int32 |
ComputeFloatSize(Single)
Computes the number of bytes that would be needed to encode a float field, including the tag.
Declaration
public static int ComputeFloatSize(float value)
Parameters
Type | Name | Description |
---|---|---|
System.Single | value |
Returns
Type | Description |
---|---|
System.Int32 |
ComputeGroupSize(IMessage)
Computes the number of bytes that would be needed to encode a group field, including the tag.
Declaration
public static int ComputeGroupSize(IMessage value)
Parameters
Type | Name | Description |
---|---|---|
IMessage | value |
Returns
Type | Description |
---|---|
System.Int32 |
ComputeInt32Size(Int32)
Computes the number of bytes that would be needed to encode an int32 field, including the tag.
Declaration
public static int ComputeInt32Size(int value)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | value |
Returns
Type | Description |
---|---|
System.Int32 |
ComputeInt64Size(Int64)
Computes the number of bytes that would be needed to encode an int64 field, including the tag.
Declaration
public static int ComputeInt64Size(long value)
Parameters
Type | Name | Description |
---|---|---|
System.Int64 | value |
Returns
Type | Description |
---|---|
System.Int32 |
ComputeLengthSize(Int32)
Computes the number of bytes that would be needed to encode a length, as written by WriteLength(Int32).
Declaration
public static int ComputeLengthSize(int length)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | length |
Returns
Type | Description |
---|---|
System.Int32 |
ComputeMessageSize(IMessage)
Computes the number of bytes that would be needed to encode an embedded message field, including the tag.
Declaration
public static int ComputeMessageSize(IMessage value)
Parameters
Type | Name | Description |
---|---|---|
IMessage | value |
Returns
Type | Description |
---|---|
System.Int32 |
ComputeRawVarint32Size(UInt32)
Computes the number of bytes that would be needed to encode a varint.
Declaration
public static int ComputeRawVarint32Size(uint value)
Parameters
Type | Name | Description |
---|---|---|
System.UInt32 | value |
Returns
Type | Description |
---|---|
System.Int32 |
ComputeRawVarint64Size(UInt64)
Computes the number of bytes that would be needed to encode a varint.
Declaration
public static int ComputeRawVarint64Size(ulong value)
Parameters
Type | Name | Description |
---|---|---|
System.UInt64 | value |
Returns
Type | Description |
---|---|
System.Int32 |
ComputeSFixed32Size(Int32)
Computes the number of bytes that would be needed to encode an sfixed32 field, including the tag.
Declaration
public static int ComputeSFixed32Size(int value)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | value |
Returns
Type | Description |
---|---|
System.Int32 |
ComputeSFixed64Size(Int64)
Computes the number of bytes that would be needed to encode an sfixed64 field, including the tag.
Declaration
public static int ComputeSFixed64Size(long value)
Parameters
Type | Name | Description |
---|---|---|
System.Int64 | value |
Returns
Type | Description |
---|---|
System.Int32 |
ComputeSInt32Size(Int32)
Computes the number of bytes that would be needed to encode an sint32 field, including the tag.
Declaration
public static int ComputeSInt32Size(int value)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | value |
Returns
Type | Description |
---|---|
System.Int32 |
ComputeSInt64Size(Int64)
Computes the number of bytes that would be needed to encode an sint64 field, including the tag.
Declaration
public static int ComputeSInt64Size(long value)
Parameters
Type | Name | Description |
---|---|---|
System.Int64 | value |
Returns
Type | Description |
---|---|
System.Int32 |
ComputeStringSize(String)
Computes the number of bytes that would be needed to encode a string field, including the tag.
Declaration
public static int ComputeStringSize(string value)
Parameters
Type | Name | Description |
---|---|---|
System.String | value |
Returns
Type | Description |
---|---|
System.Int32 |
ComputeTagSize(Int32)
Computes the number of bytes that would be needed to encode a tag.
Declaration
public static int ComputeTagSize(int fieldNumber)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | fieldNumber |
Returns
Type | Description |
---|---|
System.Int32 |
ComputeUInt32Size(UInt32)
Computes the number of bytes that would be needed to encode a uint32 field, including the tag.
Declaration
public static int ComputeUInt32Size(uint value)
Parameters
Type | Name | Description |
---|---|---|
System.UInt32 | value |
Returns
Type | Description |
---|---|
System.Int32 |
ComputeUInt64Size(UInt64)
Computes the number of bytes that would be needed to encode a uint64 field, including the tag.
Declaration
public static int ComputeUInt64Size(ulong value)
Parameters
Type | Name | Description |
---|---|---|
System.UInt64 | value |
Returns
Type | Description |
---|---|
System.Int32 |
Dispose()
Flushes any buffered data and optionally closes the underlying stream, if any.
Declaration
public void Dispose()
Implements
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()
Flushes any buffered data to the underlying stream (if there is one).
Declaration
public void Flush()
WriteBool(Boolean)
Writes a bool field value, without a tag, to the stream.
Declaration
public void WriteBool(bool value)
Parameters
Type | Name | Description |
---|---|---|
System.Boolean | value | The value to write |
WriteBytes(ByteString)
Write a byte string, without a tag, to the stream. The data is length-prefixed.
Declaration
public void WriteBytes(ByteString value)
Parameters
Type | Name | Description |
---|---|---|
ByteString | value | The value to write |
WriteDouble(Double)
Writes a double field value, without a tag, to the stream.
Declaration
public void WriteDouble(double value)
Parameters
Type | Name | Description |
---|---|---|
System.Double | value | The value to write |
WriteEnum(Int32)
Writes an enum value, without a tag, to the stream.
Declaration
public void WriteEnum(int value)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | value | The value to write |
WriteFixed32(UInt32)
Writes a fixed32 field value, without a tag, to the stream.
Declaration
public void WriteFixed32(uint value)
Parameters
Type | Name | Description |
---|---|---|
System.UInt32 | value | The value to write |
WriteFixed64(UInt64)
Writes a fixed64 field value, without a tag, to the stream.
Declaration
public void WriteFixed64(ulong value)
Parameters
Type | Name | Description |
---|---|---|
System.UInt64 | value | The value to write |
WriteFloat(Single)
Writes a float field value, without a tag, to the stream.
Declaration
public void WriteFloat(float value)
Parameters
Type | Name | Description |
---|---|---|
System.Single | value | The value to write |
WriteGroup(IMessage)
Writes a group, without a tag, to the stream.
Declaration
public void WriteGroup(IMessage value)
Parameters
Type | Name | Description |
---|---|---|
IMessage | value | The value to write |
WriteInt32(Int32)
Writes an int32 field value, without a tag, to the stream.
Declaration
public void WriteInt32(int value)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | value | The value to write |
WriteInt64(Int64)
Writes an int64 field value, without a tag, to the stream.
Declaration
public void WriteInt64(long value)
Parameters
Type | Name | Description |
---|---|---|
System.Int64 | value | The value to write |
WriteLength(Int32)
Writes a length (in bytes) for length-delimited data.
Declaration
public void WriteLength(int length)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | length | Length value, in bytes. |
Remarks
This method simply writes a rawint, but exists for clarity in calling code.
WriteMessage(IMessage)
Writes a message, without a tag, to the stream. The data is length-prefixed.
Declaration
public void WriteMessage(IMessage value)
Parameters
Type | Name | Description |
---|---|---|
IMessage | value | The value to write |
WriteRawTag(Byte)
Writes the given single-byte tag directly to the stream.
Declaration
public void WriteRawTag(byte b1)
Parameters
Type | Name | Description |
---|---|---|
System.Byte | b1 | The encoded tag |
WriteRawTag(Byte, Byte)
Writes the given two-byte tag directly to the stream.
Declaration
public void WriteRawTag(byte b1, byte b2)
Parameters
Type | Name | Description |
---|---|---|
System.Byte | b1 | The first byte of the encoded tag |
System.Byte | b2 | The second byte of the encoded tag |
WriteRawTag(Byte, Byte, Byte)
Writes the given three-byte tag directly to the stream.
Declaration
public void WriteRawTag(byte b1, byte b2, byte b3)
Parameters
Type | Name | Description |
---|---|---|
System.Byte | b1 | The first byte of the encoded tag |
System.Byte | b2 | The second byte of the encoded tag |
System.Byte | b3 | The third byte of the encoded tag |
WriteRawTag(Byte, Byte, Byte, Byte)
Writes the given four-byte tag directly to the stream.
Declaration
public void WriteRawTag(byte b1, byte b2, byte b3, byte b4)
Parameters
Type | Name | Description |
---|---|---|
System.Byte | b1 | The first byte of the encoded tag |
System.Byte | b2 | The second byte of the encoded tag |
System.Byte | b3 | The third byte of the encoded tag |
System.Byte | b4 | The fourth byte of the encoded tag |
WriteRawTag(Byte, Byte, Byte, Byte, Byte)
Writes the given five-byte tag directly to the stream.
Declaration
public void WriteRawTag(byte b1, byte b2, byte b3, byte b4, byte b5)
Parameters
Type | Name | Description |
---|---|---|
System.Byte | b1 | The first byte of the encoded tag |
System.Byte | b2 | The second byte of the encoded tag |
System.Byte | b3 | The third byte of the encoded tag |
System.Byte | b4 | The fourth byte of the encoded tag |
System.Byte | b5 | The fifth byte of the encoded tag |
WriteSFixed32(Int32)
Writes an sfixed32 value, without a tag, to the stream.
Declaration
public void WriteSFixed32(int value)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | value | The value to write. |
WriteSFixed64(Int64)
Writes an sfixed64 value, without a tag, to the stream.
Declaration
public void WriteSFixed64(long value)
Parameters
Type | Name | Description |
---|---|---|
System.Int64 | value | The value to write |
WriteSInt32(Int32)
Writes an sint32 value, without a tag, to the stream.
Declaration
public void WriteSInt32(int value)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | value | The value to write |
WriteSInt64(Int64)
Writes an sint64 value, without a tag, to the stream.
Declaration
public void WriteSInt64(long value)
Parameters
Type | Name | Description |
---|---|---|
System.Int64 | value | The value to write |
WriteString(String)
Writes a string field value, without a tag, to the stream. The data is length-prefixed.
Declaration
public void WriteString(string value)
Parameters
Type | Name | Description |
---|---|---|
System.String | value | The value to write |
WriteTag(Int32, WireFormat.WireType)
Encodes and writes a tag.
Declaration
public void WriteTag(int fieldNumber, WireFormat.WireType type)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | fieldNumber | The number of the field to write the tag for |
WireFormat.WireType | type | The wire format type of the tag to write |
WriteTag(UInt32)
Writes an already-encoded tag.
Declaration
public void WriteTag(uint tag)
Parameters
Type | Name | Description |
---|---|---|
System.UInt32 | tag | The encoded tag |
WriteUInt32(UInt32)
Writes a uint32 value, without a tag, to the stream.
Declaration
public void WriteUInt32(uint value)
Parameters
Type | Name | Description |
---|---|---|
System.UInt32 | value | The value to write |
WriteUInt64(UInt64)
Writes a uint64 field value, without a tag, to the stream.
Declaration
public void WriteUInt64(ulong value)
Parameters
Type | Name | Description |
---|---|---|
System.UInt64 | value | The value to write |