Class CodedInputStream
- java.lang.Object
-
- com.google.protobuf.CodedInputStream
-
public abstract class CodedInputStream extends java.lang.ObjectReads 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()andreadInt32()) and methods that read low-level values (e.g.readRawVarint32()andreadRawBytes(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.
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Deprecated Methods Modifier and Type Method Description abstract voidcheckLastTagWas(int value)Verifies that the last call to readTag() returned the given tag value.voidcheckRecursionLimit()static intdecodeZigZag32(int n)Decode a ZigZag-encoded 32-bit value.static longdecodeZigZag64(long n)Decode a ZigZag-encoded 64-bit value.abstract voidenableAliasing(boolean enabled)EnablesByteStringaliasing of the underlying buffer, trading off on buffer pinning for data copies.abstract intgetBytesUntilLimit()Returns the number of bytes to be read before the current limit.abstract intgetLastTag()abstract intgetTotalBytesRead()The total bytes read up to the current position.abstract booleanisAtEnd()Returns true if the stream has reached the end of the input.static CodedInputStreamnewInstance(byte[] buf)Create a new CodedInputStream wrapping the given byte array.static CodedInputStreamnewInstance(byte[] buf, int off, int len)Create a new CodedInputStream wrapping the given byte array slice.static CodedInputStreamnewInstance(java.io.InputStream input)Create a new CodedInputStream wrapping the given InputStream.static CodedInputStreamnewInstance(java.io.InputStream input, int bufferSize)Create a new CodedInputStream wrapping the given InputStream, with a specified buffer size.static CodedInputStreamnewInstance(java.lang.Iterable<java.nio.ByteBuffer> input)Create a new CodedInputStream wrapping the givenIterable <ByteBuffer>.static CodedInputStreamnewInstance(java.nio.ByteBuffer buf)Create a new CodedInputStream wrapping the given ByteBuffer.abstract voidpopLimit(int oldLimit)Discards the current limit, returning to the previous limit.abstract intpushLimit(int byteLimit)SetscurrentLimitto (current position) +byteLimit.abstract booleanreadBool()Read aboolfield value from the stream.abstract byte[]readByteArray()Read abytesfield value from the stream.abstract java.nio.ByteBufferreadByteBuffer()Read abytesfield value from the stream.abstract ByteStringreadBytes()Read abytesfield value from the stream.abstract doublereadDouble()Read adoublefield value from the stream.abstract intreadEnum()Read an enum field value from the stream.abstract intreadFixed32()Read afixed32field value from the stream.abstract longreadFixed64()Read afixed64field value from the stream.abstract floatreadFloat()Read afloatfield value from the stream.abstract voidreadGroup(int fieldNumber, MessageLite.Builder builder, ExtensionRegistryLite extensionRegistry)Read agroupfield value from the stream.abstract <T extends MessageLite>
TreadGroup(int fieldNumber, Parser<T> parser, ExtensionRegistryLite extensionRegistry)Read agroupfield value from the stream.abstract intreadInt32()Read anint32field value from the stream.abstract longreadInt64()Read anint64field value from the stream.abstract voidreadMessage(MessageLite.Builder builder, ExtensionRegistryLite extensionRegistry)Read an embedded message field value from the stream.abstract <T extends MessageLite>
TreadMessage(Parser<T> parser, ExtensionRegistryLite extensionRegistry)Read an embedded message field value from the stream.abstract bytereadRawByte()Read one byte from the input.abstract byte[]readRawBytes(int size)Read a fixed size of bytes from the input.abstract intreadRawLittleEndian32()Read a 32-bit little-endian integer from the stream.abstract longreadRawLittleEndian64()Read a 64-bit little-endian integer from the stream.abstract intreadRawVarint32()Read a raw Varint from the stream.static intreadRawVarint32(int firstByte, java.io.InputStream input)LikereadRawVarint32(InputStream), but expects that the caller has already read one byte.abstract longreadRawVarint64()Read a raw Varint from the stream.abstract intreadSFixed32()Read ansfixed32field value from the stream.abstract longreadSFixed64()Read ansfixed64field value from the stream.abstract intreadSInt32()Read ansint32field value from the stream.abstract longreadSInt64()Read ansint64field value from the stream.abstract java.lang.StringreadString()Read astringfield value from the stream.abstract java.lang.StringreadStringRequireUtf8()Read astringfield value from the stream.abstract intreadTag()Attempt to read a field tag, returning zero if we have reached EOF.abstract intreadUInt32()Read auint32field value from the stream.abstract longreadUInt64()Read auint64field value from the stream.abstract voidreadUnknownGroup(int fieldNumber, MessageLite.Builder builder)Deprecated.UnknownFieldSet.Builder now implements MessageLite.Builder, so you can just callreadGroup(int, com.google.protobuf.MessageLite.Builder, com.google.protobuf.ExtensionRegistryLite).abstract voidresetSizeCounter()Resets the current size counter to zero (seesetSizeLimit(int)).intsetRecursionLimit(int limit)Set the maximum message recursion depth.intsetSizeLimit(int limit)Only valid forInputStream-backed streams.abstract booleanskipField(int tag)Reads and discards a single field, given its tag value.abstract booleanskipField(int tag, CodedOutputStream output)Deprecated.useUnknownFieldSetorUnknownFieldSetLiteto skip to an output stream.abstract voidskipMessage()Reads and discards an entire message.abstract voidskipMessage(CodedOutputStream output)Reads an entire message and writes it to output in wire format.abstract voidskipRawBytes(int size)Reads and discardssizebytes.
-
-
-
Method Detail
-
newInstance
public static CodedInputStream newInstance(java.io.InputStream input)
Create a new CodedInputStream wrapping the given InputStream.
-
newInstance
public static CodedInputStream newInstance(java.io.InputStream input, int bufferSize)
Create a new CodedInputStream wrapping the given InputStream, with a specified buffer size.
-
newInstance
public static CodedInputStream newInstance(java.lang.Iterable<java.nio.ByteBuffer> input)
Create a new CodedInputStream wrapping the givenIterable <ByteBuffer>.
-
newInstance
public static CodedInputStream newInstance(byte[] buf)
Create a new CodedInputStream wrapping the given byte array.
-
newInstance
public static CodedInputStream newInstance(byte[] buf, int off, int len)
Create a new CodedInputStream wrapping the given byte array slice.
-
newInstance
public static CodedInputStream newInstance(java.nio.ByteBuffer buf)
Create a new CodedInputStream wrapping the given ByteBuffer. The data starting from the ByteBuffer's current position to its limit will be read. The returned CodedInputStream may or may not share the underlying data in the ByteBuffer, therefore the ByteBuffer cannot be changed while the CodedInputStream is in use. Note that the ByteBuffer's position won't be changed by this function. Concurrent calls with the same ByteBuffer object are safe if no other thread is trying to alter the ByteBuffer's status.
-
checkRecursionLimit
public void checkRecursionLimit() throws InvalidProtocolBufferException- Throws:
InvalidProtocolBufferException
-
readTag
public abstract int readTag() throws java.io.IOExceptionAttempt 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.- Throws:
java.io.IOException
-
checkLastTagWas
public abstract void checkLastTagWas(int value) throws InvalidProtocolBufferExceptionVerifies 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.- Throws:
InvalidProtocolBufferException-valuedoes not match the last tag.
-
getLastTag
public abstract int getLastTag()
-
skipField
public abstract boolean skipField(int tag) throws java.io.IOExceptionReads and discards a single field, given its tag value.- Returns:
falseif the tag is an endgroup tag, in which case nothing is skipped. Otherwise, returnstrue.- Throws:
java.io.IOException
-
skipField
@Deprecated public abstract boolean skipField(int tag, CodedOutputStream output) throws java.io.IOExceptionDeprecated.useUnknownFieldSetorUnknownFieldSetLiteto skip to an output stream.Reads a single field and writes it to output in wire format, given its tag value.- Returns:
falseif the tag is an endgroup tag, in which case nothing is skipped. Otherwise, returnstrue.- Throws:
java.io.IOException
-
skipMessage
public abstract void skipMessage() throws java.io.IOExceptionReads and discards an entire message. This will read either until EOF or until an endgroup tag, whichever comes first.- Throws:
java.io.IOException
-
skipMessage
public abstract void skipMessage(CodedOutputStream output) throws java.io.IOException
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.- Throws:
java.io.IOException
-
readDouble
public abstract double readDouble() throws java.io.IOExceptionRead adoublefield value from the stream.- Throws:
java.io.IOException
-
readFloat
public abstract float readFloat() throws java.io.IOExceptionRead afloatfield value from the stream.- Throws:
java.io.IOException
-
readUInt64
public abstract long readUInt64() throws java.io.IOExceptionRead auint64field value from the stream.- Throws:
java.io.IOException
-
readInt64
public abstract long readInt64() throws java.io.IOExceptionRead anint64field value from the stream.- Throws:
java.io.IOException
-
readInt32
public abstract int readInt32() throws java.io.IOExceptionRead anint32field value from the stream.- Throws:
java.io.IOException
-
readFixed64
public abstract long readFixed64() throws java.io.IOExceptionRead afixed64field value from the stream.- Throws:
java.io.IOException
-
readFixed32
public abstract int readFixed32() throws java.io.IOExceptionRead afixed32field value from the stream.- Throws:
java.io.IOException
-
readBool
public abstract boolean readBool() throws java.io.IOExceptionRead aboolfield value from the stream.- Throws:
java.io.IOException
-
readString
public abstract java.lang.String readString() throws java.io.IOExceptionRead astringfield value from the stream. If the stream contains malformed UTF-8, replace the offending bytes with the standard UTF-8 replacement character.- Throws:
java.io.IOException
-
readStringRequireUtf8
public abstract java.lang.String readStringRequireUtf8() throws java.io.IOExceptionRead astringfield value from the stream. If the stream contains malformed UTF-8, throw exceptionInvalidProtocolBufferException.- Throws:
java.io.IOException
-
readGroup
public abstract void readGroup(int fieldNumber, MessageLite.Builder builder, ExtensionRegistryLite extensionRegistry) throws java.io.IOExceptionRead agroupfield value from the stream.- Throws:
java.io.IOException
-
readGroup
public abstract <T extends MessageLite> T readGroup(int fieldNumber, Parser<T> parser, ExtensionRegistryLite extensionRegistry) throws java.io.IOException
Read agroupfield value from the stream.- Throws:
java.io.IOException
-
readUnknownGroup
@Deprecated public abstract void readUnknownGroup(int fieldNumber, MessageLite.Builder builder) throws java.io.IOExceptionDeprecated.UnknownFieldSet.Builder now implements MessageLite.Builder, so you can just callreadGroup(int, com.google.protobuf.MessageLite.Builder, com.google.protobuf.ExtensionRegistryLite).Reads agroupfield value from the stream and merges it into the givenUnknownFieldSet.- Throws:
java.io.IOException
-
readMessage
public abstract void readMessage(MessageLite.Builder builder, ExtensionRegistryLite extensionRegistry) throws java.io.IOException
Read an embedded message field value from the stream.- Throws:
java.io.IOException
-
readMessage
public abstract <T extends MessageLite> T readMessage(Parser<T> parser, ExtensionRegistryLite extensionRegistry) throws java.io.IOException
Read an embedded message field value from the stream.- Throws:
java.io.IOException
-
readBytes
public abstract ByteString readBytes() throws java.io.IOException
Read abytesfield value from the stream.- Throws:
java.io.IOException
-
readByteArray
public abstract byte[] readByteArray() throws java.io.IOExceptionRead abytesfield value from the stream.- Throws:
java.io.IOException
-
readByteBuffer
public abstract java.nio.ByteBuffer readByteBuffer() throws java.io.IOExceptionRead abytesfield value from the stream.- Throws:
java.io.IOException
-
readUInt32
public abstract int readUInt32() throws java.io.IOExceptionRead auint32field value from the stream.- Throws:
java.io.IOException
-
readEnum
public abstract int readEnum() throws java.io.IOExceptionRead an enum field value from the stream. Caller is responsible for converting the numeric value to an actual enum.- Throws:
java.io.IOException
-
readSFixed32
public abstract int readSFixed32() throws java.io.IOExceptionRead ansfixed32field value from the stream.- Throws:
java.io.IOException
-
readSFixed64
public abstract long readSFixed64() throws java.io.IOExceptionRead ansfixed64field value from the stream.- Throws:
java.io.IOException
-
readSInt32
public abstract int readSInt32() throws java.io.IOExceptionRead ansint32field value from the stream.- Throws:
java.io.IOException
-
readSInt64
public abstract long readSInt64() throws java.io.IOExceptionRead ansint64field value from the stream.- Throws:
java.io.IOException
-
readRawVarint32
public abstract int readRawVarint32() throws java.io.IOExceptionRead a raw Varint from the stream. If larger than 32 bits, discard the upper bits.- Throws:
java.io.IOException
-
readRawVarint64
public abstract long readRawVarint64() throws java.io.IOExceptionRead a raw Varint from the stream.- Throws:
java.io.IOException
-
readRawLittleEndian32
public abstract int readRawLittleEndian32() throws java.io.IOExceptionRead a 32-bit little-endian integer from the stream.- Throws:
java.io.IOException
-
readRawLittleEndian64
public abstract long readRawLittleEndian64() throws java.io.IOExceptionRead a 64-bit little-endian integer from the stream.- Throws:
java.io.IOException
-
enableAliasing
public abstract void enableAliasing(boolean enabled)
EnablesByteStringaliasing of the underlying buffer, trading off on buffer pinning for data copies. Only valid for buffer-backed streams.
-
setRecursionLimit
public final int setRecursionLimit(int limit)
Set the maximum message recursion depth. In order to prevent malicious messages from causing stack overflows,CodedInputStreamlimits how deeply messages may be nested. The default limit is 100.- Returns:
- the old limit.
-
setSizeLimit
public final int setSizeLimit(int limit)
Only valid forInputStream-backed streams.Set the maximum message size. In order to prevent malicious messages from exhausting memory or causing integer overflows,
CodedInputStreamlimits how large a message may be. The default limit isInteger.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 anInputStream, 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:
- the old limit.
-
resetSizeCounter
public abstract void resetSizeCounter()
Resets the current size counter to zero (seesetSizeLimit(int)). Only valid forInputStream-backed streams.
-
pushLimit
public abstract int pushLimit(int byteLimit) throws InvalidProtocolBufferExceptionSetscurrentLimitto (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.- Returns:
- the old limit.
- Throws:
InvalidProtocolBufferException
-
popLimit
public abstract void popLimit(int oldLimit)
Discards the current limit, returning to the previous limit.- Parameters:
oldLimit- The old limit, as returned bypushLimit.
-
getBytesUntilLimit
public abstract int getBytesUntilLimit()
Returns the number of bytes to be read before the current limit. If no limit is set, returns -1.
-
isAtEnd
public abstract boolean isAtEnd() throws java.io.IOExceptionReturns 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 usingpushLimit(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.- Throws:
java.io.IOException
-
getTotalBytesRead
public abstract int getTotalBytesRead()
The total bytes read up to the current position. CallingresetSizeCounter()resets this value to zero.
-
readRawByte
public abstract byte readRawByte() throws java.io.IOExceptionRead one byte from the input.- Throws:
InvalidProtocolBufferException- The end of the stream or the current limit was reached.java.io.IOException
-
readRawBytes
public abstract byte[] readRawBytes(int size) throws java.io.IOExceptionRead a fixed size of bytes from the input.- Throws:
InvalidProtocolBufferException- The end of the stream or the current limit was reached.java.io.IOException
-
skipRawBytes
public abstract void skipRawBytes(int size) throws java.io.IOExceptionReads and discardssizebytes.- Throws:
InvalidProtocolBufferException- The end of the stream or the current limit was reached.java.io.IOException
-
decodeZigZag32
public static int decodeZigZag32(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- An unsigned 32-bit integer, stored in a signed int because Java has no explicit unsigned support.- Returns:
- A signed 32-bit integer.
-
decodeZigZag64
public static long decodeZigZag64(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- An unsigned 64-bit integer, stored in a signed int because Java has no explicit unsigned support.- Returns:
- A signed 64-bit integer.
-
readRawVarint32
public static int readRawVarint32(int firstByte, java.io.InputStream input) throws java.io.IOExceptionLikereadRawVarint32(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.- Throws:
java.io.IOException
-
-