Package com.google.protobuf
Class CodedInputStream.UnsafeDirectNioDecoder
- java.lang.Object
-
- com.google.protobuf.CodedInputStream
-
- com.google.protobuf.CodedInputStream.UnsafeDirectNioDecoder
-
- Enclosing class:
- CodedInputStream
private static final class CodedInputStream.UnsafeDirectNioDecoder extends CodedInputStream
ACodedInputStream
implementation that uses a backing direct ByteBuffer as the input. Requires the use ofsun.misc.Unsafe
to perform fast reads on the buffer.
-
-
Field Summary
Fields Modifier and Type Field Description private long
address
The unsafe address of the content ofbuffer
.private java.nio.ByteBuffer
buffer
The direct buffer that is backing this stream.private int
bufferSizeAfterLimit
The amount of available data in the buffer beyondlimit
.private int
currentLimit
The absolute position of the end of the current message.private boolean
enableAliasing
Iftrue
, indicates that calls to readByteString
orbyte[]
may return slices of the underlying buffer, rather than copies.private boolean
immutable
Iftrue
, indicates that the buffer is backing aByteString
and is therefore considered to be an immutable input source.private int
lastTag
The last tag that was read from this stream.private long
limit
The unsafe address of the current read limit of the buffer.private long
pos
The unsafe address of the current read position of the buffer.private long
startPos
The unsafe address of the starting read position.-
Fields inherited from class com.google.protobuf.CodedInputStream
recursionDepth, recursionLimit, sizeLimit, wrapper
-
-
Constructor Summary
Constructors Modifier Constructor Description private
UnsafeDirectNioDecoder(java.nio.ByteBuffer buffer, boolean immutable)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description private int
bufferPos(long pos)
void
checkLastTagWas(int value)
Verifies that the last call to readTag() returned the given tag value.void
enableAliasing(boolean enabled)
EnablesByteString
aliasing of the underlying buffer, trading off on buffer pinning for data copies.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.(package private) static boolean
isSupported()
void
popLimit(int oldLimit)
Discards the current limit, returning to the previous limit.int
pushLimit(int byteLimit)
SetscurrentLimit
to (current position) +byteLimit
.boolean
readBool()
Read abool
field value from the stream.byte[]
readByteArray()
Read abytes
field value from the stream.java.nio.ByteBuffer
readByteBuffer()
Read abytes
field value from the stream.ByteString
readBytes()
Read abytes
field value from the stream.double
readDouble()
Read adouble
field value from the stream.int
readEnum()
Read an enum field value from the stream.int
readFixed32()
Read afixed32
field value from the stream.long
readFixed64()
Read afixed64
field value from the stream.float
readFloat()
Read afloat
field value from the stream.void
readGroup(int fieldNumber, MessageLite.Builder builder, ExtensionRegistryLite extensionRegistry)
Read agroup
field value from the stream.<T extends MessageLite>
TreadGroup(int fieldNumber, Parser<T> parser, ExtensionRegistryLite extensionRegistry)
Read agroup
field value from the stream.int
readInt32()
Read anint32
field value from the stream.long
readInt64()
Read anint64
field value from the stream.void
readMessage(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.byte
readRawByte()
Read one byte from the input.byte[]
readRawBytes(int length)
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.(package private) long
readRawVarint64SlowPath()
Variant of readRawVarint64 for when uncomfortably close to the limit.int
readSFixed32()
Read ansfixed32
field value from the stream.long
readSFixed64()
Read ansfixed64
field value from the stream.int
readSInt32()
Read ansint32
field value from the stream.long
readSInt64()
Read ansint64
field value from the stream.java.lang.String
readString()
Read astring
field value from the stream.java.lang.String
readStringRequireUtf8()
Read astring
field value from the stream.int
readTag()
Attempt to read a field tag, returning zero if we have reached EOF.int
readUInt32()
Read auint32
field value from the stream.long
readUInt64()
Read auint64
field value from the stream.void
readUnknownGroup(int fieldNumber, MessageLite.Builder builder)
Deprecated.private void
recomputeBufferSizeAfterLimit()
private int
remaining()
void
resetSizeCounter()
Resets the current size counter to zero (seeCodedInputStream.setSizeLimit(int)
).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 length)
Reads and discardssize
bytes.private void
skipRawVarint()
private void
skipRawVarintFastPath()
private void
skipRawVarintSlowPath()
private java.nio.ByteBuffer
slice(long begin, long end)
-
Methods inherited from class com.google.protobuf.CodedInputStream
checkRecursionLimit, decodeZigZag32, decodeZigZag64, discardUnknownFields, newInstance, newInstance, newInstance, newInstance, newInstance, newInstance, newInstance, newInstance, newInstance, readRawVarint32, readRawVarint32, setRecursionLimit, setSizeLimit, shouldDiscardUnknownFields, unsetDiscardUnknownFields
-
-
-
-
Field Detail
-
buffer
private final java.nio.ByteBuffer buffer
The direct buffer that is backing this stream.
-
immutable
private final boolean immutable
Iftrue
, indicates that the buffer is backing aByteString
and is therefore considered to be an immutable input source.
-
address
private final long address
The unsafe address of the content ofbuffer
.
-
limit
private long limit
The unsafe address of the current read limit of the buffer.
-
pos
private long pos
The unsafe address of the current read position of the buffer.
-
startPos
private long startPos
The unsafe address of the starting read position.
-
bufferSizeAfterLimit
private int bufferSizeAfterLimit
The amount of available data in the buffer beyondlimit
.
-
lastTag
private int lastTag
The last tag that was read from this stream.
-
enableAliasing
private boolean enableAliasing
Iftrue
, indicates that calls to readByteString
orbyte[]
may return slices of the underlying buffer, rather than copies.
-
currentLimit
private int currentLimit
The absolute position of the end of the current message.
-
-
Method Detail
-
isSupported
static boolean isSupported()
-
readTag
public int readTag() throws java.io.IOException
Description copied from class:CodedInputStream
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.- Specified by:
readTag
in classCodedInputStream
- Throws:
java.io.IOException
-
checkLastTagWas
public void checkLastTagWas(int value) throws InvalidProtocolBufferException
Description copied from class:CodedInputStream
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.- Specified by:
checkLastTagWas
in classCodedInputStream
- Throws:
InvalidProtocolBufferException
-value
does not match the last tag.
-
getLastTag
public int getLastTag()
- Specified by:
getLastTag
in classCodedInputStream
-
skipField
public boolean skipField(int tag) throws java.io.IOException
Description copied from class:CodedInputStream
Reads and discards a single field, given its tag value.- Specified by:
skipField
in classCodedInputStream
- Returns:
false
if the tag is an endgroup tag, in which case nothing is skipped. Otherwise, returnstrue
.- Throws:
java.io.IOException
-
skipField
public boolean skipField(int tag, CodedOutputStream output) throws java.io.IOException
Description copied from class:CodedInputStream
Reads a single field and writes it to output in wire format, given its tag value.- Specified by:
skipField
in classCodedInputStream
- Returns:
false
if the tag is an endgroup tag, in which case nothing is skipped. Otherwise, returnstrue
.- Throws:
java.io.IOException
-
skipMessage
public void skipMessage() throws java.io.IOException
Description copied from class:CodedInputStream
Reads and discards an entire message. This will read either until EOF or until an endgroup tag, whichever comes first.- Specified by:
skipMessage
in classCodedInputStream
- Throws:
java.io.IOException
-
skipMessage
public void skipMessage(CodedOutputStream output) throws java.io.IOException
Description copied from class:CodedInputStream
Reads an entire message and writes it to output in wire format. This will read either until EOF or until an endgroup tag, whichever comes first.- Specified by:
skipMessage
in classCodedInputStream
- Throws:
java.io.IOException
-
readDouble
public double readDouble() throws java.io.IOException
Description copied from class:CodedInputStream
Read adouble
field value from the stream.- Specified by:
readDouble
in classCodedInputStream
- Throws:
java.io.IOException
-
readFloat
public float readFloat() throws java.io.IOException
Description copied from class:CodedInputStream
Read afloat
field value from the stream.- Specified by:
readFloat
in classCodedInputStream
- Throws:
java.io.IOException
-
readUInt64
public long readUInt64() throws java.io.IOException
Description copied from class:CodedInputStream
Read auint64
field value from the stream.- Specified by:
readUInt64
in classCodedInputStream
- Throws:
java.io.IOException
-
readInt64
public long readInt64() throws java.io.IOException
Description copied from class:CodedInputStream
Read anint64
field value from the stream.- Specified by:
readInt64
in classCodedInputStream
- Throws:
java.io.IOException
-
readInt32
public int readInt32() throws java.io.IOException
Description copied from class:CodedInputStream
Read anint32
field value from the stream.- Specified by:
readInt32
in classCodedInputStream
- Throws:
java.io.IOException
-
readFixed64
public long readFixed64() throws java.io.IOException
Description copied from class:CodedInputStream
Read afixed64
field value from the stream.- Specified by:
readFixed64
in classCodedInputStream
- Throws:
java.io.IOException
-
readFixed32
public int readFixed32() throws java.io.IOException
Description copied from class:CodedInputStream
Read afixed32
field value from the stream.- Specified by:
readFixed32
in classCodedInputStream
- Throws:
java.io.IOException
-
readBool
public boolean readBool() throws java.io.IOException
Description copied from class:CodedInputStream
Read abool
field value from the stream.- Specified by:
readBool
in classCodedInputStream
- Throws:
java.io.IOException
-
readString
public java.lang.String readString() throws java.io.IOException
Description copied from class:CodedInputStream
Read astring
field value from the stream. If the stream contains malformed UTF-8, replace the offending bytes with the standard UTF-8 replacement character.- Specified by:
readString
in classCodedInputStream
- Throws:
java.io.IOException
-
readStringRequireUtf8
public java.lang.String readStringRequireUtf8() throws java.io.IOException
Description copied from class:CodedInputStream
Read astring
field value from the stream. If the stream contains malformed UTF-8, throw exceptionInvalidProtocolBufferException
.- Specified by:
readStringRequireUtf8
in classCodedInputStream
- Throws:
java.io.IOException
-
readGroup
public void readGroup(int fieldNumber, MessageLite.Builder builder, ExtensionRegistryLite extensionRegistry) throws java.io.IOException
Description copied from class:CodedInputStream
Read agroup
field value from the stream.- Specified by:
readGroup
in classCodedInputStream
- Throws:
java.io.IOException
-
readGroup
public <T extends MessageLite> T readGroup(int fieldNumber, Parser<T> parser, ExtensionRegistryLite extensionRegistry) throws java.io.IOException
Description copied from class:CodedInputStream
Read agroup
field value from the stream.- Specified by:
readGroup
in classCodedInputStream
- Throws:
java.io.IOException
-
readUnknownGroup
@Deprecated public void readUnknownGroup(int fieldNumber, MessageLite.Builder builder) throws java.io.IOException
Deprecated.Description copied from class:CodedInputStream
Reads agroup
field value from the stream and merges it into the givenUnknownFieldSet
.- Specified by:
readUnknownGroup
in classCodedInputStream
- Throws:
java.io.IOException
-
readMessage
public void readMessage(MessageLite.Builder builder, ExtensionRegistryLite extensionRegistry) throws java.io.IOException
Description copied from class:CodedInputStream
Read an embedded message field value from the stream.- Specified by:
readMessage
in classCodedInputStream
- Throws:
java.io.IOException
-
readMessage
public <T extends MessageLite> T readMessage(Parser<T> parser, ExtensionRegistryLite extensionRegistry) throws java.io.IOException
Description copied from class:CodedInputStream
Read an embedded message field value from the stream.- Specified by:
readMessage
in classCodedInputStream
- Throws:
java.io.IOException
-
readBytes
public ByteString readBytes() throws java.io.IOException
Description copied from class:CodedInputStream
Read abytes
field value from the stream.- Specified by:
readBytes
in classCodedInputStream
- Throws:
java.io.IOException
-
readByteArray
public byte[] readByteArray() throws java.io.IOException
Description copied from class:CodedInputStream
Read abytes
field value from the stream.- Specified by:
readByteArray
in classCodedInputStream
- Throws:
java.io.IOException
-
readByteBuffer
public java.nio.ByteBuffer readByteBuffer() throws java.io.IOException
Description copied from class:CodedInputStream
Read abytes
field value from the stream.- Specified by:
readByteBuffer
in classCodedInputStream
- Throws:
java.io.IOException
-
readUInt32
public int readUInt32() throws java.io.IOException
Description copied from class:CodedInputStream
Read auint32
field value from the stream.- Specified by:
readUInt32
in classCodedInputStream
- Throws:
java.io.IOException
-
readEnum
public int readEnum() throws java.io.IOException
Description copied from class:CodedInputStream
Read an enum field value from the stream. Caller is responsible for converting the numeric value to an actual enum.- Specified by:
readEnum
in classCodedInputStream
- Throws:
java.io.IOException
-
readSFixed32
public int readSFixed32() throws java.io.IOException
Description copied from class:CodedInputStream
Read ansfixed32
field value from the stream.- Specified by:
readSFixed32
in classCodedInputStream
- Throws:
java.io.IOException
-
readSFixed64
public long readSFixed64() throws java.io.IOException
Description copied from class:CodedInputStream
Read ansfixed64
field value from the stream.- Specified by:
readSFixed64
in classCodedInputStream
- Throws:
java.io.IOException
-
readSInt32
public int readSInt32() throws java.io.IOException
Description copied from class:CodedInputStream
Read ansint32
field value from the stream.- Specified by:
readSInt32
in classCodedInputStream
- Throws:
java.io.IOException
-
readSInt64
public long readSInt64() throws java.io.IOException
Description copied from class:CodedInputStream
Read ansint64
field value from the stream.- Specified by:
readSInt64
in classCodedInputStream
- Throws:
java.io.IOException
-
readRawVarint32
public int readRawVarint32() throws java.io.IOException
Description copied from class:CodedInputStream
Read a raw Varint from the stream. If larger than 32 bits, discard the upper bits.- Specified by:
readRawVarint32
in classCodedInputStream
- Throws:
java.io.IOException
-
skipRawVarint
private void skipRawVarint() throws java.io.IOException
- Throws:
java.io.IOException
-
skipRawVarintFastPath
private void skipRawVarintFastPath() throws java.io.IOException
- Throws:
java.io.IOException
-
skipRawVarintSlowPath
private void skipRawVarintSlowPath() throws java.io.IOException
- Throws:
java.io.IOException
-
readRawVarint64
public long readRawVarint64() throws java.io.IOException
Description copied from class:CodedInputStream
Read a raw Varint from the stream.- Specified by:
readRawVarint64
in classCodedInputStream
- Throws:
java.io.IOException
-
readRawVarint64SlowPath
long readRawVarint64SlowPath() throws java.io.IOException
Description copied from class:CodedInputStream
Variant of readRawVarint64 for when uncomfortably close to the limit.- Specified by:
readRawVarint64SlowPath
in classCodedInputStream
- Throws:
java.io.IOException
-
readRawLittleEndian32
public int readRawLittleEndian32() throws java.io.IOException
Description copied from class:CodedInputStream
Read a 32-bit little-endian integer from the stream.- Specified by:
readRawLittleEndian32
in classCodedInputStream
- Throws:
java.io.IOException
-
readRawLittleEndian64
public long readRawLittleEndian64() throws java.io.IOException
Description copied from class:CodedInputStream
Read a 64-bit little-endian integer from the stream.- Specified by:
readRawLittleEndian64
in classCodedInputStream
- Throws:
java.io.IOException
-
enableAliasing
public void enableAliasing(boolean enabled)
Description copied from class:CodedInputStream
EnablesByteString
aliasing of the underlying buffer, trading off on buffer pinning for data copies. Only valid for buffer-backed streams.- Specified by:
enableAliasing
in classCodedInputStream
-
resetSizeCounter
public void resetSizeCounter()
Description copied from class:CodedInputStream
Resets the current size counter to zero (seeCodedInputStream.setSizeLimit(int)
). Only valid forInputStream
-backed streams.- Specified by:
resetSizeCounter
in classCodedInputStream
-
pushLimit
public int pushLimit(int byteLimit) throws InvalidProtocolBufferException
Description copied from class:CodedInputStream
SetscurrentLimit
to (current position) +byteLimit
. This is called when descending into a length-delimited embedded message.Note that
pushLimit()
does NOT affect how many bytes theCodedInputStream
reads from an underlyingInputStream
when 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 yourInputStream
which limits the amount of data that can be read from it.- Specified by:
pushLimit
in classCodedInputStream
- Returns:
- the old limit.
- Throws:
InvalidProtocolBufferException
-
popLimit
public void popLimit(int oldLimit)
Description copied from class:CodedInputStream
Discards the current limit, returning to the previous limit.- Specified by:
popLimit
in classCodedInputStream
- Parameters:
oldLimit
- The old limit, as returned bypushLimit
.
-
getBytesUntilLimit
public int getBytesUntilLimit()
Description copied from class:CodedInputStream
Returns the number of bytes to be read before the current limit. If no limit is set, returns -1.- Specified by:
getBytesUntilLimit
in classCodedInputStream
-
isAtEnd
public boolean isAtEnd() throws java.io.IOException
Description copied from class:CodedInputStream
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 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:
isAtEnd
in classCodedInputStream
- Throws:
java.io.IOException
-
getTotalBytesRead
public int getTotalBytesRead()
Description copied from class:CodedInputStream
The total bytes read up to the current position. CallingCodedInputStream.resetSizeCounter()
resets this value to zero.- Specified by:
getTotalBytesRead
in classCodedInputStream
-
readRawByte
public byte readRawByte() throws java.io.IOException
Description copied from class:CodedInputStream
Read one byte from the input.- Specified by:
readRawByte
in classCodedInputStream
- Throws:
InvalidProtocolBufferException
- The end of the stream or the current limit was reached.java.io.IOException
-
readRawBytes
public byte[] readRawBytes(int length) throws java.io.IOException
Description copied from class:CodedInputStream
Read a fixed size of bytes from the input.- Specified by:
readRawBytes
in classCodedInputStream
- Throws:
InvalidProtocolBufferException
- The end of the stream or the current limit was reached.java.io.IOException
-
skipRawBytes
public void skipRawBytes(int length) throws java.io.IOException
Description copied from class:CodedInputStream
Reads and discardssize
bytes.- Specified by:
skipRawBytes
in classCodedInputStream
- Throws:
InvalidProtocolBufferException
- The end of the stream or the current limit was reached.java.io.IOException
-
recomputeBufferSizeAfterLimit
private void recomputeBufferSizeAfterLimit()
-
remaining
private int remaining()
-
bufferPos
private int bufferPos(long pos)
-
slice
private java.nio.ByteBuffer slice(long begin, long end) throws java.io.IOException
- Throws:
java.io.IOException
-
-