Package io.protostuff

Class CodedInput

java.lang.Object
io.protostuff.CodedInput
All Implemented Interfaces:
Input

public final class CodedInput extends Object implements 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

    Fields
    Modifier and Type
    Field
    Description
    private final byte[]
     
    private int
     
    private int
     
    private int
     
    private int
    The absolute position of the end of the current message.
    final boolean
    If true, the nested messages are group-encoded
    (package private) static final int
     
    (package private) static final int
     
    private final InputStream
     
    private int
     
    private int
     
    private int
    See setSizeLimit()
    private int
    The total number of bytes read before the current buffer.
  • Constructor Summary

    Constructors
    Constructor
    Description
    CodedInput(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 Type
    Method
    Description
    private 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.
    static int
    Decode a ZigZag-encoded 32-bit value.
    static long
    Decode a ZigZag-encoded 64-bit value.
    int
    Returns the number of bytes to be read before the current limit.
    int
    Returns the last tag.
    int
    The total bytes read up to the current position.
    <T> void
    handleUnknownField(int fieldNumber, Schema<T> schema)
    The underlying implementation should handle the unknown field.
    boolean
    Returns true if the stream has reached the end of the input.
    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)
    Reads a message field value from the stream (using the group encoding).
    static CodedInput
    newInstance(byte[] buf)
    Create a new CodedInput wrapping the given byte array.
    static CodedInput
    newInstance(byte[] buf, int off, int len)
    Create a new CodedInput wrapping the given byte array slice.
    static CodedInput
    Create a new CodedInput wrapping the given InputStream.
    void
    popLimit(int oldLimit)
    Discards the current limit, returning to the previous limit.
    int
    pushLimit(int byteLimit)
    Note that pushLimit() does NOT affect how many bytes the CodedInputStream reads from an underlying InputStream when refreshing its buffer.
    boolean
    Read a bool field value from the stream.
    byte[]
    Reads a byte array field value.
    Reads a byte array/ByteBuffer value.
    Read a bytes field value from the stream.
    void
    Read a string field value from the stream into a ByteBuffer.
    double
    Read a double field value from the stream.
    int
    Read an enum field value from the stream.
    <T> int
    Reads the field number of a message/object tied to the given schema.
    int
    Read a fixed32 field value from the stream.
    long
    Read a fixed64 field value from the stream.
    float
    Read a float field value from the stream.
    int
    Read an int32 field value from the stream.
    long
    Read an int64 field value from the stream.
    byte
    Read one byte from the input.
    byte[]
    readRawBytes(int size)
    Read a fixed size of bytes from the input.
    int
    Read a 32-bit little-endian integer from the stream.
    long
    Read a 64-bit little-endian integer from the stream.
    int
    Read a raw Varint from the stream.
    (package private) static int
    readRawVarint32(DataInput input, byte firstByte)
    Reads a varint from the input one byte at a time from a DataInput, so that it does not read any bytes after the end of the varint.
    (package private) static int
    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 int
    readRawVarint32(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.
    long
    Read a raw Varint from the stream.
    int
    Read an sfixed32 field value from the stream.
    long
    Read an sfixed64 field value from the stream.
    int
    Read an sint32 field value from the stream.
    long
    Read an sint64 field value from the stream.
    Read a string field value from the stream.
    int
    Attempt to read a field tag, returning zero if we have reached EOF.
    int
    Read a uint32 field value from the stream.
    long
    Read a uint64 field value from the stream.
    private void
     
    private boolean
    refillBuffer(boolean mustSucceed)
    Called with this.buffer is empty to read more bytes from the input.
    void
    Resets the buffer position and limit to re-use this CodedInput object.
    void
    Resets the current size counter to zero (see setSizeLimit(int)).
    int
    setSizeLimit(int limit)
    Set the maximum message size.
    boolean
    skipField(int tag)
    Reads and discards a single field, given its tag value.
    void
    Reads and discards an entire message.
    void
    skipRawBytes(int size)
    Reads and discards size bytes.
    void
    transferByteRangeTo(Output output, boolean utf8String, int fieldNumber, boolean repeated)
    Transfer the byte range to the output.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • buffer

      private final byte[] buffer
    • bufferSize

      private int bufferSize
    • bufferSizeAfterLimit

      private int bufferSizeAfterLimit
    • bufferPos

      private int bufferPos
    • input

      private final InputStream input
    • lastTag

      private int lastTag
    • packedLimit

      private int packedLimit
    • totalBytesRetired

      private int totalBytesRetired
      The total number of bytes read before the current buffer. The total bytes read up to the current position can be computed as totalBytesRetired + 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 currentLimit
      The absolute position of the end of the current message.
    • decodeNestedMessageAsGroup

      public final boolean decodeNestedMessageAsGroup
      If true, the nested messages are group-encoded
    • sizeLimit

      private int sizeLimit
      See 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

      public CodedInput(InputStream input, boolean decodeNestedMessageAsGroup)
    • CodedInput

      public CodedInput(InputStream input, byte[] buffer, boolean decodeNestedMessageAsGroup)
    • CodedInput

      public CodedInput(InputStream input, byte[] buffer, int offset, int limit, boolean decodeNestedMessageAsGroup)
  • Method Details

    • newInstance

      public static CodedInput newInstance(InputStream input)
      Create a new CodedInput wrapping the given InputStream.
    • newInstance

      public static CodedInput newInstance(byte[] buf)
      Create a new CodedInput wrapping the given byte array.
    • newInstance

      public static CodedInput newInstance(byte[] buf, int off, int len)
      Create a new CodedInput wrapping the given byte array slice.
    • readTag

      public int readTag() throws IOException
      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

      public void checkLastTagWas(int value) throws ProtobufException
      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

      public boolean skipField(int tag) throws IOException
      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, returns true.
      Throws:
      IOException
    • skipMessage

      public void skipMessage() throws IOException
      Reads and discards an entire message. This will read either until EOF or until an endgroup tag, whichever comes first.
      Throws:
      IOException
    • readDouble

      public double readDouble() throws IOException
      Read a double field value from the stream.
      Specified by:
      readDouble in interface Input
      Throws:
      IOException
    • readFloat

      public float readFloat() throws IOException
      Read a float field value from the stream.
      Specified by:
      readFloat in interface Input
      Throws:
      IOException
    • readUInt64

      public long readUInt64() throws IOException
      Read a uint64 field value from the stream.
      Specified by:
      readUInt64 in interface Input
      Throws:
      IOException
    • readInt64

      public long readInt64() throws IOException
      Read an int64 field value from the stream.
      Specified by:
      readInt64 in interface Input
      Throws:
      IOException
    • readInt32

      public int readInt32() throws IOException
      Read an int32 field value from the stream.
      Specified by:
      readInt32 in interface Input
      Throws:
      IOException
    • readFixed64

      public long readFixed64() throws IOException
      Read a fixed64 field value from the stream.
      Specified by:
      readFixed64 in interface Input
      Throws:
      IOException
    • readFixed32

      public int readFixed32() throws IOException
      Read a fixed32 field value from the stream.
      Specified by:
      readFixed32 in interface Input
      Throws:
      IOException
    • readBool

      public boolean readBool() throws IOException
      Read a bool field value from the stream.
      Specified by:
      readBool in interface Input
      Throws:
      IOException
    • readString

      public String readString() throws IOException
      Read a string field value from the stream.
      Specified by:
      readString in interface Input
      Throws:
      IOException
    • readBytes

      public void readBytes(ByteBuffer bb) throws IOException
      Read a string field value from the stream into a ByteBuffer.
      Specified by:
      readBytes in interface Input
      Throws:
      IOException
    • mergeObject

      public <T> T mergeObject(T value, Schema<T> schema) throws IOException
      Description copied from interface: Input
      Merges an object(with schema) field value. The provided schema handles the deserialization for the object.
      Specified by:
      mergeObject in interface Input
      Throws:
      IOException
    • mergeObjectEncodedAsGroup

      private <T> T mergeObjectEncodedAsGroup(T value, Schema<T> schema) throws IOException
      Reads a message field value from the stream (using the group encoding).
      Throws:
      IOException
    • readBytes

      public ByteString readBytes() throws IOException
      Read a bytes field value from the stream.
      Specified by:
      readBytes in interface Input
      Throws:
      IOException
    • readUInt32

      public int readUInt32() throws IOException
      Read a uint32 field value from the stream.
      Specified by:
      readUInt32 in interface Input
      Throws:
      IOException
    • readEnum

      public int readEnum() throws IOException
      Read an enum field value from the stream. Caller is responsible for converting the numeric value to an actual enum.
      Specified by:
      readEnum in interface Input
      Throws:
      IOException
    • readSFixed32

      public int readSFixed32() throws IOException
      Read an sfixed32 field value from the stream.
      Specified by:
      readSFixed32 in interface Input
      Throws:
      IOException
    • readSFixed64

      public long readSFixed64() throws IOException
      Read an sfixed64 field value from the stream.
      Specified by:
      readSFixed64 in interface Input
      Throws:
      IOException
    • readSInt32

      public int readSInt32() throws IOException
      Read an sint32 field value from the stream.
      Specified by:
      readSInt32 in interface Input
      Throws:
      IOException
    • readSInt64

      public long readSInt64() throws IOException
      Read an sint64 field value from the stream.
      Specified by:
      readSInt64 in interface Input
      Throws:
      IOException
    • readRawVarint32

      public int readRawVarint32() throws IOException
      Read a raw Varint from the stream. If larger than 32 bits, discard the upper bits.
      Throws:
      IOException
    • readRawVarint32

      static int readRawVarint32(InputStream input) throws IOException
      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 used readRawVarint32(InputStream) then you would probably end up reading past the end of the varint since CodedInput buffers its input.
      Throws:
      IOException
    • readRawVarint32

      static int readRawVarint32(InputStream input, int firstByte) throws IOException
      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 used readRawVarint32(InputStream) then you would probably end up reading past the end of the varint since CodedInput buffers its input.
      Throws:
      IOException
    • readRawVarint32

      static int readRawVarint32(DataInput input, byte firstByte) throws IOException
      Reads a varint from the input one byte at a time from a DataInput, so that it does not read any bytes after the end of the varint.
      Throws:
      IOException
    • readRawVarint64

      public long readRawVarint64() throws IOException
      Read a raw Varint from the stream.
      Throws:
      IOException
    • readRawLittleEndian32

      public int readRawLittleEndian32() throws IOException
      Read a 32-bit little-endian integer from the stream.
      Throws:
      IOException
    • readRawLittleEndian64

      public long readRawLittleEndian64() throws IOException
      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, CodedInput limits 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 an InputStream, 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 (see setSizeLimit(int)). The field totalBytesRetired will 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

      public int pushLimit(int byteLimit) throws ProtobufException
      Note that pushLimit() does NOT affect how many bytes the CodedInputStream reads from an underlying InputStream when refreshing its buffer. If you need to prevent reading past a certain point in the underlying InputStream (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 your InputStream which 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 by pushLimit.
    • 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

      public boolean isAtEnd() throws IOException
      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 using pushLimit(int).
      Throws:
      IOException
    • getTotalBytesRead

      public int getTotalBytesRead()
      The total bytes read up to the current position. Calling resetSizeCounter() resets this value to zero.
    • refillBuffer

      private boolean refillBuffer(boolean mustSucceed) throws IOException
      Called with this.buffer is empty to read more bytes from the input. If mustSucceed is true, refillBuffer() guarantees that either there will be at least one byte in the buffer when it returns or it will throw an exception. If mustSucceed is false, refillBuffer() returns false if no more bytes were available.
      Throws:
      IOException
    • readRawByte

      public byte readRawByte() throws IOException
      Read one byte from the input.
      Throws:
      ProtobufException - The end of the stream or the current limit was reached.
      IOException
    • readRawBytes

      public byte[] readRawBytes(int size) throws IOException
      Read a fixed size of bytes from the input.
      Throws:
      ProtobufException - The end of the stream or the current limit was reached.
      IOException
    • skipRawBytes

      public void skipRawBytes(int size) throws IOException
      Reads and discards size bytes.
      Throws:
      ProtobufException - The end of the stream or the current limit was reached.
      IOException
    • readFieldNumber

      public <T> int readFieldNumber(Schema<T> schema) throws IOException
      Description copied from interface: Input
      Reads the field number of a message/object tied to the given schema.
      Specified by:
      readFieldNumber in interface Input
      Throws:
      IOException
    • checkIfPackedField

      private void checkIfPackedField() throws IOException
      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

      public byte[] readByteArray() throws IOException
      Description copied from interface: Input
      Reads a byte array field value.
      Specified by:
      readByteArray in interface Input
      Throws:
      IOException
    • handleUnknownField

      public <T> void handleUnknownField(int fieldNumber, Schema<T> schema) throws IOException
      Description copied from interface: Input
      The underlying implementation should handle the unknown field.
      Specified by:
      handleUnknownField in interface Input
      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 interface Input
      Throws:
      IOException
    • getLastTag

      public int getLastTag()
      Returns the last tag.
    • readByteBuffer

      public ByteBuffer readByteBuffer() throws IOException
      Reads a byte array/ByteBuffer value.
      Specified by:
      readByteBuffer in interface Input
      Throws:
      IOException