Top Description Fields Constructors Methods
com.google.protobuf.nano

public final Class CodedInputByteBufferNano

extends Object
Class Inheritance
Imports
java.io.IOException

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). 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
[email protected] Kenton Varda

Field Summary

Modifier and TypeField and Description
private final byte[]
private int
private int
private int
private int
private int
currentLimit

The absolute position of the end of the current message.

private static final int
private static final int
private int
private int
recursionDepth

See setRecursionLimit()

private int
private int
sizeLimit

See setSizeLimit()

Constructor Summary

AccessConstructor and Description
private
CodedInputByteBufferNano(final byte[] buffer, final int off, final int len)

Method Summary

Modifier and TypeMethod and Description
public void
checkLastTagWas(final int value)

Verifies that the last call to readTag() returned the given tag value.

public static int

Returns:

A signed 32-bit integer.
decodeZigZag32
(final int
An unsigned 32-bit integer, stored in a signed int because Java has no explicit unsigned support.
n
)

Decode a ZigZag-encoded 32-bit value.

public static long

Returns:

A signed 64-bit integer.
decodeZigZag64
(final long
An unsigned 64-bit integer, stored in a signed int because Java has no explicit unsigned support.
n
)

Decode a ZigZag-encoded 64-bit value.

public int
getBytesUntilLimit()

Returns the number of bytes to be read before the current limit.

public byte[]
getData(int
the position (relative to the buffer start position) to start at.
offset
,
int
the number of bytes to retrieve.
length
)

Retrieves a subset of data in the buffer.

public int
getPosition()

Get current position in buffer relative to beginning offset.

public boolean
isAtEnd()

Returns true if the stream has reached the end of the input.

public static CodedInputByteBufferNano
newInstance(final byte[] buf)

Create a new CodedInputStream wrapping the given byte array.

public static CodedInputByteBufferNano
newInstance(final byte[] buf, final int off, final int len)

Create a new CodedInputStream wrapping the given byte array slice.

public void
popLimit(final int
The old limit, as returned by pushLimit.
oldLimit
)

Discards the current limit, returning to the previous limit.

public int

Returns:

the old limit.
pushLimit
(int byteLimit)

Sets currentLimit to (current position) + byteLimit.

public boolean
readBool()

Read a bool field value from the stream.

public byte[]
readBytes()

Read a bytes field value from the stream.

public double
readDouble()

Read a double field value from the stream.

public int
readEnum()

Read an enum field value from the stream.

public int
readFixed32()

Read a fixed32 field value from the stream.

public long
readFixed64()

Read a fixed64 field value from the stream.

public float
readFloat()

Read a float field value from the stream.

public void
readGroup(final MessageNano msg, final int fieldNumber)

Read a group field value from the stream.

public int
readInt32()

Read an int32 field value from the stream.

public long
readInt64()

Read an int64 field value from the stream.

public void
pack-priv Object
public byte
readRawByte()

Read one byte from the input.

public byte[]
readRawBytes(final int size)

Read a fixed size of bytes from the input.

public int
readRawLittleEndian32()

Read a 32-bit little-endian integer from the stream.

public long
readRawLittleEndian64()

Read a 64-bit little-endian integer from the stream.

public int
readRawVarint32()

Read a raw Varint from the stream.

public long
readRawVarint64()

Read a raw Varint from the stream.

public int
readSFixed32()

Read an sfixed32 field value from the stream.

public long
readSFixed64()

Read an sfixed64 field value from the stream.

public int
readSInt32()

Read an sint32 field value from the stream.

public long
readSInt64()

Read an sint64 field value from the stream.

public String
readString()

Read a string field value from the stream.

public int
readTag()

Attempt to read a field tag, returning zero if we have reached EOF.

public int
readUInt32()

Read a uint32 field value from the stream.

public long
readUInt64()

Read a uint64 field value from the stream.

private void
public void
resetSizeCounter()

Resets the current size counter to zero (see setSizeLimit(int)).

public void
rewindToPosition(int position)

Rewind to previous position.

public int

Returns:

the old limit.
setRecursionLimit
(final int limit)

Set the maximum message recursion depth.

public int

Returns:

the old limit.
setSizeLimit
(final int limit)

Set the maximum message size.

public boolean

Returns:

false if the tag is an endgroup tag, in which case nothing is skipped. Otherwise, returns true.
skipField
(final int tag)

Reads and discards a single field, given its tag value.

public void
skipMessage()

Reads and discards an entire message.

public void
skipRawBytes(final int size)

Reads and discards size bytes.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

bufferback to summary
private final byte[] buffer
bufferPosback to summary
private int bufferPos
bufferSizeback to summary
private int bufferSize
bufferSizeAfterLimitback to summary
private int bufferSizeAfterLimit
bufferStartback to summary
private int bufferStart
currentLimitback to summary
private int currentLimit

The absolute position of the end of the current message.

DEFAULT_RECURSION_LIMITback to summary
private static final int DEFAULT_RECURSION_LIMIT
DEFAULT_SIZE_LIMITback to summary
private static final int DEFAULT_SIZE_LIMIT
lastTagback to summary
private int lastTag
recursionDepthback to summary
private int recursionDepth

See setRecursionLimit()

recursionLimitback to summary
private int recursionLimit
sizeLimitback to summary
private int sizeLimit

See setSizeLimit()

Constructor Detail

CodedInputByteBufferNanoback to summary
private CodedInputByteBufferNano(final byte[] buffer, final int off, final int len)

Method Detail

checkLastTagWasback to summary
public void checkLastTagWas(final int value) throws InvalidProtocolBufferNanoException

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.

Exceptions
InvalidProtocolBufferNanoException:
value does not match the last tag.
decodeZigZag32back to summary
public static int decodeZigZag32(final 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:int

An unsigned 32-bit integer, stored in a signed int because Java has no explicit unsigned support.

Returns:int

A signed 32-bit integer.

decodeZigZag64back to summary
public static long decodeZigZag64(final 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:long

An unsigned 64-bit integer, stored in a signed int because Java has no explicit unsigned support.

Returns:long

A signed 64-bit integer.

getBytesUntilLimitback to summary
public int getBytesUntilLimit()

Returns the number of bytes to be read before the current limit. If no limit is set, returns -1.

getDataback to summary
public byte[] getData(int offset, int length)

Retrieves a subset of data in the buffer. The returned array is not backed by the original buffer array.

Parameters
offset:int

the position (relative to the buffer start position) to start at.

length:int

the number of bytes to retrieve.

getPositionback to summary
public int getPosition()

Get current position in buffer relative to beginning offset.

isAtEndback to summary
public boolean 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 using pushLimit(int).

newInstanceback to summary
public static CodedInputByteBufferNano newInstance(final byte[] buf)

Create a new CodedInputStream wrapping the given byte array.

newInstanceback to summary
public static CodedInputByteBufferNano newInstance(final byte[] buf, final int off, final int len)

Create a new CodedInputStream wrapping the given byte array slice.

popLimitback to summary
public void popLimit(final int oldLimit)

Discards the current limit, returning to the previous limit.

Parameters
oldLimit:int

The old limit, as returned by pushLimit.

pushLimitback to summary
public int pushLimit(int byteLimit) throws InvalidProtocolBufferNanoException

Sets currentLimit to (current position) + byteLimit. This is called when descending into a length-delimited embedded message.

Returns:int

the old limit.

readBoolback to summary
public boolean readBool() throws IOException

Read a bool field value from the stream.

readBytesback to summary
public byte[] readBytes() throws IOException

Read a bytes field value from the stream.

readDoubleback to summary
public double readDouble() throws IOException

Read a double field value from the stream.

readEnumback to summary
public int readEnum() throws IOException

Read an enum field value from the stream. Caller is responsible for converting the numeric value to an actual enum.

readFixed32back to summary
public int readFixed32() throws IOException

Read a fixed32 field value from the stream.

readFixed64back to summary
public long readFixed64() throws IOException

Read a fixed64 field value from the stream.

readFloatback to summary
public float readFloat() throws IOException

Read a float field value from the stream.

readGroupback to summary
public void readGroup(final MessageNano msg, final int fieldNumber) throws IOException

Read a group field value from the stream.

readInt32back to summary
public int readInt32() throws IOException

Read an int32 field value from the stream.

readInt64back to summary
public long readInt64() throws IOException

Read an int64 field value from the stream.

readMessageback to summary
public void readMessage(final MessageNano msg) throws IOException
readPrimitiveFieldback to summary
pack-priv Object readPrimitiveField(int type) throws IOException
readRawByteback to summary
public byte readRawByte() throws IOException

Read one byte from the input.

Exceptions
InvalidProtocolBufferNanoException:
The end of the stream or the current limit was reached.
readRawBytesback to summary
public byte[] readRawBytes(final int size) throws IOException

Read a fixed size of bytes from the input.

Exceptions
InvalidProtocolBufferNanoException:
The end of the stream or the current limit was reached.
readRawLittleEndian32back to summary
public int readRawLittleEndian32() throws IOException

Read a 32-bit little-endian integer from the stream.

readRawLittleEndian64back to summary
public long readRawLittleEndian64() throws IOException

Read a 64-bit little-endian integer from the stream.

readRawVarint32back to summary
public int readRawVarint32() throws IOException

Read a raw Varint from the stream. If larger than 32 bits, discard the upper bits.

readRawVarint64back to summary
public long readRawVarint64() throws IOException

Read a raw Varint from the stream.

readSFixed32back to summary
public int readSFixed32() throws IOException

Read an sfixed32 field value from the stream.

readSFixed64back to summary
public long readSFixed64() throws IOException

Read an sfixed64 field value from the stream.

readSInt32back to summary
public int readSInt32() throws IOException

Read an sint32 field value from the stream.

readSInt64back to summary
public long readSInt64() throws IOException

Read an sint64 field value from the stream.

readStringback to summary
public String readString() throws IOException

Read a string field value from the stream.

readTagback to summary
public int readTag() throws 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.

readUInt32back to summary
public int readUInt32() throws IOException

Read a uint32 field value from the stream.

readUInt64back to summary
public long readUInt64() throws IOException

Read a uint64 field value from the stream.

recomputeBufferSizeAfterLimitback to summary
private void recomputeBufferSizeAfterLimit()
resetSizeCounterback to summary
public void resetSizeCounter()

Resets the current size counter to zero (see setSizeLimit(int)).

rewindToPositionback to summary
public void rewindToPosition(int position)

Rewind to previous position. Cannot go forward.

setRecursionLimitback to summary
public int setRecursionLimit(final 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:int

the old limit.

setSizeLimitback to summary
public int setSizeLimit(final 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.

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:int

the old limit.

skipFieldback to summary
public boolean skipField(final int tag) throws IOException

Reads and discards a single field, given its tag value.

Returns:boolean

false if the tag is an endgroup tag, in which case nothing is skipped. Otherwise, returns true.

skipMessageback to summary
public void skipMessage() throws IOException

Reads and discards an entire message. This will read either until EOF or until an endgroup tag, whichever comes first.

skipRawBytesback to summary
public void skipRawBytes(final int size) throws IOException

Reads and discards size bytes.

Exceptions
InvalidProtocolBufferNanoException:
The end of the stream or the current limit was reached.