Class CodedInputStream
java.lang.Object
java.io.InputStream
java.io.FilterInputStream
org.fusesource.hawtbuf.proto.CodedInputStream
- All Implemented Interfaces:
Closeable, AutoCloseable
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(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.-
Field Summary
FieldsFields inherited from class FilterInputStream
in -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidcheckLastTagWas(int value) Verifies that the last call to readTag() returned the given tag value.static intdecodeZigZag32(int n) Decode a ZigZag-encoded 32-bit value.static longdecodeZigZag64(long n) Decode a ZigZag-encoded 64-bit value.voidpopLimit(int limit) intpushLimit(int limit) booleanreadBool()Read aboolfield value from the stream.Read abytesfield value from the stream.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.intRead anint32field value from the stream.longRead anint64field value from the stream.byteRead one byte from the input.readRawBytes(int size) 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.longRead a raw Varint from the stream.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.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.booleanskipField(int tag) Reads and discards a single field, given its tag value.voidReads and discards an entire message.voidskipRawBytes(int size) Reads and discardssizebytes.Methods inherited from class FilterInputStream
available, close, mark, markSupported, read, read, read, reset, skip
-
Field Details
-
lastTag
private int lastTag -
limit
private int limit -
pos
private int pos -
bis
-
-
Constructor Details
-
CodedInputStream
-
CodedInputStream
-
CodedInputStream
public CodedInputStream(byte[] data)
-
-
Method Details
-
readTag
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.- Throws:
IOException
-
checkLastTagWas
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.- Throws:
InvalidProtocolBufferException-valuedoes not match the last tag.
-
skipField
Reads 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:
IOException
-
skipMessage
Reads and discards an entire message. This will read either until EOF or until an endgroup tag, whichever comes first.- Throws:
IOException
-
readDouble
Read adoublefield value from the stream.- Throws:
IOException
-
readFloat
Read afloatfield value from the stream.- Throws:
IOException
-
readUInt64
Read auint64field value from the stream.- Throws:
IOException
-
readInt64
Read anint64field value from the stream.- Throws:
IOException
-
readInt32
Read anint32field value from the stream.- Throws:
IOException
-
readFixed64
Read afixed64field value from the stream.- Throws:
IOException
-
readFixed32
Read afixed32field value from the stream.- Throws:
IOException
-
readBool
Read aboolfield value from the stream.- Throws:
IOException
-
readString
Read astringfield value from the stream.- Throws:
IOException
-
readBytes
Read abytesfield value from the stream.- Throws:
IOException
-
readUInt32
Read auint32field value from the stream.- Throws:
IOException
-
readEnum
Read an enum field value from the stream. Caller is responsible for converting the numeric value to an actual enum.- Throws:
IOException
-
readSFixed32
Read ansfixed32field value from the stream.- Throws:
IOException
-
readSFixed64
Read ansfixed64field value from the stream.- Throws:
IOException
-
readSInt32
Read ansint32field value from the stream.- Throws:
IOException
-
readSInt64
Read ansint64field value from the stream.- Throws:
IOException
-
readRawVarint32
Read a raw Varint from the stream. If larger than 32 bits, discard the upper bits.- Throws:
IOException
-
readRawVarint64
Read a raw Varint from the stream.- Throws:
IOException
-
readRawLittleEndian32
Read a 32-bit little-endian integer from the stream.- Throws:
IOException
-
readRawLittleEndian64
Read a 64-bit little-endian integer from the stream.- Throws:
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.
-
readRawByte
Read one byte from the input.- Throws:
InvalidProtocolBufferException- The end of the stream or the current limit was reached.IOException
-
readRawBytes
Read a fixed size of bytes from the input.- Throws:
InvalidProtocolBufferException- The end of the stream or the current limit was reached.IOException
-
skipRawBytes
Reads and discardssizebytes.- Throws:
InvalidProtocolBufferException- The end of the stream or the current limit was reached.IOException
-
pushLimit
public int pushLimit(int limit) -
popLimit
public void popLimit(int limit)
-