Class ProtoInput

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

@Internal public final class ProtoInput extends Object
A shared cursor over a Protocol Buffers payload.

Nested messages, packed runs and repeated fields all read from the same underlying bytes, so they share one cursor and differ only in the limit they stop at.

Since:
3.2
  • Constructor Summary

    Constructors
    Constructor
    Description
    ProtoInput(byte[] buffer)
    Create a cursor over a complete payload.
  • Method Summary

    Modifier and Type
    Method
    Description
    byte[]
    Returns the underlying payload.
    int
    Returns the total number of bytes available.
    int
    Returns the current read position.
    void
    position(int position)
    Move the read position.
    byte[]
    readBytes(int limit)
    Read a length-prefixed byte array.
    int
    Read a fixed four-byte little-endian value.
    int
    readFixed32(int limit)
    Read a fixed four-byte little-endian value without reading past a limit.
    long
    Read a fixed eight-byte little-endian value.
    long
    readFixed64(int limit)
    Read a fixed eight-byte little-endian value without reading past a limit.
    int
    Read a length prefix and validate it against the payload.
    int
    readLength(int limit)
    Read a length prefix and validate it against an enclosing structure's limit.
    byte
    Read one byte with no tag or length prefix.
    readString(int limit)
    Read a length-prefixed UTF-8 string.
    int
    Read and validate a field tag.
    int
    Read a variable-width integer as a 32-bit value.
    long
    Read a variable-width integer.
    long
    readVarint64(int limit)
    Read a variable-width integer without reading past an enclosing structure's limit.
    void
    skip(int wireType)
    Skip the value of a field with the given wire type.
    void
    skip(int wireType, int limit)
    Skip the value of a field with the given wire type, without reading past a limit.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • ProtoInput

      public ProtoInput(byte[] buffer)
      Create a cursor over a complete payload.
      Parameters:
      buffer - The payload
  • Method Details

    • buffer

      public byte[] buffer()
      Returns the underlying payload.
      Returns:
      The payload
    • position

      public int position()
      Returns the current read position.
      Returns:
      The position
    • position

      public void position(int position)
      Move the read position.
      Parameters:
      position - The new position
    • length

      public int length()
      Returns the total number of bytes available.
      Returns:
      The length
    • readTag

      public int readTag() throws IOException
      Read and validate a field tag.

      A tag is a 32-bit value, so its varint is at most five bytes and may not be padded out with redundant continuation bytes; a longer one hides a field number that does not fit. Field number zero does not exist. Both are rejected rather than skipped, because a payload containing either is malformed rather than merely unfamiliar.

      Returns:
      The tag
      Throws:
      IOException - If the tag is malformed or the payload is truncated
    • readVarint32

      public int readVarint32() throws IOException
      Read a variable-width integer as a 32-bit value.
      Returns:
      The value
      Throws:
      IOException - If the payload is truncated or malformed
    • readVarint64

      public long readVarint64() throws IOException
      Read a variable-width integer.
      Returns:
      The value
      Throws:
      IOException - If the payload is truncated or malformed
    • readVarint64

      public long readVarint64(int limit) throws IOException
      Read a variable-width integer without reading past an enclosing structure's limit.
      Parameters:
      limit - The position the value must not extend past
      Returns:
      The value
      Throws:
      IOException - If the payload is truncated or malformed
    • readFixed32

      public int readFixed32() throws IOException
      Read a fixed four-byte little-endian value.
      Returns:
      The value
      Throws:
      IOException - If the payload is truncated
    • readFixed32

      public int readFixed32(int limit) throws IOException
      Read a fixed four-byte little-endian value without reading past a limit.
      Parameters:
      limit - The position the value must not extend past
      Returns:
      The value
      Throws:
      IOException - If the payload is truncated
    • readFixed64

      public long readFixed64() throws IOException
      Read a fixed eight-byte little-endian value.
      Returns:
      The value
      Throws:
      IOException - If the payload is truncated
    • readFixed64

      public long readFixed64(int limit) throws IOException
      Read a fixed eight-byte little-endian value without reading past a limit.
      Parameters:
      limit - The position the value must not extend past
      Returns:
      The value
      Throws:
      IOException - If the payload is truncated
    • readBytes

      public byte[] readBytes(int limit) throws IOException
      Read a length-prefixed byte array.
      Parameters:
      limit - The position the value must not extend past
      Returns:
      The bytes
      Throws:
      IOException - If the payload is truncated
    • readString

      public String readString(int limit) throws IOException
      Read a length-prefixed UTF-8 string.
      Parameters:
      limit - The position the value must not extend past
      Returns:
      The string
      Throws:
      IOException - If the payload is truncated
    • readLength

      public int readLength() throws IOException
      Read a length prefix and validate it against the payload.
      Returns:
      The length
      Throws:
      IOException - If the length is negative or exceeds the payload
    • readLength

      public int readLength(int limit) throws IOException
      Read a length prefix and validate it against an enclosing structure's limit.

      Checking against the limit rather than the whole payload is what stops a nested message from claiming bytes that belong to its parent.

      Parameters:
      limit - The position the value must not extend past
      Returns:
      The length
      Throws:
      IOException - If the length is negative or reaches past the limit
    • skip

      public void skip(int wireType) throws IOException
      Skip the value of a field with the given wire type.
      Parameters:
      wireType - The wire type
      Throws:
      IOException - If the payload is truncated or the wire type is unknown
    • skip

      public void skip(int wireType, int limit) throws IOException
      Skip the value of a field with the given wire type, without reading past a limit.
      Parameters:
      wireType - The wire type
      limit - The position the value must not extend past
      Throws:
      IOException - If the payload is truncated or the wire type is unknown
    • readRawByte

      public byte readRawByte() throws IOException
      Read one byte with no tag or length prefix.
      Returns:
      The byte
      Throws:
      IOException - If the payload is truncated