Encodes and writes protocol message fields.
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
and MapField
to serialize such fields.
Constructors and Destructors |
|
---|---|
CodedOutputStream(byte[] flatArray)
Creates a new CodedOutputStream that writes directly to the given byte array.
|
|
CodedOutputStream(Stream output)
Creates a new CodedOutputStream which write to the given stream, and disposes of that stream when the returned
CodedOutputStream is disposed. |
|
CodedOutputStream(Stream output, int bufferSize)
Creates a new CodedOutputStream which write to the given stream and uses the specified buffer size.
|
|
CodedOutputStream(Stream output, bool leaveOpen)
Creates a new CodedOutputStream which write to the given stream.
|
|
CodedOutputStream(Stream output, int bufferSize, bool leaveOpen)
Creates a new CodedOutputStream which write to the given stream and uses the specified buffer size.
|
Public static attributes |
|
---|---|
DefaultBufferSize = 4096
|
readonly int
The buffer size used by CreateInstance(Stream).
|
Properties |
|
---|---|
Position
|
long
Returns the current position in the stream, or the position in the output buffer
|
SpaceLeft
|
int
If writing to a flat array, returns the space left in the array.
|
Public static functions |
|
---|---|
ComputeBoolSize(bool value)
|
int
Computes the number of bytes that would be needed to encode a bool field, including the tag.
|
ComputeBytesSize(ByteString value)
|
int
Computes the number of bytes that would be needed to encode a bytes field, including the tag.
|
ComputeDoubleSize(double value)
|
int
Computes the number of bytes that would be needed to encode a double field, including the tag.
|
ComputeEnumSize(int value)
|
int
Computes the number of bytes that would be needed to encode a enum field, including the tag.
|
ComputeFixed32Size(uint value)
|
int
Computes the number of bytes that would be needed to encode a fixed32 field, including the tag.
|
ComputeFixed64Size(ulong value)
|
int
Computes the number of bytes that would be needed to encode a fixed64 field, including the tag.
|
ComputeFloatSize(float value)
|
int
Computes the number of bytes that would be needed to encode a float field, including the tag.
|
ComputeGroupSize(IMessage value)
|
int
Computes the number of bytes that would be needed to encode a group field, including the tag.
|
ComputeInt32Size(int value)
|
int
Computes the number of bytes that would be needed to encode an int32 field, including the tag.
|
ComputeInt64Size(long value)
|
int
Computes the number of bytes that would be needed to encode an int64 field, including the tag.
|
ComputeLengthSize(int length)
|
int
Computes the number of bytes that would be needed to encode a length, as written by WriteLength.
|
ComputeMessageSize(IMessage value)
|
int
Computes the number of bytes that would be needed to encode an embedded message field, including the tag.
|
ComputeRawVarint32Size(uint value)
|
int
Computes the number of bytes that would be needed to encode a varint.
|
ComputeRawVarint64Size(ulong value)
|
int
Computes the number of bytes that would be needed to encode a varint.
|
ComputeSFixed32Size(int value)
|
int
Computes the number of bytes that would be needed to encode an sfixed32 field, including the tag.
|
ComputeSFixed64Size(long value)
|
int
Computes the number of bytes that would be needed to encode an sfixed64 field, including the tag.
|
ComputeSInt32Size(int value)
|
int
Computes the number of bytes that would be needed to encode an sint32 field, including the tag.
|
ComputeSInt64Size(long value)
|
int
Computes the number of bytes that would be needed to encode an sint64 field, including the tag.
|
ComputeStringSize(String value)
|
int
Computes the number of bytes that would be needed to encode a string field, including the tag.
|
ComputeTagSize(int fieldNumber)
|
int
Computes the number of bytes that would be needed to encode a tag.
|
ComputeUInt32Size(uint value)
|
int
Computes the number of bytes that would be needed to encode a uint32 field, including the tag.
|
ComputeUInt64Size(ulong value)
|
int
Computes the number of bytes that would be needed to encode a uint64 field, including the tag.
|
Public functions |
|
---|---|
CheckNoSpaceLeft()
|
void
Verifies that SpaceLeft returns zero.
|
Dispose()
|
void
Flushes any buffered data and optionally closes the underlying stream, if any.
|
Flush()
|
void
Flushes any buffered data to the underlying stream (if there is one).
|
WriteBool(bool value)
|
void
Writes a bool field value, without a tag, to the stream.
|
WriteBytes(ByteString value)
|
void
Write a byte string, without a tag, to the stream.
|
WriteDouble(double value)
|
void
Writes a double field value, without a tag, to the stream.
|
WriteEnum(int value)
|
void
Writes an enum value, without a tag, to the stream.
|
WriteFixed32(uint value)
|
void
Writes a fixed32 field value, without a tag, to the stream.
|
WriteFixed64(ulong value)
|
void
Writes a fixed64 field value, without a tag, to the stream.
|
WriteFloat(float value)
|
void
Writes a float field value, without a tag, to the stream.
|
WriteInt32(int value)
|
void
Writes an int32 field value, without a tag, to the stream.
|
WriteInt64(long value)
|
void
Writes an int64 field value, without a tag, to the stream.
|
WriteLength(int length)
|
void
Writes a length (in bytes) for length-delimited data.
|
WriteMessage(IMessage value)
|
void
Writes a message, without a tag, to the stream.
|
WriteRawTag(byte b1)
|
void
Writes the given single-byte tag directly to the stream.
|
WriteRawTag(byte b1, byte b2)
|
void
Writes the given two-byte tag directly to the stream.
|
WriteRawTag(byte b1, byte b2, byte b3)
|
void
Writes the given three-byte tag directly to the stream.
|
WriteRawTag(byte b1, byte b2, byte b3, byte b4)
|
void
Writes the given four-byte tag directly to the stream.
|
WriteRawTag(byte b1, byte b2, byte b3, byte b4, byte b5)
|
void
Writes the given five-byte tag directly to the stream.
|
WriteSFixed32(int value)
|
void
Writes an sfixed32 value, without a tag, to the stream.
|
WriteSFixed64(long value)
|
void
Writes an sfixed64 value, without a tag, to the stream.
|
WriteSInt32(int value)
|
void
Writes an sint32 value, without a tag, to the stream.
|
WriteSInt64(long value)
|
void
Writes an sint64 value, without a tag, to the stream.
|
WriteString(string value)
|
void
Writes a string field value, without a tag, to the stream.
|
WriteTag(int fieldNumber, WireFormat.WireType type)
|
void
Encodes and writes a tag.
|
WriteTag(uint tag)
|
void
Writes an already-encoded tag.
|
WriteUInt32(uint value)
|
void
Writes a uint32 value, without a tag, to the stream.
|
WriteUInt64(ulong value)
|
void
Writes a uint64 field value, without a tag, to the stream.
|
Classes |
|
---|---|
Google. |
Indicates that a CodedOutputStream wrapping a flat byte array ran out of space. |
readonly int DefaultBufferSize = 4096
The buffer size used by CreateInstance(Stream).
long Position
Returns the current position in the stream, or the position in the output buffer
int SpaceLeft
If writing to a flat array, returns the space left in the array.
Otherwise, throws an InvalidOperationException.
int ComputeBoolSize( bool value )
Computes the number of bytes that would be needed to encode a bool field, including the tag.
int ComputeBytesSize( ByteString value )
Computes the number of bytes that would be needed to encode a bytes field, including the tag.
int ComputeDoubleSize( double value )
Computes the number of bytes that would be needed to encode a double field, including the tag.
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.
int ComputeFixed32Size( uint value )
Computes the number of bytes that would be needed to encode a fixed32 field, including the tag.
int ComputeFixed64Size( ulong value )
Computes the number of bytes that would be needed to encode a fixed64 field, including the tag.
int ComputeFloatSize( float value )
Computes the number of bytes that would be needed to encode a float field, including the tag.
int ComputeGroupSize( IMessage value )
Computes the number of bytes that would be needed to encode a group field, including the tag.
int ComputeInt32Size( int value )
Computes the number of bytes that would be needed to encode an int32 field, including the tag.
int ComputeInt64Size( long value )
Computes the number of bytes that would be needed to encode an int64 field, including the tag.
int ComputeLengthSize( int length )
Computes the number of bytes that would be needed to encode a length, as written by WriteLength.
int ComputeMessageSize( IMessage value )
Computes the number of bytes that would be needed to encode an embedded message field, including the tag.
int ComputeRawVarint32Size( uint value )
Computes the number of bytes that would be needed to encode a varint.
int ComputeRawVarint64Size( ulong value )
Computes the number of bytes that would be needed to encode a varint.
int ComputeSFixed32Size( int value )
Computes the number of bytes that would be needed to encode an sfixed32 field, including the tag.
int ComputeSFixed64Size( long value )
Computes the number of bytes that would be needed to encode an sfixed64 field, including the tag.
int ComputeSInt32Size( int value )
Computes the number of bytes that would be needed to encode an sint32 field, including the tag.
int ComputeSInt64Size( long value )
Computes the number of bytes that would be needed to encode an sint64 field, including the tag.
int ComputeStringSize( String value )
Computes the number of bytes that would be needed to encode a string field, including the tag.
int ComputeTagSize( int fieldNumber )
Computes the number of bytes that would be needed to encode a tag.
int ComputeUInt32Size( uint value )
Computes the number of bytes that would be needed to encode a uint32 field, including the tag.
int ComputeUInt64Size( ulong value )
Computes the number of bytes that would be needed to encode a uint64 field, including the tag.
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 bugs.
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.
CodedOutputStream( Stream output )
Creates a new CodedOutputStream which write to the given stream, and disposes of that stream when the returned CodedOutputStream
is disposed.
Details | |||
---|---|---|---|
Parameters |
|
CodedOutputStream( Stream output, int bufferSize )
Creates a new CodedOutputStream which write to the given stream and uses the specified buffer size.
Details | |||||
---|---|---|---|---|---|
Parameters |
|
CodedOutputStream( Stream output, bool leaveOpen )
Creates a new CodedOutputStream which write to the given stream.
Details | |||||
---|---|---|---|---|---|
Parameters |
|
CodedOutputStream( Stream output, int bufferSize, bool leaveOpen )
Creates a new CodedOutputStream which write to the given stream and uses the specified buffer size.
Details | |||||||
---|---|---|---|---|---|---|---|
Parameters |
|
void Dispose()
Flushes any buffered data and optionally closes the underlying stream, if any.
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.
void Flush()
Flushes any buffered data to the underlying stream (if there is one).
void WriteBool( bool value )
Writes a bool field value, without a tag, to the stream.
Details | |||
---|---|---|---|
Parameters |
|
void WriteBytes( ByteString value )
Write a byte string, without a tag, to the stream.
The data is length-prefixed.
Details | |||
---|---|---|---|
Parameters |
|
void WriteDouble( double value )
Writes a double field value, without a tag, to the stream.
Details | |||
---|---|---|---|
Parameters |
|
void WriteEnum( int value )
Writes an enum value, without a tag, to the stream.
Details | |||
---|---|---|---|
Parameters |
|
void WriteFixed32( uint value )
Writes a fixed32 field value, without a tag, to the stream.
Details | |||
---|---|---|---|
Parameters |
|
void WriteFixed64( ulong value )
Writes a fixed64 field value, without a tag, to the stream.
Details | |||
---|---|---|---|
Parameters |
|
void WriteFloat( float value )
Writes a float field value, without a tag, to the stream.
Details | |||
---|---|---|---|
Parameters |
|
void WriteInt32( int value )
Writes an int32 field value, without a tag, to the stream.
Details | |||
---|---|---|---|
Parameters |
|
void WriteInt64( long value )
Writes an int64 field value, without a tag, to the stream.
Details | |||
---|---|---|---|
Parameters |
|
void WriteLength( int length )
Writes a length (in bytes) for length-delimited data.
This method simply writes a rawint, but exists for clarity in calling code.
Details | |||
---|---|---|---|
Parameters |
|
void WriteMessage( IMessage value )
Writes a message, without a tag, to the stream.
The data is length-prefixed.
Details | |||
---|---|---|---|
Parameters |
|
void WriteRawTag( byte b1 )
Writes the given single-byte tag directly to the stream.
Details | |||
---|---|---|---|
Parameters |
|
void WriteRawTag( byte b1, byte b2 )
Writes the given two-byte tag directly to the stream.
Details | |||||
---|---|---|---|---|---|
Parameters |
|
void WriteRawTag( byte b1, byte b2, byte b3 )
Writes the given three-byte tag directly to the stream.
Details | |||||||
---|---|---|---|---|---|---|---|
Parameters |
|
void WriteRawTag( byte b1, byte b2, byte b3, byte b4 )
Writes the given four-byte tag directly to the stream.
Details | |||||||||
---|---|---|---|---|---|---|---|---|---|
Parameters |
|
void WriteRawTag( byte b1, byte b2, byte b3, byte b4, byte b5 )
Writes the given five-byte tag directly to the stream.
Details | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Parameters |
|
void WriteSFixed32( int value )
Writes an sfixed32 value, without a tag, to the stream.
Details | |||
---|---|---|---|
Parameters |
|
void WriteSFixed64( long value )
Writes an sfixed64 value, without a tag, to the stream.
Details | |||
---|---|---|---|
Parameters |
|
void WriteSInt32( int value )
Writes an sint32 value, without a tag, to the stream.
Details | |||
---|---|---|---|
Parameters |
|
void WriteSInt64( long value )
Writes an sint64 value, without a tag, to the stream.
Details | |||
---|---|---|---|
Parameters |
|
void WriteString( string value )
Writes a string field value, without a tag, to the stream.
The data is length-prefixed.
Details | |||
---|---|---|---|
Parameters |
|
void WriteTag( int fieldNumber, WireFormat.WireType type )
Encodes and writes a tag.
Details | |||||
---|---|---|---|---|---|
Parameters |
|
void WriteTag( uint tag )
Writes an already-encoded tag.
Details | |||
---|---|---|---|
Parameters |
|
void WriteUInt32( uint value )
Writes a uint32 value, without a tag, to the stream.
Details | |||
---|---|---|---|
Parameters |
|
void WriteUInt64( ulong value )
Writes a uint64 field value, without a tag, to the stream.
Details | |||
---|---|---|---|
Parameters |
|