Class ProtobufEncoder

java.lang.Object
io.micronaut.serde.LimitingStream
io.micronaut.serde.protobuf.ProtobufEncoder
All Implemented Interfaces:
Encoder, KeysAwareEncoder, AutoCloseable

@Internal public final class ProtobufEncoder extends LimitingStream implements KeysAwareEncoder
Protocol Buffers implementation of Encoder.

Two things about the wire format shape this class:

  • Tags are written lazily. Serde encodes the key before it knows whether the value is null, but protobuf has no null — an absent field is simply not on the wire. So encodeKey(String) only records the property, and the tag is written when a value actually arrives. encodeNull() discards the pending property instead.
  • Nested structures buffer. A nested message is length-prefixed, and its length is not known until the structure is finished, so each nested message and each repeated field writes into its own buffer and is copied into its parent by finishStructure(). Those buffers are pooled on the root encoder and reused for the rest of the message.
Since:
3.2
  • Constructor Details

    • ProtobufEncoder

      public ProtobufEncoder(LimitingStream.RemainingLimits remainingLimits)
      Create a root encoder. A protobuf payload is a single message, so the root itself writes nothing — it exists to receive the top-level encodeObject(Argument) call.
      Parameters:
      remainingLimits - The nesting limits
  • Method Details

    • toByteArray

      public byte[] toByteArray()
      Returns the encoded message.
      Returns:
      The bytes
    • writeTo

      public void writeTo(OutputStream outputStream) throws IOException
      Write the encoded message to a stream.
      Parameters:
      outputStream - The stream
      Throws:
      IOException - If writing fails
    • encodeObject

      public Encoder encodeObject(io.micronaut.core.type.Argument<?> type) throws IOException
      Description copied from interface: Encoder
      Encodes an object.
      Specified by:
      encodeObject in interface Encoder
      Parameters:
      type - The object type, never null
      Returns:
      The encoder, never null
      Throws:
      IOException - if an error occurs
    • encodeArray

      public Encoder encodeArray(io.micronaut.core.type.Argument<?> type) throws IOException
      Description copied from interface: Encoder
      Encodes an array.
      Specified by:
      encodeArray in interface Encoder
      Parameters:
      type - The array type, never null
      Returns:
      The encoder, never null
      Throws:
      IOException - if an error occurs
    • finishStructure

      public void finishStructure() throws IOException
      Description copied from interface: Encoder
      Finalize a previously created structure, like an array or object.
      Specified by:
      finishStructure in interface Encoder
      Throws:
      IOException - If an error occurs
    • encodeKey

      public void encodeKey(String key) throws IOException
      Description copied from interface: Encoder
      Encode a key.
      Specified by:
      encodeKey in interface Encoder
      Parameters:
      key - The key, never null
      Throws:
      IOException - If an error occurs
    • encodeKey

      public void encodeKey(Keys keys, int index) throws IOException
      Description copied from interface: KeysAwareEncoder
      Encode a key by index from the supplied key set.
      Specified by:
      encodeKey in interface KeysAwareEncoder
      Parameters:
      keys - The keys
      index - The key index
      Throws:
      IOException - If an unrecoverable error occurs
    • encodeString

      public void encodeString(String value) throws IOException
      Description copied from interface: Encoder
      Encode a string.
      Specified by:
      encodeString in interface Encoder
      Parameters:
      value - The string, never null
      Throws:
      IOException - If an error occurs
    • encodeBoolean

      public void encodeBoolean(boolean value) throws IOException
      Description copied from interface: Encoder
      Encode a boolean.
      Specified by:
      encodeBoolean in interface Encoder
      Parameters:
      value - The boolean
      Throws:
      IOException - If an error occurs
    • encodeByte

      public void encodeByte(byte value) throws IOException
      Description copied from interface: Encoder
      Encode a byte.
      Specified by:
      encodeByte in interface Encoder
      Parameters:
      value - The byte
      Throws:
      IOException - If an error occurs
    • encodeShort

      public void encodeShort(short value) throws IOException
      Description copied from interface: Encoder
      Encode a short.
      Specified by:
      encodeShort in interface Encoder
      Parameters:
      value - The short
      Throws:
      IOException - If an error occurs
    • encodeChar

      public void encodeChar(char value) throws IOException
      Description copied from interface: Encoder
      Encode a char.
      Specified by:
      encodeChar in interface Encoder
      Parameters:
      value - The char
      Throws:
      IOException - If an error occurs
    • encodeInt

      public void encodeInt(int value) throws IOException
      Description copied from interface: Encoder
      Encode an int.
      Specified by:
      encodeInt in interface Encoder
      Parameters:
      value - The int
      Throws:
      IOException - If an error occurs
    • encodeLong

      public void encodeLong(long value) throws IOException
      Description copied from interface: Encoder
      Encode a long.
      Specified by:
      encodeLong in interface Encoder
      Parameters:
      value - The long
      Throws:
      IOException - If an error occurs
    • encodeFloat

      public void encodeFloat(float value) throws IOException
      Description copied from interface: Encoder
      Encode a float.
      Specified by:
      encodeFloat in interface Encoder
      Parameters:
      value - The float
      Throws:
      IOException - If an error occurs
    • encodeDouble

      public void encodeDouble(double value) throws IOException
      Description copied from interface: Encoder
      Encode a double.
      Specified by:
      encodeDouble in interface Encoder
      Parameters:
      value - The double
      Throws:
      IOException - If an error occurs
    • encodeBigInteger

      public void encodeBigInteger(BigInteger value) throws IOException
      Description copied from interface: Encoder
      Encode a BigInteger.
      Specified by:
      encodeBigInteger in interface Encoder
      Parameters:
      value - The BigInteger, never null
      Throws:
      IOException - If an error occurs
    • encodeBigDecimal

      public void encodeBigDecimal(BigDecimal value) throws IOException
      Description copied from interface: Encoder
      Encode a BigDecimal.
      Specified by:
      encodeBigDecimal in interface Encoder
      Parameters:
      value - The BigDecimal, never null
      Throws:
      IOException - If an error occurs
    • encodeBinary

      public void encodeBinary(byte[] data) throws IOException
      Description copied from interface: Encoder
      Encode the given binary data. The shape of the data in the output is unspecified, the only requirement is that the equivalent Decoder.decodeBinary() must be able to parse to the same data.
      Specified by:
      encodeBinary in interface Encoder
      Parameters:
      data - The input data
      Throws:
      IOException
    • encodeNull

      public void encodeNull() throws IOException
      Description copied from interface: Encoder
      Encode null.
      Specified by:
      encodeNull in interface Encoder
      Throws:
      IOException - If an error occurs
    • currentPath

      public String currentPath()
      Description copied from interface: Encoder
      Return an analysis of the current path.
      Specified by:
      currentPath in interface Encoder
      Returns:
      The current path if known