Class ProtoOutput
java.lang.Object
io.micronaut.serde.protobuf.wire.ProtoOutput
A growable byte sink for Protocol Buffers output.
Nested messages are length-prefixed, so their length has to be known before their payload is
written. Each nested structure therefore writes into its own ProtoOutput, which is copied
into the parent once its length is known. Instances are pooled by the encoder and reused through
reset().
- Since:
- 3.2
-
Constructor Summary
ConstructorsConstructorDescriptionCreate an output with a default initial capacity.ProtoOutput(int initialCapacity) Create an output with the given initial capacity. -
Method Summary
Modifier and TypeMethodDescriptionvoidreset()Discard the written bytes but keep the allocated array, so the instance can be reused.intsize()Returns the number of bytes written so far.byte[]Returns a copy of the written bytes.voidwriteByte(byte value) Write a single byte with no tag or length prefix.voidwriteFixed32(int value) Write a fixed four-byte little-endian value.voidwriteFixed64(long value) Write a fixed eight-byte little-endian value.voidwriteInt32(int value) Write anint32.voidwriteLengthDelimited(byte[] bytes) Write bytes prefixed with their length.voidwriteLengthDelimited(ProtoOutput other) Append the contents of another output, prefixed with its length.voidwriteRaw(byte[] bytes, int offset, int length) Write raw bytes with no length prefix.voidwriteRaw(ProtoOutput other) Append the contents of another output verbatim.voidwriteString(String value) Write a UTF-8 string prefixed with its byte length.voidwriteTag(int tag) Write a field tag.voidwriteTo(OutputStream outputStream) Write the accumulated bytes to a stream.voidwriteVarint(long value) Write an unsigned variable-width integer.
-
Constructor Details
-
ProtoOutput
public ProtoOutput()Create an output with a default initial capacity. -
ProtoOutput
public ProtoOutput(int initialCapacity) Create an output with the given initial capacity.- Parameters:
initialCapacity- The initial capacity in bytes
-
-
Method Details
-
size
public int size()Returns the number of bytes written so far.- Returns:
- The size in bytes
-
reset
public void reset()Discard the written bytes but keep the allocated array, so the instance can be reused. -
writeTag
public void writeTag(int tag) Write a field tag.- Parameters:
tag- The pre-combined tag, fromProtoWire.tag(int, int)
-
writeVarint
public void writeVarint(long value) Write an unsigned variable-width integer.- Parameters:
value- The value, interpreted as unsigned
-
writeInt32
public void writeInt32(int value) Write anint32. Negative values are sign-extended to 64 bits and therefore always occupy ten bytes, matching the reference implementation.- Parameters:
value- The value
-
writeFixed32
public void writeFixed32(int value) Write a fixed four-byte little-endian value.- Parameters:
value- The value
-
writeFixed64
public void writeFixed64(long value) Write a fixed eight-byte little-endian value.- Parameters:
value- The value
-
writeByte
public void writeByte(byte value) Write a single byte with no tag or length prefix.- Parameters:
value- The byte
-
writeRaw
public void writeRaw(byte[] bytes, int offset, int length) Write raw bytes with no length prefix.- Parameters:
bytes- The bytesoffset- The offsetlength- The length
-
writeLengthDelimited
public void writeLengthDelimited(byte[] bytes) Write bytes prefixed with their length.- Parameters:
bytes- The bytes
-
writeString
Write a UTF-8 string prefixed with its byte length.The string is measured and then encoded straight into the buffer, so no intermediate byte array is allocated. Unpaired surrogates are replaced with
'?', which is whatString.getBytes(java.nio.charset.Charset)does for UTF-8.- Parameters:
value- The string
-
writeLengthDelimited
Append the contents of another output, prefixed with its length.- Parameters:
other- The other output
-
writeRaw
Append the contents of another output verbatim.- Parameters:
other- The other output
-
toByteArray
public byte[] toByteArray()Returns a copy of the written bytes.- Returns:
- The bytes
-
writeTo
Write the accumulated bytes to a stream.- Parameters:
outputStream- The stream- Throws:
IOException- If writing fails
-