Class CodedInputStream.ArrayDecoder
- Direct Known Subclasses:
CodedInputStream.ArrayDecoderNewVarintAllCases, CodedInputStream.ArrayDecoderNewVarintTagsLengthsOnly, CodedInputStream.ArrayDecoderOldVarint
- Enclosing class:
CodedInputStream
CodedInputStream implementation that uses a backing array as the input.-
Nested Class Summary
Nested classes/interfaces inherited from class CodedInputStream
CodedInputStream.VarintExperiment -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final byte[]private final intThe limit of the buffer this decoder is permitted to read.private intThe unclamped absolute limit of the end of the current message (as an absolute index in the buffer).private booleanprivate final booleanprivate intprivate intThe clamped reading limit, guaranteed to be invalid input: '<'=bufferLimit.private intprivate intFields inherited from class CodedInputStream
groupDepth, messageDepth, recursionLimit, sizeLimit, wrapper -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivateArrayDecoder(byte[] buffer, int offset, int len, boolean immutable) -
Method Summary
Modifier and TypeMethodDescriptionvoidcheckLastTagWas(int value) Verifies that the last call to readTag() returned the given tag value.voidenableAliasing(boolean enabled) Enables aliasing of the underlying input buffer for buffer-backed decoders.intReturns the number of bytes to be read before the current limit.intintThe total bytes read up to the current position.booleanisAtEnd()Returns true if the stream has reached the end of the input.voidpopLimit(int oldLimit) Discards the current limit, returning to the previous limit.private voidpopLimitAfterMessage(int oldLimit) intpushLimit(int byteLimit) SetscurrentLimitto (current position) +byteLimit.private intbooleanreadBool()Read aboolfield value from the stream.byte[]Read abytesfield value from the stream.Read abytesfield value from the stream.Read abytesfield value from the stream.private ByteStringreadBytesInternal(boolean requireUtf8) doubleRead adoublefield value from the stream.intreadEnum()Read an enum field value from the stream.intRead afixed32field value from the stream.longRead afixed64field value from the stream.floatRead afloatfield value from the stream.voidreadGroup(int fieldNumber, MessageLite.Builder builder, ExtensionRegistryLite extensionRegistry) Read agroupfield value from the stream.<T extends MessageLite>
TreadGroup(int fieldNumber, Parser<T> parser, ExtensionRegistryLite extensionRegistry) Read agroupfield value from the stream.intRead anint32field value from the stream.longRead anint64field value from the stream.voidreadMessage(MessageLite.Builder builder, ExtensionRegistryLite extensionRegistry) Read an embedded message field value from the stream.<T extends MessageLite>
TreadMessage(Parser<T> parser, ExtensionRegistryLite extensionRegistry) Read an embedded message field value from the stream.byteRead one byte from the input.byte[]readRawBytes(int length) Read a fixed size of bytes from the input.intRead a 32-bit little-endian integer from the stream.longRead a 64-bit little-endian integer from the stream.intRead a raw Varint from the stream.protected abstract intTemporary shim to enable new varint experiment.protected abstract intTemporary shim to enable new varint experiment.protected intprivate intFast case: the limit is not checked here, this may read in the buffer past the limit, and may throw an IndexOutOfBoundsException if the varint runs off the end of the buffer.protected intlongRead a raw Varint from the stream.(package private) longVariant of readRawVarint64 for when uncomfortably close to the limit.intRead ansfixed32field value from the stream.longRead ansfixed64field value from the stream.intRead ansint32field value from the stream.longRead ansint64field value from the stream.Read astringfield value from the stream.Read astringfield value from the stream.intreadTag()Attempt to read a field tag, returning zero if we have reached EOF.intRead auint32field value from the stream.longRead auint64field value from the stream.voidreadUnknownGroup(int fieldNumber, MessageLite.Builder builder) Deprecated.voidResets the current size counter to zero (seeCodedInputStream.setSizeLimit(int)).private voidsetCurrentLimit(int newLimit) Sets the active message boundary (currentLimit) and clamps the reading limit (limit).booleanskipField(int tag) Reads and discards a single field, given its tag value.booleanskipField(int tag, CodedOutputStream output) Reads a single field and writes it to output in wire format, given its tag value.voidskipRawBytes(int length) Reads and discardssizebytes.private voidprivate voidprivate voidintstreamRawBytes(byte[] bytes, int offset, int length) Read up tolengthbytes intobytesstarting atoffset.Methods inherited from class CodedInputStream
checkRecursionLimit, checkValidEndTag, decodeZigZag32, decodeZigZag64, discardUnknownFields, newInstance, newInstance, newInstance, newInstance, newInstance, newInstance, newInstance, newInstance, newInstance, readPrimitiveField, readRawVarint32, readRawVarint32, readString, setRecursionLimit, setSizeLimit, setVarintExperiment, shouldDiscardUnknownFields, skipMessage, skipMessage, unsetDiscardUnknownFields
-
Field Details
-
buffer
private final byte[] buffer -
bufferLimit
private final int bufferLimitThe limit of the buffer this decoder is permitted to read. Usually this is equal to buffer.length but can be smaller if we are asked to parse a slice of a larger array. -
immutable
private final boolean immutable -
limit
private int limitThe clamped reading limit, guaranteed to be invalid input: '<'=bufferLimit. Reading stops whenposreaches this index. This is the clamped version ofcurrentLimit. -
pos
private int pos -
startPos
private int startPos -
lastTag
private int lastTag -
enableAliasing
private boolean enableAliasing -
currentLimit
private int currentLimitThe unclamped absolute limit of the end of the current message (as an absolute index in the buffer). This can exceed the physical buffer capacity (e.g., when it isInteger.MAX_VALUEindicating no limit, or if the input is corrupt and specifies a length extending beyondbufferLimit).Note: when this value is modified,
setCurrentLimit()must be called to ensurelimitis updated (clamped) accordingly.
-
-
Constructor Details
-
ArrayDecoder
private ArrayDecoder(byte[] buffer, int offset, int len, boolean immutable)
-
-
Method Details
-
readTag
Description copied from class:CodedInputStreamAttempt 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.- Specified by:
readTagin classCodedInputStream- Throws:
IOException
-
checkLastTagWas
Description copied from class:CodedInputStreamVerifies 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.- Specified by:
checkLastTagWasin classCodedInputStream- Throws:
InvalidProtocolBufferException-valuedoes not match the last tag.
-
getLastTag
public int getLastTag()- Specified by:
getLastTagin classCodedInputStream
-
skipField
Description copied from class:CodedInputStreamReads and discards a single field, given its tag value.- Specified by:
skipFieldin classCodedInputStream- Returns:
falseif the tag is an endgroup tag, in which case nothing is skipped. Otherwise, returnstrue.- Throws:
IOException
-
skipField
Description copied from class:CodedInputStreamReads a single field and writes it to output in wire format, given its tag value.- Specified by:
skipFieldin classCodedInputStream- Returns:
falseif the tag is an endgroup tag, in which case nothing is skipped. Otherwise, returnstrue.- Throws:
IOException
-
readDouble
Description copied from class:CodedInputStreamRead adoublefield value from the stream.- Specified by:
readDoublein classCodedInputStream- Throws:
IOException
-
readFloat
Description copied from class:CodedInputStreamRead afloatfield value from the stream.- Specified by:
readFloatin classCodedInputStream- Throws:
IOException
-
readUInt64
Description copied from class:CodedInputStreamRead auint64field value from the stream.- Specified by:
readUInt64in classCodedInputStream- Throws:
IOException
-
readInt64
Description copied from class:CodedInputStreamRead anint64field value from the stream.- Specified by:
readInt64in classCodedInputStream- Throws:
IOException
-
readInt32
Description copied from class:CodedInputStreamRead anint32field value from the stream.- Specified by:
readInt32in classCodedInputStream- Throws:
IOException
-
readFixed64
Description copied from class:CodedInputStreamRead afixed64field value from the stream.- Specified by:
readFixed64in classCodedInputStream- Throws:
IOException
-
readFixed32
Description copied from class:CodedInputStreamRead afixed32field value from the stream.- Specified by:
readFixed32in classCodedInputStream- Throws:
IOException
-
readBool
Description copied from class:CodedInputStreamRead aboolfield value from the stream.- Specified by:
readBoolin classCodedInputStream- Throws:
IOException
-
readString
Description copied from class:CodedInputStreamRead astringfield value from the stream. If the stream contains malformed UTF-8, replace the offending bytes with the standard UTF-8 replacement character.- Specified by:
readStringin classCodedInputStream- Throws:
IOException
-
readStringRequireUtf8
Description copied from class:CodedInputStreamRead astringfield value from the stream. If the stream contains malformed UTF-8, throw exceptionInvalidProtocolBufferException.- Specified by:
readStringRequireUtf8in classCodedInputStream- Throws:
IOException
-
readGroup
public void readGroup(int fieldNumber, MessageLite.Builder builder, ExtensionRegistryLite extensionRegistry) throws IOException Description copied from class:CodedInputStreamRead agroupfield value from the stream.- Specified by:
readGroupin classCodedInputStream- Throws:
IOException
-
readGroup
public <T extends MessageLite> T readGroup(int fieldNumber, Parser<T> parser, ExtensionRegistryLite extensionRegistry) throws IOException Description copied from class:CodedInputStreamRead agroupfield value from the stream.- Specified by:
readGroupin classCodedInputStream- Throws:
IOException
-
readUnknownGroup
@Deprecated public void readUnknownGroup(int fieldNumber, MessageLite.Builder builder) throws IOException Deprecated.Description copied from class:CodedInputStreamReads agroupfield value from the stream and merges it into the givenUnknownFieldSet.- Specified by:
readUnknownGroupin classCodedInputStream- Throws:
IOException
-
readMessage
public void readMessage(MessageLite.Builder builder, ExtensionRegistryLite extensionRegistry) throws IOException Description copied from class:CodedInputStreamRead an embedded message field value from the stream.- Specified by:
readMessagein classCodedInputStream- Throws:
IOException
-
readMessage
public <T extends MessageLite> T readMessage(Parser<T> parser, ExtensionRegistryLite extensionRegistry) throws IOException Description copied from class:CodedInputStreamRead an embedded message field value from the stream.- Specified by:
readMessagein classCodedInputStream- Throws:
IOException
-
pushLimitBeforeMessage
- Throws:
IOException
-
popLimitAfterMessage
- Throws:
IOException
-
readBytesInternal
- Throws:
IOException
-
readBytes
Description copied from class:CodedInputStreamRead abytesfield value from the stream.When
CodedInputStream.enableAliasing(boolean)is enabled and supported by the underlying decoder, the returnedByteStringmay 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
ByteStringis still in use. If you cannot guarantee buffer lifetime, do not enable aliasing or copy the bytes before storing them.- Specified by:
readBytesin classCodedInputStream- Throws:
IOException
-
readByteArray
Description copied from class:CodedInputStreamRead abytesfield value from the stream.- Specified by:
readByteArrayin classCodedInputStream- Throws:
IOException
-
readByteBuffer
Description copied from class:CodedInputStreamRead abytesfield value from the stream.If aliasing is enabled, the returned
ByteBuffermay reference (alias) the underlying input buffer for decoders that are backed by an on-heapbyte[]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.- Specified by:
readByteBufferin classCodedInputStream- Throws:
IOException
-
readUInt32
Description copied from class:CodedInputStreamRead auint32field value from the stream.- Specified by:
readUInt32in classCodedInputStream- Throws:
IOException
-
readEnum
Description copied from class:CodedInputStreamRead an enum field value from the stream. Caller is responsible for converting the numeric value to an actual enum.- Specified by:
readEnumin classCodedInputStream- Throws:
IOException
-
readSFixed32
Description copied from class:CodedInputStreamRead ansfixed32field value from the stream.- Specified by:
readSFixed32in classCodedInputStream- Throws:
IOException
-
readSFixed64
Description copied from class:CodedInputStreamRead ansfixed64field value from the stream.- Specified by:
readSFixed64in classCodedInputStream- Throws:
IOException
-
readSInt32
Description copied from class:CodedInputStreamRead ansint32field value from the stream.- Specified by:
readSInt32in classCodedInputStream- Throws:
IOException
-
readSInt64
Description copied from class:CodedInputStreamRead ansint64field value from the stream.- Specified by:
readSInt64in classCodedInputStream- Throws:
IOException
-
readRawVarint32Expected5BytesMax
Temporary shim to enable new varint experiment.Same as readRawVarint32 but for callers where the varint being longer than 5 bytes should never happen in practice (eg tags and lengths).
- Throws:
IOException
-
readRawVarint32Expected10BytesMax
Temporary shim to enable new varint experiment.Same as readRawVarint32 but for callers where a 10-byte varint is 'normal' (eg int32 fields where negative values will be 10 bytes).
- Throws:
IOException
-
readRawVarint32
Description copied from class:CodedInputStreamRead a raw Varint from the stream. If larger than 32 bits, discard the upper bits.- Specified by:
readRawVarint32in classCodedInputStream- Throws:
IOException
-
readRawVarint32Old
- Throws:
IOException
-
readRawVarint32New
- Throws:
IOException
-
readRawVarint32NewFast
Fast case: the limit is not checked here, this may read in the buffer past the limit, and may throw an IndexOutOfBoundsException if the varint runs off the end of the buffer.- Throws:
IOException
-
skipRawVarint
- Throws:
IOException
-
skipRawVarintFastPath
- Throws:
IOException
-
skipRawVarintSlowPath
- Throws:
IOException
-
readRawVarint64
Description copied from class:CodedInputStreamRead a raw Varint from the stream.- Specified by:
readRawVarint64in classCodedInputStream- Throws:
IOException
-
readRawVarint64SlowPath
Description copied from class:CodedInputStreamVariant of readRawVarint64 for when uncomfortably close to the limit.- Specified by:
readRawVarint64SlowPathin classCodedInputStream- Throws:
IOException
-
readRawLittleEndian32
Description copied from class:CodedInputStreamRead a 32-bit little-endian integer from the stream.- Specified by:
readRawLittleEndian32in classCodedInputStream- Throws:
IOException
-
readRawLittleEndian64
Description copied from class:CodedInputStreamRead a 64-bit little-endian integer from the stream.- Specified by:
readRawLittleEndian64in classCodedInputStream- Throws:
IOException
-
enableAliasing
public void enableAliasing(boolean enabled) Description copied from class:CodedInputStreamEnables aliasing of the underlying input buffer for buffer-backed decoders.When aliasing is enabled and supported by the underlying decoder,
CodedInputStream.readBytes()andCodedInputStream.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-delimitedbytesfields.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
ByteStringorByteBufferis 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:
CodedInputStream.readByteBuffer()may return a mutableByteBuffereven when it aliases the underlying input. Callers that require safety-by-default should treat the returned buffer as read-only or callByteBuffer.asReadOnlyBuffer()immediately.- Specified by:
enableAliasingin classCodedInputStream
-
resetSizeCounter
public void resetSizeCounter()Description copied from class:CodedInputStreamResets the current size counter to zero (seeCodedInputStream.setSizeLimit(int)). Only valid forInputStream-backed streams.- Specified by:
resetSizeCounterin classCodedInputStream
-
pushLimit
Description copied from class:CodedInputStreamSetscurrentLimitto (current position) +byteLimit. This is called when descending into a length-delimited embedded message.Note that
pushLimit()does NOT affect how many bytes theCodedInputStreamreads from an underlyingInputStreamwhen refreshing its buffer. If you need to prevent reading past a certain point in the underlyingInputStream(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 yourInputStreamwhich limits the amount of data that can be read from it.- Specified by:
pushLimitin classCodedInputStream- Returns:
- the old limit.
- Throws:
InvalidProtocolBufferException
-
setCurrentLimit
private void setCurrentLimit(int newLimit) Sets the active message boundary (currentLimit) and clamps the reading limit (limit). -
popLimit
public void popLimit(int oldLimit) Description copied from class:CodedInputStreamDiscards the current limit, returning to the previous limit.- Specified by:
popLimitin classCodedInputStream- Parameters:
oldLimit- The old limit, as returned bypushLimit.
-
getBytesUntilLimit
public int getBytesUntilLimit()Description copied from class:CodedInputStreamReturns the number of bytes to be read before the current limit. If no limit is set, returns -1.- Specified by:
getBytesUntilLimitin classCodedInputStream
-
isAtEnd
Description copied from class:CodedInputStreamReturns 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 usingCodedInputStream.pushLimit(int). This function may get blocked when using StreamDecoder as it invokesCodedInputStream.StreamDecoder.tryRefillBuffer(int)in this function which will try to read bytes from input.- Specified by:
isAtEndin classCodedInputStream- Throws:
IOException
-
getTotalBytesRead
public int getTotalBytesRead()Description copied from class:CodedInputStreamThe total bytes read up to the current position. CallingCodedInputStream.resetSizeCounter()resets this value to zero.- Specified by:
getTotalBytesReadin classCodedInputStream
-
readRawByte
Description copied from class:CodedInputStreamRead one byte from the input.- Specified by:
readRawBytein classCodedInputStream- Throws:
IOException
-
readRawBytes
Description copied from class:CodedInputStreamRead a fixed size of bytes from the input.- Specified by:
readRawBytesin classCodedInputStream- Throws:
IOException
-
streamRawBytes
Description copied from class:CodedInputStreamRead up tolengthbytes intobytesstarting atoffset.Behaves similarly to
InputStream.read(byte[],int,int); a zero-sized read will read zero bytes, other sizes will attempt to read up to the requested number of bytes, but may read less.- Specified by:
streamRawBytesin classCodedInputStream- Returns:
- The number of bytes read into
bytes, or -1 if the end of data or current limit has been reached. - Throws:
IOException
-
skipRawBytes
Description copied from class:CodedInputStreamReads and discardssizebytes.- Specified by:
skipRawBytesin classCodedInputStream- Throws:
IOException
-