Class CodedInputStream.StreamDecoder

    • Field Detail

      • input

        private final java.io.InputStream input
      • buffer

        private final byte[] buffer
      • bufferSize

        private int bufferSize
        bufferSize represents how many bytes are currently filled in the buffer
      • bufferSizeAfterLimit

        private int bufferSizeAfterLimit
      • pos

        private int pos
      • lastTag

        private int lastTag
      • 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 + pos. 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.
    • Constructor Detail

      • StreamDecoder

        private StreamDecoder​(java.io.InputStream input,
                              int bufferSize)
    • Method Detail

      • read

        private static int read​(java.io.InputStream input,
                                byte[] data,
                                int offset,
                                int length)
                         throws java.io.IOException
        Throws:
        java.io.IOException
      • skip

        private static long skip​(java.io.InputStream input,
                                 long length)
                          throws java.io.IOException
        Throws:
        java.io.IOException
      • available

        private static int available​(java.io.InputStream input)
                              throws java.io.IOException
        Throws:
        java.io.IOException
      • 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
      • 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
      • 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
      • isAtEnd

        public boolean isAtEnd()
                        throws java.io.IOException
        Description copied from class: CodedInputStream
        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 CodedInputStream.pushLimit(int). This function may get blocked when using StreamDecoder as it invokes tryRefillBuffer(int) in this function which will try to read bytes from input.
        Specified by:
        isAtEnd in class CodedInputStream
        Throws:
        java.io.IOException
      • refillBuffer

        private void refillBuffer​(int n)
                           throws java.io.IOException
        Reads more bytes from the input, making at least n bytes available in the buffer. Caller must ensure that the requested space is not yet available, and that the requested space is less than BUFFER_SIZE.
        Throws:
        InvalidProtocolBufferException - The end of the stream or the current limit was reached.
        java.io.IOException
      • tryRefillBuffer

        private boolean tryRefillBuffer​(int n)
                                 throws java.io.IOException
        Tries to read more bytes from the input, making at least n bytes available in the buffer. Caller must ensure that the requested space is not yet available, and that the requested space is less than BUFFER_SIZE.
        Returns:
        true If the bytes could be made available; false 1. Current at the end of the stream 2. The current limit was reached 3. The total size limit was reached
        Throws:
        java.io.IOException
      • readRawBytesSlowPath

        private byte[] readRawBytesSlowPath​(int size,
                                            boolean ensureNoLeakedReferences)
                                     throws java.io.IOException
        Exactly like readRawBytes, but caller must have already checked the fast path: (size <= (bufferSize - pos) && size > 0)

        If ensureNoLeakedReferences is true, the value is guaranteed to have not escaped to untrusted code.

        Throws:
        java.io.IOException
      • readRawBytesSlowPathOneChunk

        private byte[] readRawBytesSlowPathOneChunk​(int size)
                                             throws java.io.IOException
        Attempts to read the data in one byte array when it's safe to do. Returns null if the size to read is too large and needs to be allocated in smaller chunks for security reasons.

        Returns a byte[] that may have escaped to user code via InputStream APIs.

        Throws:
        java.io.IOException
      • readRawBytesSlowPathRemainingChunks

        private java.util.List<byte[]> readRawBytesSlowPathRemainingChunks​(int sizeLeft)
                                                                    throws java.io.IOException
        Reads the remaining data in small chunks from the input stream.

        Returns a byte[] that may have escaped to user code via InputStream APIs.

        Throws:
        java.io.IOException
      • readBytesSlowPath

        private ByteString readBytesSlowPath​(int size)
                                      throws java.io.IOException
        Like readBytes, but caller must have already checked the fast path: (size <= (bufferSize - pos) && size > 0 || size == 0)
        Throws:
        java.io.IOException
      • skipRawBytesSlowPath

        private void skipRawBytesSlowPath​(int size)
                                   throws java.io.IOException
        Exactly like skipRawBytes, but caller must have already checked the fast path: (size <= (bufferSize - pos) && size >= 0)
        Throws:
        java.io.IOException