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