Class ProtoOutput

java.lang.Object
io.micronaut.serde.protobuf.wire.ProtoOutput

@Internal public final class ProtoOutput extends Object
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

    Constructors
    Constructor
    Description
    Create an output with a default initial capacity.
    ProtoOutput(int initialCapacity)
    Create an output with the given initial capacity.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Discard the written bytes but keep the allocated array, so the instance can be reused.
    int
    Returns the number of bytes written so far.
    byte[]
    Returns a copy of the written bytes.
    void
    writeByte(byte value)
    Write a single byte with no tag or length prefix.
    void
    writeFixed32(int value)
    Write a fixed four-byte little-endian value.
    void
    writeFixed64(long value)
    Write a fixed eight-byte little-endian value.
    void
    writeInt32(int value)
    Write an int32.
    void
    writeLengthDelimited(byte[] bytes)
    Write bytes prefixed with their length.
    void
    Append the contents of another output, prefixed with its length.
    void
    writeRaw(byte[] bytes, int offset, int length)
    Write raw bytes with no length prefix.
    void
    Append the contents of another output verbatim.
    void
    Write a UTF-8 string prefixed with its byte length.
    void
    writeTag(int tag)
    Write a field tag.
    void
    writeTo(OutputStream outputStream)
    Write the accumulated bytes to a stream.
    void
    writeVarint(long value)
    Write an unsigned variable-width integer.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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, from ProtoWire.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 an int32. 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 bytes
      offset - The offset
      length - The length
    • writeLengthDelimited

      public void writeLengthDelimited(byte[] bytes)
      Write bytes prefixed with their length.
      Parameters:
      bytes - The bytes
    • writeString

      public void writeString(String value)
      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 what String.getBytes(java.nio.charset.Charset) does for UTF-8.

      Parameters:
      value - The string
    • writeLengthDelimited

      public void writeLengthDelimited(ProtoOutput other)
      Append the contents of another output, prefixed with its length.
      Parameters:
      other - The other output
    • writeRaw

      public void writeRaw(ProtoOutput other)
      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

      public void writeTo(OutputStream outputStream) throws IOException
      Write the accumulated bytes to a stream.
      Parameters:
      outputStream - The stream
      Throws:
      IOException - If writing fails