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 booleanIf true, the nested messages are group-encodedprivate intprivate intprivate intprivate int -
Constructor Summary
ConstructorsConstructorDescriptionByteArrayInput(byte[] buffer, boolean decodeNestedMessageAsGroup) ByteArrayInput(byte[] buffer, int offset, int len, boolean decodeNestedMessageAsGroup) -
Method Summary
Modifier and TypeMethodDescriptionprivate voidCheck if this field have been packed into a length-delimited field.voidcheckLastTagWas(int value) Verifies that the last call to readTag() returned the given tag value.intReturns the current limit (the end index).intReturns the current offset (the position).intReturns the last tag.<T> voidhandleUnknownField(int fieldNumber, Schema<T> schema) The underlying implementation should handle the unknown field.booleanReturn true if currently reading packed field<T> TmergeObject(T value, Schema<T> schema) Merges an object(with schema) field value.private <T> TmergeObjectEncodedAsGroup(T value, Schema<T> schema) booleanreadBool()Read aboolfield value from the internal buffer.byte[]Reads a byte array field value.Reads a byte array/ByteBuffer value.Reads aByteStringfield value.voidreadBytes(ByteBuffer bb) Reads a field value into aByteBuffer.doubleRead adoublefield value from the internal buffer.intreadEnum()Read an enum field value from the internal buffer.<T> intreadFieldNumber(Schema<T> schema) Reads the field number of a message/object tied to the givenschema.intRead afixed32field value from the internal buffer.longRead afixed64field value from the internal buffer.floatRead afloatfield value from the internal buffer.intRead anint32field value from the internal buffer.longRead anint64field value from the internal buffer.intRead a 32-bit little-endian integer from the internal buffer.longRead a 64-bit little-endian integer from the internal byte buffer.intReads a var int 32 from the internal byte buffer.longReads a var int 64 from the internal byte buffer.intRead ansfixed32field value from the internal buffer.longRead ansfixed64field value from the internal buffer.intRead ansint32field value from the internal buffer.longRead ansint64field value from the internal buffer.Reads aStringfield value.intreadTag()Attempt to read a field tag, returning zero if we have reached EOF.intRead auint32field value from the internal buffer.longRead auint64field 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).booleanskipField(int tag) Reads and discards a single field, given its tag value.voidReads and discards an entire message.voidtransferByteRangeTo(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-valuedoes not match the last tag.
-
skipField
Reads and discards a single field, given its tag value.- Returns:
falseif 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:InputThe underlying implementation should handle the unknown field.- Specified by:
handleUnknownFieldin interfaceInput- Throws:
IOException
-
readFieldNumber
Description copied from interface:InputReads the field number of a message/object tied to the givenschema.- Specified by:
readFieldNumberin 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 adoublefield value from the internal buffer.- Specified by:
readDoublein interfaceInput- Throws:
IOException
-
readFloat
Read afloatfield value from the internal buffer.- Specified by:
readFloatin interfaceInput- Throws:
IOException
-
readUInt64
Read auint64field value from the internal buffer.- Specified by:
readUInt64in interfaceInput- Throws:
IOException
-
readInt64
Read anint64field value from the internal buffer.- Specified by:
readInt64in interfaceInput- Throws:
IOException
-
readInt32
Read anint32field value from the internal buffer.- Specified by:
readInt32in interfaceInput- Throws:
IOException
-
readFixed64
Read afixed64field value from the internal buffer.- Specified by:
readFixed64in interfaceInput- Throws:
IOException
-
readFixed32
Read afixed32field value from the internal buffer.- Specified by:
readFixed32in interfaceInput- Throws:
IOException
-
readBool
Read aboolfield value from the internal buffer.- Specified by:
readBoolin interfaceInput- Throws:
IOException
-
readUInt32
Read auint32field value from the internal buffer.- Specified by:
readUInt32in 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:
readEnumin interfaceInput- Throws:
IOException
-
readSFixed32
Read ansfixed32field value from the internal buffer.- Specified by:
readSFixed32in interfaceInput- Throws:
IOException
-
readSFixed64
Read ansfixed64field value from the internal buffer.- Specified by:
readSFixed64in interfaceInput- Throws:
IOException
-
readSInt32
Read ansint32field value from the internal buffer.- Specified by:
readSInt32in interfaceInput- Throws:
IOException
-
readSInt64
Read ansint64field value from the internal buffer.- Specified by:
readSInt64in interfaceInput- Throws:
IOException
-
readString
Description copied from interface:InputReads aStringfield value.- Specified by:
readStringin interfaceInput- Throws:
IOException
-
readBytes
Description copied from interface:InputReads aByteStringfield value.- Specified by:
readBytesin interfaceInput- Throws:
IOException
-
readBytes
Description copied from interface:InputReads a field value into aByteBuffer.- Specified by:
readBytesin interfaceInput- Throws:
IOException
-
readByteArray
Description copied from interface:InputReads a byte array field value.- Specified by:
readByteArrayin interfaceInput- Throws:
IOException
-
mergeObject
Description copied from interface:InputMerges an object(with schema) field value. The providedschemahandles the deserialization for the object.- Specified by:
mergeObjectin 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:InputTransfer the byte range to the output. Capable of zero-copy transfer depending on the type of input.- Specified by:
transferByteRangeToin interfaceInput- Throws:
IOException
-
readByteBuffer
Reads a byte array/ByteBuffer value.- Specified by:
readByteBufferin interfaceInput- Throws:
IOException
-