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