public final class CodedInputStream extends Object
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 |
---|---|
void |
checkLastTagWas(int value)
Verifies that the last call to readTag() returned the given tag value.
|
void |
checkRecursionLimit() |
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 |
getBytesUntilLimit()
Returns the number of bytes to be read before the current limit.
|
int |
getLastTag() |
int |
getTotalBytesRead()
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(ByteBuffer buf)
Create a new CodedInputStream wrapping the given ByteBuffer.
|
static CodedInputStream |
newInstance(InputStream input)
Create a new CodedInputStream wrapping the given InputStream and using a buffer of 4096 bytes.
|
static CodedInputStream |
newInstance(InputStream input,
byte[] buffer)
Create a new CodedInputStream wrapping the given InputStream and using the given buffer.
|
void |
popLimit(int oldLimit)
Discards the current limit, returning to the previous limit.
|
int |
pushLimit(int byteLimit)
Sets
currentLimit to (current position) + byteLimit . |
boolean |
readBool()
Read a
bool field value from the stream. |
byte[] |
readByteArray()
Read a
bytes field value from the stream. |
ByteBuffer |
readByteBuffer()
Read a
bytes field value from the stream. |
ByteString |
readBytes()
Read a
bytes field value from the stream. |
double |
readDouble()
Read a
double field value from the stream. |
int |
readEnum()
Read an enum field value from the stream.
|
int |
readFixed32()
Read a
fixed32 field value from the stream. |
long |
readFixed64()
Read a
fixed64 field value from the stream. |
float |
readFloat()
Read a
float field value from the stream. |
void |
readGroup(int fieldNumber,
MessageLite.Builder builder,
ExtensionRegistryLite extensionRegistry)
Read a
group field value from the stream. |
<T extends MessageLite> |
readGroup(int fieldNumber,
Parser<T> parser,
ExtensionRegistryLite extensionRegistry)
Read a
group field value from the stream. |
int |
readInt32()
Read an
int32 field value from the stream. |
long |
readInt64()
Read an
int64 field value from the stream. |
void |
readMessage(MessageLite.Builder builder,
ExtensionRegistryLite extensionRegistry)
Read an embedded message field value from the stream.
|
<T extends MessageLite> |
readMessage(Parser<T> parser,
ExtensionRegistryLite extensionRegistry)
Read an embedded message field value from the stream.
|
byte |
readRawByte()
Read one byte from the input.
|
byte[] |
readRawBytes(int size)
Read a fixed size of bytes from the input.
|
int |
readRawLittleEndian32()
Read a 32-bit little-endian integer from the stream.
|
long |
readRawLittleEndian64()
Read a 64-bit little-endian integer from the stream.
|
int |
readRawVarint32()
Read a raw Varint from the stream.
|
static int |
readRawVarint32(int firstByte,
InputStream input)
Like
readRawVarint32(InputStream) , but expects that the caller has already read one byte. |
long |
readRawVarint64()
Read a raw Varint from the stream.
|
int |
readSFixed32()
Read an
sfixed32 field value from the stream. |
long |
readSFixed64()
Read an
sfixed64 field value from the stream. |
int |
readSInt32()
Read an
sint32 field value from the stream. |
long |
readSInt64()
Read an
sint64 field value from the stream. |
String |
readString()
Read a
string field value from the stream. |
String |
readStringRequireUtf8()
Read a
string field value from the stream. |
int |
readTag()
Attempt to read a field tag, returning zero if we have reached EOF.
|
int |
readUInt32()
Read a
uint32 field value from the stream. |
long |
readUInt64()
Read a
uint64 field value from the stream. |
void |
resetSizeCounter()
Resets the current size counter to zero (see
setSizeLimit(int) ). |
void |
setInputStream(InputStream input)
Sets or replaces the input stream to wrap.
|
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 |
skipMessage()
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 discards
size bytes. |
public void checkLastTagWas(int value) throws InvalidProtocolBufferException
InvalidProtocolBufferException
- value
does not match the last tag.public void checkRecursionLimit() throws InvalidProtocolBufferException
InvalidProtocolBufferException
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 enableAliasing(boolean enabled)
public int getBytesUntilLimit()
public int getLastTag()
public int getTotalBytesRead()
resetSizeCounter()
resets this value to zero.public boolean isAtEnd() throws IOException
pushLimit(int)
.IOException
public static CodedInputStream newInstance(byte[] buf)
buf
- the buffer.public static CodedInputStream newInstance(byte[] buf, int off, int len)
buf
- the buffer.off
- the offset.len
- the length.public static CodedInputStream newInstance(ByteBuffer buf)
public static CodedInputStream newInstance(InputStream input)
input
- the InputStream from which the CodedInputStream will be instantiated.public static CodedInputStream newInstance(InputStream input, byte[] buffer)
input
- the InputStream from which the CodedInputStream will be instantiated.buffer
- the buffer to use.public void popLimit(int oldLimit)
oldLimit
- The old limit, as returned by pushLimit
.public 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.
InvalidProtocolBufferException
public boolean readBool() throws IOException
bool
field value from the stream.IOException
public byte[] readByteArray() throws IOException
bytes
field value from the stream.IOException
public ByteBuffer readByteBuffer() throws IOException
bytes
field value from the stream.IOException
public ByteString readBytes() throws IOException
bytes
field value from the stream.IOException
public double readDouble() throws IOException
double
field value from the stream.IOException
public int readEnum() throws IOException
IOException
public int readFixed32() throws IOException
fixed32
field value from the stream.IOException
public long readFixed64() throws IOException
fixed64
field value from the stream.IOException
public float readFloat() throws IOException
float
field value from the stream.IOException
public void readGroup(int fieldNumber, MessageLite.Builder builder, @Nullable ExtensionRegistryLite extensionRegistry) throws IOException
group
field value from the stream.IOException
public <T extends MessageLite> T readGroup(int fieldNumber, Parser<T> parser, ExtensionRegistryLite extensionRegistry) throws IOException
group
field value from the stream.IOException
public int readInt32() throws IOException
int32
field value from the stream.IOException
public long readInt64() throws IOException
int64
field value from the stream.IOException
public void readMessage(MessageLite.Builder builder, ExtensionRegistryLite extensionRegistry) throws IOException
IOException
public <T extends MessageLite> T readMessage(Parser<T> parser, ExtensionRegistryLite extensionRegistry) throws IOException
IOException
public byte readRawByte() throws IOException
InvalidProtocolBufferException
- The end of the stream or the current limit was reached.IOException
public byte[] readRawBytes(int size) throws IOException
InvalidProtocolBufferException
- The end of the stream or the current limit was reached.IOException
public int readRawLittleEndian32() throws IOException
IOException
public long readRawLittleEndian64() throws IOException
IOException
public int readRawVarint32() throws IOException
IOException
public 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.IOException
public long readRawVarint64() throws IOException
IOException
public int readSFixed32() throws IOException
sfixed32
field value from the stream.IOException
public long readSFixed64() throws IOException
sfixed64
field value from the stream.IOException
public int readSInt32() throws IOException
sint32
field value from the stream.IOException
public long readSInt64() throws IOException
sint64
field value from the stream.IOException
public 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.IOException
public String readStringRequireUtf8() throws IOException
string
field value from the stream. If the stream contains malformed UTF-8, throw exception
InvalidProtocolBufferException
.IOException
public int readTag() throws IOException
IOException
public int readUInt32() throws IOException
uint32
field value from the stream.IOException
public long readUInt64() throws IOException
uint64
field value from the stream.IOException
public void resetSizeCounter()
setSizeLimit(int)
).public void setInputStream(InputStream input)
This CodedInputStream is reset to its initial state. The same buffer will be used to read from the new input stream.
input
- the InputStream that this CodedInputStream will wrap.IllegalStateException
- if this CodedInputStream was created with an immutable buffer.public int setRecursionLimit(int limit)
CodedInputStream
limits how deeply messages may be nested. The default limit is 64.public int setSizeLimit(int limit)
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.
public boolean skipField(int tag) throws IOException
false
if the tag is an endgroup tag, in which case nothing is skipped. Otherwise, returns
true
.IOException
public boolean skipField(int tag, CodedOutputStream output) throws IOException
false
if the tag is an endgroup tag, in which case nothing is skipped. Otherwise, returns
true
.IOException
public void skipMessage() throws IOException
IOException
public void skipMessage(CodedOutputStream output) throws IOException
IOException
public void skipRawBytes(int size) throws IOException
size
bytes.InvalidProtocolBufferException
- The end of the stream or the current limit was reached.IOException