Class CodedInputStream.IterableDirectByteBufferDecoder

  • Enclosing class:
    CodedInputStream

    private static final class CodedInputStream.IterableDirectByteBufferDecoder
    extends CodedInputStream
    Implementation of CodedInputStream that uses an as the data source. Requires the use of sun.misc.Unsafe to perform fast reads on the buffer.
    • Field Detail

      • input

        private final java.lang.Iterable<java.nio.ByteBuffer> input
        The object that need to decode.
      • iterator

        private final java.util.Iterator<java.nio.ByteBuffer> iterator
        The Iterator with type ByteBuffer of input
      • currentByteBuffer

        private java.nio.ByteBuffer currentByteBuffer
        The current ByteBuffer;
      • immutable

        private final boolean immutable
        If true, indicates that all the buffers are backing a ByteString and are therefore considered to be an immutable input source.
      • enableAliasing

        private boolean enableAliasing
        If true, indicates that calls to read ByteString or byte[] may return slices of the underlying buffer, rather than copies.
      • totalBufferSize

        private int totalBufferSize
        The global total message length limit
      • bufferSizeAfterCurrentLimit

        private int bufferSizeAfterCurrentLimit
        The amount of available data in the input beyond currentLimit.
      • currentLimit

        private int currentLimit
        The absolute position of the end of the current message.
      • lastTag

        private int lastTag
        The last tag that was read from this stream.
      • totalBytesRead

        private int totalBytesRead
        Total Bytes have been Read from the Iterable ByteBuffer
      • startOffset

        private int startOffset
        The start position offset of the whole message, used as to reset the totalBytesRead
      • currentByteBufferPos

        private long currentByteBufferPos
        The current position for current ByteBuffer
      • currentByteBufferStartPos

        private long currentByteBufferStartPos
      • currentAddress

        private long currentAddress
        If the current ByteBuffer is unsafe-direct based, currentAddress is the start address of this ByteBuffer; otherwise should be zero.
      • currentByteBufferLimit

        private long currentByteBufferLimit
        The limit position for current ByteBuffer
    • Constructor Detail

      • IterableDirectByteBufferDecoder

        private IterableDirectByteBufferDecoder​(java.lang.Iterable<java.nio.ByteBuffer> inputBufs,
                                                int size,
                                                boolean immutableFlag)
        The constructor of Iterable<ByteBuffer> decoder.
        Parameters:
        inputBufs - The input data.
        size - The total size of the input data.
        immutableFlag - whether the input data is immutable.
    • Method Detail

      • tryGetNextByteBuffer

        private void tryGetNextByteBuffer()
      • readTag

        public int readTag()
                    throws java.io.IOException
        Description copied from class: CodedInputStream
        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.
        Specified by:
        readTag in class CodedInputStream
        Throws:
        java.io.IOException
      • skipField

        public boolean skipField​(int tag)
                          throws java.io.IOException
        Description copied from class: CodedInputStream
        Reads and discards a single field, given its tag value.
        Specified by:
        skipField in class CodedInputStream
        Returns:
        false if the tag is an endgroup tag, in which case nothing is skipped. Otherwise, returns true.
        Throws:
        java.io.IOException
      • skipField

        public boolean skipField​(int tag,
                                 CodedOutputStream output)
                          throws java.io.IOException
        Description copied from class: CodedInputStream
        Reads a single field and writes it to output in wire format, given its tag value.
        Specified by:
        skipField in class CodedInputStream
        Returns:
        false if the tag is an endgroup tag, in which case nothing is skipped. Otherwise, returns true.
        Throws:
        java.io.IOException
      • readDouble

        public double readDouble()
                          throws java.io.IOException
        Description copied from class: CodedInputStream
        Read a double field value from the stream.
        Specified by:
        readDouble in class CodedInputStream
        Throws:
        java.io.IOException
      • readFloat

        public float readFloat()
                        throws java.io.IOException
        Description copied from class: CodedInputStream
        Read a float field value from the stream.
        Specified by:
        readFloat in class CodedInputStream
        Throws:
        java.io.IOException
      • readUInt64

        public long readUInt64()
                        throws java.io.IOException
        Description copied from class: CodedInputStream
        Read a uint64 field value from the stream.
        Specified by:
        readUInt64 in class CodedInputStream
        Throws:
        java.io.IOException
      • readInt64

        public long readInt64()
                       throws java.io.IOException
        Description copied from class: CodedInputStream
        Read an int64 field value from the stream.
        Specified by:
        readInt64 in class CodedInputStream
        Throws:
        java.io.IOException
      • readInt32

        public int readInt32()
                      throws java.io.IOException
        Description copied from class: CodedInputStream
        Read an int32 field value from the stream.
        Specified by:
        readInt32 in class CodedInputStream
        Throws:
        java.io.IOException
      • readFixed64

        public long readFixed64()
                         throws java.io.IOException
        Description copied from class: CodedInputStream
        Read a fixed64 field value from the stream.
        Specified by:
        readFixed64 in class CodedInputStream
        Throws:
        java.io.IOException
      • readFixed32

        public int readFixed32()
                        throws java.io.IOException
        Description copied from class: CodedInputStream
        Read a fixed32 field value from the stream.
        Specified by:
        readFixed32 in class CodedInputStream
        Throws:
        java.io.IOException
      • readBool

        public boolean readBool()
                         throws java.io.IOException
        Description copied from class: CodedInputStream
        Read a bool field value from the stream.
        Specified by:
        readBool in class CodedInputStream
        Throws:
        java.io.IOException
      • readString

        public java.lang.String readString()
                                    throws java.io.IOException
        Description copied from class: CodedInputStream
        Read a string field value from the stream. If the stream contains malformed UTF-8, replace the offending bytes with the standard UTF-8 replacement character.
        Specified by:
        readString in class CodedInputStream
        Throws:
        java.io.IOException
      • readByteArray

        public byte[] readByteArray()
                             throws java.io.IOException
        Description copied from class: CodedInputStream
        Read a bytes field value from the stream.
        Specified by:
        readByteArray in class CodedInputStream
        Throws:
        java.io.IOException
      • readByteBuffer

        public java.nio.ByteBuffer readByteBuffer()
                                           throws java.io.IOException
        Description copied from class: CodedInputStream
        Read a bytes field value from the stream.
        Specified by:
        readByteBuffer in class CodedInputStream
        Throws:
        java.io.IOException
      • readUInt32

        public int readUInt32()
                       throws java.io.IOException
        Description copied from class: CodedInputStream
        Read a uint32 field value from the stream.
        Specified by:
        readUInt32 in class CodedInputStream
        Throws:
        java.io.IOException
      • readEnum

        public int readEnum()
                     throws java.io.IOException
        Description copied from class: CodedInputStream
        Read an enum field value from the stream. Caller is responsible for converting the numeric value to an actual enum.
        Specified by:
        readEnum in class CodedInputStream
        Throws:
        java.io.IOException
      • readSFixed32

        public int readSFixed32()
                         throws java.io.IOException
        Description copied from class: CodedInputStream
        Read an sfixed32 field value from the stream.
        Specified by:
        readSFixed32 in class CodedInputStream
        Throws:
        java.io.IOException
      • readSFixed64

        public long readSFixed64()
                          throws java.io.IOException
        Description copied from class: CodedInputStream
        Read an sfixed64 field value from the stream.
        Specified by:
        readSFixed64 in class CodedInputStream
        Throws:
        java.io.IOException
      • readSInt32

        public int readSInt32()
                       throws java.io.IOException
        Description copied from class: CodedInputStream
        Read an sint32 field value from the stream.
        Specified by:
        readSInt32 in class CodedInputStream
        Throws:
        java.io.IOException
      • readSInt64

        public long readSInt64()
                        throws java.io.IOException
        Description copied from class: CodedInputStream
        Read an sint64 field value from the stream.
        Specified by:
        readSInt64 in class CodedInputStream
        Throws:
        java.io.IOException
      • readRawVarint32

        public int readRawVarint32()
                            throws java.io.IOException
        Description copied from class: CodedInputStream
        Read a raw Varint from the stream. If larger than 32 bits, discard the upper bits.
        Specified by:
        readRawVarint32 in class CodedInputStream
        Throws:
        java.io.IOException
      • readRawVarint64

        public long readRawVarint64()
                             throws java.io.IOException
        Description copied from class: CodedInputStream
        Read a raw Varint from the stream.
        Specified by:
        readRawVarint64 in class CodedInputStream
        Throws:
        java.io.IOException
      • readRawVarint64SlowPath

        long readRawVarint64SlowPath()
                              throws java.io.IOException
        Description copied from class: CodedInputStream
        Variant of readRawVarint64 for when uncomfortably close to the limit.
        Specified by:
        readRawVarint64SlowPath in class CodedInputStream
        Throws:
        java.io.IOException
      • readRawLittleEndian32

        public int readRawLittleEndian32()
                                  throws java.io.IOException
        Description copied from class: CodedInputStream
        Read a 32-bit little-endian integer from the stream.
        Specified by:
        readRawLittleEndian32 in class CodedInputStream
        Throws:
        java.io.IOException
      • readRawLittleEndian64

        public long readRawLittleEndian64()
                                   throws java.io.IOException
        Description copied from class: CodedInputStream
        Read a 64-bit little-endian integer from the stream.
        Specified by:
        readRawLittleEndian64 in class CodedInputStream
        Throws:
        java.io.IOException
      • enableAliasing

        public void enableAliasing​(boolean enabled)
        Description copied from class: CodedInputStream
        Enables ByteString aliasing of the underlying buffer, trading off on buffer pinning for data copies. Only valid for buffer-backed streams.
        Specified by:
        enableAliasing in class CodedInputStream
      • pushLimit

        public int pushLimit​(int byteLimit)
                      throws InvalidProtocolBufferException
        Description copied from class: CodedInputStream
        Sets currentLimit to (current position) + byteLimit. This is called when descending into a length-delimited embedded message.

        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.

        Specified by:
        pushLimit in class CodedInputStream
        Returns:
        the old limit.
        Throws:
        InvalidProtocolBufferException
      • recomputeBufferSizeAfterLimit

        private void recomputeBufferSizeAfterLimit()
      • popLimit

        public void popLimit​(int oldLimit)
        Description copied from class: CodedInputStream
        Discards the current limit, returning to the previous limit.
        Specified by:
        popLimit in class CodedInputStream
        Parameters:
        oldLimit - The old limit, as returned by pushLimit.
      • getBytesUntilLimit

        public int getBytesUntilLimit()
        Description copied from class: CodedInputStream
        Returns the number of bytes to be read before the current limit. If no limit is set, returns -1.
        Specified by:
        getBytesUntilLimit in class CodedInputStream
      • readRawBytesTo

        private void readRawBytesTo​(byte[] bytes,
                                    int offset,
                                    int length)
                             throws java.io.IOException
        Try to get raw bytes from input with the size of length and copy to bytes array. If the size is bigger than the number of remaining bytes in the input, then throw truncatedMessage exception.
        Throws:
        java.io.IOException
      • skipRawVarint

        private void skipRawVarint()
                            throws java.io.IOException
        Throws:
        java.io.IOException
      • remaining

        private int remaining()
        Try to get the number of remaining bytes in input.
        Returns:
        the number of remaining bytes in input.
      • currentRemaining

        private long currentRemaining()
        Try to get the number of remaining bytes in currentByteBuffer.
        Returns:
        the number of remaining bytes in currentByteBuffer
      • slice

        private java.nio.ByteBuffer slice​(int begin,
                                          int end)
                                   throws java.io.IOException
        Throws:
        java.io.IOException