Package org.fusesource.hawtbuf.proto
Class CodedInputStream
- java.lang.Object
-
- java.io.InputStream
-
- java.io.FilterInputStream
-
- org.fusesource.hawtbuf.proto.CodedInputStream
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable
public final class CodedInputStream extends java.io.FilterInputStreamReads 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.
-
-
Field Summary
Fields Modifier and Type Field Description private BufferInputStreambisprivate intlastTagprivate intlimitprivate intpos
-
Constructor Summary
Constructors Constructor Description CodedInputStream(byte[] data)CodedInputStream(java.io.InputStream in)CodedInputStream(Buffer data)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidcheckLastTagWas(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.BufferreadBytes()Read abytesfield value from the stream.doublereadDouble()Read adoublefield value from the stream.intreadEnum()Read an enum field value from the stream.intreadFixed32()Read afixed32field value from the stream.longreadFixed64()Read afixed64field value from the stream.floatreadFloat()Read afloatfield value from the stream.intreadInt32()Read anint32field value from the stream.longreadInt64()Read anint64field value from the stream.bytereadRawByte()Read one byte from the input.BufferreadRawBytes(int size)Read a fixed size of bytes from the input.intreadRawLittleEndian32()Read a 32-bit little-endian integer from the stream.longreadRawLittleEndian64()Read a 64-bit little-endian integer from the stream.intreadRawVarint32()Read a raw Varint from the stream.longreadRawVarint64()Read a raw Varint from the stream.intreadSFixed32()Read ansfixed32field value from the stream.longreadSFixed64()Read ansfixed64field value from the stream.intreadSInt32()Read ansint32field value from the stream.longreadSInt64()Read ansint64field value from the stream.java.lang.StringreadString()Read astringfield value from the stream.intreadTag()Attempt to read a field tag, returning zero if we have reached EOF.intreadUInt32()Read auint32field value from the stream.longreadUInt64()Read auint64field value from the stream.booleanskipField(int tag)Reads and discards a single field, given its tag value.voidskipMessage()Reads and discards an entire message.voidskipRawBytes(int size)Reads and discardssizebytes.
-
-
-
Field Detail
-
lastTag
private int lastTag
-
limit
private int limit
-
pos
private int pos
-
bis
private BufferInputStream bis
-
-
Constructor Detail
-
CodedInputStream
public CodedInputStream(java.io.InputStream in)
-
CodedInputStream
public CodedInputStream(Buffer data)
-
CodedInputStream
public CodedInputStream(byte[] data)
-
-
Method Detail
-
readTag
public 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 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.
-
skipField
public 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
-
skipMessage
public 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
-
readDouble
public double readDouble() throws java.io.IOExceptionRead adoublefield value from the stream.- Throws:
java.io.IOException
-
readFloat
public float readFloat() throws java.io.IOExceptionRead afloatfield value from the stream.- Throws:
java.io.IOException
-
readUInt64
public long readUInt64() throws java.io.IOExceptionRead auint64field value from the stream.- Throws:
java.io.IOException
-
readInt64
public long readInt64() throws java.io.IOExceptionRead anint64field value from the stream.- Throws:
java.io.IOException
-
readInt32
public int readInt32() throws java.io.IOExceptionRead anint32field value from the stream.- Throws:
java.io.IOException
-
readFixed64
public long readFixed64() throws java.io.IOExceptionRead afixed64field value from the stream.- Throws:
java.io.IOException
-
readFixed32
public int readFixed32() throws java.io.IOExceptionRead afixed32field value from the stream.- Throws:
java.io.IOException
-
readBool
public boolean readBool() throws java.io.IOExceptionRead aboolfield value from the stream.- Throws:
java.io.IOException
-
readString
public java.lang.String readString() throws java.io.IOExceptionRead astringfield value from the stream.- Throws:
java.io.IOException
-
readBytes
public Buffer readBytes() throws java.io.IOException
Read abytesfield value from the stream.- Throws:
java.io.IOException
-
readUInt32
public int readUInt32() throws java.io.IOExceptionRead auint32field value from the stream.- Throws:
java.io.IOException
-
readEnum
public 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 int readSFixed32() throws java.io.IOExceptionRead ansfixed32field value from the stream.- Throws:
java.io.IOException
-
readSFixed64
public long readSFixed64() throws java.io.IOExceptionRead ansfixed64field value from the stream.- Throws:
java.io.IOException
-
readSInt32
public int readSInt32() throws java.io.IOExceptionRead ansint32field value from the stream.- Throws:
java.io.IOException
-
readSInt64
public long readSInt64() throws java.io.IOExceptionRead ansint64field value from the stream.- Throws:
java.io.IOException
-
readRawVarint32
public 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 long readRawVarint64() throws java.io.IOExceptionRead a raw Varint from the stream.- Throws:
java.io.IOException
-
readRawLittleEndian32
public int readRawLittleEndian32() throws java.io.IOExceptionRead a 32-bit little-endian integer from the stream.- Throws:
java.io.IOException
-
readRawLittleEndian64
public long readRawLittleEndian64() throws java.io.IOExceptionRead a 64-bit little-endian integer from the stream.- Throws:
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.
-
readRawByte
public 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 Buffer readRawBytes(int size) throws java.io.IOException
Read 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 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
-
pushLimit
public int pushLimit(int limit)
-
popLimit
public void popLimit(int limit)
-
-