Package com.google.protobuf
Class CodedInputStream
java.lang.Object
com.google.protobuf.CodedInputStream
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.- Author:
- kenton@google.com Kenton Varda
-
Method Summary
Modifier and TypeMethodDescriptionvoid
checkLastTagWas
(int value) Verifies that the last call to readTag() returned the given tag value.static int
decodeZigZag32
(int n) Decode a ZigZag-encoded 32-bit value.static long
decodeZigZag64
(long n) Decode a ZigZag-encoded 64-bit value.void
enableAliasing
(boolean enabled) int
Returns the number of bytes to be read before the current limit.int
int
The total bytes read up to the current position.boolean
isAtEnd()
Returns true if the stream has reached the end of the input.static CodedInputStream
newInstance
(byte[] buf) Create a new CodedInputStream wrapping the given byte array.static CodedInputStream
newInstance
(byte[] buf, int off, int len) Create a new CodedInputStream wrapping the given byte array slice.static CodedInputStream
newInstance
(InputStream input) Create a new CodedInputStream wrapping the given InputStream.static CodedInputStream
newInstance
(ByteBuffer buf) Create a new CodedInputStream wrapping the given ByteBuffer.void
popLimit
(int oldLimit) Discards the current limit, returning to the previous limit.int
pushLimit
(int byteLimit) SetscurrentLimit
to (current position) +byteLimit
.boolean
readBool()
Read abool
field value from the stream.byte[]
Read abytes
field value from the stream.Read abytes
field value from the stream.Read abytes
field value from the stream.double
Read adouble
field value from the stream.int
readEnum()
Read an enum field value from the stream.int
Read afixed32
field value from the stream.long
Read afixed64
field value from the stream.float
Read afloat
field value from the stream.void
readGroup
(int fieldNumber, MessageLite.Builder builder, ExtensionRegistryLite extensionRegistry) Read agroup
field value from the stream.<T extends MessageLite>
TreadGroup
(int fieldNumber, Parser<T> parser, ExtensionRegistryLite extensionRegistry) Read agroup
field value from the stream.int
Read anint32
field value from the stream.long
Read anint64
field value from the stream.void
readMessage
(MessageLite.Builder builder, ExtensionRegistryLite extensionRegistry) Read an embedded message field value from the stream.<T extends MessageLite>
TreadMessage
(Parser<T> parser, ExtensionRegistryLite extensionRegistry) Read an embedded message field value from the stream.byte
Read one byte from the input.byte[]
readRawBytes
(int size) Read a fixed size of bytes from the input.int
Read a 32-bit little-endian integer from the stream.long
Read a 64-bit little-endian integer from the stream.int
Read a raw Varint from the stream.static int
readRawVarint32
(int firstByte, InputStream input) LikereadRawVarint32(InputStream)
, but expects that the caller has already read one byte.long
Read a raw Varint from the stream.int
Read ansfixed32
field value from the stream.long
Read ansfixed64
field value from the stream.int
Read ansint32
field value from the stream.long
Read ansint64
field value from the stream.Read astring
field value from the stream.Read astring
field value from the stream.int
readTag()
Attempt to read a field tag, returning zero if we have reached EOF.int
Read auint32
field value from the stream.long
Read auint64
field value from the stream.void
readUnknownGroup
(int fieldNumber, MessageLite.Builder builder) Deprecated.void
Resets the current size counter to zero (seesetSizeLimit(int)
).int
setRecursionLimit
(int limit) Set the maximum message recursion depth.int
setSizeLimit
(int limit) Set the maximum message size.boolean
skipField
(int tag) Reads and discards a single field, given its tag value.boolean
skipField
(int tag, CodedOutputStream output) Reads a single field and writes it to output in wire format, given its tag value.void
Reads and discards an entire message.void
skipMessage
(CodedOutputStream output) Reads an entire message and writes it to output in wire format.void
skipRawBytes
(int size) Reads and discardssize
bytes.
-
Method Details
-
newInstance
Create a new CodedInputStream wrapping the given InputStream. -
newInstance
Create a new CodedInputStream wrapping the given byte array. -
newInstance
Create a new CodedInputStream wrapping the given byte array slice. -
newInstance
Create a new CodedInputStream wrapping the given ByteBuffer. The data starting from the ByteBuffer's current position to its limit will be read. The returned CodedInputStream may or may not share the underlying data in the ByteBuffer, therefore the ByteBuffer cannot be changed while the CodedInputStream is in use. Note that the ByteBuffer's position won't be changed by this function. Concurrent calls with the same ByteBuffer object are safe if no other thread is trying to alter the ByteBuffer's status. -
readTag
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
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.
-
getLastTag
public int getLastTag() -
skipField
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, returnstrue
.- Throws:
IOException
-
skipField
Reads a single field and writes it to output in wire format, given its tag value.- Returns:
false
if the tag is an endgroup tag, in which case nothing is skipped. Otherwise, returnstrue
.- Throws:
IOException
-
skipMessage
Reads and discards an entire message. This will read either until EOF or until an endgroup tag, whichever comes first.- Throws:
IOException
-
skipMessage
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.- Throws:
IOException
-
readDouble
Read adouble
field value from the stream.- Throws:
IOException
-
readFloat
Read afloat
field value from the stream.- Throws:
IOException
-
readUInt64
Read auint64
field value from the stream.- Throws:
IOException
-
readInt64
Read anint64
field value from the stream.- Throws:
IOException
-
readInt32
Read anint32
field value from the stream.- Throws:
IOException
-
readFixed64
Read afixed64
field value from the stream.- Throws:
IOException
-
readFixed32
Read afixed32
field value from the stream.- Throws:
IOException
-
readBool
Read abool
field value from the stream.- Throws:
IOException
-
readString
Read astring
field value from the stream. If the stream contains malformed UTF-8, replace the offending bytes with the standard UTF-8 replacement character.- Throws:
IOException
-
readStringRequireUtf8
Read astring
field value from the stream. If the stream contains malformed UTF-8, throw exceptionInvalidProtocolBufferException
.- Throws:
IOException
-
readGroup
public void readGroup(int fieldNumber, MessageLite.Builder builder, ExtensionRegistryLite extensionRegistry) throws IOException Read agroup
field value from the stream.- Throws:
IOException
-
readGroup
public <T extends MessageLite> T readGroup(int fieldNumber, Parser<T> parser, ExtensionRegistryLite extensionRegistry) throws IOException Read agroup
field value from the stream.- Throws:
IOException
-
readUnknownGroup
@Deprecated public void readUnknownGroup(int fieldNumber, MessageLite.Builder builder) throws IOException Deprecated.UnknownFieldSet.Builder now implements MessageLite.Builder, so you can just callreadGroup(int, com.google.protobuf.MessageLite.Builder, com.google.protobuf.ExtensionRegistryLite)
.Reads agroup
field value from the stream and merges it into the givenUnknownFieldSet
.- Throws:
IOException
-
readMessage
public void readMessage(MessageLite.Builder builder, ExtensionRegistryLite extensionRegistry) throws IOException Read an embedded message field value from the stream.- Throws:
IOException
-
readMessage
public <T extends MessageLite> T readMessage(Parser<T> parser, ExtensionRegistryLite extensionRegistry) throws IOException Read an embedded message field value from the stream.- Throws:
IOException
-
readBytes
Read abytes
field value from the stream.- Throws:
IOException
-
readByteArray
Read abytes
field value from the stream.- Throws:
IOException
-
readByteBuffer
Read abytes
field value from the stream.- Throws:
IOException
-
readUInt32
Read auint32
field value from the stream.- Throws:
IOException
-
readEnum
Read an enum field value from the stream. Caller is responsible for converting the numeric value to an actual enum.- Throws:
IOException
-
readSFixed32
Read ansfixed32
field value from the stream.- Throws:
IOException
-
readSFixed64
Read ansfixed64
field value from the stream.- Throws:
IOException
-
readSInt32
Read ansint32
field value from the stream.- Throws:
IOException
-
readSInt64
Read ansint64
field value from the stream.- Throws:
IOException
-
readRawVarint32
Read a raw Varint from the stream. If larger than 32 bits, discard the upper bits.- Throws:
IOException
-
readRawVarint32
LikereadRawVarint32(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:
IOException
-
readRawVarint64
Read a raw Varint from the stream.- Throws:
IOException
-
readRawLittleEndian32
Read a 32-bit little-endian integer from the stream.- Throws:
IOException
-
readRawLittleEndian64
Read a 64-bit little-endian integer from the stream.- Throws:
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.
-
enableAliasing
public void enableAliasing(boolean enabled) -
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 anInputStream
, not when constructed around a raw byte array (nor withByteString.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 (seesetSizeLimit(int)
). -
pushLimit
SetscurrentLimit
to (current position) +byteLimit
. This is called when descending into a length-delimited embedded message.Note that
pushLimit()
does NOT affect how many bytes theCodedInputStream
reads from an underlyingInputStream
when refreshing its buffer. If you need to prevent reading past a certain point in the underlyingInputStream
(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 yourInputStream
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 bypushLimit
.
-
getBytesUntilLimit
public int getBytesUntilLimit()Returns the number of bytes to be read before the current limit. If no limit is set, returns -1. -
isAtEnd
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 usingpushLimit(int)
.- Throws:
IOException
-
getTotalBytesRead
public int getTotalBytesRead()The total bytes read up to the current position. CallingresetSizeCounter()
resets this value to zero. -
readRawByte
Read one byte from the input.- Throws:
InvalidProtocolBufferException
- The end of the stream or the current limit was reached.IOException
-
readRawBytes
Read a fixed size of bytes from the input.- Throws:
InvalidProtocolBufferException
- The end of the stream or the current limit was reached.IOException
-
skipRawBytes
Reads and discardssize
bytes.- Throws:
InvalidProtocolBufferException
- The end of the stream or the current limit was reached.IOException
-
readGroup(int, com.google.protobuf.MessageLite.Builder, com.google.protobuf.ExtensionRegistryLite)
.