Top Description Inners Fields Constructors Methods
com.google.protobuf

public abstract Class CodedOutputStream

extends ByteOutput
Class Inheritance
Known Direct Subclasses
com.google.protobuf.CodedOutputStream.ArrayEncoder, com.google.protobuf.CodedOutputStream.SafeDirectNioEncoder, com.google.protobuf.CodedOutputStream.UnsafeDirectNioEncoder, com.google.protobuf.CodedOutputStream.AbstractBufferedEncoder
Static Imports
com.google.protobuf.WireFormat.FIXED32_SIZE, .WireFormat.FIXED64_SIZE, .WireFormat.MAX_VARINT_SIZE, java.lang.Math.max

Encodes and writes protocol message fields.

This class contains two kinds of methods: methods that write specific protocol message constructs and field types (e.g. writeTag and writeInt32) and methods that write low-level values (e.g. writeRawVarint32 and writeRawBytes). If you are writing encoded protocol messages, you should use the former methods, but if you are writing some other format of your own design, use the latter.

This class is totally unsynchronized.

Nested and Inner Type Summary

Modifier and TypeClass and Description
private abstract static class
CodedOutputStream.AbstractBufferedEncoder

Abstract base class for buffered encoders.

private static class
CodedOutputStream.ArrayEncoder

A CodedOutputStream that writes directly to a byte array.

private static class
private static class
public static class
CodedOutputStream.OutOfSpaceException

If you create a CodedOutputStream around a simple flat array, you must not attempt to write more bytes than the array has space.

private static class
private static class
CodedOutputStream.SafeDirectNioEncoder

A CodedOutputStream that writes directly to a direct ByteBuffer, using only safe operations..

private static class
CodedOutputStream.UnsafeDirectNioEncoder

A CodedOutputStream that writes directly to a direct ByteBuffer using sun.misc.Unsafe.

Field Summary

Modifier and TypeField and Description
public static final int
private static final boolean
public static final int
private static final Logger
private boolean

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public final void
checkNoSpaceLeft()

Verifies that spaceLeft() returns zero.

public static int
computeBoolSize(final int fieldNumber, final boolean value)

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

public static int
computeBoolSizeNoTag(final boolean unused)

Compute the number of bytes that would be needed to encode a bool field.

public static int
computeByteArraySize(final int fieldNumber, final byte[] value)

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

public static int
computeByteArraySizeNoTag(final byte[] value)

Compute the number of bytes that would be needed to encode a bytes field.

public static int
computeByteBufferSize(final int fieldNumber, final ByteBuffer value)

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

public static int
computeByteBufferSizeNoTag(final ByteBuffer value)

Compute the number of bytes that would be needed to encode a bytes field.

public static int
computeBytesSize(final int fieldNumber, final ByteString value)

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

public static int
computeBytesSizeNoTag(final ByteString value)

Compute the number of bytes that would be needed to encode a bytes field.

public static int
computeDoubleSize(final int fieldNumber, final double value)

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

public static int
computeDoubleSizeNoTag(final double unused)

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

public static int
computeEnumSize(final int fieldNumber, final int value)

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

public static int
computeEnumSizeNoTag(final int value)

Compute the number of bytes that would be needed to encode an enum field.

public static int
computeFixed32Size(final int fieldNumber, final int value)

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

public static int
computeFixed32SizeNoTag(final int unused)

Compute the number of bytes that would be needed to encode a fixed32 field.

public static int
computeFixed64Size(final int fieldNumber, final long value)

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

public static int
computeFixed64SizeNoTag(final long unused)

Compute the number of bytes that would be needed to encode a fixed64 field.

public static int
computeFloatSize(final int fieldNumber, final float value)

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

public static int
computeFloatSizeNoTag(final float unused)

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

public static int
computeGroupSize(final int fieldNumber, final MessageLite value)

Deprecated groups are deprecated.
Compute the number of bytes that would be needed to encode a group field, including tag.
public static int
computeGroupSizeNoTag(final MessageLite value)

Deprecated
Compute the number of bytes that would be needed to encode a group field.
public static int
computeInt32Size(final int fieldNumber, final int value)

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

public static int
computeInt32SizeNoTag(final int value)

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

public static int
computeInt64Size(final int fieldNumber, final long value)

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

public static int
computeInt64SizeNoTag(final long value)

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

public static int
computeLazyFieldMessageSetExtensionSize(final int fieldNumber, final LazyFieldLite value)

Compute the number of bytes that would be needed to encode an lazily parsed MessageSet extension field to the stream.

public static int
computeLazyFieldSize(final int fieldNumber, final LazyFieldLite value)

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

public static int
computeLazyFieldSizeNoTag(final LazyFieldLite value)

Compute the number of bytes that would be needed to encode an embedded message stored in lazy field.

pack-priv static int
public static int
computeMessageSetExtensionSize(final int fieldNumber, final MessageLite value)

Compute the number of bytes that would be needed to encode a MessageSet extension to the stream.

public static int
computeMessageSize(final int fieldNumber, final MessageLite value)

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

public static int
computeMessageSizeNoTag(final MessageLite value)

Compute the number of bytes that would be needed to encode an embedded message field.

pack-priv static int

Returns:

the buffer size to efficiently write dataLength bytes to this CodedOutputStream.
computePreferredBufferSize
(int dataLength)

Returns the buffer size to efficiently write dataLength bytes to this CodedOutputStream.

public static int
computeRawMessageSetExtensionSize(final int fieldNumber, final ByteString value)

Compute the number of bytes that would be needed to encode an unparsed MessageSet extension field to the stream.

public static int
computeRawVarint32Size(final int value)

Deprecated use computeUInt32SizeNoTag(int) instead.
Compute the number of bytes that would be needed to encode a varint.
public static int
computeRawVarint64Size(long value)

Deprecated use computeUInt64SizeNoTag(long) instead.
Compute the number of bytes that would be needed to encode a varint.
public static int
computeSFixed32Size(final int fieldNumber, final int value)

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

public static int
computeSFixed32SizeNoTag(final int unused)

Compute the number of bytes that would be needed to encode an sfixed32 field.

public static int
computeSFixed64Size(final int fieldNumber, final long value)

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

public static int
computeSFixed64SizeNoTag(final long unused)

Compute the number of bytes that would be needed to encode an sfixed64 field.

public static int
computeSInt32Size(final int fieldNumber, final int value)

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

public static int
computeSInt32SizeNoTag(final int value)

Compute the number of bytes that would be needed to encode an sint32 field.

public static int
computeSInt64Size(final int fieldNumber, final long value)

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

public static int
computeSInt64SizeNoTag(final long value)

Compute the number of bytes that would be needed to encode an sint64 field.

public static int
computeStringSize(final int fieldNumber, final String value)

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

public static int
computeStringSizeNoTag(final String value)

Compute the number of bytes that would be needed to encode a string field.

public static int
computeTagSize(final int fieldNumber)

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

public static int
computeUInt32Size(final int fieldNumber, final int value)

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

public static int
computeUInt32SizeNoTag(final int value)

Compute the number of bytes that would be needed to encode a uint32 field.

public static int
computeUInt64Size(final int fieldNumber, final long value)

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

public static int
computeUInt64SizeNoTag(long value)

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

public static int

Returns:

An unsigned 32-bit integer, stored in a signed int because Java has no explicit unsigned support.
encodeZigZag32
(final int
A signed 32-bit integer.
n
)

Encode a ZigZag-encoded 32-bit value.

public static long

Returns:

An unsigned 64-bit integer, stored in a signed int because Java has no explicit unsigned support.
encodeZigZag64
(final long
A signed 64-bit integer.
n
)

Encode a ZigZag-encoded 64-bit value.

public abstract void
flush()

Flushes the stream and forces any buffered bytes to be written.

public abstract int
getTotalBytesWritten()

Get the total number of bytes successfully written to this stream.

pack-priv final void
pack-priv boolean
public static CodedOutputStream
newInstance(final OutputStream output)

Create a new CodedOutputStream wrapping the given OutputStream.

public static CodedOutputStream
newInstance(final OutputStream output, final int bufferSize)

Create a new CodedOutputStream wrapping the given OutputStream with a given buffer size.

public static CodedOutputStream
newInstance(final byte[] flatArray)

Create a new CodedOutputStream that writes directly to the given byte array.

public static CodedOutputStream
newInstance(final byte[] flatArray, final int offset, final int length)

Create a new CodedOutputStream that writes directly to the given byte array slice.

public static CodedOutputStream
newInstance(ByteBuffer buffer)

Create a new CodedOutputStream that writes to the given ByteBuffer.

public static CodedOutputStream
newInstance(ByteBuffer byteBuffer, int unused)

Deprecated the size parameter is no longer used since use of an internal buffer is useless (and wasteful) when writing to a ByteBuffer.
Create a new CodedOutputStream that writes to the given ByteBuffer.
pack-priv static CodedOutputStream
newInstance(ByteOutput
the output target for encoded bytes.
byteOutput
,
int
the size of the internal scratch buffer to be used for string encoding. Setting this to 0 will disable buffering, requiring an allocation for each encoded string.
bufferSize
)

Create a new CodedOutputStream that writes to the provided ByteOutput.

pack-priv static CodedOutputStream
newSafeInstance(ByteBuffer buffer)

For testing purposes only.

pack-priv static CodedOutputStream
newUnsafeInstance(ByteBuffer buffer)

For testing purposes only.

public abstract int
spaceLeft()

If writing to a flat array, return the space left in the array.

public void
useDeterministicSerialization()

Configures serialization to be deterministic.

public abstract void
write(byte
the byte to be written
value
)

Implements abstract com.google.protobuf.ByteOutput.write.

Writes a single byte.
public abstract void
write(byte[]
the bytes to be written
value
,
int
the offset of the start of the writable range
offset
,
int
the number of bytes to write starting from offset
length
)

Implements abstract com.google.protobuf.ByteOutput.write.

Writes a sequence of bytes.
public abstract void
write(ByteBuffer
the bytes to be written. Upon returning from this call, the position of this buffer will be set to the limit
value
)

Implements abstract com.google.protobuf.ByteOutput.write.

Writes a sequence of bytes.
public abstract void
writeBool(int fieldNumber, boolean value)

Write a bool field, including tag, to the stream.

public final void
writeBoolNoTag(final boolean value)

Write a bool field to the stream.

public abstract void
writeByteArray(int fieldNumber, byte[] value)

Write a bytes field, including tag, to the stream.

public abstract void
writeByteArray(int fieldNumber, byte[] value, int offset, int length)

Write a bytes field, including tag, to the stream.

public final void
writeByteArrayNoTag(final byte[] value)

Write a bytes field to the stream.

pack-priv abstract void
writeByteArrayNoTag(final byte[] value, final int offset, final int length)

Write a bytes field to the stream.

public abstract void
writeByteBuffer(int fieldNumber, ByteBuffer value)

Write a bytes field, including tag, to the stream.

public abstract void
writeBytes(int fieldNumber, ByteString value)

Write a bytes field, including tag, to the stream.

public abstract void
writeBytesNoTag(final ByteString value)

Write a bytes field to the stream.

public final void
writeDouble(final int fieldNumber, final double value)

Write a double field, including tag, to the stream.

public final void
writeDoubleNoTag(final double value)

Write a double field to the stream.

public final void
writeEnum(final int fieldNumber, final int value)

Write an enum field, including tag, to the stream.

public final void
writeEnumNoTag(final int value)

Write an enum field to the stream.

public abstract void
writeFixed32(int fieldNumber, int value)

Write a fixed32 field, including tag, to the stream.

public abstract void
writeFixed32NoTag(int value)

Write a fixed32 field to the stream.

public abstract void
writeFixed64(int fieldNumber, long value)

Write a fixed64 field, including tag, to the stream.

public abstract void
writeFixed64NoTag(long value)

Write a fixed64 field to the stream.

public final void
writeFloat(final int fieldNumber, final float value)

Write a float field, including tag, to the stream.

public final void
writeFloatNoTag(final float value)

Write a float field to the stream.

public final void
writeGroup(final int fieldNumber, final MessageLite value)

Deprecated groups are deprecated.
Write a group field, including tag, to the stream.
public final void
writeGroupNoTag(final MessageLite value)

Deprecated groups are deprecated.
Write a group field to the stream.
public abstract void
writeInt32(int fieldNumber, int value)

Write an int32 field, including tag, to the stream.

public abstract void
writeInt32NoTag(final int value)

Write an int32 field to the stream.

public final void
writeInt64(final int fieldNumber, final long value)

Write an int64 field, including tag, to the stream.

public final void
writeInt64NoTag(final long value)

Write an int64 field to the stream.

public abstract void
writeLazy(byte[]
the bytes to be written
value
,
int
the offset of the start of the writable range
offset
,
int
the number of bytes to write starting from offset
length
)

Implements abstract com.google.protobuf.ByteOutput.writeLazy.

Writes a sequence of bytes.
public abstract void
writeLazy(ByteBuffer
the bytes to be written. Upon returning from this call, the position of this buffer will be set to the limit
value
)

Implements abstract com.google.protobuf.ByteOutput.writeLazy.

Writes a sequence of bytes.
public abstract void
writeMessage(final int fieldNumber, final MessageLite value)

Write an embedded message field, including tag, to the stream.

public abstract void
writeMessageNoTag(final MessageLite value)

Write an embedded message field to the stream.

public abstract void
writeMessageSetExtension(final int fieldNumber, final MessageLite value)

Write a MessageSet extension field to the stream.

public final void
writeRawByte(final byte value)

Write a single byte.

public final void
writeRawByte(final int value)

Write a single byte, represented by an integer value.

public final void
writeRawBytes(final byte[] value)

Write an array of bytes.

public final void
writeRawBytes(final byte[] value, int offset, int length)

Write part of an array of bytes.

public final void
writeRawBytes(final ByteString value)

Write a byte string.

public abstract void
writeRawBytes(final ByteBuffer value)

Write a ByteBuffer.

public final void
writeRawLittleEndian32(final int value)

Deprecated Use writeFixed32NoTag instead.
Write a little-endian 32-bit integer.
public final void
writeRawLittleEndian64(final long value)

Deprecated Use writeFixed64NoTag instead.
Write a little-endian 64-bit integer.
public abstract void
writeRawMessageSetExtension(final int fieldNumber, final ByteString value)

Write an unparsed MessageSet extension field to the stream.

public final void
writeRawVarint32(int value)

Deprecated use writeUInt32NoTag instead.
Encode and write a varint.
public final void
writeRawVarint64(long value)

Deprecated use writeUInt64NoTag instead.
Encode and write a varint.
public final void
writeSFixed32(final int fieldNumber, final int value)

Write an sfixed32 field, including tag, to the stream.

public final void
writeSFixed32NoTag(final int value)

Write a sfixed32 field to the stream.

public final void
writeSFixed64(final int fieldNumber, final long value)

Write an sfixed64 field, including tag, to the stream.

public final void
writeSFixed64NoTag(final long value)

Write a sfixed64 field to the stream.

public final void
writeSInt32(final int fieldNumber, final int value)

Write a sint32 field, including tag, to the stream.

public final void
writeSInt32NoTag(final int value)

Write a sint32 field to the stream.

public final void
writeSInt64(final int fieldNumber, final long value)

Write an sint64 field, including tag, to the stream.

public final void
writeSInt64NoTag(final long value)

Write a sint64 field to the stream.

public abstract void
writeString(int fieldNumber, String value)

Write a string field, including tag, to the stream.

public abstract void
writeStringNoTag(String value)

Write a string field to the stream.

public abstract void
writeTag(int fieldNumber, int wireType)

Encode and write a tag.

public abstract void
writeUInt32(int fieldNumber, int value)

Write a uint32 field, including tag, to the stream.

public abstract void
writeUInt32NoTag(int value)

Write a uint32 field to the stream.

public abstract void
writeUInt64(int fieldNumber, long value)

Write a uint64 field, including tag, to the stream.

public abstract void
writeUInt64NoTag(long value)

Write a uint64 field to the stream.