Class ProtoInput
java.lang.Object
io.micronaut.serde.protobuf.wire.ProtoInput
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 -
Method Summary
Modifier and TypeMethodDescriptionbyte[]buffer()Returns the underlying payload.intlength()Returns the total number of bytes available.intposition()Returns the current read position.voidposition(int position) Move the read position.byte[]readBytes(int limit) Read a length-prefixed byte array.intRead a fixed four-byte little-endian value.intreadFixed32(int limit) Read a fixed four-byte little-endian value without reading past a limit.longRead a fixed eight-byte little-endian value.longreadFixed64(int limit) Read a fixed eight-byte little-endian value without reading past a limit.intRead a length prefix and validate it against the payload.intreadLength(int limit) Read a length prefix and validate it against an enclosing structure's limit.byteRead one byte with no tag or length prefix.readString(int limit) Read a length-prefixed UTF-8 string.intreadTag()Read and validate a field tag.intRead a variable-width integer as a 32-bit value.longRead a variable-width integer.longreadVarint64(int limit) Read a variable-width integer without reading past an enclosing structure's limit.voidskip(int wireType) Skip the value of a field with the given wire type.voidskip(int wireType, int limit) Skip the value of a field with the given wire type, without reading past a limit.
-
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
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
Read a variable-width integer as a 32-bit value.- Returns:
- The value
- Throws:
IOException- If the payload is truncated or malformed
-
readVarint64
Read a variable-width integer.- Returns:
- The value
- Throws:
IOException- If the payload is truncated or malformed
-
readVarint64
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
Read a fixed four-byte little-endian value.- Returns:
- The value
- Throws:
IOException- If the payload is truncated
-
readFixed32
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
Read a fixed eight-byte little-endian value.- Returns:
- The value
- Throws:
IOException- If the payload is truncated
-
readFixed64
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
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
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
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
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
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
Skip the value of a field with the given wire type, without reading past a limit.- Parameters:
wireType- The wire typelimit- The position the value must not extend past- Throws:
IOException- If the payload is truncated or the wire type is unknown
-
readRawByte
Read one byte with no tag or length prefix.- Returns:
- The byte
- Throws:
IOException- If the payload is truncated
-