-
public final class ProtobufWriterProtobuf wire-format writer.
Instances of this are backed with a
{@code byte}buffer and are used to write and encode a Protobuf payload. The{@code write}family of methods are used to write tagged values. Once writing is complete, the finish method should be invoked. The finish method returns a buffer of the encoded message, with storage shared with buffer backing the writer.{@code * var writer = new ProtobufWriter(); * * writer.writeUtf8(1, "Hello"); * * var first = writer.finish(); * * // ...use `first`... * * // begin writing new message (NOTE: `first` will now be invalid): * writer.writeUtf8(1, "World"); * * var second = writer.finish(); * * // ...use `second`... * }
-
-
Constructor Summary
Constructors Constructor Description ProtobufWriter()Construct new writer.
-
Method Summary
Modifier and Type Method Description ProtobufWriterwriteUnsignedLong(int tag, long val)Write unsigned {@code long}value as{@code varint}, with field tag.ProtobufWriterwriteLong(int tag, long val)Write {@code long}value as{@code varint}, with field tag.ProtobufWriterwriteUnsignedInt(int tag, int val)Write unsigned {@code int}value as{@code varint}, with field tag.ProtobufWriterwriteInt(int tag, int val)Write {@code int}value as{@code varint}, with field tag.ProtobufWriterwriteBoolean(int tag, boolean val)Write {@code boolean}value as{@code varint}, with field tag.ProtobufWriterwriteFixedLong(int tag, long val)Write {@code long}value as{@code fixed64}, with field tag.ProtobufWriterwriteFixedInt(int tag, int val)Write {@code int}value as{@code fixed32}, with field tag.ProtobufWriterwriteFixedDouble(int tag, double val)Write {@code double}value as{@code fixed64}, with field tag.ProtobufWriterwriteFixedFloat(int tag, float val)Write {@code float}value as{@code fixed32}, with field tag.ProtobufWriterwriteByteBuffer(int tag, ByteBuffer src)Write contents of {@code byte}buffer as length-delimited sequence,with field tag.ProtobufWriterwriteByteArray(int tag, Array<byte> src, int off, int len)Write subsequence of {@code byte}array contents as length-delimited sequence, with fieldtag.ProtobufWriterwriteByteArray(int tag, Array<byte> src)Write {@code byte}array contents as length-delimited sequence, with field tag.ProtobufWriterwriteUnsignedLongArray(int tag, Array<long> src, int off, int len)Write subsequence of unsigned {@code long}array contents as packed length-delimitedsequence of{@code varint}values, with field tag.ProtobufWriterwriteUnsignedLongArray(int tag, Array<long> src)Write unsigned {@code long}array contents as packed length-delimited sequence of{@code * varint}values, with field tag.ProtobufWriterwriteLongArray(int tag, Array<long> src, int off, int len)Write subsequence of {@code long}array contents as packed length-delimited sequence of{@code varint}values, with field tag.ProtobufWriterwriteLongArray(int tag, Array<long> src)Write {@code long}array contents as packed length-delimited sequence of{@code varint}values, with field tag.ProtobufWriterwriteUnsignedIntArray(int tag, Array<int> src, int off, int len)Write subsequence of unsigned {@code int}array contents as packed length-delimitedsequence of{@code varint}values, with field tag.ProtobufWriterwriteUnsignedIntArray(int tag, Array<int> src)Write unsigned {@code int}array contents as packed length-delimited sequence of{@code * varint}values, with field tag.ProtobufWriterwriteIntArray(int tag, Array<int> src, int off, int len)Write subsequence of {@code int}array contents as packed length-delimited sequence of{@code varint}values, with field tag.ProtobufWriterwriteIntArray(int tag, Array<int> src)Write {@code int}array contents as packed length-delimited sequence of{@code varint}values, with field tag.ProtobufWriterwriteBooleanArray(int tag, Array<boolean> src, int off, int len)Write subsequence of {@code boolean}array contents as packed length-delimited sequence of{@code varint}values, with field tag.ProtobufWriterwriteBooleanArray(int tag, Array<boolean> src)Write {@code boolean}array contents as packed length-delimited sequence of{@code varint}values, with field tag.ProtobufWriterwriteFixedLongArray(int tag, Array<long> src, int off, int len)Write subsequence of {@code long}array contents as packed length-delimited sequence of{@code fixed64}values, with field tag.ProtobufWriterwriteFixedLongArray(int tag, Array<long> src)Write {@code long}array contents as packed length-delimited sequence of{@code fixed64}values, with field tag.ProtobufWriterwriteFixedIntArray(int tag, Array<int> src, int off, int len)Write subsequence of {@code int}array contents as packed length-delimited sequence of{@code fixed32}values, with field tag.ProtobufWriterwriteFixedIntArray(int tag, Array<int> src)Write {@code int}array contents as packed length-delimited sequence of{@code fixed32}values, with field tag.ProtobufWriterwriteFixedDoubleArray(int tag, Array<double> src, int off, int len)Write subsequence of {@code double}array contents as packed length-delimited sequence of{@code fixed64}values, with field tag.ProtobufWriterwriteFixedDoubleArray(int tag, Array<double> src)Write {@code double}array contents as packed length-delimited sequence of{@code fixed64}values, with field tag.ProtobufWriterwriteFixedFloatArray(int tag, Array<float> src, int off, int len)Write subsequence of {@code float}array contents as packed length-delimited sequence of{@code fixed32}values, with field tag.ProtobufWriterwriteFixedFloatArray(int tag, Array<float> src)Write {@code float}array contents as packed length-delimited sequence of{@code fixed32}values, with field tag.ProtobufWriterwriteUtf8(int tag, String src)Write string as length-delimited UTF-8 sequence, with field tag. ProtobufWriterwriteUtf8Pair(int tag, Pair<String, String> pair)Write pair of strings as length-delimited UTF-8 sequences, with field tag. ProtobufWriterwriteMessage(int tag, ProtobufMessage msg)Write message as length-delimited {@code byte}sequence, with field tag.ProtobufWriterwriteMessage(int tag, Consumer<ProtobufWriter> writer)Write message as length-delimited {@code byte}sequence, with field tag, using a writer.ProtobufWriterwriteRepeatedMessage(int tag, Iterable<out ProtobufMessage> msgs)Write repeated messages as length-delimited {@code byte}sequences, with same field tag.ByteBufferfinish()Finish writing and return shared view of encoded payload. -
-
Method Detail
-
writeUnsignedLong
ProtobufWriter writeUnsignedLong(int tag, long val)
Write unsigned
{@code long}value as{@code varint}, with field tag.- Parameters:
tag- field tagval- value to write
-
writeLong
ProtobufWriter writeLong(int tag, long val)
Write
{@code long}value as{@code varint}, with field tag.- Parameters:
tag- field tagval- value to write
-
writeUnsignedInt
ProtobufWriter writeUnsignedInt(int tag, int val)
Write unsigned
{@code int}value as{@code varint}, with field tag.- Parameters:
tag- field tagval- value to write
-
writeInt
ProtobufWriter writeInt(int tag, int val)
Write
{@code int}value as{@code varint}, with field tag.- Parameters:
tag- field tagval- value to write
-
writeBoolean
ProtobufWriter writeBoolean(int tag, boolean val)
Write
{@code boolean}value as{@code varint}, with field tag.- Parameters:
tag- field tagval- value to write
-
writeFixedLong
ProtobufWriter writeFixedLong(int tag, long val)
Write
{@code long}value as{@code fixed64}, with field tag.- Parameters:
tag- field tagval- value to write
-
writeFixedInt
ProtobufWriter writeFixedInt(int tag, int val)
Write
{@code int}value as{@code fixed32}, with field tag.- Parameters:
tag- field tagval- value to write
-
writeFixedDouble
ProtobufWriter writeFixedDouble(int tag, double val)
Write
{@code double}value as{@code fixed64}, with field tag.- Parameters:
tag- field tagval- value to write
-
writeFixedFloat
ProtobufWriter writeFixedFloat(int tag, float val)
Write
{@code float}value as{@code fixed32}, with field tag.- Parameters:
tag- field tagval- value to write
-
writeByteBuffer
ProtobufWriter writeByteBuffer(int tag, ByteBuffer src)
Write contents of
{@code byte}buffer as length-delimited sequence,with field tag.This writes bytes from
{@code src}, starting at its current position (inclusive) and ending at its limit (exclusive). Upon return the position of{@code src}will beequal to its limit.If
{@code src}has no bytes remaining, this simplyreturns.- Parameters:
tag- field tagsrc- buffer to write from
-
writeByteArray
ProtobufWriter writeByteArray(int tag, Array<byte> src, int off, int len)
Write subsequence of
{@code byte}array contents as length-delimited sequence, with fieldtag.This writes bytes from
{@code src}, starting at{@code off}(inclusive) and ending at{@code off + len}(exclusive). If{@code len}is{@code 0}, this simply returns.- Parameters:
tag- field tagsrc- array to write fromoff- offset, within{@code src}, to begin writing from (inclusive)len- number of bytes to write
-
writeByteArray
ProtobufWriter writeByteArray(int tag, Array<byte> src)
Write
{@code byte}array contents as length-delimited sequence, with field tag.Shorthand for:
{@code * this.writeByteArray(tag, src, 0, src.length); * }- Parameters:
tag- field tagsrc- array to write from
-
writeUnsignedLongArray
ProtobufWriter writeUnsignedLongArray(int tag, Array<long> src, int off, int len)
Write subsequence of unsigned
{@code long}array contents as packed length-delimitedsequence of{@code varint}values, with field tag.This writes unsigned longs from
{@code src}, starting at{@code off}(inclusive) andending at{@code off + len}(exclusive), as a packed sequence of{@code varint}values. If{@code len}is{@code 0}, this simply returns.- Parameters:
tag- field tagsrc- array to write fromoff- offset, within{@code src}, to begin writing from (inclusive)len- number of longs to write
-
writeUnsignedLongArray
ProtobufWriter writeUnsignedLongArray(int tag, Array<long> src)
Write unsigned
{@code long}array contents as packed length-delimited sequence of{@code * varint}values, with field tag.Shorthand for:
{@code * this.writeUnsignedLongArray(tag, src, 0, src.length); * }- Parameters:
tag- field tagsrc- array to write from
-
writeLongArray
ProtobufWriter writeLongArray(int tag, Array<long> src, int off, int len)
Write subsequence of
{@code long}array contents as packed length-delimited sequence of{@code varint}values, with field tag.This writes longs from
{@code src}, starting at{@code off}(inclusive) and ending at{@code off + len}(exclusive), as a packed sequence of{@code varint}values. If{@code len}is{@code 0}, this simply returns.- Parameters:
tag- field tagsrc- array to write fromoff- offset, within{@code src}, to begin writing from (inclusive)len- number of longs to write
-
writeLongArray
ProtobufWriter writeLongArray(int tag, Array<long> src)
Write
{@code long}array contents as packed length-delimited sequence of{@code varint}values, with field tag.Shorthand for:
{@code * this.writeLongArray(tag, src, 0, src.length); * }- Parameters:
tag- field tagsrc- array to write from
-
writeUnsignedIntArray
ProtobufWriter writeUnsignedIntArray(int tag, Array<int> src, int off, int len)
Write subsequence of unsigned
{@code int}array contents as packed length-delimitedsequence of{@code varint}values, with field tag.This writes unsigned ints from
{@code src}, starting at{@code off}(inclusive) andending at{@code off + len}(exclusive), as a packed sequence of{@code varint}values. If{@code len}is{@code 0}, this simply returns.- Parameters:
tag- field tagsrc- array to write fromoff- offset, within{@code src}, to begin writing from (inclusive)len- number of ints to write
-
writeUnsignedIntArray
ProtobufWriter writeUnsignedIntArray(int tag, Array<int> src)
Write unsigned
{@code int}array contents as packed length-delimited sequence of{@code * varint}values, with field tag.Shorthand for:
{@code * this.writeUnsignedIntArray(tag, src, 0, src.length); * }- Parameters:
tag- field tagsrc- array to write from
-
writeIntArray
ProtobufWriter writeIntArray(int tag, Array<int> src, int off, int len)
Write subsequence of
{@code int}array contents as packed length-delimited sequence of{@code varint}values, with field tag.This writes ints from
{@code src}, starting at{@code off}(inclusive) and ending at{@code off + len}(exclusive), as a packed sequence of{@code varint}values. If{@code len}is{@code 0}, this simply returns.- Parameters:
tag- field tagsrc- array to write fromoff- offset, within{@code src}, to begin writing from (inclusive)len- number of ints to write
-
writeIntArray
ProtobufWriter writeIntArray(int tag, Array<int> src)
Write
{@code int}array contents as packed length-delimited sequence of{@code varint}values, with field tag.Shorthand for:
{@code * this.writeIntArray(tag, src, 0, src.length); * }- Parameters:
tag- field tagsrc- array to write from
-
writeBooleanArray
ProtobufWriter writeBooleanArray(int tag, Array<boolean> src, int off, int len)
Write subsequence of
{@code boolean}array contents as packed length-delimited sequence of{@code varint}values, with field tag.This writes booleans from
{@code src}, starting at{@code off}(inclusive) andending at{@code off + len}(exclusive), as a packed sequence of{@code varint}values. If{@code len}is{@code 0}, this simply returns.- Parameters:
tag- field tagsrc- array to write fromoff- offset, within{@code src}, to begin writing from (inclusive)len- number of booleans to write
-
writeBooleanArray
ProtobufWriter writeBooleanArray(int tag, Array<boolean> src)
Write
{@code boolean}array contents as packed length-delimited sequence of{@code varint}values, with field tag.Shorthand for:
{@code * this.writeBooleanArray(tag, src, 0, src.length); * }- Parameters:
tag- field tagsrc- array to write from
-
writeFixedLongArray
ProtobufWriter writeFixedLongArray(int tag, Array<long> src, int off, int len)
Write subsequence of
{@code long}array contents as packed length-delimited sequence of{@code fixed64}values, with field tag.This writes longs from
{@code src}, starting at{@code off}(inclusive) and ending at{@code off + len}(exclusive), as a packed sequence of{@code fixed64}values. If{@code * len}is{@code 0}, this simply returns.- Parameters:
tag- field tagsrc- array to write fromoff- offset, within{@code src}, to begin writing from (inclusive)len- number of longs to write
-
writeFixedLongArray
ProtobufWriter writeFixedLongArray(int tag, Array<long> src)
Write
{@code long}array contents as packed length-delimited sequence of{@code fixed64}values, with field tag.Shorthand for:
{@code * this.writeFixedLongArray(tag, src, 0, src.length); * }- Parameters:
tag- field tagsrc- array to write from
-
writeFixedIntArray
ProtobufWriter writeFixedIntArray(int tag, Array<int> src, int off, int len)
Write subsequence of
{@code int}array contents as packed length-delimited sequence of{@code fixed32}values, with field tag.This writes ints from
{@code src}, starting at{@code off}(inclusive) and ending at{@code off + len}(exclusive), as a packed sequence of{@code fixed32}values. If{@code * len}is{@code 0}, this simply returns.- Parameters:
tag- field tagsrc- array to write fromoff- offset, within{@code src}, to begin writing from (inclusive)len- number of ints to write
-
writeFixedIntArray
ProtobufWriter writeFixedIntArray(int tag, Array<int> src)
Write
{@code int}array contents as packed length-delimited sequence of{@code fixed32}values, with field tag.Shorthand for:
{@code * this.writeFixedIntArray(tag, src, 0, src.length); * }- Parameters:
tag- field tagsrc- array to write from
-
writeFixedDoubleArray
ProtobufWriter writeFixedDoubleArray(int tag, Array<double> src, int off, int len)
Write subsequence of
{@code double}array contents as packed length-delimited sequence of{@code fixed64}values, with field tag.This writes doubles from
{@code src}, starting at{@code off}(inclusive) and ending at{@code off + len}(exclusive), as a packed sequence of{@code fixed64}values. If{@code * len}is{@code 0}, this simply returns.- Parameters:
tag- field tagsrc- array to write fromoff- offset, within{@code src}, to begin writing from (inclusive)len- number of doubles to write
-
writeFixedDoubleArray
ProtobufWriter writeFixedDoubleArray(int tag, Array<double> src)
Write
{@code double}array contents as packed length-delimited sequence of{@code fixed64}values, with field tag.Shorthand for:
{@code * this.writeFixedDoubleArray(tag, src, 0, src.length); * }- Parameters:
tag- field tagsrc- array to write from
-
writeFixedFloatArray
ProtobufWriter writeFixedFloatArray(int tag, Array<float> src, int off, int len)
Write subsequence of
{@code float}array contents as packed length-delimited sequence of{@code fixed32}values, with field tag.This writes floats from
{@code src}, starting at{@code off}(inclusive) and ending at{@code off + len}(exclusive), as a packed sequence of{@code fixed32}values. If{@code * len}is{@code 0}, this simply returns.- Parameters:
tag- field tagsrc- array to write fromoff- offset, within{@code src}, to begin writing from (inclusive)len- number of floats to write
-
writeFixedFloatArray
ProtobufWriter writeFixedFloatArray(int tag, Array<float> src)
Write
{@code float}array contents as packed length-delimited sequence of{@code fixed32}values, with field tag.Shorthand for:
{@code * this.writeFixedFloatArray(tag, src, 0, src.length); * }- Parameters:
tag- field tagsrc- array to write from
-
writeUtf8
ProtobufWriter writeUtf8(int tag, String src)
Write string as length-delimited UTF-8 sequence, with field tag.
If
{@code src}is empty, this simply returns.- Parameters:
tag- field tagsrc- string to write
-
writeUtf8Pair
ProtobufWriter writeUtf8Pair(int tag, Pair<String, String> pair)
Write pair of strings as length-delimited UTF-8 sequences, with field tag.
If both strings in
{@code pair}are empty, this simplyreturns.- Parameters:
tag- field tagpair- pair of strings to write
-
writeMessage
ProtobufWriter writeMessage(int tag, ProtobufMessage msg)
Write message as length-delimited
{@code byte}sequence, with field tag.If the encoded length of
{@code msg}is{@code 0}, this simply returns.- Parameters:
tag- field tagmsg- message to write
-
writeMessage
ProtobufWriter writeMessage(int tag, Consumer<ProtobufWriter> writer)
Write message as length-delimited
{@code byte}sequence, with field tag, using a writer.If the encoded length of
{@code msg}is{@code 0}, this simply returns.- Parameters:
tag- field tagwriter- function to write message with
-
writeRepeatedMessage
ProtobufWriter writeRepeatedMessage(int tag, Iterable<out ProtobufMessage> msgs)
Write repeated messages as length-delimited
{@code byte}sequences, with same field tag.Shorthand for:
{@code * for (var msg : msgs) * this.writeMessage(tag, msg); * }- Parameters:
tag- field tagmsgs- messages to write
-
finish
ByteBuffer finish()
Finish writing and return shared view of encoded payload.
The buffer returned will share its underlying data with this writer. As such, if thiswriter is used again to write additional fields, the returned buffer will contain a malformedcoding. Additionally, upon return, this writer may be used to write another payload.
-
-
-
-