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 | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame] | 98 | public void writeString(TTransport out, String str) throws TException { |
| 99 | byte[] dat = str.getBytes(); |
| 100 | writeI32(out, dat.length); |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 101 | out.write(dat, 0, dat.length); |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Reading methods. |
| 106 | */ |
| 107 | |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 108 | public TMessage readMessageBegin(TTransport in) throws TException { |
| 109 | TMessage message = new TMessage(); |
| 110 | message.name = readString(in); |
| 111 | message.type = readByte(in); |
| 112 | message.seqid = readI32(in); |
| 113 | return message; |
| 114 | } |
| 115 | |
| 116 | public void readMessageEnd(TTransport in) throws TException {} |
| 117 | |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame] | 118 | public TStruct readStructBegin(TTransport in) throws TException { |
| 119 | return new TStruct(); |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame] | 122 | public void readStructEnd(TTransport in) throws TException {} |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 123 | |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame] | 124 | public TField readFieldBegin(TTransport in) throws TException { |
| 125 | TField field = new TField(); |
| 126 | field.type = readByte(in); |
| 127 | if (field.type != TType.STOP) { |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 128 | field.id = readI16(in); |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 129 | } |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame] | 130 | return field; |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame] | 133 | public void readFieldEnd(TTransport in) throws TException {} |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 134 | |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame] | 135 | public TMap readMapBegin(TTransport in) throws TException { |
| 136 | TMap map = new TMap(); |
| 137 | map.keyType = readByte(in); |
| 138 | map.valueType = readByte(in); |
| 139 | map.size = readI32(in); |
| 140 | return map; |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame] | 143 | public void readMapEnd(TTransport in) throws TException {} |
| 144 | |
| 145 | public TList readListBegin(TTransport in) throws TException { |
| 146 | TList list = new TList(); |
| 147 | list.elemType = readByte(in); |
| 148 | list.size = readI32(in); |
| 149 | return list; |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame] | 152 | public void readListEnd(TTransport in) throws TException {} |
| 153 | |
| 154 | public TSet readSetBegin(TTransport in) throws TException { |
| 155 | TSet set = new TSet(); |
| 156 | set.elemType = readByte(in); |
| 157 | set.size = readI32(in); |
| 158 | return set; |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 159 | } |
| 160 | |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame] | 161 | public void readSetEnd(TTransport in) throws TException {} |
| 162 | |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 163 | public boolean readBool(TTransport in) throws TException { |
| 164 | return (readByte(in) == 1); |
| 165 | } |
| 166 | |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame] | 167 | byte[] bin = new byte[1]; |
| 168 | public byte readByte(TTransport in) throws TException { |
| 169 | in.readAll(bin, 0, 1); |
| 170 | return bin[0]; |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 173 | byte[] i16rd = new byte[2]; |
| 174 | public short readI16(TTransport in) throws TException { |
| 175 | in.readAll(i16rd, 0, 2); |
| 176 | return |
| 177 | (short) |
| 178 | (((i16rd[0] & 0xff) << 8) | |
| 179 | ((i16rd[1] & 0xff))); |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame] | 182 | byte[] i32rd = new byte[4]; |
| 183 | public int readI32(TTransport in) throws TException { |
| 184 | in.readAll(i32rd, 0, 4); |
| 185 | return |
| 186 | ((i32rd[0] & 0xff) << 24) | |
| 187 | ((i32rd[1] & 0xff) << 16) | |
| 188 | ((i32rd[2] & 0xff) << 8) | |
| 189 | ((i32rd[3] & 0xff)); |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 190 | } |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 191 | |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame] | 192 | byte[] i64rd = new byte[8]; |
| 193 | public long readI64(TTransport in) throws TException { |
| 194 | in.readAll(i64rd, 0, 8); |
| 195 | return |
| 196 | ((long)(i64rd[0] & 0xff) << 56) | |
| 197 | ((long)(i64rd[1] & 0xff) << 48) | |
| 198 | ((long)(i64rd[2] & 0xff) << 40) | |
| 199 | ((long)(i64rd[3] & 0xff) << 32) | |
| 200 | ((long)(i64rd[4] & 0xff) << 24) | |
| 201 | ((long)(i64rd[5] & 0xff) << 16) | |
| 202 | ((long)(i64rd[6] & 0xff) << 8) | |
| 203 | ((long)(i64rd[7] & 0xff)); |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame] | 206 | public String readString(TTransport in) throws TException { |
| 207 | int size = readI32(in); |
| 208 | byte[] buf = new byte[size]; |
| 209 | in.readAll(buf, 0, size); |
| 210 | return new String(buf); |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 211 | } |
| 212 | } |