summaryrefslogtreecommitdiff
path: root/lib/java/src/main/java/org/apache/thrift/protocol/TCompactProtocol.java
diff options
context:
space:
mode:
Diffstat (limited to 'lib/java/src/main/java/org/apache/thrift/protocol/TCompactProtocol.java')
-rw-r--r--lib/java/src/main/java/org/apache/thrift/protocol/TCompactProtocol.java489
1 files changed, 211 insertions, 278 deletions
diff --git a/lib/java/src/main/java/org/apache/thrift/protocol/TCompactProtocol.java b/lib/java/src/main/java/org/apache/thrift/protocol/TCompactProtocol.java
index 3bace8e90..8adb25e69 100644
--- a/lib/java/src/main/java/org/apache/thrift/protocol/TCompactProtocol.java
+++ b/lib/java/src/main/java/org/apache/thrift/protocol/TCompactProtocol.java
@@ -17,36 +17,32 @@
* under the License.
*/
-
package org.apache.thrift.protocol;
-import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
-
import org.apache.thrift.TException;
import org.apache.thrift.transport.TTransport;
import org.apache.thrift.transport.TTransportException;
/**
- * 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.
+ * 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.
*/
public class TCompactProtocol extends TProtocol {
- private final static byte[] EMPTY_BYTES = new byte[0];
- private final static ByteBuffer EMPTY_BUFFER = ByteBuffer.wrap(EMPTY_BYTES);
+ private static final byte[] EMPTY_BYTES = new byte[0];
+ private static final ByteBuffer EMPTY_BUFFER = ByteBuffer.wrap(EMPTY_BYTES);
- private final static long NO_LENGTH_LIMIT = -1;
+ private static final long NO_LENGTH_LIMIT = -1;
- private final static TStruct ANONYMOUS_STRUCT = new TStruct("");
- private final static TField TSTOP = new TField("", TType.STOP, (short)0);
+ private static final TStruct ANONYMOUS_STRUCT = new TStruct("");
+ private static final TField TSTOP = new TField("", TType.STOP, (short) 0);
- private final static byte[] ttypeToCompactType = new byte[16];
+ private static final byte[] ttypeToCompactType = new byte[16];
static {
ttypeToCompactType[TType.STOP] = TType.STOP;
@@ -63,9 +59,7 @@ public class TCompactProtocol extends TProtocol {
ttypeToCompactType[TType.STRUCT] = Types.STRUCT;
}
- /**
- * TProtocolFactory that produces TCompactProtocols.
- */
+ /** TProtocolFactory that produces TCompactProtocols. */
public static class Factory implements TProtocolFactory {
private final long stringLengthLimit_;
private final long containerLengthLimit_;
@@ -88,67 +82,63 @@ public class TCompactProtocol extends TProtocol {
}
}
- private static final byte PROTOCOL_ID = (byte)0x82;
+ private static final byte PROTOCOL_ID = (byte) 0x82;
private static final byte VERSION = 1;
private static final byte VERSION_MASK = 0x1f; // 0001 1111
- private static final byte TYPE_MASK = (byte)0xE0; // 1110 0000
+ private static final byte TYPE_MASK = (byte) 0xE0; // 1110 0000
private static final byte TYPE_BITS = 0x07; // 0000 0111
- private static final int TYPE_SHIFT_AMOUNT = 5;
+ private static final int TYPE_SHIFT_AMOUNT = 5;
- /**
- * All of the on-wire type codes.
- */
+ /** All of the on-wire type codes. */
private static class Types {
- public static final byte BOOLEAN_TRUE = 0x01;
- public static final byte BOOLEAN_FALSE = 0x02;
- public static final byte BYTE = 0x03;
- public static final byte I16 = 0x04;
- public static final byte I32 = 0x05;
- public static final byte I64 = 0x06;
- public static final byte DOUBLE = 0x07;
- public static final byte BINARY = 0x08;
- public static final byte LIST = 0x09;
- public static final byte SET = 0x0A;
- public static final byte MAP = 0x0B;
- public static final byte STRUCT = 0x0C;
+ public static final byte BOOLEAN_TRUE = 0x01;
+ public static final byte BOOLEAN_FALSE = 0x02;
+ public static final byte BYTE = 0x03;
+ public static final byte I16 = 0x04;
+ public static final byte I32 = 0x05;
+ public static final byte I64 = 0x06;
+ public static final byte DOUBLE = 0x07;
+ public static final byte BINARY = 0x08;
+ public static final byte LIST = 0x09;
+ public static final byte SET = 0x0A;
+ public static final byte MAP = 0x0B;
+ public static final byte STRUCT = 0x0C;
}
/**
- * Used to keep track of the last field for the current and previous structs,
- * so we can do the delta stuff.
+ * Used to keep track of the last field for the current and previous structs, so we can do the
+ * delta stuff.
*/
private final ShortStack lastField_ = new ShortStack(15);
private short lastFieldId_ = 0;
/**
- * If we encounter a boolean field begin, save the TField here so it can
- * have the value incorporated.
+ * If we encounter a boolean field begin, save the TField here so it can have the value
+ * incorporated.
*/
private TField booleanField_ = null;
/**
- * If we read a field header, and it's a boolean field, save the boolean
- * value here so that readBool can use it.
+ * If we read a field header, and it's a boolean field, save the boolean value here so that
+ * readBool can use it.
*/
private Boolean boolValue_ = null;
/**
- * The maximum number of bytes to read from the transport for
- * variable-length fields (such as strings or binary) or {@link #NO_LENGTH_LIMIT} for
- * unlimited.
+ * The maximum number of bytes to read from the transport for variable-length fields (such as
+ * strings or binary) or {@link #NO_LENGTH_LIMIT} for unlimited.
*/
private final long stringLengthLimit_;
/**
- * The maximum number of elements to read from the network for
- * containers (maps, sets, lists), or {@link #NO_LENGTH_LIMIT} for unlimited.
+ * The maximum number of elements to read from the network for containers (maps, sets, lists), or
+ * {@link #NO_LENGTH_LIMIT} for unlimited.
*/
private final long containerLengthLimit_;
/**
- * Temporary buffer used for various operations that would otherwise require a
- * small allocation.
+ * Temporary buffer used for various operations that would otherwise require a small allocation.
*/
private final byte[] temp = new byte[10];
@@ -156,10 +146,8 @@ public class TCompactProtocol extends TProtocol {
* Create a TCompactProtocol.
*
* @param transport the TTransport object to read from or write to.
- * @param stringLengthLimit the maximum number of bytes to read for
- * variable-length fields.
- * @param containerLengthLimit the maximum number of elements to read
- * for containers.
+ * @param stringLengthLimit the maximum number of bytes to read for variable-length fields.
+ * @param containerLengthLimit the maximum number of elements to read for containers.
*/
public TCompactProtocol(TTransport transport, long stringLengthLimit, long containerLengthLimit) {
super(transport);
@@ -171,8 +159,7 @@ public class TCompactProtocol extends TProtocol {
* Create a TCompactProtocol.
*
* @param transport the TTransport object to read from or write to.
- * @param stringLengthLimit the maximum number of bytes to read for
- * variable-length fields.
+ * @param stringLengthLimit the maximum number of bytes to read for variable-length fields.
* @deprecated Use constructor specifying both string limit and container limit instead
*/
@Deprecated
@@ -200,8 +187,8 @@ public class TCompactProtocol extends TProtocol {
//
/**
- * 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.
+ * 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.
*/
@Override
public void writeMessageBegin(TMessage message) throws TException {
@@ -212,9 +199,9 @@ public class TCompactProtocol extends TProtocol {
}
/**
- * 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.
+ * 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.
*/
@Override
public void writeStructBegin(TStruct struct) throws TException {
@@ -223,33 +210,30 @@ public class TCompactProtocol extends TProtocol {
}
/**
- * 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.
+ * 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.
*/
public void writeStructEnd() throws TException {
lastFieldId_ = lastField_.pop();
}
/**
- * 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.
+ * 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.
*/
public void writeFieldBegin(TField field) throws TException {
if (field.type == TType.BOOL) {
// we want to possibly include the value, so we'll wait.
booleanField_ = field;
} else {
- writeFieldBeginInternal(field, (byte)-1);
+ writeFieldBeginInternal(field, (byte) -1);
}
}
/**
- * The workhorse of writeFieldBegin. It has the option of doing a
- * 'type override' of the type header. This is used specifically in the
- * boolean field case.
+ * The workhorse of writeFieldBegin. It has the option of doing a 'type override' of the type
+ * header. This is used specifically in the boolean field case.
*/
private void writeFieldBeginInternal(TField field, byte typeOverride) throws TException {
// short lastField = lastField_.pop();
@@ -271,16 +255,14 @@ public class TCompactProtocol extends TProtocol {
// lastField_.push(field.id);
}
- /**
- * Write the STOP symbol so we know there are no more fields in this struct.
- */
+ /** Write the STOP symbol so we know there are no more fields in this struct. */
public void writeFieldStop() throws TException {
writeByteDirect(TType.STOP);
}
/**
- * 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.
+ * 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.
*/
public void writeMapBegin(TMap map) throws TException {
if (map.size == 0) {
@@ -291,25 +273,20 @@ public class TCompactProtocol extends TProtocol {
}
}
- /**
- * Write a list header.
- */
+ /** Write a list header. */
public void writeListBegin(TList list) throws TException {
writeCollectionBegin(list.elemType, list.size);
}
- /**
- * Write a set header.
- */
+ /** Write a set header. */
public void writeSetBegin(TSet set) throws TException {
writeCollectionBegin(set.elemType, set.size);
}
/**
- * 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.
+ * 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.
*/
public void writeBool(boolean b) throws TException {
if (booleanField_ != null) {
@@ -322,54 +299,40 @@ public class TCompactProtocol extends TProtocol {
}
}
- /**
- * Write a byte. Nothing to see here!
- */
+ /** Write a byte. Nothing to see here! */
public void writeByte(byte b) throws TException {
writeByteDirect(b);
}
- /**
- * Write an I16 as a zigzag varint.
- */
+ /** Write an I16 as a zigzag varint. */
public void writeI16(short i16) throws TException {
writeVarint32(intToZigZag(i16));
}
- /**
- * Write an i32 as a zigzag varint.
- */
+ /** Write an i32 as a zigzag varint. */
public void writeI32(int i32) throws TException {
writeVarint32(intToZigZag(i32));
}
- /**
- * Write an i64 as a zigzag varint.
- */
+ /** Write an i64 as a zigzag varint. */
public void writeI64(long i64) throws TException {
writeVarint64(longToZigzag(i64));
}
- /**
- * Write a double to the wire as 8 bytes.
- */
+ /** Write a double to the wire as 8 bytes. */
public void writeDouble(double dub) throws TException {
fixedLongToBytes(Double.doubleToLongBits(dub), temp, 0);
trans_.write(temp, 0, 8);
}
- /**
- * Write a string to the wire with a varint size preceding.
- */
+ /** Write a string to the wire with a varint size preceding. */
public void writeString(String str) throws TException {
byte[] bytes = str.getBytes(StandardCharsets.UTF_8);
writeVarint32(bytes.length);
trans_.write(bytes, 0, bytes.length);
}
- /**
- * Write a byte array, using a varint for the size.
- */
+ /** Write a byte array, using a varint for the size. */
public void writeBinary(ByteBuffer bin) throws TException {
ByteBuffer bb = bin.asReadOnlyBuffer();
writeVarint32(bb.remaining());
@@ -382,9 +345,13 @@ public class TCompactProtocol extends TProtocol {
//
public void writeMessageEnd() throws TException {}
+
public void writeMapEnd() throws TException {}
+
public void writeListEnd() throws TException {}
+
public void writeSetEnd() throws TException {}
+
public void writeFieldEnd() throws TException {}
//
@@ -392,8 +359,8 @@ public class TCompactProtocol extends TProtocol {
//
/**
- * Abstract method for writing the start of lists and sets. List and sets on
- * the wire differ only by the type indicator.
+ * Abstract method for writing the start of lists and sets. List and sets on the wire differ only
+ * by the type indicator.
*/
protected void writeCollectionBegin(byte elemType, int size) throws TException {
if (size <= 14) {
@@ -405,19 +372,19 @@ public class TCompactProtocol extends TProtocol {
}
/**
- * Write an i32 as a varint. Results in 1-5 bytes on the wire.
- * TODO: make a permanent buffer like writeVarint64?
+ * Write an i32 as a varint. Results in 1-5 bytes on the wire. TODO: make a permanent buffer like
+ * writeVarint64?
*/
private void writeVarint32(int n) throws TException {
int idx = 0;
while (true) {
if ((n & ~0x7F) == 0) {
- temp[idx++] = (byte)n;
+ temp[idx++] = (byte) n;
// writeByteDirect((byte)n);
break;
// return;
} else {
- temp[idx++] = (byte)((n & 0x7F) | 0x80);
+ temp[idx++] = (byte) ((n & 0x7F) | 0x80);
// writeByteDirect((byte)((n & 0x7F) | 0x80));
n >>>= 7;
}
@@ -425,17 +392,15 @@ public class TCompactProtocol extends TProtocol {
trans_.write(temp, 0, idx);
}
- /**
- * Write an i64 as a varint. Results in 1-10 bytes on the wire.
- */
+ /** Write an i64 as a varint. Results in 1-10 bytes on the wire. */
private void writeVarint64(long n) throws TException {
int idx = 0;
while (true) {
if ((n & ~0x7FL) == 0) {
- temp[idx++] = (byte)n;
+ temp[idx++] = (byte) n;
break;
} else {
- temp[idx++] = ((byte)((n & 0x7F) | 0x80));
+ temp[idx++] = ((byte) ((n & 0x7F) | 0x80));
n >>>= 7;
}
}
@@ -443,79 +408,75 @@ public class TCompactProtocol extends TProtocol {
}
/**
- * Convert l into a zigzag long. This allows negative numbers to be
- * represented compactly as a varint.
+ * Convert l into a zigzag long. This allows negative numbers to be represented compactly as a
+ * varint.
*/
private long longToZigzag(long l) {
return (l << 1) ^ (l >> 63);
}
/**
- * Convert n into a zigzag int. This allows negative numbers to be
- * represented compactly as a varint.
+ * Convert n into a zigzag int. This allows negative numbers to be represented compactly as a
+ * varint.
*/
private int intToZigZag(int n) {
return (n << 1) ^ (n >> 31);
}
- /**
- * Convert a long into little-endian bytes in buf starting at off and going
- * until off+7.
- */
+ /** Convert a long into little-endian bytes in buf starting at off and going until off+7. */
private void fixedLongToBytes(long n, byte[] buf, int off) {
- buf[off+0] = (byte)( n & 0xff);
- buf[off+1] = (byte)((n >> 8 ) & 0xff);
- buf[off+2] = (byte)((n >> 16) & 0xff);
- buf[off+3] = (byte)((n >> 24) & 0xff);
- buf[off+4] = (byte)((n >> 32) & 0xff);
- buf[off+5] = (byte)((n >> 40) & 0xff);
- buf[off+6] = (byte)((n >> 48) & 0xff);
- buf[off+7] = (byte)((n >> 56) & 0xff);
+ buf[off + 0] = (byte) (n & 0xff);
+ buf[off + 1] = (byte) ((n >> 8) & 0xff);
+ buf[off + 2] = (byte) ((n >> 16) & 0xff);
+ buf[off + 3] = (byte) ((n >> 24) & 0xff);
+ buf[off + 4] = (byte) ((n >> 32) & 0xff);
+ buf[off + 5] = (byte) ((n >> 40) & 0xff);
+ buf[off + 6] = (byte) ((n >> 48) & 0xff);
+ buf[off + 7] = (byte) ((n >> 56) & 0xff);
}
/**
- * Writes a byte without any possibility of all that field header nonsense.
- * Used internally by other writing methods that know they need to write a byte.
+ * Writes a byte without any possibility of all that field header nonsense. Used internally by
+ * other writing methods that know they need to write a byte.
*/
private void writeByteDirect(byte b) throws TException {
temp[0] = b;
trans_.write(temp, 0, 1);
}
- /**
- * Writes a byte without any possibility of all that field header nonsense.
- */
+ /** Writes a byte without any possibility of all that field header nonsense. */
private void writeByteDirect(int n) throws TException {
- writeByteDirect((byte)n);
+ writeByteDirect((byte) n);
}
-
//
// Reading methods.
//
- /**
- * Read a message header.
- */
+ /** Read a message header. */
public TMessage readMessageBegin() throws TException {
byte protocolId = readByte();
if (protocolId != PROTOCOL_ID) {
- throw new TProtocolException("Expected protocol id " + Integer.toHexString(PROTOCOL_ID) + " but got " + Integer.toHexString(protocolId));
+ throw new TProtocolException(
+ "Expected protocol id "
+ + Integer.toHexString(PROTOCOL_ID)
+ + " but got "
+ + Integer.toHexString(protocolId));
}
byte versionAndType = readByte();
- byte version = (byte)(versionAndType & VERSION_MASK);
+ byte version = (byte) (versionAndType & VERSION_MASK);
if (version != VERSION) {
throw new TProtocolException("Expected version " + VERSION + " but got " + version);
}
- byte type = (byte)((versionAndType >> TYPE_SHIFT_AMOUNT) & TYPE_BITS);
+ byte type = (byte) ((versionAndType >> TYPE_SHIFT_AMOUNT) & TYPE_BITS);
int seqid = readVarint32();
String messageName = readString();
return new TMessage(messageName, type, seqid);
}
/**
- * 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.
+ * 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.
*/
public TStruct readStructBegin() throws TException {
lastField_.push(lastFieldId_);
@@ -524,17 +485,15 @@ public class TCompactProtocol extends TProtocol {
}
/**
- * Doesn't actually consume any wire data, just removes the last field for
- * this struct from the field stack.
+ * Doesn't actually consume any wire data, just removes the last field for this struct from the
+ * field stack.
*/
public void readStructEnd() throws TException {
// consume the last field we read off the wire.
lastFieldId_ = lastField_.pop();
}
- /**
- * Read a field header off the wire.
- */
+ /** Read a field header off the wire. */
public TField readFieldBegin() throws TException {
byte type = readByte();
@@ -546,21 +505,21 @@ public class TCompactProtocol extends TProtocol {
short fieldId;
// mask off the 4 MSB of the type header. it could contain a field id delta.
- short modifier = (short)((type & 0xf0) >> 4);
+ short modifier = (short) ((type & 0xf0) >> 4);
if (modifier == 0) {
// not a delta. look ahead for the zigzag varint field id.
fieldId = readI16();
} else {
// has a delta. add the delta to the last read field id.
- fieldId = (short)(lastFieldId_ + modifier);
+ fieldId = (short) (lastFieldId_ + modifier);
}
- TField field = new TField("", getTType((byte)(type & 0x0f)), fieldId);
+ TField field = new TField("", getTType((byte) (type & 0x0f)), fieldId);
// if this happens to be a boolean field, the value is encoded in the type
if (isBoolType(type)) {
// save the boolean value in a special instance variable.
- boolValue_ = (byte)(type & 0x0f) == Types.BOOLEAN_TRUE ? Boolean.TRUE : Boolean.FALSE;
+ boolValue_ = (byte) (type & 0x0f) == Types.BOOLEAN_TRUE ? Boolean.TRUE : Boolean.FALSE;
}
// push the new field onto the field stack so we can keep the deltas going.
@@ -569,24 +528,26 @@ public class TCompactProtocol extends TProtocol {
}
/**
- * 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.
+ * 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.
*/
public TMap readMapBegin() throws TException {
int size = readVarint32();
checkContainerReadLength(size);
byte keyAndValueType = size == 0 ? 0 : readByte();
- TMap map = new TMap(getTType((byte)(keyAndValueType >> 4)), getTType((byte)(keyAndValueType & 0xf)), size);
+ TMap map =
+ new TMap(
+ getTType((byte) (keyAndValueType >> 4)),
+ getTType((byte) (keyAndValueType & 0xf)),
+ size);
checkReadBytesAvailable(map);
return map;
}
/**
- * 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.
+ * 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.
*/
public TList readListBegin() throws TException {
byte size_and_type = readByte();
@@ -601,19 +562,17 @@ public class TCompactProtocol extends TProtocol {
}
/**
- * 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.
+ * 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.
*/
public TSet readSetBegin() throws TException {
return new TSet(readListBegin());
}
/**
- * 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.
+ * 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.
*/
public boolean readBool() throws TException {
if (boolValue_ != null) {
@@ -624,9 +583,7 @@ public class TCompactProtocol extends TProtocol {
return readByte() == Types.BOOLEAN_TRUE;
}
- /**
- * Read a single byte off the wire. Nothing interesting here.
- */
+ /** Read a single byte off the wire. Nothing interesting here. */
public byte readByte() throws TException {
byte b;
if (trans_.getBytesRemainingInBuffer() > 0) {
@@ -639,38 +596,28 @@ public class TCompactProtocol extends TProtocol {
return b;
}
- /**
- * Read an i16 from the wire as a zigzag varint.
- */
+ /** Read an i16 from the wire as a zigzag varint. */
public short readI16() throws TException {
- return (short)zigzagToInt(readVarint32());
+ return (short) zigzagToInt(readVarint32());
}
- /**
- * Read an i32 from the wire as a zigzag varint.
- */
+ /** Read an i32 from the wire as a zigzag varint. */
public int readI32() throws TException {
return zigzagToInt(readVarint32());
}
- /**
- * Read an i64 from the wire as a zigzag varint.
- */
+ /** Read an i64 from the wire as a zigzag varint. */
public long readI64() throws TException {
return zigzagToLong(readVarint64());
}
- /**
- * No magic here - just read a double off the wire.
- */
+ /** No magic here - just read a double off the wire. */
public double readDouble() throws TException {
trans_.readAll(temp, 0, 8);
return Double.longBitsToDouble(bytesToLong(temp));
}
- /**
- * Reads a byte[] (via readBinary), and then UTF-8 decodes it.
- */
+ /** Reads a byte[] (via readBinary), and then UTF-8 decodes it. */
public String readString() throws TException {
int length = readVarint32();
checkStringReadLength(length);
@@ -681,8 +628,9 @@ public class TCompactProtocol extends TProtocol {
final String str;
if (trans_.getBytesRemainingInBuffer() >= length) {
- str = new String(trans_.getBuffer(), trans_.getBufferPosition(),
- length, StandardCharsets.UTF_8);
+ str =
+ new String(
+ trans_.getBuffer(), trans_.getBufferPosition(), length, StandardCharsets.UTF_8);
trans_.consumeBuffer(length);
} else {
str = new String(readBinary(length), StandardCharsets.UTF_8);
@@ -690,9 +638,7 @@ public class TCompactProtocol extends TProtocol {
return str;
}
- /**
- * Read a ByteBuffer from the wire.
- */
+ /** Read a ByteBuffer from the wire. */
public ByteBuffer readBinary() throws TException {
int length = readVarint32();
if (length == 0) {
@@ -710,9 +656,7 @@ public class TCompactProtocol extends TProtocol {
return ByteBuffer.wrap(buf);
}
- /**
- * Read a byte[] of a known length from the wire.
- */
+ /** Read a byte[] of a known length from the wire. */
private byte[] readBinary(int length) throws TException {
if (length == 0) return EMPTY_BYTES;
@@ -723,26 +667,24 @@ public class TCompactProtocol extends TProtocol {
private void checkStringReadLength(int length) throws TException {
if (length < 0) {
- throw new TProtocolException(TProtocolException.NEGATIVE_SIZE,
- "Negative length: " + length);
+ throw new TProtocolException(TProtocolException.NEGATIVE_SIZE, "Negative length: " + length);
}
getTransport().checkReadBytesAvailable(length);
if (stringLengthLimit_ != NO_LENGTH_LIMIT && length > stringLengthLimit_) {
- throw new TProtocolException(TProtocolException.SIZE_LIMIT,
- "Length exceeded max allowed: " + length);
+ throw new TProtocolException(
+ TProtocolException.SIZE_LIMIT, "Length exceeded max allowed: " + length);
}
}
private void checkContainerReadLength(int length) throws TProtocolException {
if (length < 0) {
- throw new TProtocolException(TProtocolException.NEGATIVE_SIZE,
- "Negative length: " + length);
+ throw new TProtocolException(TProtocolException.NEGATIVE_SIZE, "Negative length: " + length);
}
if (containerLengthLimit_ != NO_LENGTH_LIMIT && length > containerLengthLimit_) {
- throw new TProtocolException(TProtocolException.SIZE_LIMIT,
- "Length exceeded max allowed: " + length);
+ throw new TProtocolException(
+ TProtocolException.SIZE_LIMIT, "Length exceeded max allowed: " + length);
}
}
@@ -752,12 +694,16 @@ public class TCompactProtocol extends TProtocol {
//
@Override
public void readMessageEnd() throws TException {}
+
@Override
public void readFieldEnd() throws TException {}
+
@Override
public void readMapEnd() throws TException {}
+
@Override
public void readListEnd() throws TException {}
+
@Override
public void readSetEnd() throws TException {}
@@ -766,8 +712,8 @@ public class TCompactProtocol extends TProtocol {
//
/**
- * Read an i32 from the wire as a varint. The MSB of each byte is set
- * if there is another byte to follow. This can read up to 5 bytes.
+ * Read an i32 from the wire as a varint. The MSB of each byte is set if there is another byte to
+ * follow. This can read up to 5 bytes.
*/
private int readVarint32() throws TException {
int result = 0;
@@ -777,13 +723,13 @@ public class TCompactProtocol extends TProtocol {
int pos = trans_.getBufferPosition();
int off = 0;
while (true) {
- byte b = buf[pos+off];
+ byte b = buf[pos + off];
result |= (b & 0x7f) << shift;
if ((b & 0x80) != 0x80) break;
shift += 7;
off++;
}
- trans_.consumeBuffer(off+1);
+ trans_.consumeBuffer(off + 1);
} else {
while (true) {
byte b = readByte();
@@ -796,8 +742,8 @@ public class TCompactProtocol extends TProtocol {
}
/**
- * Read an i64 from the wire as a proper varint. The MSB of each byte is set
- * if there is another byte to follow. This can read up to 10 bytes.
+ * Read an i64 from the wire as a proper varint. The MSB of each byte is set if there is another
+ * byte to follow. This can read up to 10 bytes.
*/
private long readVarint64() throws TException {
int shift = 0;
@@ -807,19 +753,19 @@ public class TCompactProtocol extends TProtocol {
int pos = trans_.getBufferPosition();
int off = 0;
while (true) {
- byte b = buf[pos+off];
+ byte b = buf[pos + off];
result |= (long) (b & 0x7f) << shift;
if ((b & 0x80) != 0x80) break;
shift += 7;
off++;
}
- trans_.consumeBuffer(off+1);
+ trans_.consumeBuffer(off + 1);
} else {
while (true) {
byte b = readByte();
result |= (long) (b & 0x7f) << shift;
if ((b & 0x80) != 0x80) break;
- shift +=7;
+ shift += 7;
}
}
return result;
@@ -829,35 +775,29 @@ public class TCompactProtocol extends TProtocol {
// encoding helpers
//
- /**
- * Convert from zigzag int to int.
- */
+ /** Convert from zigzag int to int. */
private int zigzagToInt(int n) {
return (n >>> 1) ^ -(n & 1);
}
- /**
- * Convert from zigzag long to long.
- */
+ /** Convert from zigzag long to long. */
private long zigzagToLong(long n) {
return (n >>> 1) ^ -(n & 1);
}
/**
- * Note that it's important that the mask bytes are long literals,
- * otherwise they'll default to ints, and when you shift an int left 56 bits,
- * you just get a messed up int.
+ * Note that it's important that the mask bytes are long literals, otherwise they'll default to
+ * ints, and when you shift an int left 56 bits, you just get a messed up int.
*/
private long bytesToLong(byte[] bytes) {
- return
- ((bytes[7] & 0xffL) << 56) |
- ((bytes[6] & 0xffL) << 48) |
- ((bytes[5] & 0xffL) << 40) |
- ((bytes[4] & 0xffL) << 32) |
- ((bytes[3] & 0xffL) << 24) |
- ((bytes[2] & 0xffL) << 16) |
- ((bytes[1] & 0xffL) << 8) |
- ((bytes[0] & 0xffL));
+ return ((bytes[7] & 0xffL) << 56)
+ | ((bytes[6] & 0xffL) << 48)
+ | ((bytes[5] & 0xffL) << 40)
+ | ((bytes[4] & 0xffL) << 32)
+ | ((bytes[3] & 0xffL) << 24)
+ | ((bytes[2] & 0xffL) << 16)
+ | ((bytes[1] & 0xffL) << 8)
+ | ((bytes[0] & 0xffL));
}
//
@@ -869,12 +809,9 @@ public class TCompactProtocol extends TProtocol {
return lowerNibble == Types.BOOLEAN_TRUE || lowerNibble == Types.BOOLEAN_FALSE;
}
- /**
- * Given a TCompactProtocol.Types constant, convert it to its corresponding
- * TType value.
- */
+ /** Given a TCompactProtocol.Types constant, convert it to its corresponding TType value. */
private byte getTType(byte type) throws TProtocolException {
- switch ((byte)(type & 0x0f)) {
+ switch ((byte) (type & 0x0f)) {
case TType.STOP:
return TType.STOP;
case Types.BOOLEAN_FALSE:
@@ -901,51 +838,47 @@ public class TCompactProtocol extends TProtocol {
case Types.STRUCT:
return TType.STRUCT;
default:
- throw new TProtocolException("don't know what type: " + (byte)(type & 0x0f));
+ throw new TProtocolException("don't know what type: " + (byte) (type & 0x0f));
}
}
- /**
- * Given a TType value, find the appropriate TCompactProtocol.Types constant.
- */
+ /** Given a TType value, find the appropriate TCompactProtocol.Types constant. */
private byte getCompactType(byte ttype) {
return ttypeToCompactType[ttype];
}
- /**
- * Return the minimum number of bytes a type will consume on the wire
- */
+ /** Return the minimum number of bytes a type will consume on the wire */
public int getMinSerializedSize(byte type) throws TTransportException {
- switch (type) {
- case 0:
- return 0; // Stop
- case 1:
- return 0; // Void
- case 2:
- return 1; // Bool sizeof(byte)
- case 3:
- return 1; // Byte sizeof(byte)
- case 4:
- return 8; // Double sizeof(double)
- case 6:
- return 1; // I16 sizeof(byte)
- case 8:
- return 1; // I32 sizeof(byte)
- case 10:
- return 1;// I64 sizeof(byte)
- case 11:
- return 1; // string length sizeof(byte)
- case 12:
- return 0; // empty struct
- case 13:
- return 1; // element count Map sizeof(byte)
- case 14:
- return 1; // element count Set sizeof(byte)
- case 15:
- return 1; // element count List sizeof(byte)
- default:
- throw new TTransportException(TTransportException.UNKNOWN, "unrecognized type code");
- }
+ switch (type) {
+ case 0:
+ return 0; // Stop
+ case 1:
+ return 0; // Void
+ case 2:
+ return 1; // Bool sizeof(byte)
+ case 3:
+ return 1; // Byte sizeof(byte)
+ case 4:
+ return 8; // Double sizeof(double)
+ case 6:
+ return 1; // I16 sizeof(byte)
+ case 8:
+ return 1; // I32 sizeof(byte)
+ case 10:
+ return 1; // I64 sizeof(byte)
+ case 11:
+ return 1; // string length sizeof(byte)
+ case 12:
+ return 0; // empty struct
+ case 13:
+ return 1; // element count Map sizeof(byte)
+ case 14:
+ return 1; // element count Set sizeof(byte)
+ case 15:
+ return 1; // element count List sizeof(byte)
+ default:
+ throw new TTransportException(TTransportException.UNKNOWN, "unrecognized type code");
+ }
}
// -----------------------------------------------------------------
// Additional methods to improve performance.