Package com.google.protobuf
Class BinaryWriter
java.lang.Object
com.google.protobuf.ByteOutput
com.google.protobuf.BinaryWriter
- All Implemented Interfaces:
Writer
- Direct Known Subclasses:
BinaryWriter.SafeDirectWriter
,BinaryWriter.SafeHeapWriter
,BinaryWriter.UnsafeDirectWriter
,BinaryWriter.UnsafeHeapWriter
A protobuf writer that serializes messages in their binary form. Messages are serialized in
reverse in order to avoid calculating the serialized size of each nested message. Since the
message size is not known in advance, the writer employs a strategy of chunking and buffer
chaining. Buffers are allocated as-needed by a provided
BufferAllocator
. Once writing is
finished, the application can access the buffers in forward-writing order by calling complete()
.
Once complete()
has been called, the writer can not be reused for additional writes.
The getTotalBytesWritten()
will continue to reflect the total of the write and will not
be reset.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static final class
Writer that uses safe operations on a targetByteBuffer
.private static final class
Writer that uses safe operations on target array.private static final class
Writer that uses unsafe operations on a targetByteBuffer
.private static final class
Writer that uses unsafe operations on a target array.Nested classes/interfaces inherited from interface com.google.protobuf.Writer
Writer.FieldOrder
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final BufferAllocator
(package private) final ArrayDeque
<AllocatedBuffer> private final int
static final int
private static final int
private static final int
(package private) int
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprivate
BinaryWriter
(BufferAllocator alloc, int chunkSize) Only allow subclassing for inner classes. -
Method Summary
Modifier and TypeMethodDescriptionfinal Queue
<AllocatedBuffer> complete()
Completes the write operation and returns a queue ofAllocatedBuffer
objects in forward-writing order.private static byte
computeUInt64SizeNoTag
(long value) Not using the version in CodedOutputStream due to the fact that benchmarks have shown a performance improvement when returning a byte (rather than an int).final Writer.FieldOrder
Indicates the order in which the fields are written by thisWriter
.(package private) abstract void
abstract int
Gets the total number of bytes that have been written.(package private) static boolean
(package private) static boolean
(package private) final AllocatedBuffer
(package private) final AllocatedBuffer
newDirectBuffer
(int capacity) static BinaryWriter
newDirectInstance
(BufferAllocator alloc) Creates a newBinaryWriter
that will allocate direct (i.e.static BinaryWriter
newDirectInstance
(BufferAllocator alloc, int chunkSize) Creates a newBinaryWriter
that will allocate direct (i.e.(package private) final AllocatedBuffer
(package private) final AllocatedBuffer
newHeapBuffer
(int capacity) static BinaryWriter
newHeapInstance
(BufferAllocator alloc) Creates a newBinaryWriter
that will allocate heap buffers ofDEFAULT_CHUNK_SIZE
as necessary.static BinaryWriter
newHeapInstance
(BufferAllocator alloc, int chunkSize) Creates a newBinaryWriter
that will allocate heap buffers ofchunkSize
as necessary.(package private) static BinaryWriter
newSafeDirectInstance
(BufferAllocator alloc, int chunkSize) (package private) static BinaryWriter
newSafeHeapInstance
(BufferAllocator alloc, int chunkSize) (package private) static BinaryWriter
newUnsafeDirectInstance
(BufferAllocator alloc, int chunkSize) (package private) static BinaryWriter
newUnsafeHeapInstance
(BufferAllocator alloc, int chunkSize) (package private) abstract void
requireSpace
(int size) (package private) abstract void
writeBool
(boolean value) final void
writeBoolList
(int fieldNumber, List<Boolean> list, boolean packed) Writes a list field of typeFieldType.BOOL
.private void
writeBoolList_Internal
(int fieldNumber, BooleanArrayList list, boolean packed) private void
writeBoolList_Internal
(int fieldNumber, List<Boolean> list, boolean packed) final void
writeBytesList
(int fieldNumber, List<ByteString> list) Writes a list field of typeFieldType.BYTES
.final void
writeDouble
(int fieldNumber, double value) Writes a field of typeFieldType.DOUBLE
.final void
writeDoubleList
(int fieldNumber, List<Double> list, boolean packed) Writes a list field of typeFieldType.DOUBLE
.private void
writeDoubleList_Internal
(int fieldNumber, DoubleArrayList list, boolean packed) private void
writeDoubleList_Internal
(int fieldNumber, List<Double> list, boolean packed) final void
writeEnum
(int fieldNumber, int value) Writes a field of typeFieldType.ENUM
.final void
writeEnumList
(int fieldNumber, List<Integer> list, boolean packed) Writes a list field of typeFieldType.ENUM
.(package private) abstract void
writeFixed32
(int value) final void
writeFixed32List
(int fieldNumber, List<Integer> list, boolean packed) Writes a list field of typeFieldType.FIXED32
.private void
writeFixed32List_Internal
(int fieldNumber, IntArrayList list, boolean packed) private void
writeFixed32List_Internal
(int fieldNumber, List<Integer> list, boolean packed) (package private) abstract void
writeFixed64
(long value) final void
writeFixed64List
(int fieldNumber, List<Long> list, boolean packed) Writes a list field of typeFieldType.FIXED64
.private void
writeFixed64List_Internal
(int fieldNumber, LongArrayList list, boolean packed) private void
writeFixed64List_Internal
(int fieldNumber, List<Long> list, boolean packed) final void
writeFloat
(int fieldNumber, float value) Writes a field of typeFieldType.FLOAT
.final void
writeFloatList
(int fieldNumber, List<Float> list, boolean packed) Writes a list field of typeFieldType.FLOAT
.private void
writeFloatList_Internal
(int fieldNumber, FloatArrayList list, boolean packed) private void
writeFloatList_Internal
(int fieldNumber, List<Float> list, boolean packed) final void
writeGroupList
(int fieldNumber, List<?> list) Deprecated.final void
writeGroupList
(int fieldNumber, List<?> list, Schema schema) Deprecated.(package private) abstract void
writeInt32
(int value) final void
writeInt32List
(int fieldNumber, List<Integer> list, boolean packed) Writes a list field of typeFieldType.INT32
.private void
writeInt32List_Internal
(int fieldNumber, IntArrayList list, boolean packed) private void
writeInt32List_Internal
(int fieldNumber, List<Integer> list, boolean packed) final void
writeInt64
(int fieldNumber, long value) Writes a field of typeFieldType.INT64
.final void
writeInt64List
(int fieldNumber, List<Long> list, boolean packed) Writes a list field of typeFieldType.INT64
.private void
writeLazyString
(int fieldNumber, Object value) <K,
V> void writeMap
(int fieldNumber, MapEntryLite.Metadata<K, V> metadata, Map<K, V> map) Writes a map field.(package private) static final void
writeMapEntryField
(Writer writer, int fieldNumber, WireFormat.FieldType fieldType, Object object) final void
writeMessageList
(int fieldNumber, List<?> list) Writes a list field of typeFieldType.MESSAGE
.final void
writeMessageList
(int fieldNumber, List<?> list, Schema schema) Writes a list field of typeFieldType.MESSAGE
.final void
writeMessageSetItem
(int fieldNumber, Object value) Writes a message field inMessageSet
wire-format.final void
writeSFixed32
(int fieldNumber, int value) Writes a field of typeFieldType.SFIXED32
.final void
writeSFixed32List
(int fieldNumber, List<Integer> list, boolean packed) Writes a list field of typeFieldType.SFIXED32
.final void
writeSFixed64
(int fieldNumber, long value) Writes a field of typeFieldType.SFIXED64
.final void
writeSFixed64List
(int fieldNumber, List<Long> list, boolean packed) Writes a list field of typeFieldType.SFIXED64
.(package private) abstract void
writeSInt32
(int value) final void
writeSInt32List
(int fieldNumber, List<Integer> list, boolean packed) Writes a list field of typeFieldType.SINT32
.private void
writeSInt32List_Internal
(int fieldNumber, IntArrayList list, boolean packed) private void
writeSInt32List_Internal
(int fieldNumber, List<Integer> list, boolean packed) (package private) abstract void
writeSInt64
(long value) final void
writeSInt64List
(int fieldNumber, List<Long> list, boolean packed) Writes a list field of typeFieldType.SINT64
.private void
writeSInt64List_Internal
(int fieldNumber, LongArrayList list, boolean packed) private void
writeSInt64List_Internal
(int fieldNumber, List<Long> list, boolean packed) (package private) abstract void
writeString
(String in) final void
writeStringList
(int fieldNumber, List<String> list) Writes a list field of typeFieldType.STRING
.(package private) abstract void
writeTag
(int fieldNumber, int wireType) final void
writeUInt32List
(int fieldNumber, List<Integer> list, boolean packed) Writes a list field of typeFieldType.UINT32
.private void
writeUInt32List_Internal
(int fieldNumber, IntArrayList list, boolean packed) private void
writeUInt32List_Internal
(int fieldNumber, List<Integer> list, boolean packed) final void
writeUInt64List
(int fieldNumber, List<Long> list, boolean packed) Writes a list field of typeFieldType.UINT64
.private void
writeUInt64List_Internal
(int fieldNumber, LongArrayList list, boolean packed) private void
writeUInt64List_Internal
(int fieldNumber, List<Long> list, boolean packed) (package private) abstract void
writeVarint32
(int value) (package private) abstract void
writeVarint64
(long value) Methods inherited from class com.google.protobuf.ByteOutput
write, write, write, writeLazy, writeLazy
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface com.google.protobuf.Writer
writeBool, writeBytes, writeEndGroup, writeFixed32, writeFixed64, writeGroup, writeGroup, writeInt32, writeMessage, writeMessage, writeSInt32, writeSInt64, writeStartGroup, writeString, writeUInt32, writeUInt64
-
Field Details
-
DEFAULT_CHUNK_SIZE
public static final int DEFAULT_CHUNK_SIZE- See Also:
-
alloc
-
chunkSize
private final int chunkSize -
buffers
-
totalDoneBytes
int totalDoneBytes -
MAP_KEY_NUMBER
private static final int MAP_KEY_NUMBER- See Also:
-
MAP_VALUE_NUMBER
private static final int MAP_VALUE_NUMBER- See Also:
-
-
Constructor Details
-
BinaryWriter
Only allow subclassing for inner classes.
-
-
Method Details
-
newHeapInstance
Creates a newBinaryWriter
that will allocate heap buffers ofDEFAULT_CHUNK_SIZE
as necessary. -
newHeapInstance
Creates a newBinaryWriter
that will allocate heap buffers ofchunkSize
as necessary. -
newDirectInstance
Creates a newBinaryWriter
that will allocate direct (i.e. non-heap) buffers ofDEFAULT_CHUNK_SIZE
as necessary. -
newDirectInstance
Creates a newBinaryWriter
that will allocate direct (i.e. non-heap) buffers ofchunkSize
as necessary. -
isUnsafeHeapSupported
static boolean isUnsafeHeapSupported() -
isUnsafeDirectSupported
static boolean isUnsafeDirectSupported() -
newSafeHeapInstance
-
newUnsafeHeapInstance
-
newSafeDirectInstance
-
newUnsafeDirectInstance
-
fieldOrder
Description copied from interface:Writer
Indicates the order in which the fields are written by thisWriter
.- Specified by:
fieldOrder
in interfaceWriter
-
complete
Completes the write operation and returns a queue ofAllocatedBuffer
objects in forward-writing order. This method should only be called once.After calling this method, the writer can not be reused. Create a new writer for future writes.
-
writeSFixed32
Description copied from interface:Writer
Writes a field of typeFieldType.SFIXED32
.- Specified by:
writeSFixed32
in interfaceWriter
- Throws:
IOException
-
writeInt64
Description copied from interface:Writer
Writes a field of typeFieldType.INT64
.- Specified by:
writeInt64
in interfaceWriter
- Throws:
IOException
-
writeSFixed64
Description copied from interface:Writer
Writes a field of typeFieldType.SFIXED64
.- Specified by:
writeSFixed64
in interfaceWriter
- Throws:
IOException
-
writeFloat
Description copied from interface:Writer
Writes a field of typeFieldType.FLOAT
.- Specified by:
writeFloat
in interfaceWriter
- Throws:
IOException
-
writeDouble
Description copied from interface:Writer
Writes a field of typeFieldType.DOUBLE
.- Specified by:
writeDouble
in interfaceWriter
- Throws:
IOException
-
writeEnum
Description copied from interface:Writer
Writes a field of typeFieldType.ENUM
.- Specified by:
writeEnum
in interfaceWriter
- Throws:
IOException
-
writeInt32List
public final void writeInt32List(int fieldNumber, List<Integer> list, boolean packed) throws IOException Description copied from interface:Writer
Writes a list field of typeFieldType.INT32
.- Specified by:
writeInt32List
in interfaceWriter
- Throws:
IOException
-
writeInt32List_Internal
private void writeInt32List_Internal(int fieldNumber, List<Integer> list, boolean packed) throws IOException - Throws:
IOException
-
writeInt32List_Internal
private void writeInt32List_Internal(int fieldNumber, IntArrayList list, boolean packed) throws IOException - Throws:
IOException
-
writeFixed32List
public final void writeFixed32List(int fieldNumber, List<Integer> list, boolean packed) throws IOException Description copied from interface:Writer
Writes a list field of typeFieldType.FIXED32
.- Specified by:
writeFixed32List
in interfaceWriter
- Throws:
IOException
-
writeFixed32List_Internal
private void writeFixed32List_Internal(int fieldNumber, List<Integer> list, boolean packed) throws IOException - Throws:
IOException
-
writeFixed32List_Internal
private void writeFixed32List_Internal(int fieldNumber, IntArrayList list, boolean packed) throws IOException - Throws:
IOException
-
writeInt64List
public final void writeInt64List(int fieldNumber, List<Long> list, boolean packed) throws IOException Description copied from interface:Writer
Writes a list field of typeFieldType.INT64
.- Specified by:
writeInt64List
in interfaceWriter
- Throws:
IOException
-
writeUInt64List
public final void writeUInt64List(int fieldNumber, List<Long> list, boolean packed) throws IOException Description copied from interface:Writer
Writes a list field of typeFieldType.UINT64
.- Specified by:
writeUInt64List
in interfaceWriter
- Throws:
IOException
-
writeUInt64List_Internal
private void writeUInt64List_Internal(int fieldNumber, List<Long> list, boolean packed) throws IOException - Throws:
IOException
-
writeUInt64List_Internal
private void writeUInt64List_Internal(int fieldNumber, LongArrayList list, boolean packed) throws IOException - Throws:
IOException
-
writeFixed64List
public final void writeFixed64List(int fieldNumber, List<Long> list, boolean packed) throws IOException Description copied from interface:Writer
Writes a list field of typeFieldType.FIXED64
.- Specified by:
writeFixed64List
in interfaceWriter
- Throws:
IOException
-
writeFixed64List_Internal
private void writeFixed64List_Internal(int fieldNumber, List<Long> list, boolean packed) throws IOException - Throws:
IOException
-
writeFixed64List_Internal
private void writeFixed64List_Internal(int fieldNumber, LongArrayList list, boolean packed) throws IOException - Throws:
IOException
-
writeFloatList
public final void writeFloatList(int fieldNumber, List<Float> list, boolean packed) throws IOException Description copied from interface:Writer
Writes a list field of typeFieldType.FLOAT
.- Specified by:
writeFloatList
in interfaceWriter
- Throws:
IOException
-
writeFloatList_Internal
private void writeFloatList_Internal(int fieldNumber, List<Float> list, boolean packed) throws IOException - Throws:
IOException
-
writeFloatList_Internal
private void writeFloatList_Internal(int fieldNumber, FloatArrayList list, boolean packed) throws IOException - Throws:
IOException
-
writeDoubleList
public final void writeDoubleList(int fieldNumber, List<Double> list, boolean packed) throws IOException Description copied from interface:Writer
Writes a list field of typeFieldType.DOUBLE
.- Specified by:
writeDoubleList
in interfaceWriter
- Throws:
IOException
-
writeDoubleList_Internal
private void writeDoubleList_Internal(int fieldNumber, List<Double> list, boolean packed) throws IOException - Throws:
IOException
-
writeDoubleList_Internal
private void writeDoubleList_Internal(int fieldNumber, DoubleArrayList list, boolean packed) throws IOException - Throws:
IOException
-
writeEnumList
public final void writeEnumList(int fieldNumber, List<Integer> list, boolean packed) throws IOException Description copied from interface:Writer
Writes a list field of typeFieldType.ENUM
.- Specified by:
writeEnumList
in interfaceWriter
- Throws:
IOException
-
writeBoolList
public final void writeBoolList(int fieldNumber, List<Boolean> list, boolean packed) throws IOException Description copied from interface:Writer
Writes a list field of typeFieldType.BOOL
.- Specified by:
writeBoolList
in interfaceWriter
- Throws:
IOException
-
writeBoolList_Internal
private void writeBoolList_Internal(int fieldNumber, List<Boolean> list, boolean packed) throws IOException - Throws:
IOException
-
writeBoolList_Internal
private void writeBoolList_Internal(int fieldNumber, BooleanArrayList list, boolean packed) throws IOException - Throws:
IOException
-
writeStringList
Description copied from interface:Writer
Writes a list field of typeFieldType.STRING
.- Specified by:
writeStringList
in interfaceWriter
- Throws:
IOException
-
writeLazyString
- Throws:
IOException
-
writeBytesList
Description copied from interface:Writer
Writes a list field of typeFieldType.BYTES
.- Specified by:
writeBytesList
in interfaceWriter
- Throws:
IOException
-
writeUInt32List
public final void writeUInt32List(int fieldNumber, List<Integer> list, boolean packed) throws IOException Description copied from interface:Writer
Writes a list field of typeFieldType.UINT32
.- Specified by:
writeUInt32List
in interfaceWriter
- Throws:
IOException
-
writeUInt32List_Internal
private void writeUInt32List_Internal(int fieldNumber, List<Integer> list, boolean packed) throws IOException - Throws:
IOException
-
writeUInt32List_Internal
private void writeUInt32List_Internal(int fieldNumber, IntArrayList list, boolean packed) throws IOException - Throws:
IOException
-
writeSFixed32List
public final void writeSFixed32List(int fieldNumber, List<Integer> list, boolean packed) throws IOException Description copied from interface:Writer
Writes a list field of typeFieldType.SFIXED32
.- Specified by:
writeSFixed32List
in interfaceWriter
- Throws:
IOException
-
writeSFixed64List
public final void writeSFixed64List(int fieldNumber, List<Long> list, boolean packed) throws IOException Description copied from interface:Writer
Writes a list field of typeFieldType.SFIXED64
.- Specified by:
writeSFixed64List
in interfaceWriter
- Throws:
IOException
-
writeSInt32List
public final void writeSInt32List(int fieldNumber, List<Integer> list, boolean packed) throws IOException Description copied from interface:Writer
Writes a list field of typeFieldType.SINT32
.- Specified by:
writeSInt32List
in interfaceWriter
- Throws:
IOException
-
writeSInt32List_Internal
private void writeSInt32List_Internal(int fieldNumber, List<Integer> list, boolean packed) throws IOException - Throws:
IOException
-
writeSInt32List_Internal
private void writeSInt32List_Internal(int fieldNumber, IntArrayList list, boolean packed) throws IOException - Throws:
IOException
-
writeSInt64List
public final void writeSInt64List(int fieldNumber, List<Long> list, boolean packed) throws IOException Description copied from interface:Writer
Writes a list field of typeFieldType.SINT64
.- Specified by:
writeSInt64List
in interfaceWriter
- Throws:
IOException
-
writeMap
public <K,V> void writeMap(int fieldNumber, MapEntryLite.Metadata<K, V> metadata, Map<K, throws IOExceptionV> map) Description copied from interface:Writer
Writes a map field.- Specified by:
writeMap
in interfaceWriter
- Throws:
IOException
-
writeMapEntryField
static final void writeMapEntryField(Writer writer, int fieldNumber, WireFormat.FieldType fieldType, Object object) throws IOException - Throws:
IOException
-
writeSInt64List_Internal
private void writeSInt64List_Internal(int fieldNumber, List<Long> list, boolean packed) throws IOException - Throws:
IOException
-
writeSInt64List_Internal
private void writeSInt64List_Internal(int fieldNumber, LongArrayList list, boolean packed) throws IOException - Throws:
IOException
-
writeMessageList
Description copied from interface:Writer
Writes a list field of typeFieldType.MESSAGE
.- Specified by:
writeMessageList
in interfaceWriter
- Throws:
IOException
-
writeMessageList
Description copied from interface:Writer
Writes a list field of typeFieldType.MESSAGE
.- Specified by:
writeMessageList
in interfaceWriter
- Throws:
IOException
-
writeGroupList
Deprecated.Description copied from interface:Writer
Writes a list field of typeFieldType.GROUP
.- Specified by:
writeGroupList
in interfaceWriter
- Throws:
IOException
-
writeGroupList
@Deprecated public final void writeGroupList(int fieldNumber, List<?> list, Schema schema) throws IOException Deprecated.Description copied from interface:Writer
Writes a list field of typeFieldType.GROUP
.- Specified by:
writeGroupList
in interfaceWriter
- Throws:
IOException
-
writeMessageSetItem
Description copied from interface:Writer
Writes a message field inMessageSet
wire-format.- Specified by:
writeMessageSetItem
in interfaceWriter
- Parameters:
value
- A message instance or an opaqueByteString
for an unknown field.- Throws:
IOException
-
newHeapBuffer
-
newHeapBuffer
-
newDirectBuffer
-
newDirectBuffer
-
getTotalBytesWritten
public abstract int getTotalBytesWritten()Gets the total number of bytes that have been written. This will not be reset by a call tocomplete()
. -
requireSpace
abstract void requireSpace(int size) -
finishCurrentBuffer
abstract void finishCurrentBuffer() -
writeTag
abstract void writeTag(int fieldNumber, int wireType) -
writeVarint32
abstract void writeVarint32(int value) -
writeInt32
abstract void writeInt32(int value) -
writeSInt32
abstract void writeSInt32(int value) -
writeFixed32
abstract void writeFixed32(int value) -
writeVarint64
abstract void writeVarint64(long value) -
writeSInt64
abstract void writeSInt64(long value) -
writeFixed64
abstract void writeFixed64(long value) -
writeBool
abstract void writeBool(boolean value) -
writeString
-
computeUInt64SizeNoTag
private static byte computeUInt64SizeNoTag(long value) Not using the version in CodedOutputStream due to the fact that benchmarks have shown a performance improvement when returning a byte (rather than an int).
-