Package io.protostuff
Class ByteBufferInput
- java.lang.Object
-
- io.protostuff.ByteBufferInput
-
- All Implemented Interfaces:
Input
public final class ByteBufferInput extends java.lang.Object implements 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 byIOUtilwhere it catchesArrayIndexOutOfBoundsExceptionwhen a message is truncated.
-
-
Field Summary
Fields Modifier and Type Field Description private java.nio.ByteBufferbufferbooleandecodeNestedMessageAsGroupIf true, the nested messages are group-encodedprivate intlastTagprivate intpackedLimit
-
Constructor Summary
Constructors Constructor Description ByteBufferInput(java.nio.ByteBuffer buffer, boolean protostuffMessage)An input for a ByteBuffer
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private voidcheckIfPackedField()Check 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.intcurrentLimit()Returns the current limit (the end index).intcurrentOffset()Returns the current offset (the position).intgetLastTag()Returns the last tag.<T> voidhandleUnknownField(int fieldNumber, Schema<T> schema)The underlying implementation should handle the unknown field.booleanisCurrentFieldPacked()Return 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[]readByteArray()Reads a byte array field value.java.nio.ByteBufferreadByteBuffer()Reads a byte array/ByteBuffer value.ByteStringreadBytes()Reads aByteStringfield value.voidreadBytes(java.nio.ByteBuffer bb)Reads a field value into aByteBuffer.doublereadDouble()Read 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.intreadFixed32()Read afixed32field value from the internal buffer.longreadFixed64()Read afixed64field value from the internal buffer.floatreadFloat()Read afloatfield value from the internal buffer.intreadInt32()Read anint32field value from the internal buffer.longreadInt64()Read anint64field value from the internal buffer.intreadRawLittleEndian32()Read a 32-bit little-endian integer from the internal buffer.longreadRawLittleEndian64()Read a 64-bit little-endian integer from the internal byte buffer.intreadRawVarint32()Reads a var int 32 from the internal byte buffer.longreadRawVarint64()Reads a var int 64 from the internal byte buffer.intreadSFixed32()Read ansfixed32field value from the internal buffer.longreadSFixed64()Read ansfixed64field value from the internal buffer.intreadSInt32()Read ansint32field value from the internal buffer.longreadSInt64()Read ansint64field value from the internal buffer.java.lang.StringreadString()Reads aStringfield value.intreadTag()Attempt to read a field tag, returning zero if we have reached EOF.intreadUInt32()Read auint32field value from the internal buffer.longreadUInt64()Read auint64field value from the internal buffer.ByteBufferInputreset(int offset, int len)Resets the offset and the limit of the internal buffer.booleanskipField(int tag)Reads and discards a single field, given its tag value.voidskipMessage()Reads and discards an entire message.voidtransferByteRangeTo(Output output, boolean utf8String, int fieldNumber, boolean repeated)Transfer the byte range to the output.
-
-
-
Method Detail
-
reset
public ByteBufferInput reset(int offset, int len)
Resets the offset and the limit of the internal buffer.
-
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
public int readTag() throws java.io.IOExceptionAttempt 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:
java.io.IOException
-
checkLastTagWas
public void checkLastTagWas(int value) throws ProtobufExceptionVerifies 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
public boolean skipField(int tag) throws java.io.IOExceptionReads 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:
java.io.IOException
-
skipMessage
public void skipMessage() throws java.io.IOExceptionReads and discards an entire message. This will read either until EOF or until an endgroup tag, whichever comes first.- Throws:
java.io.IOException
-
handleUnknownField
public <T> void handleUnknownField(int fieldNumber, Schema<T> schema) throws java.io.IOExceptionDescription copied from interface:InputThe underlying implementation should handle the unknown field.- Specified by:
handleUnknownFieldin interfaceInput- Throws:
java.io.IOException
-
readFieldNumber
public <T> int readFieldNumber(Schema<T> schema) throws java.io.IOException
Description copied from interface:InputReads the field number of a message/object tied to the givenschema.- Specified by:
readFieldNumberin interfaceInput- Throws:
java.io.IOException
-
checkIfPackedField
private void checkIfPackedField() throws java.io.IOExceptionCheck 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:
java.io.IOException
-
readDouble
public double readDouble() throws java.io.IOExceptionRead adoublefield value from the internal buffer.- Specified by:
readDoublein interfaceInput- Throws:
java.io.IOException
-
readFloat
public float readFloat() throws java.io.IOExceptionRead afloatfield value from the internal buffer.
-
readUInt64
public long readUInt64() throws java.io.IOExceptionRead auint64field value from the internal buffer.- Specified by:
readUInt64in interfaceInput- Throws:
java.io.IOException
-
readInt64
public long readInt64() throws java.io.IOExceptionRead anint64field value from the internal buffer.
-
readInt32
public int readInt32() throws java.io.IOExceptionRead anint32field value from the internal buffer.
-
readFixed64
public long readFixed64() throws java.io.IOExceptionRead afixed64field value from the internal buffer.- Specified by:
readFixed64in interfaceInput- Throws:
java.io.IOException
-
readFixed32
public int readFixed32() throws java.io.IOExceptionRead afixed32field value from the internal buffer.- Specified by:
readFixed32in interfaceInput- Throws:
java.io.IOException
-
readBool
public boolean readBool() throws java.io.IOExceptionRead aboolfield value from the internal buffer.
-
readUInt32
public int readUInt32() throws java.io.IOExceptionRead auint32field value from the internal buffer.- Specified by:
readUInt32in interfaceInput- Throws:
java.io.IOException
-
readEnum
public int readEnum() throws java.io.IOExceptionRead an enum field value from the internal buffer. Caller is responsible for converting the numeric value to an actual enum.
-
readSFixed32
public int readSFixed32() throws java.io.IOExceptionRead ansfixed32field value from the internal buffer.- Specified by:
readSFixed32in interfaceInput- Throws:
java.io.IOException
-
readSFixed64
public long readSFixed64() throws java.io.IOExceptionRead ansfixed64field value from the internal buffer.- Specified by:
readSFixed64in interfaceInput- Throws:
java.io.IOException
-
readSInt32
public int readSInt32() throws java.io.IOExceptionRead ansint32field value from the internal buffer.- Specified by:
readSInt32in interfaceInput- Throws:
java.io.IOException
-
readSInt64
public long readSInt64() throws java.io.IOExceptionRead ansint64field value from the internal buffer.- Specified by:
readSInt64in interfaceInput- Throws:
java.io.IOException
-
readString
public java.lang.String readString() throws java.io.IOExceptionDescription copied from interface:InputReads aStringfield value.- Specified by:
readStringin interfaceInput- Throws:
java.io.IOException
-
readBytes
public ByteString readBytes() throws java.io.IOException
Description copied from interface:InputReads aByteStringfield value.
-
readBytes
public void readBytes(java.nio.ByteBuffer bb) throws java.io.IOExceptionDescription copied from interface:InputReads a field value into aByteBuffer.
-
readByteArray
public byte[] readByteArray() throws java.io.IOExceptionDescription copied from interface:InputReads a byte array field value.- Specified by:
readByteArrayin interfaceInput- Throws:
java.io.IOException
-
mergeObject
public <T> T mergeObject(T value, Schema<T> schema) throws java.io.IOExceptionDescription copied from interface:InputMerges an object(with schema) field value. The providedschemahandles the deserialization for the object.- Specified by:
mergeObjectin interfaceInput- Throws:
java.io.IOException
-
mergeObjectEncodedAsGroup
private <T> T mergeObjectEncodedAsGroup(T value, Schema<T> schema) throws java.io.IOException- Throws:
java.io.IOException
-
readRawVarint32
public int readRawVarint32() throws java.io.IOExceptionReads a var int 32 from the internal byte buffer.- Throws:
java.io.IOException
-
readRawVarint64
public long readRawVarint64() throws java.io.IOExceptionReads a var int 64 from the internal byte buffer.- Throws:
java.io.IOException
-
readRawLittleEndian32
public int readRawLittleEndian32() throws java.io.IOExceptionRead a 32-bit little-endian integer from the internal buffer.- Throws:
java.io.IOException
-
readRawLittleEndian64
public long readRawLittleEndian64() throws java.io.IOExceptionRead a 64-bit little-endian integer from the internal byte buffer.- Throws:
java.io.IOException
-
transferByteRangeTo
public void transferByteRangeTo(Output output, boolean utf8String, int fieldNumber, boolean repeated) throws java.io.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:
java.io.IOException
-
readByteBuffer
public java.nio.ByteBuffer readByteBuffer() throws java.io.IOExceptionReads a byte array/ByteBuffer value.- Specified by:
readByteBufferin interfaceInput- Throws:
java.io.IOException
-
-