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