public abstract class CodedInputStream extends Object
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.
| Modifier and Type | Method and Description |
|---|---|
abstract void |
checkLastTagWas(int value)
Verifies that the last call to readTag() returned the given tag value.
|
void |
checkRecursionLimit() |
void |
checkValidEndTag()
Verifies that the last tag was 0 if we aren't inside a group.
|
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 |
decrementGroupDepth()
Decrement the group depth counter.
|
abstract void |
enableAliasing(boolean enabled)
Enables aliasing of the underlying input buffer for buffer-backed decoders.
|
abstract int |
getBytesUntilLimit()
Returns the number of bytes to be read before the current limit.
|
abstract int |
getLastTag() |
abstract int |
getTotalBytesRead()
The total bytes read up to the current position.
|
void |
incrementGroupDepth()
Increment the group depth counter.
|
abstract 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(ByteBuffer buf)
Create a new CodedInputStream wrapping the given ByteBuffer.
|
static CodedInputStream |
newInstance(InputStream input)
Create a new CodedInputStream wrapping the given InputStream.
|
static CodedInputStream |
newInstance(InputStream input,
int bufferSize)
Create a new CodedInputStream wrapping the given InputStream, with a specified buffer size.
|
static CodedInputStream |
newInstance(Iterable<ByteBuffer> input)
Create a new CodedInputStream wrapping the given
Iterable <ByteBuffer>. |
abstract void |
popLimit(int oldLimit)
Discards the current limit, returning to the previous limit.
|
abstract int |
pushLimit(int byteLimit)
Sets
currentLimit to (current position) + byteLimit. |
abstract boolean |
readBool()
Read a
bool field value from the stream. |
abstract byte[] |
readByteArray()
Read a
bytes field value from the stream. |
abstract ByteBuffer |
readByteBuffer()
Read a
bytes field value from the stream. |
abstract ByteString |
readBytes()
Read a
bytes field value from the stream. |
abstract double |
readDouble()
Read a
double field value from the stream. |
abstract int |
readEnum()
Read an enum field value from the stream.
|
abstract int |
readFixed32()
Read a
fixed32 field value from the stream. |
abstract long |
readFixed64()
Read a
fixed64 field value from the stream. |
abstract float |
readFloat()
Read a
float field value from the stream. |
abstract void |
readGroup(int fieldNumber,
MessageLite.Builder builder,
ExtensionRegistryLite extensionRegistry)
Read a
group field value from the stream. |
abstract <T extends MessageLite> |
readGroup(int fieldNumber,
Parser<T> parser,
ExtensionRegistryLite extensionRegistry)
Read a
group field value from the stream. |
abstract int |
readInt32()
Read an
int32 field value from the stream. |
abstract long |
readInt64()
Read an
int64 field value from the stream. |
abstract void |
readMessage(MessageLite.Builder builder,
ExtensionRegistryLite extensionRegistry)
Read an embedded message field value from the stream.
|
abstract <T extends MessageLite> |
readMessage(Parser<T> parser,
ExtensionRegistryLite extensionRegistry)
Read an embedded message field value from the stream.
|
abstract byte |
readRawByte()
Read one byte from the input.
|
abstract byte[] |
readRawBytes(int size)
Read a fixed size of bytes from the input.
|
abstract int |
readRawLittleEndian32()
Read a 32-bit little-endian integer from the stream.
|
abstract long |
readRawLittleEndian64()
Read a 64-bit little-endian integer from the stream.
|
abstract int |
readRawVarint32()
Read a raw Varint from the stream.
|
static int |
readRawVarint32(InputStream input)
Reads a varint from the input one byte at a time, so that it does not read any bytes after the end of the varint.
|
static int |
readRawVarint32(int firstByte,
InputStream input)
Like
readRawVarint32(InputStream), but expects that the caller has already read one byte. |
abstract long |
readRawVarint64()
Read a raw Varint from the stream.
|
abstract int |
readSFixed32()
Read an
sfixed32 field value from the stream. |
abstract long |
readSFixed64()
Read an
sfixed64 field value from the stream. |
abstract int |
readSInt32()
Read an
sint32 field value from the stream. |
abstract long |
readSInt64()
Read an
sint64 field value from the stream. |
abstract String |
readString()
Read a
string field value from the stream. |
abstract String |
readStringRequireUtf8()
Read a
string field value from the stream. |
abstract int |
readTag()
Attempt to read a field tag, returning zero if we have reached EOF.
|
abstract int |
readUInt32()
Read a
uint32 field value from the stream. |
abstract long |
readUInt64()
Read a
uint64 field value from the stream. |
abstract void |
resetSizeCounter()
Resets the current size counter to zero (see
setSizeLimit(int)). |
int |
setRecursionLimit(int limit)
Set the maximum message recursion depth.
|
int |
setSizeLimit(int limit)
Only valid for
InputStream-backed streams. |
abstract boolean |
skipField(int tag)
Reads and discards a single field, given its tag value.
|
void |
skipMessage()
Reads and discards an entire message.
|
void |
skipMessage(CodedOutputStream output)
Reads an entire message and writes it to output in wire format.
|
abstract void |
skipRawBytes(int size)
Reads and discards
size bytes. |
public abstract void checkLastTagWas(int value)
throws InvalidProtocolBufferException
InvalidProtocolBufferException - value does not match the last tag.public void checkRecursionLimit()
throws InvalidProtocolBufferException
InvalidProtocolBufferExceptionpublic void checkValidEndTag()
throws InvalidProtocolBufferException
InvalidProtocolBufferException - The last tag was not 0 and we aren't inside a group.public static int decodeZigZag32(int n)
n - An unsigned 32-bit integer, stored in a signed int because Java has no explicit unsigned support.public static long decodeZigZag64(long n)
n - An unsigned 64-bit integer, stored in a signed int because Java has no explicit unsigned support.public void decrementGroupDepth()
public abstract void enableAliasing(boolean enabled)
When aliasing is enabled and supported by the underlying decoder, readBytes() and
readByteBuffer() may return views into the underlying input buffer instead of copying the bytes. This
can reduce allocations and improve throughput for workloads that parse many length-delimited bytes
fields.
Some decoder implementations may ignore this setting (i.e., treat it as a no-op), such as stream-backed decoders
whose internal buffers are refilled and reused (and therefore may overwrite previously returned views), or
direct-backed decoders that are not backed by a stable on-heap byte[].
Safety contract: If aliasing is enabled, the caller must ensure the underlying input buffer is not mutated or
reused while any returned ByteString or ByteBuffer is still in use. If you cannot guarantee
buffer lifetime (for example, when using pooled buffers), do not enable aliasing or copy the bytes before storing
them.
Aliasing is generally only possible for array-backed decoders. Stream-backed decoders typically do not support aliasing because their internal buffers are refilled and reused.
Note: readByteBuffer() may return a mutable ByteBuffer even when it aliases the underlying
input. Callers that require safety-by-default should treat the returned buffer as read-only or call
ByteBuffer.asReadOnlyBuffer() immediately.
public abstract int getBytesUntilLimit()
public abstract int getLastTag()
public abstract int getTotalBytesRead()
resetSizeCounter() resets this value to zero.public void incrementGroupDepth()
public abstract boolean isAtEnd()
throws IOException
pushLimit(int). This
function may get blocked when using StreamDecoder as it invokes StreamDecoder#tryRefillBuffer(int) in
this function which will try to read bytes from input.IOExceptionpublic static CodedInputStream newInstance(byte[] buf)
public static CodedInputStream newInstance(byte[] buf, int off, int len)
public static CodedInputStream newInstance(ByteBuffer buf)
public static CodedInputStream newInstance(InputStream input)
public static CodedInputStream newInstance(InputStream input, int bufferSize)
bufferSize must be greater than 0. If bufferSize is less than 8, a minimum buffer size of 8 will
be used to ensure efficient reading of 64-bit values.
public static CodedInputStream newInstance(Iterable<ByteBuffer> input)
Iterable <ByteBuffer>.public abstract void popLimit(int oldLimit)
oldLimit - The old limit, as returned by pushLimit.@CanIgnoreReturnValue
public abstract int pushLimit(int byteLimit)
throws InvalidProtocolBufferException
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.
InvalidProtocolBufferExceptionpublic abstract boolean readBool()
throws IOException
bool field value from the stream.IOExceptionpublic abstract byte[] readByteArray()
throws IOException
bytes field value from the stream.IOExceptionpublic abstract ByteBuffer readByteBuffer() throws IOException
bytes field value from the stream.
If aliasing is enabled, the returned ByteBuffer may reference (alias) the underlying input buffer for
decoders that are backed by an on-heap byte[] whose contents will not be overwritten or reused for the
lifetime of the returned view.
Safety contract: Callers should treat the returned buffer as read-only and should not rely on its contents
remaining valid after the input advances. If you need a stable, read-only view, consider calling
ByteBuffer.asReadOnlyBuffer() immediately on the returned buffer.
IOExceptionpublic abstract ByteString readBytes() throws IOException
bytes field value from the stream.
When enableAliasing(boolean) is enabled and supported by the underlying decoder, the returned
ByteString may reference (alias) the underlying input buffer instead of copying the bytes.
Safety contract: Callers must ensure the underlying input buffer is not mutated or reused while the returned
ByteString is still in use. If you cannot guarantee buffer lifetime, do not enable aliasing or copy the
bytes before storing them.
IOExceptionpublic abstract double readDouble()
throws IOException
double field value from the stream.IOExceptionpublic abstract int readEnum()
throws IOException
IOExceptionpublic abstract int readFixed32()
throws IOException
fixed32 field value from the stream.IOExceptionpublic abstract long readFixed64()
throws IOException
fixed64 field value from the stream.IOExceptionpublic abstract float readFloat()
throws IOException
float field value from the stream.IOExceptionpublic abstract void readGroup(int fieldNumber,
MessageLite.Builder builder,
ExtensionRegistryLite extensionRegistry)
throws IOException
group field value from the stream.IOExceptionpublic abstract <T extends MessageLite> T readGroup(int fieldNumber, Parser<T> parser, ExtensionRegistryLite extensionRegistry) throws IOException
group field value from the stream.IOExceptionpublic abstract int readInt32()
throws IOException
int32 field value from the stream.IOExceptionpublic abstract long readInt64()
throws IOException
int64 field value from the stream.IOExceptionpublic abstract void readMessage(MessageLite.Builder builder, ExtensionRegistryLite extensionRegistry) throws IOException
IOExceptionpublic abstract <T extends MessageLite> T readMessage(Parser<T> parser, ExtensionRegistryLite extensionRegistry) throws IOException
IOExceptionpublic abstract byte readRawByte()
throws IOException
InvalidProtocolBufferException - The end of the stream or the current limit was reached.IOExceptionpublic abstract byte[] readRawBytes(int size)
throws IOException
InvalidProtocolBufferException - The end of the stream or the current limit was reached.IOExceptionpublic abstract int readRawLittleEndian32()
throws IOException
IOExceptionpublic abstract long readRawLittleEndian64()
throws IOException
IOExceptionpublic abstract int readRawVarint32()
throws IOException
IOExceptionpublic static int readRawVarint32(InputStream input) throws IOException
readRawVarint32(InputStream) then you
would probably end up reading past the end of the varint since CodedInputStream buffers its input.IOExceptionpublic static int readRawVarint32(int firstByte,
InputStream input)
throws IOException
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.IOExceptionpublic abstract long readRawVarint64()
throws IOException
IOExceptionpublic abstract int readSFixed32()
throws IOException
sfixed32 field value from the stream.IOExceptionpublic abstract long readSFixed64()
throws IOException
sfixed64 field value from the stream.IOExceptionpublic abstract int readSInt32()
throws IOException
sint32 field value from the stream.IOExceptionpublic abstract long readSInt64()
throws IOException
sint64 field value from the stream.IOExceptionpublic abstract String readString() throws IOException
string field value from the stream. If the stream contains malformed UTF-8, replace the offending
bytes with the standard UTF-8 replacement character.IOExceptionpublic abstract String readStringRequireUtf8() throws IOException
string field value from the stream. If the stream contains malformed UTF-8, throw exception
InvalidProtocolBufferException.IOExceptionpublic abstract int readTag()
throws IOException
IOExceptionpublic abstract int readUInt32()
throws IOException
uint32 field value from the stream.IOExceptionpublic abstract long readUInt64()
throws IOException
uint64 field value from the stream.IOExceptionpublic abstract void resetSizeCounter()
setSizeLimit(int)). Only valid for
InputStream-backed streams.@CanIgnoreReturnValue public final int setRecursionLimit(int limit)
CodedInputStream limits how deeply messages may be nested. The default limit is 100.@CanIgnoreReturnValue public final int setSizeLimit(int limit)
InputStream-backed streams.
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
Integer.MAX_VALUE. 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.
If you want to read several messages from a single CodedInputStream, you could call resetSizeCounter()
after each one to avoid hitting the size limit.
public abstract boolean skipField(int tag)
throws IOException
false if the tag is an endgroup tag, in which case nothing is skipped. Otherwise, returns
true.IOExceptionpublic void skipMessage()
throws IOException
IOExceptionpublic void skipMessage(CodedOutputStream output) throws IOException
IOExceptionpublic abstract void skipRawBytes(int size)
throws IOException
size bytes.InvalidProtocolBufferException - The end of the stream or the current limit was reached.IOException