C# Класс 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.

Показать файл Открыть проект Примеры использования класса

Открытые свойства

Свойство Тип Описание
DefaultBufferSize int

Открытые методы

Метод Описание
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.

Приватные методы

Метод Описание
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

Описание методов

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.
public CheckNoSpaceLeft ( ) : void
Результат void

CodedOutputStream() публичный Метод

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.
Результат System

CodedOutputStream() публичный Метод

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.
Результат System

CodedOutputStream() публичный Метод

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.
Результат System

CodedOutputStream() публичный Метод

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.
Результат System

CodedOutputStream() публичный Метод

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
Результат System

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.

public Dispose ( ) : void
Результат void

Flush() публичный Метод

Flushes any buffered data to the underlying stream (if there is one).
public Flush ( ) : void
Результат void

WriteBool() публичный Метод

Writes a bool field value, without a tag, to the stream.
public WriteBool ( bool value ) : void
value bool The value to write
Результат void

WriteBytes() публичный Метод

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
Результат void

WriteDouble() публичный Метод

Writes a double field value, without a tag, to the stream.
public WriteDouble ( double value ) : void
value double The value to write
Результат void

WriteEnum() публичный Метод

Writes an enum value, without a tag, to the stream.
public WriteEnum ( int value ) : void
value int The value to write
Результат void

WriteFixed32() публичный Метод

Writes a fixed32 field value, without a tag, to the stream.
public WriteFixed32 ( uint value ) : void
value uint The value to write
Результат void

WriteFixed64() публичный Метод

Writes a fixed64 field value, without a tag, to the stream.
public WriteFixed64 ( ulong value ) : void
value ulong The value to write
Результат void

WriteFloat() публичный Метод

Writes a float field value, without a tag, to the stream.
public WriteFloat ( float value ) : void
value float The value to write
Результат void

WriteInt32() публичный Метод

Writes an int32 field value, without a tag, to the stream.
public WriteInt32 ( int value ) : void
value int The value to write
Результат void

WriteInt64() публичный Метод

Writes an int64 field value, without a tag, to the stream.
public WriteInt64 ( long value ) : void
value long The value to write
Результат void

WriteLength() публичный Метод

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.
Результат void

WriteMessage() публичный Метод

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
Результат void

WriteRawTag() публичный Метод

Writes the given single-byte tag directly to the stream.
public WriteRawTag ( byte b1 ) : void
b1 byte The encoded tag
Результат void

WriteRawTag() публичный Метод

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
Результат void

WriteRawTag() публичный Метод

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
Результат void

WriteRawTag() публичный Метод

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
Результат void

WriteRawTag() публичный Метод

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
Результат void

WriteSFixed32() публичный Метод

Writes an sfixed32 value, without a tag, to the stream.
public WriteSFixed32 ( int value ) : void
value int The value to write.
Результат void

WriteSFixed64() публичный Метод

Writes an sfixed64 value, without a tag, to the stream.
public WriteSFixed64 ( long value ) : void
value long The value to write
Результат void

WriteSInt32() публичный Метод

Writes an sint32 value, without a tag, to the stream.
public WriteSInt32 ( int value ) : void
value int The value to write
Результат void

WriteSInt64() публичный Метод

Writes an sint64 value, without a tag, to the stream.
public WriteSInt64 ( long value ) : void
value long The value to write
Результат void

WriteString() публичный Метод

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
Результат void

WriteTag() публичный Метод

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
Результат void

WriteTag() публичный Метод

Writes an already-encoded tag.
public WriteTag ( uint tag ) : void
tag uint The encoded tag
Результат void

WriteUInt32() публичный Метод

Writes a uint32 value, without a tag, to the stream.
public WriteUInt32 ( uint value ) : void
value uint The value to write
Результат void

WriteUInt64() публичный Метод

Writes a uint64 field value, without a tag, to the stream.
public WriteUInt64 ( ulong value ) : void
value ulong The value to write
Результат void

Описание свойств

DefaultBufferSize публичное статическое свойство

The buffer size used by CreateInstance(Stream).
public static int DefaultBufferSize
Результат int