Package org.apache.thrift.protocol
Class TCompactProtocol
- java.lang.Object
-
- org.apache.thrift.protocol.TProtocol
-
- org.apache.thrift.protocol.TCompactProtocol
-
- Direct Known Subclasses:
TTupleProtocol
public class TCompactProtocol extends TProtocol
TCompactProtocol2 is the Java implementation of the compact protocol specified in THRIFT-110. The fundamental approach to reducing the overhead of structures is a) use variable-length integers all over the place and b) make use of unused bits wherever possible. Your savings will obviously vary based on the specific makeup of your structs, but in general, the more fields, nested structures, short strings and collections, and low-value i32 and i64 fields you have, the more benefit you'll see.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classTCompactProtocol.FactoryTProtocolFactory that produces TCompactProtocols.
-
Constructor Summary
Constructors Constructor Description TCompactProtocol(TTransport transport)Create a TCompactProtocol.TCompactProtocol(TTransport transport, long stringLengthLimit)Deprecated.Use constructor specifying both string limit and container limit insteadTCompactProtocol(TTransport transport, long stringLengthLimit, long containerLengthLimit)Create a TCompactProtocol.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.nio.ByteBufferreadBinary()Read a byte[] from the wire.booleanreadBool()Read a boolean off the wire.bytereadByte()Read a single byte off the wire.doublereadDouble()No magic here - just read a double off the wire.TFieldreadFieldBegin()Read a field header off the wire.voidreadFieldEnd()shortreadI16()Read an i16 from the wire as a zigzag varint.intreadI32()Read an i32 from the wire as a zigzag varint.longreadI64()Read an i64 from the wire as a zigzag varint.TListreadListBegin()Read a list header off the wire.voidreadListEnd()TMapreadMapBegin()Read a map header off the wire.voidreadMapEnd()TMessagereadMessageBegin()Read a message header.voidreadMessageEnd()TSetreadSetBegin()Read a set header off the wire.voidreadSetEnd()java.lang.StringreadString()Reads a byte[] (via readBinary), and then UTF-8 decodes it.TStructreadStructBegin()Read a struct begin.voidreadStructEnd()Doesn't actually consume any wire data, just removes the last field for this struct from the field stack.voidreset()Reset any internal state back to a blank slate.voidwriteBinary(java.nio.ByteBuffer bin)Write a byte array, using a varint for the size.voidwriteBool(boolean b)Write a boolean value.voidwriteByte(byte b)Write a byte.protected voidwriteCollectionBegin(byte elemType, int size)Abstract method for writing the start of lists and sets.voidwriteDouble(double dub)Write a double to the wire as 8 bytes.voidwriteFieldBegin(TField field)Write a field header containing the field id and field type.voidwriteFieldEnd()voidwriteFieldStop()Write the STOP symbol so we know there are no more fields in this struct.voidwriteI16(short i16)Write an I16 as a zigzag varint.voidwriteI32(int i32)Write an i32 as a zigzag varint.voidwriteI64(long i64)Write an i64 as a zigzag varint.voidwriteListBegin(TList list)Write a list header.voidwriteListEnd()voidwriteMapBegin(TMap map)Write a map header.voidwriteMapEnd()voidwriteMessageBegin(TMessage message)Write a message header to the wire.voidwriteMessageEnd()voidwriteSetBegin(TSet set)Write a set header.voidwriteSetEnd()voidwriteString(java.lang.String str)Write a string to the wire with a varint size preceding.voidwriteStructBegin(TStruct struct)Write a struct begin.voidwriteStructEnd()Write a struct end.-
Methods inherited from class org.apache.thrift.protocol.TProtocol
getScheme, getTransport
-
-
-
-
Constructor Detail
-
TCompactProtocol
public TCompactProtocol(TTransport transport, long stringLengthLimit, long containerLengthLimit)
Create a TCompactProtocol.- Parameters:
transport- the TTransport object to read from or write to.stringLengthLimit- the maximum number of bytes to read for variable-length fields.containerLengthLimit- the maximum number of elements to read for containers.
-
TCompactProtocol
@Deprecated public TCompactProtocol(TTransport transport, long stringLengthLimit)
Deprecated.Use constructor specifying both string limit and container limit insteadCreate a TCompactProtocol.- Parameters:
transport- the TTransport object to read from or write to.stringLengthLimit- the maximum number of bytes to read for variable-length fields.
-
TCompactProtocol
public TCompactProtocol(TTransport transport)
Create a TCompactProtocol.- Parameters:
transport- the TTransport object to read from or write to.
-
-
Method Detail
-
reset
public void reset()
Description copied from class:TProtocolReset any internal state back to a blank slate. This method only needs to be implemented for stateful protocols.
-
writeMessageBegin
public void writeMessageBegin(TMessage message) throws TException
Write a message header to the wire. Compact Protocol messages contain the protocol version so we can migrate forwards in the future if need be.- Specified by:
writeMessageBeginin classTProtocol- Throws:
TException
-
writeStructBegin
public void writeStructBegin(TStruct struct) throws TException
Write a struct begin. This doesn't actually put anything on the wire. We use it as an opportunity to put special placeholder markers on the field stack so we can get the field id deltas correct.- Specified by:
writeStructBeginin classTProtocol- Throws:
TException
-
writeStructEnd
public void writeStructEnd() throws TExceptionWrite a struct end. This doesn't actually put anything on the wire. We use this as an opportunity to pop the last field from the current struct off of the field stack.- Specified by:
writeStructEndin classTProtocol- Throws:
TException
-
writeFieldBegin
public void writeFieldBegin(TField field) throws TException
Write a field header containing the field id and field type. If the difference between the current field id and the last one is small (< 15), then the field id will be encoded in the 4 MSB as a delta. Otherwise, the field id will follow the type header as a zigzag varint.- Specified by:
writeFieldBeginin classTProtocol- Throws:
TException
-
writeFieldStop
public void writeFieldStop() throws TExceptionWrite the STOP symbol so we know there are no more fields in this struct.- Specified by:
writeFieldStopin classTProtocol- Throws:
TException
-
writeMapBegin
public void writeMapBegin(TMap map) throws TException
Write a map header. If the map is empty, omit the key and value type headers, as we don't need any additional information to skip it.- Specified by:
writeMapBeginin classTProtocol- Throws:
TException
-
writeListBegin
public void writeListBegin(TList list) throws TException
Write a list header.- Specified by:
writeListBeginin classTProtocol- Throws:
TException
-
writeSetBegin
public void writeSetBegin(TSet set) throws TException
Write a set header.- Specified by:
writeSetBeginin classTProtocol- Throws:
TException
-
writeBool
public void writeBool(boolean b) throws TExceptionWrite a boolean value. Potentially, this could be a boolean field, in which case the field header info isn't written yet. If so, decide what the right type header is for the value and then write the field header. Otherwise, write a single byte.- Specified by:
writeBoolin classTProtocol- Throws:
TException
-
writeByte
public void writeByte(byte b) throws TExceptionWrite a byte. Nothing to see here!- Specified by:
writeBytein classTProtocol- Throws:
TException
-
writeI16
public void writeI16(short i16) throws TExceptionWrite an I16 as a zigzag varint.- Specified by:
writeI16in classTProtocol- Throws:
TException
-
writeI32
public void writeI32(int i32) throws TExceptionWrite an i32 as a zigzag varint.- Specified by:
writeI32in classTProtocol- Throws:
TException
-
writeI64
public void writeI64(long i64) throws TExceptionWrite an i64 as a zigzag varint.- Specified by:
writeI64in classTProtocol- Throws:
TException
-
writeDouble
public void writeDouble(double dub) throws TExceptionWrite a double to the wire as 8 bytes.- Specified by:
writeDoublein classTProtocol- Throws:
TException
-
writeString
public void writeString(java.lang.String str) throws TExceptionWrite a string to the wire with a varint size preceding.- Specified by:
writeStringin classTProtocol- Throws:
TException
-
writeBinary
public void writeBinary(java.nio.ByteBuffer bin) throws TExceptionWrite a byte array, using a varint for the size.- Specified by:
writeBinaryin classTProtocol- Throws:
TException
-
writeMessageEnd
public void writeMessageEnd() throws TException- Specified by:
writeMessageEndin classTProtocol- Throws:
TException
-
writeMapEnd
public void writeMapEnd() throws TException- Specified by:
writeMapEndin classTProtocol- Throws:
TException
-
writeListEnd
public void writeListEnd() throws TException- Specified by:
writeListEndin classTProtocol- Throws:
TException
-
writeSetEnd
public void writeSetEnd() throws TException- Specified by:
writeSetEndin classTProtocol- Throws:
TException
-
writeFieldEnd
public void writeFieldEnd() throws TException- Specified by:
writeFieldEndin classTProtocol- Throws:
TException
-
writeCollectionBegin
protected void writeCollectionBegin(byte elemType, int size) throws TExceptionAbstract method for writing the start of lists and sets. List and sets on the wire differ only by the type indicator.- Throws:
TException
-
readMessageBegin
public TMessage readMessageBegin() throws TException
Read a message header.- Specified by:
readMessageBeginin classTProtocol- Throws:
TException
-
readStructBegin
public TStruct readStructBegin() throws TException
Read a struct begin. There's nothing on the wire for this, but it is our opportunity to push a new struct begin marker onto the field stack.- Specified by:
readStructBeginin classTProtocol- Throws:
TException
-
readStructEnd
public void readStructEnd() throws TExceptionDoesn't actually consume any wire data, just removes the last field for this struct from the field stack.- Specified by:
readStructEndin classTProtocol- Throws:
TException
-
readFieldBegin
public TField readFieldBegin() throws TException
Read a field header off the wire.- Specified by:
readFieldBeginin classTProtocol- Throws:
TException
-
readMapBegin
public TMap readMapBegin() throws TException
Read a map header off the wire. If the size is zero, skip reading the key and value type. This means that 0-length maps will yield TMaps without the "correct" types.- Specified by:
readMapBeginin classTProtocol- Throws:
TException
-
readListBegin
public TList readListBegin() throws TException
Read a list header off the wire. If the list size is 0-14, the size will be packed into the element type header. If it's a longer list, the 4 MSB of the element type header will be 0xF, and a varint will follow with the true size.- Specified by:
readListBeginin classTProtocol- Throws:
TException
-
readSetBegin
public TSet readSetBegin() throws TException
Read a set header off the wire. If the set size is 0-14, the size will be packed into the element type header. If it's a longer set, the 4 MSB of the element type header will be 0xF, and a varint will follow with the true size.- Specified by:
readSetBeginin classTProtocol- Throws:
TException
-
readBool
public boolean readBool() throws TExceptionRead a boolean off the wire. If this is a boolean field, the value should already have been read during readFieldBegin, so we'll just consume the pre-stored value. Otherwise, read a byte.- Specified by:
readBoolin classTProtocol- Throws:
TException
-
readByte
public byte readByte() throws TExceptionRead a single byte off the wire. Nothing interesting here.- Specified by:
readBytein classTProtocol- Throws:
TException
-
readI16
public short readI16() throws TExceptionRead an i16 from the wire as a zigzag varint.- Specified by:
readI16in classTProtocol- Throws:
TException
-
readI32
public int readI32() throws TExceptionRead an i32 from the wire as a zigzag varint.- Specified by:
readI32in classTProtocol- Throws:
TException
-
readI64
public long readI64() throws TExceptionRead an i64 from the wire as a zigzag varint.- Specified by:
readI64in classTProtocol- Throws:
TException
-
readDouble
public double readDouble() throws TExceptionNo magic here - just read a double off the wire.- Specified by:
readDoublein classTProtocol- Throws:
TException
-
readString
public java.lang.String readString() throws TExceptionReads a byte[] (via readBinary), and then UTF-8 decodes it.- Specified by:
readStringin classTProtocol- Throws:
TException
-
readBinary
public java.nio.ByteBuffer readBinary() throws TExceptionRead a byte[] from the wire.- Specified by:
readBinaryin classTProtocol- Throws:
TException
-
readMessageEnd
public void readMessageEnd() throws TException- Specified by:
readMessageEndin classTProtocol- Throws:
TException
-
readFieldEnd
public void readFieldEnd() throws TException- Specified by:
readFieldEndin classTProtocol- Throws:
TException
-
readMapEnd
public void readMapEnd() throws TException- Specified by:
readMapEndin classTProtocol- Throws:
TException
-
readListEnd
public void readListEnd() throws TException- Specified by:
readListEndin classTProtocol- Throws:
TException
-
readSetEnd
public void readSetEnd() throws TException- Specified by:
readSetEndin classTProtocol- Throws:
TException
-
-