public final class CodedInput extends Object implements Input
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 | Field and Description |
|---|---|
boolean |
decodeNestedMessageAsGroup
If true, the nested messages are group-encoded
|
| Constructor and Description |
|---|
CodedInput(byte[] buffer,
int off,
int len,
boolean decodeNestedMessageAsGroup) |
CodedInput(InputStream input,
boolean decodeNestedMessageAsGroup) |
CodedInput(InputStream input,
byte[] buffer,
boolean decodeNestedMessageAsGroup) |
CodedInput(InputStream input,
byte[] buffer,
int offset,
int limit,
boolean decodeNestedMessageAsGroup) |
| Modifier and Type | Method and Description |
|---|---|
void |
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.
|
int |
getBytesUntilLimit()
Returns the number of bytes to be read before the current limit.
|
int |
getLastTag()
Returns the last tag.
|
int |
getTotalBytesRead()
The total bytes read up to the current position.
|
<T> void |
handleUnknownField(int fieldNumber,
Schema<T> schema) |
boolean |
isAtEnd()
Returns true if the stream has reached the end of the input.
|
boolean |
isCurrentFieldPacked()
Return true if currently reading packed field
|
<T> T |
mergeObject(T value,
Schema<T> schema) |
static CodedInput |
newInstance(byte[] buf)
Create a new CodedInput wrapping the given byte array.
|
static CodedInput |
newInstance(byte[] buf,
int off,
int len)
Create a new CodedInput wrapping the given byte array slice.
|
static CodedInput |
newInstance(InputStream input)
Create a new CodedInput wrapping the given InputStream.
|
void |
popLimit(int oldLimit)
Discards the current limit, returning to the previous limit.
|
int |
pushLimit(int byteLimit)
Note that
pushLimit() does NOT affect how many bytes the CodedInputStream reads from an
underlying InputStream when refreshing its buffer. |
boolean |
readBool()
Read a
bool field value from the stream. |
byte[] |
readByteArray() |
ByteBuffer |
readByteBuffer()
Reads a byte array/ByteBuffer value.
|
ByteString |
readBytes()
Read a
bytes field value from the stream. |
void |
readBytes(ByteBuffer bb)
Read a
string field value from the stream into a ByteBuffer. |
double |
readDouble()
Read a
double field value from the stream. |
int |
readEnum()
Read an enum field value from the stream.
|
<T> int |
readFieldNumber(Schema<T> schema) |
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. |
int |
readInt32()
Read an
int32 field value from the stream. |
long |
readInt64()
Read an
int64 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.
|
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. |
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 |
reset()
Resets the buffer position and limit to re-use this CodedInput object.
|
void |
resetSizeCounter()
Resets the current size counter to zero (see
setSizeLimit(int)). |
int |
setSizeLimit(int limit)
Set the maximum message size.
|
boolean |
skipField(int tag)
Reads and discards a single field, given its tag value.
|
void |
skipMessage()
Reads and discards an entire message.
|
void |
skipRawBytes(int size)
Reads and discards
size bytes. |
void |
transferByteRangeTo(Output output,
boolean utf8String,
int fieldNumber,
boolean repeated) |
public final boolean decodeNestedMessageAsGroup
public CodedInput(byte[] buffer,
int off,
int len,
boolean decodeNestedMessageAsGroup)
public CodedInput(InputStream input, boolean decodeNestedMessageAsGroup)
public CodedInput(InputStream input, byte[] buffer, boolean decodeNestedMessageAsGroup)
public CodedInput(InputStream input, byte[] buffer, int offset, int limit, boolean decodeNestedMessageAsGroup)
public static CodedInput newInstance(InputStream input)
public static CodedInput newInstance(byte[] buf)
public static CodedInput newInstance(byte[] buf, int off, int len)
public int readTag()
throws IOException
IOExceptionpublic void checkLastTagWas(int value)
throws ProtobufException
ProtobufException - value does not match the last tag.public 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 double readDouble()
throws IOException
double field value from the stream.readDouble in interface InputIOExceptionpublic float readFloat()
throws IOException
float field value from the stream.readFloat in interface InputIOExceptionpublic long readUInt64()
throws IOException
uint64 field value from the stream.readUInt64 in interface InputIOExceptionpublic long readInt64()
throws IOException
int64 field value from the stream.readInt64 in interface InputIOExceptionpublic int readInt32()
throws IOException
int32 field value from the stream.readInt32 in interface InputIOExceptionpublic long readFixed64()
throws IOException
fixed64 field value from the stream.readFixed64 in interface InputIOExceptionpublic int readFixed32()
throws IOException
fixed32 field value from the stream.readFixed32 in interface InputIOExceptionpublic boolean readBool()
throws IOException
bool field value from the stream.readBool in interface InputIOExceptionpublic String readString() throws IOException
string field value from the stream.readString in interface InputIOExceptionpublic void readBytes(ByteBuffer bb) throws IOException
string field value from the stream into a ByteBuffer.readBytes in interface InputIOExceptionpublic <T> T mergeObject(T value,
Schema<T> schema)
throws IOException
mergeObject in interface InputIOExceptionpublic ByteString readBytes() throws IOException
bytes field value from the stream.readBytes in interface InputIOExceptionpublic int readUInt32()
throws IOException
uint32 field value from the stream.readUInt32 in interface InputIOExceptionpublic int readEnum()
throws IOException
readEnum in interface InputIOExceptionpublic int readSFixed32()
throws IOException
sfixed32 field value from the stream.readSFixed32 in interface InputIOExceptionpublic long readSFixed64()
throws IOException
sfixed64 field value from the stream.readSFixed64 in interface InputIOExceptionpublic int readSInt32()
throws IOException
sint32 field value from the stream.readSInt32 in interface InputIOExceptionpublic long readSInt64()
throws IOException
sint64 field value from the stream.readSInt64 in interface InputIOExceptionpublic int readRawVarint32()
throws IOException
IOExceptionpublic long readRawVarint64()
throws IOException
IOExceptionpublic int readRawLittleEndian32()
throws IOException
IOExceptionpublic long readRawLittleEndian64()
throws IOException
IOExceptionpublic 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 int setSizeLimit(int limit)
CodedInput 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.
If you want to read several messages from a single CodedInput, you could call resetSizeCounter() after
each one to avoid hitting the size limit.
public void resetSizeCounter()
setSizeLimit(int)). The field totalBytesRetired
will be negative if the initial position was not zero.public void reset()
public int pushLimit(int byteLimit)
throws ProtobufException
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.ProtobufExceptionpublic void popLimit(int oldLimit)
oldLimit - The old limit, as returned by pushLimit.public int getBytesUntilLimit()
public boolean isCurrentFieldPacked()
public boolean isAtEnd()
throws IOException
pushLimit(int).IOExceptionpublic int getTotalBytesRead()
resetSizeCounter() resets this value to zero.public byte readRawByte()
throws IOException
ProtobufException - The end of the stream or the current limit was reached.IOExceptionpublic byte[] readRawBytes(int size)
throws IOException
ProtobufException - The end of the stream or the current limit was reached.IOExceptionpublic void skipRawBytes(int size)
throws IOException
size bytes.ProtobufException - The end of the stream or the current limit was reached.IOExceptionpublic <T> int readFieldNumber(Schema<T> schema) throws IOException
readFieldNumber in interface InputIOExceptionpublic byte[] readByteArray()
throws IOException
readByteArray in interface InputIOExceptionpublic <T> void handleUnknownField(int fieldNumber,
Schema<T> schema)
throws IOException
handleUnknownField in interface InputIOExceptionpublic void transferByteRangeTo(Output output, boolean utf8String, int fieldNumber, boolean repeated) throws IOException
transferByteRangeTo in interface InputIOExceptionpublic int getLastTag()
public ByteBuffer readByteBuffer() throws IOException
readByteBuffer in interface InputIOExceptionCopyright © 2009–2022. All rights reserved.