C# Class Google.Protobuf.CodedOutputStream

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<T> and MapField<TKey, TValue> to serialize such fields.

Show file Open project: Arctium-Emulation/Project-WoW Class Usage Examples

Public Properties

Property Type Description
DefaultBufferSize int

Public Methods

Method Description
CheckNoSpaceLeft ( ) : void

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 ( Stream output ) : System

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

CodedOutputStream ( Stream output, bool leaveOpen ) : System

Creates a new CodedOutputStream which write to the given stream.

CodedOutputStream ( Stream output, int bufferSize ) : System

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

CodedOutputStream ( Stream output, int bufferSize, bool leaveOpen ) : System

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

CodedOutputStream ( byte flatArray ) : System

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.

Dispose ( ) : void

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.

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. The data is length-prefixed.

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.

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

WriteMessage ( IMessage value ) : void

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

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. The data is length-prefixed.

WriteTag ( int fieldNumber, WireFormat 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.

Private Methods

Method Description
CodedOutputStream ( Stream output, byte buffer, bool leaveOpen ) : System
CodedOutputStream ( byte buffer, int offset, int length ) : System

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

EncodeZigZag32 ( int n ) : uint

Encode a 32-bit value with ZigZag encoding.

ZigZag encodes signed integers into values that can be efficiently encoded with varint. (Otherwise, negative values must be sign-extended to 64 bits to be varint encoded, thus always taking 10 bytes on the wire.)

EncodeZigZag64 ( long n ) : ulong

Encode a 64-bit value with ZigZag encoding.

ZigZag encodes signed integers into values that can be efficiently encoded with varint. (Otherwise, negative values must be sign-extended to 64 bits to be varint encoded, thus always taking 10 bytes on the wire.)

RefreshBuffer ( ) : void
WriteRawByte ( byte value ) : void
WriteRawByte ( uint value ) : void
WriteRawBytes ( byte value ) : void

Writes out an array of bytes.

WriteRawBytes ( byte value, int offset, int length ) : void

Writes out part of an array of bytes.

WriteRawLittleEndian32 ( uint value ) : void
WriteRawLittleEndian64 ( ulong value ) : void
WriteRawVarint32 ( uint value ) : void

Writes a 32 bit value as a varint. The fast route is taken when there's enough buffer space left to whizz through without checking for each byte; otherwise, we resort to calling WriteRawByte each time.

WriteRawVarint64 ( ulong value ) : void

Method Details

CheckNoSpaceLeft() public method

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.
public CheckNoSpaceLeft ( ) : void
return void

CodedOutputStream() public method

Creates a new CodedOutputStream which write to the given stream, and disposes of that stream when the returned CodedOutputStream is disposed.
public CodedOutputStream ( Stream output ) : System
output Stream The stream to write to. It will be disposed when the returned CodedOutputStream is disposed.
return System

CodedOutputStream() public method

Creates a new CodedOutputStream which write to the given stream.
public CodedOutputStream ( Stream output, bool leaveOpen ) : System
output Stream The stream to write to.
leaveOpen bool If true, is left open when the returned CodedOutputStream is disposed; /// if false, the provided stream is disposed as well.
return System

CodedOutputStream() public method

Creates a new CodedOutputStream which write to the given stream and uses the specified buffer size.
public CodedOutputStream ( Stream output, int bufferSize ) : System
output Stream The stream to write to. It will be disposed when the returned CodedOutputStream is disposed.
bufferSize int The size of buffer to use internally.
return System

CodedOutputStream() public method

Creates a new CodedOutputStream which write to the given stream and uses the specified buffer size.
public CodedOutputStream ( Stream output, int bufferSize, bool leaveOpen ) : System
output Stream The stream to write to.
bufferSize int The size of buffer to use internally.
leaveOpen bool If true, is left open when the returned CodedOutputStream is disposed; /// if false, the provided stream is disposed as well.
return System

CodedOutputStream() public method

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.
public CodedOutputStream ( byte flatArray ) : System
flatArray byte
return System

Dispose() public method

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.

public Dispose ( ) : void
return void

Flush() public method

Flushes any buffered data to the underlying stream (if there is one).
public Flush ( ) : void
return void

WriteBool() public method

Writes a bool field value, without a tag, to the stream.
public WriteBool ( bool value ) : void
value bool The value to write
return void

WriteBytes() public method

Write a byte string, without a tag, to the stream. The data is length-prefixed.
public WriteBytes ( ByteString value ) : void
value ByteString The value to write
return void

WriteDouble() public method

Writes a double field value, without a tag, to the stream.
public WriteDouble ( double value ) : void
value double The value to write
return void

WriteEnum() public method

Writes an enum value, without a tag, to the stream.
public WriteEnum ( int value ) : void
value int The value to write
return void

WriteFixed32() public method

Writes a fixed32 field value, without a tag, to the stream.
public WriteFixed32 ( uint value ) : void
value uint The value to write
return void

WriteFixed64() public method

Writes a fixed64 field value, without a tag, to the stream.
public WriteFixed64 ( ulong value ) : void
value ulong The value to write
return void

WriteFloat() public method

Writes a float field value, without a tag, to the stream.
public WriteFloat ( float value ) : void
value float The value to write
return void

WriteInt32() public method

Writes an int32 field value, without a tag, to the stream.
public WriteInt32 ( int value ) : void
value int The value to write
return void

WriteInt64() public method

Writes an int64 field value, without a tag, to the stream.
public WriteInt64 ( long value ) : void
value long The value to write
return void

WriteLength() public method

Writes a length (in bytes) for length-delimited data.
This method simply writes a rawint, but exists for clarity in calling code.
public WriteLength ( int length ) : void
length int Length value, in bytes.
return void

WriteMessage() public method

Writes a message, without a tag, to the stream. The data is length-prefixed.
public WriteMessage ( IMessage value ) : void
value IMessage The value to write
return void

WriteRawTag() public method

Writes the given single-byte tag directly to the stream.
public WriteRawTag ( byte b1 ) : void
b1 byte The encoded tag
return void

WriteRawTag() public method

Writes the given two-byte tag directly to the stream.
public WriteRawTag ( byte b1, byte b2 ) : void
b1 byte The first byte of the encoded tag
b2 byte The second byte of the encoded tag
return void

WriteRawTag() public method

Writes the given three-byte tag directly to the stream.
public WriteRawTag ( byte b1, byte b2, byte b3 ) : void
b1 byte The first byte of the encoded tag
b2 byte The second byte of the encoded tag
b3 byte The third byte of the encoded tag
return void

WriteRawTag() public method

Writes the given four-byte tag directly to the stream.
public WriteRawTag ( byte b1, byte b2, byte b3, byte b4 ) : void
b1 byte The first byte of the encoded tag
b2 byte The second byte of the encoded tag
b3 byte The third byte of the encoded tag
b4 byte The fourth byte of the encoded tag
return void

WriteRawTag() public method

Writes the given five-byte tag directly to the stream.
public WriteRawTag ( byte b1, byte b2, byte b3, byte b4, byte b5 ) : void
b1 byte The first byte of the encoded tag
b2 byte The second byte of the encoded tag
b3 byte The third byte of the encoded tag
b4 byte The fourth byte of the encoded tag
b5 byte The fifth byte of the encoded tag
return void

WriteSFixed32() public method

Writes an sfixed32 value, without a tag, to the stream.
public WriteSFixed32 ( int value ) : void
value int The value to write.
return void

WriteSFixed64() public method

Writes an sfixed64 value, without a tag, to the stream.
public WriteSFixed64 ( long value ) : void
value long The value to write
return void

WriteSInt32() public method

Writes an sint32 value, without a tag, to the stream.
public WriteSInt32 ( int value ) : void
value int The value to write
return void

WriteSInt64() public method

Writes an sint64 value, without a tag, to the stream.
public WriteSInt64 ( long value ) : void
value long The value to write
return void

WriteString() public method

Writes a string field value, without a tag, to the stream. The data is length-prefixed.
public WriteString ( string value ) : void
value string The value to write
return void

WriteTag() public method

Encodes and writes a tag.
public WriteTag ( int fieldNumber, WireFormat type ) : void
fieldNumber int The number of the field to write the tag for
type WireFormat The wire format type of the tag to write
return void

WriteTag() public method

Writes an already-encoded tag.
public WriteTag ( uint tag ) : void
tag uint The encoded tag
return void

WriteUInt32() public method

Writes a uint32 value, without a tag, to the stream.
public WriteUInt32 ( uint value ) : void
value uint The value to write
return void

WriteUInt64() public method

Writes a uint64 field value, without a tag, to the stream.
public WriteUInt64 ( ulong value ) : void
value ulong The value to write
return void

Property Details

DefaultBufferSize public static property

The buffer size used by CreateInstance(Stream).
public static int DefaultBufferSize
return int