Class CodedInput
java.lang.Object
io.protostuff.CodedInput
- All Implemented Interfaces:
Input
Reads and decodes protocol message fields.
This class contains two kinds of methods: methods that read specific protocol message constructs and field types
(e.g. readTag() and readInt32()) and methods that read low-level values (e.g.
readRawVarint32() and readRawBytes(int)). If you are reading encoded protocol messages, you should use
the former methods, but if you are reading some other format of your own design, use the latter.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final byte[]private intprivate intprivate intprivate intThe absolute position of the end of the current message.final booleanIf true, the nested messages are group-encoded(package private) static final int(package private) static final intprivate final InputStreamprivate intprivate intprivate intSee setSizeLimit()private intThe total number of bytes read before the current buffer. -
Constructor Summary
ConstructorsConstructorDescriptionCodedInput(byte[] buffer, int off, int len, boolean decodeNestedMessageAsGroup) CodedInput(InputStream input, boolean decodeNestedMessageAsGroup) CodedInput(InputStream input, byte[] buffer, boolean decodeNestedMessageAsGroup) CodedInput(InputStream input, byte[] buffer, int offset, int limit, 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.static intdecodeZigZag32(int n) Decode a ZigZag-encoded 32-bit value.static longdecodeZigZag64(long n) Decode a ZigZag-encoded 64-bit value.intReturns the number of bytes to be read before the current limit.intReturns the last tag.intThe total bytes read up to the current position.<T> voidhandleUnknownField(int fieldNumber, Schema<T> schema) The underlying implementation should handle the unknown field.booleanisAtEnd()Returns true if the stream has reached the end of the input.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) Reads a message field value from the stream (using thegroupencoding).static CodedInputnewInstance(byte[] buf) Create a new CodedInput wrapping the given byte array.static CodedInputnewInstance(byte[] buf, int off, int len) Create a new CodedInput wrapping the given byte array slice.static CodedInputnewInstance(InputStream input) Create a new CodedInput wrapping the given InputStream.voidpopLimit(int oldLimit) Discards the current limit, returning to the previous limit.intpushLimit(int byteLimit) Note thatpushLimit()does NOT affect how many bytes theCodedInputStreamreads from an underlyingInputStreamwhen refreshing its buffer.booleanreadBool()Read aboolfield value from the stream.byte[]Reads a byte array field value.Reads a byte array/ByteBuffer value.Read abytesfield value from the stream.voidreadBytes(ByteBuffer bb) Read astringfield value from the stream into a ByteBuffer.doubleRead adoublefield value from the stream.intreadEnum()Read an enum field value from the stream.<T> intreadFieldNumber(Schema<T> schema) Reads the field number of a message/object tied to the givenschema.intRead afixed32field value from the stream.longRead afixed64field value from the stream.floatRead afloatfield value from the stream.intRead anint32field value from the stream.longRead anint64field value from the stream.byteRead one byte from the input.byte[]readRawBytes(int size) Read a fixed size of bytes from the input.intRead a 32-bit little-endian integer from the stream.longRead a 64-bit little-endian integer from the stream.intRead a raw Varint from the stream.(package private) static intreadRawVarint32(DataInput input, byte firstByte) Reads a varint from the input one byte at a time from aDataInput, so that it does not read any bytes after the end of the varint.(package private) static intreadRawVarint32(InputStream input) Reads a varint from the input one byte at a time, so that it does not read any bytes after the end of the varint.(package private) static intreadRawVarint32(InputStream input, int firstByte) Reads a varint from the input one byte at a time, so that it does not read any bytes after the end of the varint.longRead a raw Varint from the stream.intRead ansfixed32field value from the stream.longRead ansfixed64field value from the stream.intRead ansint32field value from the stream.longRead ansint64field value from the stream.Read astringfield value from the stream.intreadTag()Attempt to read a field tag, returning zero if we have reached EOF.intRead auint32field value from the stream.longRead auint64field value from the stream.private voidprivate booleanrefillBuffer(boolean mustSucceed) Called withthis.bufferis empty to read more bytes from the input.voidreset()Resets the buffer position and limit to re-use this CodedInput object.voidResets the current size counter to zero (seesetSizeLimit(int)).intsetSizeLimit(int limit) Set the maximum message size.booleanskipField(int tag) Reads and discards a single field, given its tag value.voidReads and discards an entire message.voidskipRawBytes(int size) Reads and discardssizebytes.voidtransferByteRangeTo(Output output, boolean utf8String, int fieldNumber, boolean repeated) Transfer the byte range to the output.
-
Field Details
-
buffer
private final byte[] buffer -
bufferSize
private int bufferSize -
bufferSizeAfterLimit
private int bufferSizeAfterLimit -
bufferPos
private int bufferPos -
input
-
lastTag
private int lastTag -
packedLimit
private int packedLimit -
totalBytesRetired
private int totalBytesRetiredThe total number of bytes read before the current buffer. The total bytes read up to the current position can be computed astotalBytesRetired + bufferPos. This value may be negative if reading started in the middle of the current buffer (e.g. if the constructor that takes a byte array and an offset was used). -
currentLimit
private int currentLimitThe absolute position of the end of the current message. -
decodeNestedMessageAsGroup
public final boolean decodeNestedMessageAsGroupIf true, the nested messages are group-encoded -
sizeLimit
private int sizeLimitSee setSizeLimit() -
DEFAULT_SIZE_LIMIT
static final int DEFAULT_SIZE_LIMIT- See Also:
-
DEFAULT_BUFFER_SIZE
static final int DEFAULT_BUFFER_SIZE- See Also:
-
-
Constructor Details
-
CodedInput
public CodedInput(byte[] buffer, int off, int len, boolean decodeNestedMessageAsGroup) -
CodedInput
-
CodedInput
-
CodedInput
public CodedInput(InputStream input, byte[] buffer, int offset, int limit, boolean decodeNestedMessageAsGroup)
-
-
Method Details
-
newInstance
Create a new CodedInput wrapping the given InputStream. -
newInstance
Create a new CodedInput wrapping the given byte array. -
newInstance
Create a new CodedInput wrapping the given byte array slice. -
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
-
readDouble
Read adoublefield value from the stream.- Specified by:
readDoublein interfaceInput- Throws:
IOException
-
readFloat
Read afloatfield value from the stream.- Specified by:
readFloatin interfaceInput- Throws:
IOException
-
readUInt64
Read auint64field value from the stream.- Specified by:
readUInt64in interfaceInput- Throws:
IOException
-
readInt64
Read anint64field value from the stream.- Specified by:
readInt64in interfaceInput- Throws:
IOException
-
readInt32
Read anint32field value from the stream.- Specified by:
readInt32in interfaceInput- Throws:
IOException
-
readFixed64
Read afixed64field value from the stream.- Specified by:
readFixed64in interfaceInput- Throws:
IOException
-
readFixed32
Read afixed32field value from the stream.- Specified by:
readFixed32in interfaceInput- Throws:
IOException
-
readBool
Read aboolfield value from the stream.- Specified by:
readBoolin interfaceInput- Throws:
IOException
-
readString
Read astringfield value from the stream.- Specified by:
readStringin interfaceInput- Throws:
IOException
-
readBytes
Read astringfield value from the stream into a ByteBuffer.- Specified by:
readBytesin 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
Reads a message field value from the stream (using thegroupencoding).- Throws:
IOException
-
readBytes
Read abytesfield value from the stream.- Specified by:
readBytesin interfaceInput- Throws:
IOException
-
readUInt32
Read auint32field value from the stream.- Specified by:
readUInt32in interfaceInput- Throws:
IOException
-
readEnum
Read an enum field value from the stream. Caller is responsible for converting the numeric value to an actual enum.- Specified by:
readEnumin interfaceInput- Throws:
IOException
-
readSFixed32
Read ansfixed32field value from the stream.- Specified by:
readSFixed32in interfaceInput- Throws:
IOException
-
readSFixed64
Read ansfixed64field value from the stream.- Specified by:
readSFixed64in interfaceInput- Throws:
IOException
-
readSInt32
Read ansint32field value from the stream.- Specified by:
readSInt32in interfaceInput- Throws:
IOException
-
readSInt64
Read ansint64field value from the stream.- Specified by:
readSInt64in interfaceInput- Throws:
IOException
-
readRawVarint32
Read a raw Varint from the stream. If larger than 32 bits, discard the upper bits.- Throws:
IOException
-
readRawVarint32
Reads a varint from the input one byte at a time, so that it does not read any bytes after the end of the varint. If you simply wrapped the stream in a CodedInput and usedreadRawVarint32(InputStream)then you would probably end up reading past the end of the varint since CodedInput buffers its input.- Throws:
IOException
-
readRawVarint32
Reads a varint from the input one byte at a time, so that it does not read any bytes after the end of the varint. If you simply wrapped the stream in a CodedInput and usedreadRawVarint32(InputStream)then you would probably end up reading past the end of the varint since CodedInput buffers its input.- Throws:
IOException
-
readRawVarint32
Reads a varint from the input one byte at a time from aDataInput, so that it does not read any bytes after the end of the varint.- Throws:
IOException
-
readRawVarint64
Read a raw Varint from the stream.- Throws:
IOException
-
readRawLittleEndian32
Read a 32-bit little-endian integer from the stream.- Throws:
IOException
-
readRawLittleEndian64
Read a 64-bit little-endian integer from the stream.- Throws:
IOException
-
decodeZigZag32
public static int decodeZigZag32(int n) Decode a ZigZag-encoded 32-bit value. ZigZag encodes signed integers into values that can be efficiently encoded with varint. (Otherwise, negative values must be sign-extended to 64 bits to be varint encoded, thus always taking 10 bytes on the wire.)- Parameters:
n- An unsigned 32-bit integer, stored in a signed int because Java has no explicit unsigned support.- Returns:
- A signed 32-bit integer.
-
decodeZigZag64
public static long decodeZigZag64(long n) Decode a ZigZag-encoded 64-bit value. ZigZag encodes signed integers into values that can be efficiently encoded with varint. (Otherwise, negative values must be sign-extended to 64 bits to be varint encoded, thus always taking 10 bytes on the wire.)- Parameters:
n- An unsigned 64-bit integer, stored in a signed int because Java has no explicit unsigned support.- Returns:
- A signed 64-bit integer.
-
setSizeLimit
public int setSizeLimit(int limit) Set the maximum message size. In order to prevent malicious messages from exhausting memory or causing integer overflows,CodedInputlimits how large a message may be. The default limit is 64MB. You should set this limit as small as you can without harming your app's functionality. Note that size limits only apply when reading from anInputStream, not when constructed around a raw byte array.If you want to read several messages from a single CodedInput, you could call
resetSizeCounter()after each one to avoid hitting the size limit.- Returns:
- the old limit.
-
resetSizeCounter
public void resetSizeCounter()Resets the current size counter to zero (seesetSizeLimit(int)). The fieldtotalBytesRetiredwill be negative if the initial position was not zero. -
reset
public void reset()Resets the buffer position and limit to re-use this CodedInput object. -
pushLimit
Note thatpushLimit()does NOT affect how many bytes theCodedInputStreamreads from an underlyingInputStreamwhen refreshing its buffer. If you need to prevent reading past a certain point in the underlyingInputStream(e.g. because you expect it to contain more data after the end of the message which you need to handle differently) then you must place a wrapper around yourInputStreamwhich limits the amount of data that can be read from it.- Returns:
- the old limit.
- Throws:
ProtobufException
-
recomputeBufferSizeAfterLimit
private void recomputeBufferSizeAfterLimit() -
popLimit
public void popLimit(int oldLimit) Discards the current limit, returning to the previous limit.- Parameters:
oldLimit- The old limit, as returned bypushLimit.
-
getBytesUntilLimit
public int getBytesUntilLimit()Returns the number of bytes to be read before the current limit. If no limit is set, returns -1. -
isCurrentFieldPacked
public boolean isCurrentFieldPacked()Return true if currently reading packed field -
isAtEnd
Returns true if the stream has reached the end of the input. This is the case if either the end of the underlying input source has been reached or if the stream has reached a limit created usingpushLimit(int).- Throws:
IOException
-
getTotalBytesRead
public int getTotalBytesRead()The total bytes read up to the current position. CallingresetSizeCounter()resets this value to zero. -
refillBuffer
Called withthis.bufferis empty to read more bytes from the input. IfmustSucceedis true, refillBuffer() guarantees that either there will be at least one byte in the buffer when it returns or it will throw an exception. IfmustSucceedis false, refillBuffer() returns false if no more bytes were available.- Throws:
IOException
-
readRawByte
Read one byte from the input.- Throws:
ProtobufException- The end of the stream or the current limit was reached.IOException
-
readRawBytes
Read a fixed size of bytes from the input.- Throws:
ProtobufException- The end of the stream or the current limit was reached.IOException
-
skipRawBytes
Reads and discardssizebytes.- Throws:
ProtobufException- The end of the stream or the current limit was reached.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
-
readByteArray
Description copied from interface:InputReads a byte array field value.- Specified by:
readByteArrayin interfaceInput- Throws:
IOException
-
handleUnknownField
Description copied from interface:InputThe underlying implementation should handle the unknown field.- Specified by:
handleUnknownFieldin interfaceInput- 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
-
getLastTag
public int getLastTag()Returns the last tag. -
readByteBuffer
Reads a byte array/ByteBuffer value.- Specified by:
readByteBufferin interfaceInput- Throws:
IOException
-