Class CodedInputStream.UnsafeDirectNioDecoder

  • Enclosing class:
    CodedInputStream

    private static final class CodedInputStream.UnsafeDirectNioDecoder
    extends CodedInputStream
    A CodedInputStream implementation that uses a backing direct ByteBuffer as the input. Requires the use of sun.misc.Unsafe to perform fast reads on the buffer.
    • Field Detail

      • buffer

        private final java.nio.ByteBuffer buffer
        The direct buffer that is backing this stream.
      • immutable

        private final boolean immutable
        If true, indicates that the buffer is backing a ByteString and is therefore considered to be an immutable input source.
      • address

        private final long address
        The unsafe address of the content of buffer.
      • limit

        private long limit
        The unsafe address of the current read limit of the buffer.
      • pos

        private long pos
        The unsafe address of the current read position of the buffer.
      • startPos

        private long startPos
        The unsafe address of the starting read position.
      • bufferSizeAfterLimit

        private int bufferSizeAfterLimit
        The amount of available data in the buffer beyond limit.
      • lastTag

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

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

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

      • UnsafeDirectNioDecoder

        private UnsafeDirectNioDecoder​(java.nio.ByteBuffer buffer,
                                       boolean immutable)
    • Method Detail

      • isSupported

        static boolean isSupported()
      • 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
      • skipMessage

        public void skipMessage()
                         throws java.io.IOException
        Description copied from class: CodedInputStream
        Reads and discards an entire message. This will read either until EOF or until an endgroup tag, whichever comes first.
        Specified by:
        skipMessage in class CodedInputStream
        Throws:
        java.io.IOException
      • skipMessage

        public void skipMessage​(CodedOutputStream output)
                         throws java.io.IOException
        Description copied from class: CodedInputStream
        Reads an entire message and writes it to output in wire format. This will read either until EOF or until an endgroup tag, whichever comes first.
        Specified by:
        skipMessage in class CodedInputStream
        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
      • skipRawVarint

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

        private void skipRawVarintFastPath()
                                    throws java.io.IOException
        Throws:
        java.io.IOException
      • skipRawVarintSlowPath

        private void skipRawVarintSlowPath()
                                    throws java.io.IOException
        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
      • 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
      • recomputeBufferSizeAfterLimit

        private void recomputeBufferSizeAfterLimit()
      • remaining

        private int remaining()
      • bufferPos

        private int bufferPos​(long pos)
      • slice

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