public abstract class ProtoSource 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 readRawVarint64()). 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 |
|---|---|
protected int |
currentLimit
The absolute position of the end of the current message.
|
protected static int |
NO_LIMIT |
protected int |
recursionDepth
See setRecursionLimit()
|
protected int |
recursionLimit |
| Modifier | Constructor and Description |
|---|---|
protected |
ProtoSource() |
| Modifier and Type | Method and Description |
|---|---|
void |
checkLastTagWas(int value)
Verifies that the last call to readTag() returned the given tag value.
|
abstract ProtoSource |
clear()
Clears internal state and removes any references to previous inputs.
|
static int |
decodeZigZag32(int n)
Decode a ZigZag-encoded 32-bit value.
|
static long |
decodeZigZag64(long n)
Decode a ZigZag-encoded 64-bit value.
|
ProtoSource |
discardUnknownFields()
Sets this
ProtoSource to discard unknown fields. |
int |
getBytesUntilLimit()
Returns the number of bytes to be read before the current limit.
|
abstract int |
getTotalBytesRead()
The total bytes read up to the current position.
|
abstract boolean |
isAtEnd()
Returns true if the source has reached the end of the input.
|
static ProtoSource |
newArraySource()
Creates a new
ProtoSource that reads directly from a byte array. |
static ProtoSource |
newBufferSource()
Creates a new
ProtoSource that reads from an ByteBuffer. |
static ProtoSource |
newDirectSource()
Creates a new
ProtoSource that reads from a byte array or
direct / off-heap memory. |
static ProtoSource |
newInstance(byte[] buf)
Create a new ProtoSource reading from the given byte array.
|
static ProtoSource |
newInstance(byte[] buf,
int off,
int len)
Create a new ProtoSource reading from the given byte array slice.
|
static ProtoSource |
newInstance(ByteBuffer buffer)
Create a new ProtoSource reading from the given
ByteBuffer. |
static ProtoSource |
newInstance(InputStream stream)
Create a new ProtoSource reading from the given
InputStream. |
static ProtoSource |
newInstance(RepeatedByte bytes)
Create a new ProtoSource reading from the current array of the given
RepeatedByte. |
static ProtoSource |
newStreamSource()
Creates a new
ProtoSource that reads from an InputStream. |
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 source. |
void |
readBytes(RepeatedByte store)
Read a
bytes field value from the source. |
double |
readDouble()
Read a
double field value from the source. |
int |
readEnum()
Read an enum field value from the source.
|
int |
readFixed32()
Read a
fixed32 field value from the source. |
long |
readFixed64()
Read a
fixed64 field value from the source. |
float |
readFloat()
Read a
float field value from the source. |
void |
readGroup(ProtoMessage<?> msg,
int fieldNumber)
Read a
group field value from the source. |
int |
readInt32()
Read an
int32 field value from the source. |
long |
readInt64()
Read an
int64 field value from the source. |
int |
readLength()
Read a length delimiter from the source.
|
void |
readMessage(ProtoMessage<?> msg) |
void |
readPackedBool(RepeatedBoolean store)
Read a repeated (packed)
bool field value from the source. |
void |
readPackedDouble(RepeatedDouble store)
Read a repeated (packed)
double field value from the source. |
void |
readPackedEnum(RepeatedEnum<?> store,
int tag)
Read a repeated (packed)
enum field value from the source. |
void |
readPackedFixed32(RepeatedInt store)
Read a repeated (packed)
fixed32 field value from the source. |
void |
readPackedFixed64(RepeatedLong store)
Read a repeated (packed)
fixed64 field value from the source. |
void |
readPackedFloat(RepeatedFloat store)
Read a repeated (packed)
float field value from the source. |
void |
readPackedInt32(RepeatedInt store,
int tag)
Read a repeated (packed)
int32 field value from the source. |
void |
readPackedInt64(RepeatedLong store,
int tag)
Read a repeated (packed)
int64 field value from the source. |
void |
readPackedSFixed32(RepeatedInt store)
Read a repeated (packed)
sfixed32 field value from the source. |
void |
readPackedSFixed64(RepeatedLong store)
Read a repeated (packed)
sfixed64 field value from the source. |
void |
readPackedSInt32(RepeatedInt store,
int tag)
Read a repeated (packed)
sint32 field value from the source. |
void |
readPackedSInt64(RepeatedLong store,
int tag)
Read a repeated (packed)
sint64 field value from the source. |
void |
readPackedUInt32(RepeatedInt store,
int tag)
Read a repeated (packed)
uint32 field value from the source. |
void |
readPackedUInt64(RepeatedLong store,
int tag)
Read a repeated (packed)
uint64 field value from the source. |
abstract byte |
readRawByte()
Read one byte from the input.
|
void |
readRawBytes(byte[] values,
int offset,
int length)
Read raw
bytes from the source. |
protected void |
readRawDoubles(double[] values,
int offset,
int length) |
protected void |
readRawFixed32s(int[] values,
int offset,
int length) |
protected void |
readRawFixed64s(long[] values,
int offset,
int length) |
protected void |
readRawFloats(float[] values,
int offset,
int length) |
short |
readRawLittleEndian16()
Read a 16-bit little-endian integer from the source.
|
int |
readRawLittleEndian32()
Read a 32-bit little-endian integer from the source.
|
long |
readRawLittleEndian64()
Read a 64-bit little-endian integer from the source.
|
int |
readRawVarint32() |
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. |
long |
readRawVarint64()
Read a raw Varint from the source.
|
int |
readRepeatedBool(RepeatedBoolean store,
int tag)
Read a repeated (non-packed)
bool field value from the source. |
int |
readRepeatedBytes(RepeatedBytes store,
int tag)
Read a repeated
bytes field value from the source. |
int |
readRepeatedDouble(RepeatedDouble store,
int tag)
Read a repeated (non-packed)
double field value from the source. |
int |
readRepeatedEnum(RepeatedEnum<?> store,
int tag)
Read a repeated
enum field value from the source. |
int |
readRepeatedFixed32(RepeatedInt store,
int tag)
Read a repeated (non-packed)
fixed32 field value from the source. |
int |
readRepeatedFixed64(RepeatedLong store,
int tag)
Read a repeated (non-packed)
fixed64 field value from the source. |
int |
readRepeatedFloat(RepeatedFloat store,
int tag)
Read a repeated (non-packed)
float field value from the source. |
int |
readRepeatedGroup(RepeatedMessage<?> store,
int tag)
Read a repeated
group field value from the source. |
int |
readRepeatedInt32(RepeatedInt store,
int tag)
Read a repeated
int32 field value from the source. |
int |
readRepeatedInt64(RepeatedLong store,
int tag)
Read a repeated
int64 field value from the source. |
int |
readRepeatedMessage(RepeatedMessage<?> store,
int tag)
Read a repeated
message field value from the source. |
int |
readRepeatedSFixed32(RepeatedInt store,
int tag)
Read a repeated (non-packed)
sfixed32 field value from the source. |
int |
readRepeatedSFixed64(RepeatedLong store,
int tag)
Read a repeated (non-packed)
sfixed64 field value from the source. |
int |
readRepeatedSInt32(RepeatedInt store,
int tag)
Read a repeated
sint32 field value from the source. |
int |
readRepeatedSInt64(RepeatedLong store,
int tag)
Read a repeated
sint64 field value from the source. |
int |
readRepeatedString(RepeatedString store,
int tag)
Read a repeated
string field value from the source. |
int |
readRepeatedUInt32(RepeatedInt store,
int tag)
Read a repeated
uint32 field value from the source. |
int |
readRepeatedUInt64(RepeatedLong store,
int tag)
Read a repeated
uint64 field value from the source. |
int |
readSFixed32()
Read an
sfixed32 field value from the source. |
long |
readSFixed64()
Read an
sfixed64 field value from the source. |
int |
readSInt32()
Read an
sint32 field value from the source. |
long |
readSInt64()
Read an
sint64 field value from the source. |
void |
readString(Utf8String store)
Read a
string field value from the source. |
int |
readTag()
Attempt to read a field tag, returning zero if we have reached EOF.
|
int |
readUInt32()
Read a
uint32 field value from the source. |
long |
readUInt64()
Read a
uint64 field value from the source. |
protected void |
reservePackedVarintCapacity(us.hebi.quickbuf.RepeatedField<?,?> store)
Reserves space in the repeated field store to hold the number of remaining varints until
the limit or end of stream is reached.
|
protected void |
reserveRepeatedFieldCapacity(us.hebi.quickbuf.RepeatedField<?,?> store,
int tag)
Reserves space in the repeated field store to hold additional values.
|
protected ProtoSource |
resetInternalState() |
ProtoSource |
resetSizeCounter()
Resets the current size counter to zero (see
setSizeLimit(int)). |
abstract void |
rewindTo(int totalBytesRead)
Rewinds the internal position to match the desired number of bytes read.
|
ProtoSource |
setInput(byte[] buffer)
Changes the input to the given array.
|
ProtoSource |
setInput(byte[] buffer,
long off,
int len)
Changes the input to the given array.
|
ProtoSource |
setInput(ByteBuffer buffer)
Changes the input to the given buffer.
|
ProtoSource |
setInput(InputStream stream)
Changes the input to the given stream.
|
ProtoSource |
setInput(RepeatedByte bytes)
Changes the input to the current array of the given
RepeatedByte. |
int |
setRecursionLimit(int limit)
Set the maximum message recursion depth.
|
int |
setSizeLimit(int limit)
Only valid for
InputStream-backed streams. |
void |
skipEnum(int tag,
int value,
RepeatedByte unknownBytes)
Skips an enum that has already been read, but had a value that is not known.
|
boolean |
skipField(int tag)
Reads and discards a single field, given its tag value.
|
boolean |
skipField(int tag,
RepeatedByte unknownBytes)
Reads and discards a single field, given its tag value.
|
protected void |
skipMessage()
Reads and discards an entire message.
|
protected void |
skipMessage(RepeatedByte unknownBytes) |
abstract void |
skipRawBytes(int size)
Reads and discards
size bytes. |
ProtoSource |
unsetDiscardUnknownFields()
Reverts the unknown fields preservation behavior to the default.
|
protected int recursionDepth
protected int recursionLimit
protected int currentLimit
protected static final int NO_LIMIT
public static ProtoSource newInstance(byte[] buf)
public static ProtoSource newInstance(byte[] buf, int off, int len)
public static ProtoSource newInstance(RepeatedByte bytes)
RepeatedByte.public static ProtoSource newInstance(InputStream stream)
InputStream.public static ProtoSource newInstance(ByteBuffer buffer)
ByteBuffer.public static ProtoSource newArraySource()
ProtoSource that reads directly from a byte array.
This method will return the fastest implementation available for the
current platform and may leverage features from sun.misc.Unsafe if
available.public static ProtoSource newDirectSource()
ProtoSource that reads from a byte array or
direct / off-heap memory. It is similar to the array source, but
it allows null buffers to support raw memory addresses.
This source requires availability of sun.misc.Unsafe. Be aware that
passing incorrect memory addresses may cause the entire runtime to
segfault.public static ProtoSource newStreamSource()
ProtoSource that reads from an InputStream.
The current implementation is a very lightweight wrapper that reads
byte-by-byte and does not do any internal buffering. This is slower
than reading from an array, but it does not require extra memory.public static ProtoSource newBufferSource()
ProtoSource that reads from an ByteBuffer.
The current implementation is a very lightweight wrapper that reads
individual bytes. This is slower than reading from an array, but it
works on all platforms.public final ProtoSource setInput(byte[] buffer)
public ProtoSource setInput(byte[] buffer, long off, int len)
public final ProtoSource setInput(RepeatedByte bytes)
RepeatedByte.public ProtoSource setInput(InputStream stream)
public ProtoSource setInput(ByteBuffer buffer)
public abstract ProtoSource clear()
public int readTag()
throws IOException
IOExceptionpublic void checkLastTagWas(int value)
throws InvalidProtocolBufferException
InvalidProtocolBufferException - 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 boolean skipField(int tag,
RepeatedByte unknownBytes)
throws IOException
false if the tag is an endgroup tag, in which case
nothing is skipped. Otherwise, returns true.IOExceptionpublic void skipEnum(int tag,
int value,
RepeatedByte unknownBytes)
throws IOException
IOExceptionprotected void skipMessage()
throws IOException
IOExceptionprotected void skipMessage(RepeatedByte unknownBytes) throws IOException
IOExceptionpublic void readPackedDouble(RepeatedDouble store) throws IOException
double field value from the source.IOExceptionprotected void readRawDoubles(double[] values,
int offset,
int length)
throws IOException
IOExceptionpublic int readRepeatedDouble(RepeatedDouble store, int tag) throws IOException
double field value from the source.IOExceptionpublic double readDouble()
throws IOException
double field value from the source.IOExceptionpublic void readPackedFloat(RepeatedFloat store) throws IOException
float field value from the source.IOExceptionprotected void readRawFloats(float[] values,
int offset,
int length)
throws IOException
IOExceptionpublic int readRepeatedFloat(RepeatedFloat store, int tag) throws IOException
float field value from the source.IOExceptionpublic float readFloat()
throws IOException
float field value from the source.IOExceptionpublic void readPackedSFixed64(RepeatedLong store) throws IOException
sfixed64 field value from the source.IOExceptionpublic void readPackedFixed64(RepeatedLong store) throws IOException
fixed64 field value from the source.IOExceptionprotected void readRawFixed64s(long[] values,
int offset,
int length)
throws IOException
IOExceptionpublic int readRepeatedSFixed64(RepeatedLong store, int tag) throws IOException
sfixed64 field value from the source.IOExceptionpublic int readRepeatedFixed64(RepeatedLong store, int tag) throws IOException
fixed64 field value from the source.IOExceptionpublic long readSFixed64()
throws IOException
sfixed64 field value from the source.IOExceptionpublic long readFixed64()
throws IOException
fixed64 field value from the source.IOExceptionpublic void readPackedSFixed32(RepeatedInt store) throws IOException
sfixed32 field value from the source.IOExceptionpublic void readPackedFixed32(RepeatedInt store) throws IOException
fixed32 field value from the source.IOExceptionprotected void readRawFixed32s(int[] values,
int offset,
int length)
throws IOException
IOExceptionpublic int readRepeatedSFixed32(RepeatedInt store, int tag) throws IOException
sfixed32 field value from the source.IOExceptionpublic int readRepeatedFixed32(RepeatedInt store, int tag) throws IOException
fixed32 field value from the source.IOExceptionpublic int readSFixed32()
throws IOException
sfixed32 field value from the source.IOExceptionpublic int readFixed32()
throws IOException
fixed32 field value from the source.IOExceptionpublic void readPackedUInt64(RepeatedLong store, int tag) throws IOException
uint64 field value from the source.IOExceptionpublic void readPackedInt64(RepeatedLong store, int tag) throws IOException
int64 field value from the source.IOExceptionpublic void readPackedSInt64(RepeatedLong store, int tag) throws IOException
sint64 field value from the source.IOExceptionpublic int readRepeatedUInt64(RepeatedLong store, int tag) throws IOException
uint64 field value from the source.IOExceptionpublic int readRepeatedInt64(RepeatedLong store, int tag) throws IOException
int64 field value from the source.IOExceptionpublic int readRepeatedSInt64(RepeatedLong store, int tag) throws IOException
sint64 field value from the source.IOExceptionpublic long readUInt64()
throws IOException
uint64 field value from the source.IOExceptionpublic long readInt64()
throws IOException
int64 field value from the source.IOExceptionpublic long readSInt64()
throws IOException
sint64 field value from the source.IOExceptionpublic void readPackedUInt32(RepeatedInt store, int tag) throws IOException
uint32 field value from the source.IOExceptionpublic void readPackedInt32(RepeatedInt store, int tag) throws IOException
int32 field value from the source.IOExceptionpublic void readPackedSInt32(RepeatedInt store, int tag) throws IOException
sint32 field value from the source.IOExceptionpublic int readRepeatedUInt32(RepeatedInt store, int tag) throws IOException
uint32 field value from the source.IOExceptionpublic int readRepeatedInt32(RepeatedInt store, int tag) throws IOException
int32 field value from the source.IOExceptionpublic int readRepeatedSInt32(RepeatedInt store, int tag) throws IOException
sint32 field value from the source.IOExceptionpublic int readUInt32()
throws IOException
uint32 field value from the source.IOExceptionpublic int readInt32()
throws IOException
int32 field value from the source.IOExceptionpublic int readSInt32()
throws IOException
sint32 field value from the source.IOExceptionpublic void readPackedBool(RepeatedBoolean store) throws IOException
bool field value from the source.IOExceptionpublic int readRepeatedBool(RepeatedBoolean store, int tag) throws IOException
bool field value from the source.IOExceptionpublic boolean readBool()
throws IOException
bool field value from the source.IOExceptionpublic void readPackedEnum(RepeatedEnum<?> store, int tag) throws IOException
enum field value from the source.IOExceptionpublic int readRepeatedEnum(RepeatedEnum<?> store, int tag) throws IOException
enum field value from the source.IOExceptionpublic int readEnum()
throws IOException
IOExceptionpublic int readLength()
throws IOException
IOExceptionpublic int readRepeatedString(RepeatedString store, int tag) throws IOException
string field value from the source.IOExceptionpublic void readString(Utf8String store) throws IOException
string field value from the source.IOExceptionpublic int readRepeatedGroup(RepeatedMessage<?> store, int tag) throws IOException
group field value from the source.IOExceptionpublic void readGroup(ProtoMessage<?> msg, int fieldNumber) throws IOException
group field value from the source.IOExceptionpublic int readRepeatedMessage(RepeatedMessage<?> store, int tag) throws IOException
message field value from the source.IOExceptionpublic void readMessage(ProtoMessage<?> msg) throws IOException
IOExceptionpublic int readRepeatedBytes(RepeatedBytes store, int tag) throws IOException
bytes field value from the source.IOExceptionpublic void readBytes(RepeatedByte store) throws IOException
bytes field value from the source.IOExceptionpublic void readRawBytes(byte[] values,
int offset,
int length)
throws IOException
bytes from the source.IOExceptionpublic int readRawVarint32()
throws IOException
IOExceptionpublic long readRawVarint64()
throws IOException
IOExceptionpublic short readRawLittleEndian16()
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.protected ProtoSource resetInternalState()
public int setRecursionLimit(int limit)
ProtoSource limits
how deeply messages may be nested. The default limit is 64.public 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, ProtoSource 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 ProtoSource resetSizeCounter()
setSizeLimit(int)). Only valid for InputStream-backed streams.public final ProtoSource discardUnknownFields()
ProtoSource to discard unknown fields. This only impacts
messages that were generated with support for retaining unknown fields.
Note calling this function alone will have NO immediate effect on the underlying input data. The unknown fields will be discarded during parsing.
public final ProtoSource unsetDiscardUnknownFields()
public int pushLimit(int byteLimit)
throws InvalidProtocolBufferException
currentLimit to (current position) + byteLimit. This
is called when descending into a length-delimited embedded message.InvalidProtocolBufferExceptionpublic void popLimit(int oldLimit)
oldLimit - The old limit, as returned by pushLimit.public int getBytesUntilLimit()
public abstract boolean isAtEnd()
throws IOException
pushLimit(int).
This function may get blocked when using InputStream-backed
streams as it tries to peek ahead.IOExceptionpublic abstract int getTotalBytesRead()
resetSizeCounter()
resets this value to zero on supported implementations.public abstract void rewindTo(int totalBytesRead)
public abstract byte readRawByte()
throws IOException
InvalidProtocolBufferException - The end of the source or the current
limit was reached.IOExceptionpublic abstract void skipRawBytes(int size)
throws IOException
size bytes.InvalidProtocolBufferException - The end of the source or the current
limit was reached.IOExceptionprotected void reserveRepeatedFieldCapacity(us.hebi.quickbuf.RepeatedField<?,?> store,
int tag)
throws IOException
store - target storetag - tag of the contained repeated fieldIOExceptionprotected void reservePackedVarintCapacity(us.hebi.quickbuf.RepeatedField<?,?> store)
throws IOException
store - target storeIOExceptionpublic static int readRawVarint32(InputStream input) throws IOException
readRawVarint32() then you may end up reading too many bytes.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.IOExceptionCopyright © 2019–2024 HEBI Robotics. All rights reserved.