Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 1 | package com.facebook.thrift.protocol; |
| 2 | |
| 3 | import com.facebook.thrift.TException; |
| 4 | import com.facebook.thrift.transport.TTransport; |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 5 | |
| 6 | /** |
| 7 | * Binary protocol implementation for thrift. |
| 8 | * |
| 9 | * @author Mark Slee <mcslee@facebook.com> |
| 10 | */ |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 11 | public class TBinaryProtocol extends TProtocol { |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 12 | |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 13 | /** |
| 14 | * Factory |
| 15 | */ |
| 16 | public static class Factory implements TProtocolFactory { |
Aditya Agarwal | 5a42958 | 2007-02-06 02:51:15 +0000 | [diff] [blame^] | 17 | public TProtocol getProtocol(TTransport trans) { |
| 18 | return new TBinaryProtocol(trans); |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 19 | } |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 20 | } |
| 21 | |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 22 | /** |
| 23 | * Constructor |
| 24 | */ |
Aditya Agarwal | 5a42958 | 2007-02-06 02:51:15 +0000 | [diff] [blame^] | 25 | public TBinaryProtocol(TTransport trans) { |
| 26 | super(trans); |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 27 | } |
| 28 | |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 29 | public void writeMessageBegin(TMessage message) throws TException { |
| 30 | writeString(message.name); |
| 31 | writeByte(message.type); |
| 32 | writeI32(message.seqid); |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 33 | } |
| 34 | |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 35 | public void writeMessageEnd() {} |
| 36 | |
| 37 | public void writeStructBegin(TStruct struct) {} |
| 38 | |
| 39 | public void writeStructEnd() {} |
| 40 | |
| 41 | public void writeFieldBegin(TField field) throws TException { |
| 42 | writeByte(field.type); |
| 43 | writeI16(field.id); |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 46 | public void writeFieldEnd() {} |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame] | 47 | |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 48 | public void writeFieldStop() throws TException { |
| 49 | writeByte(TType.STOP); |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 52 | public void writeMapBegin(TMap map) throws TException { |
| 53 | writeByte(map.keyType); |
| 54 | writeByte(map.valueType); |
| 55 | writeI32(map.size); |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 58 | public void writeMapEnd() {} |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame] | 59 | |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 60 | public void writeListBegin(TList list) throws TException { |
| 61 | writeByte(list.elemType); |
| 62 | writeI32(list.size); |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 65 | public void writeListEnd() {} |
| 66 | |
| 67 | public void writeSetBegin(TSet set) throws TException { |
| 68 | writeByte(set.elemType); |
| 69 | writeI32(set.size); |
| 70 | } |
| 71 | |
| 72 | public void writeSetEnd() {} |
| 73 | |
| 74 | public void writeBool(boolean b) throws TException { |
| 75 | writeByte(b ? (byte)1 : (byte)0); |
| 76 | } |
| 77 | |
| 78 | private byte [] bout = new byte[1]; |
| 79 | public void writeByte(byte b) throws TException { |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame] | 80 | bout[0] = b; |
Aditya Agarwal | 5a42958 | 2007-02-06 02:51:15 +0000 | [diff] [blame^] | 81 | trans_.write(bout, 0, 1); |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 84 | private byte[] i16out = new byte[2]; |
| 85 | public void writeI16(short i16) throws TException { |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 86 | i16out[0] = (byte)(0xff & (i16 >> 8)); |
| 87 | i16out[1] = (byte)(0xff & (i16)); |
Aditya Agarwal | 5a42958 | 2007-02-06 02:51:15 +0000 | [diff] [blame^] | 88 | trans_.write(i16out, 0, 2); |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 91 | private byte[] i32out = new byte[4]; |
| 92 | public void writeI32(int i32) throws TException { |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame] | 93 | i32out[0] = (byte)(0xff & (i32 >> 24)); |
| 94 | i32out[1] = (byte)(0xff & (i32 >> 16)); |
| 95 | i32out[2] = (byte)(0xff & (i32 >> 8)); |
| 96 | i32out[3] = (byte)(0xff & (i32)); |
Aditya Agarwal | 5a42958 | 2007-02-06 02:51:15 +0000 | [diff] [blame^] | 97 | trans_.write(i32out, 0, 4); |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 100 | private byte[] i64out = new byte[8]; |
| 101 | public void writeI64(long i64) throws TException { |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame] | 102 | i64out[0] = (byte)(0xff & (i64 >> 56)); |
| 103 | i64out[1] = (byte)(0xff & (i64 >> 48)); |
| 104 | i64out[2] = (byte)(0xff & (i64 >> 40)); |
| 105 | i64out[3] = (byte)(0xff & (i64 >> 32)); |
| 106 | i64out[4] = (byte)(0xff & (i64 >> 24)); |
| 107 | i64out[5] = (byte)(0xff & (i64 >> 16)); |
| 108 | i64out[6] = (byte)(0xff & (i64 >> 8)); |
| 109 | i64out[7] = (byte)(0xff & (i64)); |
Aditya Agarwal | 5a42958 | 2007-02-06 02:51:15 +0000 | [diff] [blame^] | 110 | trans_.write(i64out, 0, 8); |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 113 | public void writeDouble(double dub) throws TException { |
| 114 | writeI64(Double.doubleToLongBits(dub)); |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 115 | } |
| 116 | |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 117 | public void writeString(String str) throws TException { |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame] | 118 | byte[] dat = str.getBytes(); |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 119 | writeI32(dat.length); |
Aditya Agarwal | 5a42958 | 2007-02-06 02:51:15 +0000 | [diff] [blame^] | 120 | trans_.write(dat, 0, dat.length); |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Reading methods. |
| 125 | */ |
| 126 | |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 127 | public TMessage readMessageBegin() throws TException { |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 128 | TMessage message = new TMessage(); |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 129 | message.name = readString(); |
| 130 | message.type = readByte(); |
| 131 | message.seqid = readI32(); |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 132 | return message; |
| 133 | } |
| 134 | |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 135 | public void readMessageEnd() {} |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 136 | |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 137 | public TStruct readStructBegin() { |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame] | 138 | return new TStruct(); |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 139 | } |
| 140 | |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 141 | public void readStructEnd() {} |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 142 | |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 143 | public TField readFieldBegin() throws TException { |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame] | 144 | TField field = new TField(); |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 145 | field.type = readByte(); |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame] | 146 | if (field.type != TType.STOP) { |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 147 | field.id = readI16(); |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 148 | } |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame] | 149 | return field; |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 152 | public void readFieldEnd() {} |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 153 | |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 154 | public TMap readMapBegin() throws TException { |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame] | 155 | TMap map = new TMap(); |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 156 | map.keyType = readByte(); |
| 157 | map.valueType = readByte(); |
| 158 | map.size = readI32(); |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame] | 159 | return map; |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 160 | } |
| 161 | |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 162 | public void readMapEnd() {} |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame] | 163 | |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 164 | public TList readListBegin() throws TException { |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame] | 165 | TList list = new TList(); |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 166 | list.elemType = readByte(); |
| 167 | list.size = readI32(); |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame] | 168 | return list; |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 171 | public void readListEnd() {} |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame] | 172 | |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 173 | public TSet readSetBegin() throws TException { |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame] | 174 | TSet set = new TSet(); |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 175 | set.elemType = readByte(); |
| 176 | set.size = readI32(); |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame] | 177 | return set; |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 178 | } |
| 179 | |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 180 | public void readSetEnd() {} |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame] | 181 | |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 182 | public boolean readBool() throws TException { |
| 183 | return (readByte() == 1); |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 186 | private byte[] bin = new byte[1]; |
| 187 | public byte readByte() throws TException { |
Aditya Agarwal | 5a42958 | 2007-02-06 02:51:15 +0000 | [diff] [blame^] | 188 | trans_.readAll(bin, 0, 1); |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame] | 189 | return bin[0]; |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 190 | } |
| 191 | |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 192 | private byte[] i16rd = new byte[2]; |
| 193 | public short readI16() throws TException { |
Aditya Agarwal | 5a42958 | 2007-02-06 02:51:15 +0000 | [diff] [blame^] | 194 | trans_.readAll(i16rd, 0, 2); |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 195 | return |
| 196 | (short) |
| 197 | (((i16rd[0] & 0xff) << 8) | |
| 198 | ((i16rd[1] & 0xff))); |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 199 | } |
| 200 | |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 201 | private byte[] i32rd = new byte[4]; |
| 202 | public int readI32() throws TException { |
Aditya Agarwal | 5a42958 | 2007-02-06 02:51:15 +0000 | [diff] [blame^] | 203 | trans_.readAll(i32rd, 0, 4); |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame] | 204 | return |
| 205 | ((i32rd[0] & 0xff) << 24) | |
| 206 | ((i32rd[1] & 0xff) << 16) | |
| 207 | ((i32rd[2] & 0xff) << 8) | |
| 208 | ((i32rd[3] & 0xff)); |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 209 | } |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 210 | |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 211 | private byte[] i64rd = new byte[8]; |
| 212 | public long readI64() throws TException { |
Aditya Agarwal | 5a42958 | 2007-02-06 02:51:15 +0000 | [diff] [blame^] | 213 | trans_.readAll(i64rd, 0, 8); |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame] | 214 | return |
| 215 | ((long)(i64rd[0] & 0xff) << 56) | |
| 216 | ((long)(i64rd[1] & 0xff) << 48) | |
| 217 | ((long)(i64rd[2] & 0xff) << 40) | |
| 218 | ((long)(i64rd[3] & 0xff) << 32) | |
| 219 | ((long)(i64rd[4] & 0xff) << 24) | |
| 220 | ((long)(i64rd[5] & 0xff) << 16) | |
| 221 | ((long)(i64rd[6] & 0xff) << 8) | |
| 222 | ((long)(i64rd[7] & 0xff)); |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 223 | } |
| 224 | |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 225 | public double readDouble() throws TException { |
| 226 | return Double.longBitsToDouble(readI64()); |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 227 | } |
| 228 | |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 229 | public String readString() throws TException { |
| 230 | int size = readI32(); |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame] | 231 | byte[] buf = new byte[size]; |
Aditya Agarwal | 5a42958 | 2007-02-06 02:51:15 +0000 | [diff] [blame^] | 232 | trans_.readAll(buf, 0, size); |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame] | 233 | return new String(buf); |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 234 | } |
| 235 | } |