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 classWriter that uses safe operations on a targetByteBuffer.private static final classWriter that uses safe operations on target array.private static final classWriter that uses unsafe operations on a targetByteBuffer.private static final classWriter 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 intstatic final intprivate static final intprivate static final int(package private) int -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivateBinaryWriter(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 ofAllocatedBufferobjects in forward-writing order.private static bytecomputeUInt64SizeNoTag(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.FieldOrderIndicates the order in which the fields are written by thisWriter.(package private) abstract voidabstract intGets the total number of bytes that have been written.(package private) static boolean(package private) static boolean(package private) final AllocatedBuffer(package private) final AllocatedBuffernewDirectBuffer(int capacity) static BinaryWriternewDirectInstance(BufferAllocator alloc) Creates a newBinaryWriterthat will allocate direct (i.e.static BinaryWriternewDirectInstance(BufferAllocator alloc, int chunkSize) Creates a newBinaryWriterthat will allocate direct (i.e.(package private) final AllocatedBuffer(package private) final AllocatedBuffernewHeapBuffer(int capacity) static BinaryWriternewHeapInstance(BufferAllocator alloc) Creates a newBinaryWriterthat will allocate heap buffers ofDEFAULT_CHUNK_SIZEas necessary.static BinaryWriternewHeapInstance(BufferAllocator alloc, int chunkSize) Creates a newBinaryWriterthat will allocate heap buffers ofchunkSizeas necessary.(package private) static BinaryWriternewSafeDirectInstance(BufferAllocator alloc, int chunkSize) (package private) static BinaryWriternewSafeHeapInstance(BufferAllocator alloc, int chunkSize) (package private) static BinaryWriternewUnsafeDirectInstance(BufferAllocator alloc, int chunkSize) (package private) static BinaryWriternewUnsafeHeapInstance(BufferAllocator alloc, int chunkSize) (package private) abstract voidrequireSpace(int size) (package private) abstract voidwriteBool(boolean value) final voidwriteBoolList(int fieldNumber, List<Boolean> list, boolean packed) Writes a list field of typeFieldType.BOOL.private voidwriteBoolList_Internal(int fieldNumber, BooleanArrayList list, boolean packed) private voidwriteBoolList_Internal(int fieldNumber, List<Boolean> list, boolean packed) final voidwriteBytesList(int fieldNumber, List<ByteString> list) Writes a list field of typeFieldType.BYTES.final voidwriteDouble(int fieldNumber, double value) Writes a field of typeFieldType.DOUBLE.final voidwriteDoubleList(int fieldNumber, List<Double> list, boolean packed) Writes a list field of typeFieldType.DOUBLE.private voidwriteDoubleList_Internal(int fieldNumber, DoubleArrayList list, boolean packed) private voidwriteDoubleList_Internal(int fieldNumber, List<Double> list, boolean packed) final voidwriteEnum(int fieldNumber, int value) Writes a field of typeFieldType.ENUM.final voidwriteEnumList(int fieldNumber, List<Integer> list, boolean packed) Writes a list field of typeFieldType.ENUM.(package private) abstract voidwriteFixed32(int value) final voidwriteFixed32List(int fieldNumber, List<Integer> list, boolean packed) Writes a list field of typeFieldType.FIXED32.private voidwriteFixed32List_Internal(int fieldNumber, IntArrayList list, boolean packed) private voidwriteFixed32List_Internal(int fieldNumber, List<Integer> list, boolean packed) (package private) abstract voidwriteFixed64(long value) final voidwriteFixed64List(int fieldNumber, List<Long> list, boolean packed) Writes a list field of typeFieldType.FIXED64.private voidwriteFixed64List_Internal(int fieldNumber, LongArrayList list, boolean packed) private voidwriteFixed64List_Internal(int fieldNumber, List<Long> list, boolean packed) final voidwriteFloat(int fieldNumber, float value) Writes a field of typeFieldType.FLOAT.final voidwriteFloatList(int fieldNumber, List<Float> list, boolean packed) Writes a list field of typeFieldType.FLOAT.private voidwriteFloatList_Internal(int fieldNumber, FloatArrayList list, boolean packed) private voidwriteFloatList_Internal(int fieldNumber, List<Float> list, boolean packed) final voidwriteGroupList(int fieldNumber, List<?> list) Deprecated.final voidwriteGroupList(int fieldNumber, List<?> list, Schema schema) Deprecated.(package private) abstract voidwriteInt32(int value) final voidwriteInt32List(int fieldNumber, List<Integer> list, boolean packed) Writes a list field of typeFieldType.INT32.private voidwriteInt32List_Internal(int fieldNumber, IntArrayList list, boolean packed) private voidwriteInt32List_Internal(int fieldNumber, List<Integer> list, boolean packed) final voidwriteInt64(int fieldNumber, long value) Writes a field of typeFieldType.INT64.final voidwriteInt64List(int fieldNumber, List<Long> list, boolean packed) Writes a list field of typeFieldType.INT64.private voidwriteLazyString(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 voidwriteMapEntryField(Writer writer, int fieldNumber, WireFormat.FieldType fieldType, Object object) final voidwriteMessageList(int fieldNumber, List<?> list) Writes a list field of typeFieldType.MESSAGE.final voidwriteMessageList(int fieldNumber, List<?> list, Schema schema) Writes a list field of typeFieldType.MESSAGE.final voidwriteMessageSetItem(int fieldNumber, Object value) Writes a message field inMessageSetwire-format.final voidwriteSFixed32(int fieldNumber, int value) Writes a field of typeFieldType.SFIXED32.final voidwriteSFixed32List(int fieldNumber, List<Integer> list, boolean packed) Writes a list field of typeFieldType.SFIXED32.final voidwriteSFixed64(int fieldNumber, long value) Writes a field of typeFieldType.SFIXED64.final voidwriteSFixed64List(int fieldNumber, List<Long> list, boolean packed) Writes a list field of typeFieldType.SFIXED64.(package private) abstract voidwriteSInt32(int value) final voidwriteSInt32List(int fieldNumber, List<Integer> list, boolean packed) Writes a list field of typeFieldType.SINT32.private voidwriteSInt32List_Internal(int fieldNumber, IntArrayList list, boolean packed) private voidwriteSInt32List_Internal(int fieldNumber, List<Integer> list, boolean packed) (package private) abstract voidwriteSInt64(long value) final voidwriteSInt64List(int fieldNumber, List<Long> list, boolean packed) Writes a list field of typeFieldType.SINT64.private voidwriteSInt64List_Internal(int fieldNumber, LongArrayList list, boolean packed) private voidwriteSInt64List_Internal(int fieldNumber, List<Long> list, boolean packed) (package private) abstract voidwriteString(String in) final voidwriteStringList(int fieldNumber, List<String> list) Writes a list field of typeFieldType.STRING.(package private) abstract voidwriteTag(int fieldNumber, int wireType) final voidwriteUInt32List(int fieldNumber, List<Integer> list, boolean packed) Writes a list field of typeFieldType.UINT32.private voidwriteUInt32List_Internal(int fieldNumber, IntArrayList list, boolean packed) private voidwriteUInt32List_Internal(int fieldNumber, List<Integer> list, boolean packed) final voidwriteUInt64List(int fieldNumber, List<Long> list, boolean packed) Writes a list field of typeFieldType.UINT64.private voidwriteUInt64List_Internal(int fieldNumber, LongArrayList list, boolean packed) private voidwriteUInt64List_Internal(int fieldNumber, List<Long> list, boolean packed) (package private) abstract voidwriteVarint32(int value) (package private) abstract voidwriteVarint64(long value) Methods inherited from class com.google.protobuf.ByteOutput
write, write, write, writeLazy, writeLazyMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods 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 newBinaryWriterthat will allocate heap buffers ofDEFAULT_CHUNK_SIZEas necessary. -
newHeapInstance
Creates a newBinaryWriterthat will allocate heap buffers ofchunkSizeas necessary. -
newDirectInstance
Creates a newBinaryWriterthat will allocate direct (i.e. non-heap) buffers ofDEFAULT_CHUNK_SIZEas necessary. -
newDirectInstance
Creates a newBinaryWriterthat will allocate direct (i.e. non-heap) buffers ofchunkSizeas necessary. -
isUnsafeHeapSupported
static boolean isUnsafeHeapSupported() -
isUnsafeDirectSupported
static boolean isUnsafeDirectSupported() -
newSafeHeapInstance
-
newUnsafeHeapInstance
-
newSafeDirectInstance
-
newUnsafeDirectInstance
-
fieldOrder
Description copied from interface:WriterIndicates the order in which the fields are written by thisWriter.- Specified by:
fieldOrderin interfaceWriter
-
complete
Completes the write operation and returns a queue ofAllocatedBufferobjects 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:WriterWrites a field of typeFieldType.SFIXED32.- Specified by:
writeSFixed32in interfaceWriter- Throws:
IOException
-
writeInt64
Description copied from interface:WriterWrites a field of typeFieldType.INT64.- Specified by:
writeInt64in interfaceWriter- Throws:
IOException
-
writeSFixed64
Description copied from interface:WriterWrites a field of typeFieldType.SFIXED64.- Specified by:
writeSFixed64in interfaceWriter- Throws:
IOException
-
writeFloat
Description copied from interface:WriterWrites a field of typeFieldType.FLOAT.- Specified by:
writeFloatin interfaceWriter- Throws:
IOException
-
writeDouble
Description copied from interface:WriterWrites a field of typeFieldType.DOUBLE.- Specified by:
writeDoublein interfaceWriter- Throws:
IOException
-
writeEnum
Description copied from interface:WriterWrites a field of typeFieldType.ENUM.- Specified by:
writeEnumin interfaceWriter- Throws:
IOException
-
writeInt32List
public final void writeInt32List(int fieldNumber, List<Integer> list, boolean packed) throws IOException Description copied from interface:WriterWrites a list field of typeFieldType.INT32.- Specified by:
writeInt32Listin 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:WriterWrites a list field of typeFieldType.FIXED32.- Specified by:
writeFixed32Listin 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:WriterWrites a list field of typeFieldType.INT64.- Specified by:
writeInt64Listin interfaceWriter- Throws:
IOException
-
writeUInt64List
public final void writeUInt64List(int fieldNumber, List<Long> list, boolean packed) throws IOException Description copied from interface:WriterWrites a list field of typeFieldType.UINT64.- Specified by:
writeUInt64Listin 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:WriterWrites a list field of typeFieldType.FIXED64.- Specified by:
writeFixed64Listin 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:WriterWrites a list field of typeFieldType.FLOAT.- Specified by:
writeFloatListin 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:WriterWrites a list field of typeFieldType.DOUBLE.- Specified by:
writeDoubleListin 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:WriterWrites a list field of typeFieldType.ENUM.- Specified by:
writeEnumListin interfaceWriter- Throws:
IOException
-
writeBoolList
public final void writeBoolList(int fieldNumber, List<Boolean> list, boolean packed) throws IOException Description copied from interface:WriterWrites a list field of typeFieldType.BOOL.- Specified by:
writeBoolListin 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:WriterWrites a list field of typeFieldType.STRING.- Specified by:
writeStringListin interfaceWriter- Throws:
IOException
-
writeLazyString
- Throws:
IOException
-
writeBytesList
Description copied from interface:WriterWrites a list field of typeFieldType.BYTES.- Specified by:
writeBytesListin interfaceWriter- Throws:
IOException
-
writeUInt32List
public final void writeUInt32List(int fieldNumber, List<Integer> list, boolean packed) throws IOException Description copied from interface:WriterWrites a list field of typeFieldType.UINT32.- Specified by:
writeUInt32Listin 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:WriterWrites a list field of typeFieldType.SFIXED32.- Specified by:
writeSFixed32Listin interfaceWriter- Throws:
IOException
-
writeSFixed64List
public final void writeSFixed64List(int fieldNumber, List<Long> list, boolean packed) throws IOException Description copied from interface:WriterWrites a list field of typeFieldType.SFIXED64.- Specified by:
writeSFixed64Listin interfaceWriter- Throws:
IOException
-
writeSInt32List
public final void writeSInt32List(int fieldNumber, List<Integer> list, boolean packed) throws IOException Description copied from interface:WriterWrites a list field of typeFieldType.SINT32.- Specified by:
writeSInt32Listin 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:WriterWrites a list field of typeFieldType.SINT64.- Specified by:
writeSInt64Listin 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:WriterWrites a map field.- Specified by:
writeMapin 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:WriterWrites a list field of typeFieldType.MESSAGE.- Specified by:
writeMessageListin interfaceWriter- Throws:
IOException
-
writeMessageList
Description copied from interface:WriterWrites a list field of typeFieldType.MESSAGE.- Specified by:
writeMessageListin interfaceWriter- Throws:
IOException
-
writeGroupList
Deprecated.Description copied from interface:WriterWrites a list field of typeFieldType.GROUP.- Specified by:
writeGroupListin interfaceWriter- Throws:
IOException
-
writeGroupList
@Deprecated public final void writeGroupList(int fieldNumber, List<?> list, Schema schema) throws IOException Deprecated.Description copied from interface:WriterWrites a list field of typeFieldType.GROUP.- Specified by:
writeGroupListin interfaceWriter- Throws:
IOException
-
writeMessageSetItem
Description copied from interface:WriterWrites a message field inMessageSetwire-format.- Specified by:
writeMessageSetItemin interfaceWritervalue- A message instance or an opaqueByteStringfor 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).
-