Package org.apache.thrift.protocol
Class TCompactProtocol
java.lang.Object
org.apache.thrift.protocol.TProtocol
org.apache.thrift.protocol.TCompactProtocol
- All Implemented Interfaces:
TReadProtocol
,TWriteProtocol
- Direct Known Subclasses:
TTupleProtocol
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
Modifier and TypeClassDescriptionstatic class
TProtocolFactory that produces TCompactProtocols.Nested classes/interfaces inherited from class org.apache.thrift.protocol.TProtocol
TProtocol.ReadCallback<T,
R>, TProtocol.ReadCollectionCallback<R>, TProtocol.ReadMapEntryCallback<K, V>, TProtocol.WriteCallback<T> -
Field Summary
Fields inherited from class org.apache.thrift.protocol.TProtocol
skippedBytes, trans_
-
Constructor Summary
ConstructorDescriptionTCompactProtocol
(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
Modifier and TypeMethodDescriptionint
getMinSerializedSize
(byte type) Return the minimum number of bytes a type will consume on the wireRead a ByteBuffer from the wire.boolean
readBool()
Read a boolean off the wire.byte
readByte()
Read a single byte off the wire.double
No magic here - just read a double off the wire.Read a field header off the wire.void
short
readI16()
Read an i16 from the wire as a zigzag varint.int
readI32()
Read an i32 from the wire as a zigzag varint.long
readI64()
Read an i64 from the wire as a zigzag varint.Read a list header off the wire.void
Read a map header off the wire.void
Read a message header.void
Read a set header off the wire.void
Reads a byte[] (via readBinary), and then UTF-8 decodes it.Read a struct begin.void
Doesn't actually consume any wire data, just removes the last field for this struct from the field stack.readUuid()
void
reset()
Reset any internal state back to a blank slate.protected void
void
writeBinary
(ByteBuffer bin) Write a byte array, using a varint for the size.void
writeBool
(boolean b) Write a boolean value.void
writeByte
(byte b) Write a byte.protected void
writeCollectionBegin
(byte elemType, int size) Abstract method for writing the start of lists and sets.void
writeDouble
(double dub) Write a double to the wire as 8 bytes.void
writeFieldBegin
(TField field) Write a field header containing the field id and field type.void
void
Write the STOP symbol so we know there are no more fields in this struct.void
writeI16
(short i16) Write an I16 as a zigzag varint.void
writeI32
(int i32) Write an i32 as a zigzag varint.void
writeI64
(long i64) Write an i64 as a zigzag varint.void
writeListBegin
(TList list) Write a list header.void
void
writeMapBegin
(TMap map) Write a map header.void
void
writeMessageBegin
(TMessage message) Write a message header to the wire.void
void
writeSetBegin
(TSet set) Write a set header.void
void
writeString
(String str) Write a string to the wire with a varint size preceding.void
writeStructBegin
(TStruct struct) Write a struct begin.void
Write a struct end.void
Methods inherited from class org.apache.thrift.protocol.TProtocol
checkReadBytesAvailable, checkReadBytesAvailable, checkReadBytesAvailable, getScheme, getTransport, readField, readFieldBeginData, readList, readList, readList, readMap, readMap, readMap, readMessage, readSet, readSet, readSet, readStruct, skip, skip, skipBool, skipByte, skipBytes, skipDouble, skipI16, skipI32, skipI64, writeField, writeList, writeMap, writeMessage, writeSet, writeStruct
-
Constructor Details
-
TCompactProtocol
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.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
Create a TCompactProtocol.- Parameters:
transport
- the TTransport object to read from or write to.
-
-
Method Details
-
reset
public void reset()Description copied from class:TProtocol
Reset any internal state back to a blank slate. This method only needs to be implemented for stateful protocols. -
writeMessageBegin
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.- Throws:
TException
-
writeStructBegin
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.- Throws:
TException
-
writeStructEnd
Write 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.- Throws:
TException
-
writeFieldBegin
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.- Throws:
TException
-
writeFieldStop
Write the STOP symbol so we know there are no more fields in this struct.- Throws:
TException
-
writeMapBegin
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.- Throws:
TException
-
writeListBegin
Write a list header.- Throws:
TException
-
writeSetBegin
Write a set header.- Throws:
TException
-
writeBool
Write 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.- Throws:
TException
-
writeByte
Write a byte. Nothing to see here!- Throws:
TException
-
writeI16
Write an I16 as a zigzag varint.- Throws:
TException
-
writeI32
Write an i32 as a zigzag varint.- Throws:
TException
-
writeI64
Write an i64 as a zigzag varint.- Throws:
TException
-
writeDouble
Write a double to the wire as 8 bytes.- Throws:
TException
-
writeUuid
- Throws:
TException
-
writeString
Write a string to the wire with a varint size preceding.- Throws:
TException
-
writeBinary
Write a byte array, using a varint for the size.- Throws:
TException
-
writeMessageEnd
- Throws:
TException
-
writeMapEnd
- Throws:
TException
-
writeListEnd
- Throws:
TException
-
writeSetEnd
- Throws:
TException
-
writeFieldEnd
- Throws:
TException
-
writeCollectionBegin
Abstract method for writing the start of lists and sets. List and sets on the wire differ only by the type indicator.- Throws:
TException
-
readMessageBegin
Read a message header.- Throws:
TException
-
readStructBegin
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.- Throws:
TException
-
readStructEnd
Doesn't actually consume any wire data, just removes the last field for this struct from the field stack.- Throws:
TException
-
readFieldBegin
Read a field header off the wire.- Throws:
TException
-
readMapBegin
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.- Throws:
TException
-
readListBegin
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.- Throws:
TException
-
readSetBegin
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.- Throws:
TException
-
readBool
Read 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.- Throws:
TException
-
readByte
Read a single byte off the wire. Nothing interesting here.- Throws:
TException
-
readI16
Read an i16 from the wire as a zigzag varint.- Throws:
TException
-
readI32
Read an i32 from the wire as a zigzag varint.- Throws:
TException
-
readI64
Read an i64 from the wire as a zigzag varint.- Throws:
TException
-
readDouble
No magic here - just read a double off the wire.- Throws:
TException
-
readUuid
- Throws:
TException
-
readString
Reads a byte[] (via readBinary), and then UTF-8 decodes it.- Throws:
TException
-
readBinary
Read a ByteBuffer from the wire.- Throws:
TException
-
readMessageEnd
- Throws:
TException
-
readFieldEnd
- Throws:
TException
-
readMapEnd
- Throws:
TException
-
readListEnd
- Throws:
TException
-
readSetEnd
- Throws:
TException
-
getMinSerializedSize
Return the minimum number of bytes a type will consume on the wire- Specified by:
getMinSerializedSize
in classTProtocol
- Parameters:
type
- Returns the minimum amount of bytes needed to store the smallest possible instance of TType.- Returns:
- min serialized size
- Throws:
TTransportException
-
skipBinary
- Overrides:
skipBinary
in classTProtocol
- Throws:
TException
-