Class ByteArrayInput

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

public final class ByteArrayInput extends 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 by IOUtil where it catches ArrayIndexOutOfBoundsException when a message is truncated.
Author:
David Yu
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    final boolean
    If true, the nested messages are group-encoded
  • Constructor Summary

    Constructors
    Constructor
    Description
    ByteArrayInput(byte[] buffer, boolean decodeNestedMessageAsGroup)
     
    ByteArrayInput(byte[] buffer, int offset, int len, boolean decodeNestedMessageAsGroup)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    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.
    boolean
    Read a bool field value from the internal buffer.
    byte[]
    Reads a byte array field value.
    Reads a byte array/ByteBuffer value.
    Reads a ByteString field value.
    void
    Reads a field value into a ByteBuffer.
    double
    Read a double field value from the internal buffer.
    int
    Read an enum field value from the internal buffer.
    <T> int
    Reads the field number of a message/object tied to the given schema.
    int
    Read a fixed32 field value from the internal buffer.
    long
    Read a fixed64 field value from the internal buffer.
    float
    Read a float field value from the internal buffer.
    int
    Read an int32 field value from the internal buffer.
    long
    Read an int64 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 an sfixed32 field value from the internal buffer.
    long
    Read an sfixed64 field value from the internal buffer.
    int
    Read an sint32 field value from the internal buffer.
    long
    Read an sint64 field value from the internal buffer.
    Reads a String field value.
    int
    Attempt to read a field tag, returning zero if we have reached EOF.
    int
    Read a uint32 field value from the internal buffer.
    long
    Read a uint64 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.

    Methods inherited from class Object

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

    • decodeNestedMessageAsGroup

      public final boolean decodeNestedMessageAsGroup
      If 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

      public ByteArrayInput reset(int offset, int len)
      Resets the offset and the limit of the internal buffer.
    • setBounds

      public ByteArrayInput setBounds(int offset, int limit)
      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

      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
    • 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
    • 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
    • readDouble

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

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

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

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

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

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

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

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

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

      public int readEnum() throws IOException
      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 interface Input
      Throws:
      IOException
    • readSFixed32

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

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

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

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

      public String readString() throws IOException
      Description copied from interface: Input
      Reads a String field value.
      Specified by:
      readString in interface Input
      Throws:
      IOException
    • readBytes

      public ByteString readBytes() throws IOException
      Description copied from interface: Input
      Reads a ByteString field value.
      Specified by:
      readBytes in interface Input
      Throws:
      IOException
    • readBytes

      public void readBytes(ByteBuffer bb) throws IOException
      Description copied from interface: Input
      Reads a field value into a ByteBuffer.
      Specified by:
      readBytes in interface Input
      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
    • 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
    • readRawVarint32

      public int readRawVarint32() throws IOException
      Reads a var int 32 from the internal byte buffer.
      Throws:
      IOException
    • readRawVarint64

      public long readRawVarint64() throws IOException
      Reads a var int 64 from the internal byte buffer.
      Throws:
      IOException
    • readRawLittleEndian32

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

      public long readRawLittleEndian64() throws IOException
      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 interface Input
      Throws:
      IOException
    • readByteBuffer

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