Package akka.protobuf

Class CodedInputStream


  • public final class CodedInputStream
    extends java.lang.Object
    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.
    • Method Detail

      • newInstance

        public static CodedInputStream newInstance​(java.io.InputStream input)
        Create a new CodedInputStream wrapping the given InputStream.
      • newInstance

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

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

        public int readTag()
                    throws java.io.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:
        java.io.IOException
      • checkLastTagWas

        public void checkLastTagWas​(int value)
                             throws InvalidProtocolBufferException
        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:
        InvalidProtocolBufferException - value does not match the last tag.
      • skipField

        public boolean skipField​(int tag)
                          throws java.io.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:
        java.io.IOException
      • skipMessage

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

        public double readDouble()
                          throws java.io.IOException
        Read a double field value from the stream.
        Throws:
        java.io.IOException
      • readFloat

        public float readFloat()
                        throws java.io.IOException
        Read a float field value from the stream.
        Throws:
        java.io.IOException
      • readUInt64

        public long readUInt64()
                        throws java.io.IOException
        Read a uint64 field value from the stream.
        Throws:
        java.io.IOException
      • readInt64

        public long readInt64()
                       throws java.io.IOException
        Read an int64 field value from the stream.
        Throws:
        java.io.IOException
      • readInt32

        public int readInt32()
                      throws java.io.IOException
        Read an int32 field value from the stream.
        Throws:
        java.io.IOException
      • readFixed64

        public long readFixed64()
                         throws java.io.IOException
        Read a fixed64 field value from the stream.
        Throws:
        java.io.IOException
      • readFixed32

        public int readFixed32()
                        throws java.io.IOException
        Read a fixed32 field value from the stream.
        Throws:
        java.io.IOException
      • readBool

        public boolean readBool()
                         throws java.io.IOException
        Read a bool field value from the stream.
        Throws:
        java.io.IOException
      • readString

        public java.lang.String readString()
                                    throws java.io.IOException
        Read a string field value from the stream.
        Throws:
        java.io.IOException
      • readGroup

        public void readGroup​(int fieldNumber,
                              MessageLite.Builder builder,
                              ExtensionRegistryLite extensionRegistry)
                       throws java.io.IOException
        Read a group field value from the stream.
        Throws:
        java.io.IOException
      • readGroup

        public <T extends MessageLite> T readGroup​(int fieldNumber,
                                                   Parser<T> parser,
                                                   ExtensionRegistryLite extensionRegistry)
                                            throws java.io.IOException
        Read a group field value from the stream.
        Throws:
        java.io.IOException
      • readMessage

        public void readMessage​(MessageLite.Builder builder,
                                ExtensionRegistryLite extensionRegistry)
                         throws java.io.IOException
        Read an embedded message field value from the stream.
        Throws:
        java.io.IOException
      • readMessage

        public <T extends MessageLite> T readMessage​(Parser<T> parser,
                                                     ExtensionRegistryLite extensionRegistry)
                                              throws java.io.IOException
        Read an embedded message field value from the stream.
        Throws:
        java.io.IOException
      • readBytes

        public ByteString readBytes()
                             throws java.io.IOException
        Read a bytes field value from the stream.
        Throws:
        java.io.IOException
      • readUInt32

        public int readUInt32()
                       throws java.io.IOException
        Read a uint32 field value from the stream.
        Throws:
        java.io.IOException
      • readEnum

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

        public int readSFixed32()
                         throws java.io.IOException
        Read an sfixed32 field value from the stream.
        Throws:
        java.io.IOException
      • readSFixed64

        public long readSFixed64()
                          throws java.io.IOException
        Read an sfixed64 field value from the stream.
        Throws:
        java.io.IOException
      • readSInt32

        public int readSInt32()
                       throws java.io.IOException
        Read an sint32 field value from the stream.
        Throws:
        java.io.IOException
      • readSInt64

        public long readSInt64()
                        throws java.io.IOException
        Read an sint64 field value from the stream.
        Throws:
        java.io.IOException
      • readRawVarint32

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

        public static int readRawVarint32​(int firstByte,
                                          java.io.InputStream input)
                                   throws java.io.IOException
        Like readRawVarint32(InputStream), but expects that the caller has already read one byte. This allows the caller to determine if EOF has been reached before attempting to read.
        Throws:
        java.io.IOException
      • readRawVarint64

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

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

        public long readRawLittleEndian64()
                                   throws java.io.IOException
        Read a 64-bit little-endian integer from the stream.
        Throws:
        java.io.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.
      • setRecursionLimit

        public int setRecursionLimit​(int limit)
        Set the maximum message recursion depth. In order to prevent malicious messages from causing stack overflows, CodedInputStream limits how deeply messages may be nested. The default limit is 64.
        Returns:
        the old limit.
      • setSizeLimit

        public int setSizeLimit​(int limit)
        Set the maximum message size. In order to prevent malicious messages from exhausting memory or causing integer overflows, CodedInputStream 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 (nor with ByteString.newCodedInput()).

        If you want to read several messages from a single CodedInputStream, 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)).
      • pushLimit

        public int pushLimit​(int byteLimit)
                      throws InvalidProtocolBufferException
        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.

        Returns:
        the old limit.
        Throws:
        InvalidProtocolBufferException
      • 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.
      • isAtEnd

        public boolean isAtEnd()
                        throws java.io.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:
        java.io.IOException
      • getTotalBytesRead

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

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

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

        public void skipRawBytes​(int size)
                          throws java.io.IOException
        Reads and discards size bytes.
        Throws:
        InvalidProtocolBufferException - The end of the stream or the current limit was reached.
        java.io.IOException