Package io.protostuff
Class ByteArrayInput
java.lang.Object
io.protostuff.ByteArrayInput
- All Implemented Interfaces:
Input
Reads and decodes protocol buffer message fields from an internal byte array buffer. This object is re-usable via
doing a reset on the byte array position and length. This is used internally by
IOUtil
where it catches
ArrayIndexOutOfBoundsException
when a message is truncated.-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final byte[]
final boolean
If true, the nested messages are group-encodedprivate int
private int
private int
private int
-
Constructor Summary
ConstructorsConstructorDescriptionByteArrayInput
(byte[] buffer, boolean decodeNestedMessageAsGroup) ByteArrayInput
(byte[] buffer, int offset, int len, boolean decodeNestedMessageAsGroup) -
Method Summary
Modifier and TypeMethodDescriptionprivate void
Check if this field have been packed into a length-delimited field.void
checkLastTagWas
(int value) Verifies that the last call to readTag() returned the given tag value.int
Returns the current limit (the end index).int
Returns the current offset (the position).int
Returns the last tag.<T> void
handleUnknownField
(int fieldNumber, Schema<T> schema) The underlying implementation should handle the unknown field.boolean
Return true if currently reading packed field<T> T
mergeObject
(T value, Schema<T> schema) Merges an object(with schema) field value.private <T> T
mergeObjectEncodedAsGroup
(T value, Schema<T> schema) boolean
readBool()
Read abool
field value from the internal buffer.byte[]
Reads a byte array field value.Reads a byte array/ByteBuffer value.Reads aByteString
field value.void
readBytes
(ByteBuffer bb) Reads a field value into aByteBuffer
.double
Read adouble
field value from the internal buffer.int
readEnum()
Read an enum field value from the internal buffer.<T> int
readFieldNumber
(Schema<T> schema) Reads the field number of a message/object tied to the givenschema
.int
Read afixed32
field value from the internal buffer.long
Read afixed64
field value from the internal buffer.float
Read afloat
field value from the internal buffer.int
Read anint32
field value from the internal buffer.long
Read anint64
field value from the internal buffer.int
Read a 32-bit little-endian integer from the internal buffer.long
Read a 64-bit little-endian integer from the internal byte buffer.int
Reads a var int 32 from the internal byte buffer.long
Reads a var int 64 from the internal byte buffer.int
Read ansfixed32
field value from the internal buffer.long
Read ansfixed64
field value from the internal buffer.int
Read ansint32
field value from the internal buffer.long
Read ansint64
field value from the internal buffer.Reads aString
field value.int
readTag()
Attempt to read a field tag, returning zero if we have reached EOF.int
Read auint32
field value from the internal buffer.long
Read auint64
field value from the internal buffer.reset
(int offset, int len) Resets the offset and the limit of the internal buffer.setBounds
(int offset, int limit) Sets the offset and limit (which effectively re-uses this input).boolean
skipField
(int tag) Reads and discards a single field, given its tag value.void
Reads and discards an entire message.void
transferByteRangeTo
(Output output, boolean utf8String, int fieldNumber, boolean repeated) Transfer the byte range to the output.
-
Field Details
-
buffer
private final byte[] buffer -
offset
private int offset -
limit
private int limit -
lastTag
private int lastTag -
packedLimit
private int packedLimit -
decodeNestedMessageAsGroup
public final boolean decodeNestedMessageAsGroupIf true, the nested messages are group-encoded
-
-
Constructor Details
-
ByteArrayInput
public ByteArrayInput(byte[] buffer, boolean decodeNestedMessageAsGroup) -
ByteArrayInput
public ByteArrayInput(byte[] buffer, int offset, int len, boolean decodeNestedMessageAsGroup)
-
-
Method Details
-
reset
Resets the offset and the limit of the internal buffer. -
setBounds
Sets the offset and limit (which effectively re-uses this input). -
currentOffset
public int currentOffset()Returns the current offset (the position). -
currentLimit
public int currentLimit()Returns the current limit (the end index). -
isCurrentFieldPacked
public boolean isCurrentFieldPacked()Return true if currently reading packed field -
getLastTag
public int getLastTag()Returns the last tag. -
readTag
Attempt to read a field tag, returning zero if we have reached EOF. Protocol message parsers use this to read tags, since a protocol message may legally end wherever a tag occurs, and zero is not a valid tag number.- Throws:
IOException
-
checkLastTagWas
Verifies that the last call to readTag() returned the given tag value. This is used to verify that a nested group ended with the correct end tag.- Throws:
ProtobufException
-value
does not match the last tag.
-
skipField
Reads and discards a single field, given its tag value.- Returns:
false
if the tag is an endgroup tag, in which case nothing is skipped. Otherwise, returnstrue
.- Throws:
IOException
-
skipMessage
Reads and discards an entire message. This will read either until EOF or until an endgroup tag, whichever comes first.- Throws:
IOException
-
handleUnknownField
Description copied from interface:Input
The underlying implementation should handle the unknown field.- Specified by:
handleUnknownField
in interfaceInput
- Throws:
IOException
-
readFieldNumber
Description copied from interface:Input
Reads the field number of a message/object tied to the givenschema
.- Specified by:
readFieldNumber
in interfaceInput
- Throws:
IOException
-
checkIfPackedField
Check if this field have been packed into a length-delimited field. If so, update internal state to reflect that packed fields are being read.- Throws:
IOException
-
readDouble
Read adouble
field value from the internal buffer.- Specified by:
readDouble
in interfaceInput
- Throws:
IOException
-
readFloat
Read afloat
field value from the internal buffer.- Specified by:
readFloat
in interfaceInput
- Throws:
IOException
-
readUInt64
Read auint64
field value from the internal buffer.- Specified by:
readUInt64
in interfaceInput
- Throws:
IOException
-
readInt64
Read anint64
field value from the internal buffer.- Specified by:
readInt64
in interfaceInput
- Throws:
IOException
-
readInt32
Read anint32
field value from the internal buffer.- Specified by:
readInt32
in interfaceInput
- Throws:
IOException
-
readFixed64
Read afixed64
field value from the internal buffer.- Specified by:
readFixed64
in interfaceInput
- Throws:
IOException
-
readFixed32
Read afixed32
field value from the internal buffer.- Specified by:
readFixed32
in interfaceInput
- Throws:
IOException
-
readBool
Read abool
field value from the internal buffer.- Specified by:
readBool
in interfaceInput
- Throws:
IOException
-
readUInt32
Read auint32
field value from the internal buffer.- Specified by:
readUInt32
in interfaceInput
- Throws:
IOException
-
readEnum
Read an enum field value from the internal buffer. Caller is responsible for converting the numeric value to an actual enum.- Specified by:
readEnum
in interfaceInput
- Throws:
IOException
-
readSFixed32
Read ansfixed32
field value from the internal buffer.- Specified by:
readSFixed32
in interfaceInput
- Throws:
IOException
-
readSFixed64
Read ansfixed64
field value from the internal buffer.- Specified by:
readSFixed64
in interfaceInput
- Throws:
IOException
-
readSInt32
Read ansint32
field value from the internal buffer.- Specified by:
readSInt32
in interfaceInput
- Throws:
IOException
-
readSInt64
Read ansint64
field value from the internal buffer.- Specified by:
readSInt64
in interfaceInput
- Throws:
IOException
-
readString
Description copied from interface:Input
Reads aString
field value.- Specified by:
readString
in interfaceInput
- Throws:
IOException
-
readBytes
Description copied from interface:Input
Reads aByteString
field value.- Specified by:
readBytes
in interfaceInput
- Throws:
IOException
-
readBytes
Description copied from interface:Input
Reads a field value into aByteBuffer
.- Specified by:
readBytes
in interfaceInput
- Throws:
IOException
-
readByteArray
Description copied from interface:Input
Reads a byte array field value.- Specified by:
readByteArray
in interfaceInput
- Throws:
IOException
-
mergeObject
Description copied from interface:Input
Merges an object(with schema) field value. The providedschema
handles the deserialization for the object.- Specified by:
mergeObject
in interfaceInput
- Throws:
IOException
-
mergeObjectEncodedAsGroup
- Throws:
IOException
-
readRawVarint32
Reads a var int 32 from the internal byte buffer.- Throws:
IOException
-
readRawVarint64
Reads a var int 64 from the internal byte buffer.- Throws:
IOException
-
readRawLittleEndian32
Read a 32-bit little-endian integer from the internal buffer.- Throws:
IOException
-
readRawLittleEndian64
Read a 64-bit little-endian integer from the internal byte buffer.- Throws:
IOException
-
transferByteRangeTo
public void transferByteRangeTo(Output output, boolean utf8String, int fieldNumber, boolean repeated) throws IOException Description copied from interface:Input
Transfer the byte range to the output. Capable of zero-copy transfer depending on the type of input.- Specified by:
transferByteRangeTo
in interfaceInput
- Throws:
IOException
-
readByteBuffer
Reads a byte array/ByteBuffer value.- Specified by:
readByteBuffer
in interfaceInput
- Throws:
IOException
-