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 | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame^] | 12 | public void writeStructBegin(TTransport out, TStruct struct) throws TException {} |
| 13 | |
| 14 | public void writeStructEnd(TTransport out) throws TException {} |
| 15 | |
| 16 | public void writeFieldBegin(TTransport out, TField field) throws TException { |
| 17 | writeByte(out, field.type); |
| 18 | writeI32(out, field.id); |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 19 | } |
| 20 | |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame^] | 21 | public void writeFieldEnd(TTransport out) throws TException {} |
| 22 | |
| 23 | public void writeFieldStop(TTransport out) throws TException { |
| 24 | writeByte(out, TType.STOP); |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 25 | } |
| 26 | |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame^] | 27 | public void writeMapBegin(TTransport out, TMap map) throws TException { |
| 28 | writeByte(out, map.keyType); |
| 29 | writeByte(out, map.valueType); |
| 30 | writeI32(out, map.size); |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 31 | } |
| 32 | |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame^] | 33 | public void writeMapEnd(TTransport out) throws TException {} |
| 34 | |
| 35 | public void writeListBegin(TTransport out, TList list) throws TException { |
| 36 | writeByte(out, list.elemType); |
| 37 | writeI32(out, list.size); |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 38 | } |
| 39 | |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame^] | 40 | public void writeListEnd(TTransport out) throws TException {} |
| 41 | |
| 42 | public void writeSetBegin(TTransport out, TSet set) throws TException { |
| 43 | writeByte(out, set.elemType); |
| 44 | writeI32(out, set.size); |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 45 | } |
| 46 | |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame^] | 47 | public void writeSetEnd(TTransport out) throws TException {} |
| 48 | |
| 49 | byte[] bout = new byte[1]; |
| 50 | public void writeByte(TTransport out, byte b) throws TException { |
| 51 | bout[0] = b; |
| 52 | out.write(bout, 0, 1); |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame^] | 55 | public void writeU32(TTransport out, int u32) throws TException { |
| 56 | writeI32(out, u32); |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame^] | 59 | byte[] i32out = new byte[4]; |
| 60 | public void writeI32(TTransport out, int i32) throws TException { |
| 61 | i32out[0] = (byte)(0xff & (i32 >> 24)); |
| 62 | i32out[1] = (byte)(0xff & (i32 >> 16)); |
| 63 | i32out[2] = (byte)(0xff & (i32 >> 8)); |
| 64 | i32out[3] = (byte)(0xff & (i32)); |
| 65 | out.write(i32out, 0, 4); |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame^] | 68 | public void writeU64(TTransport out, long u64) throws TException { |
| 69 | writeI64(out, u64); |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame^] | 72 | byte[] i64out = new byte[8]; |
| 73 | public void writeI64(TTransport out, long i64) throws TException { |
| 74 | i64out[0] = (byte)(0xff & (i64 >> 56)); |
| 75 | i64out[1] = (byte)(0xff & (i64 >> 48)); |
| 76 | i64out[2] = (byte)(0xff & (i64 >> 40)); |
| 77 | i64out[3] = (byte)(0xff & (i64 >> 32)); |
| 78 | i64out[4] = (byte)(0xff & (i64 >> 24)); |
| 79 | i64out[5] = (byte)(0xff & (i64 >> 16)); |
| 80 | i64out[6] = (byte)(0xff & (i64 >> 8)); |
| 81 | i64out[7] = (byte)(0xff & (i64)); |
| 82 | out.write(i64out, 0, 8); |
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 | public void writeString(TTransport out, String str) throws TException { |
| 86 | byte[] dat = str.getBytes(); |
| 87 | writeI32(out, dat.length); |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 88 | out.write(dat, 0, dat.length); |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Reading methods. |
| 93 | */ |
| 94 | |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame^] | 95 | public TStruct readStructBegin(TTransport in) throws TException { |
| 96 | return new TStruct(); |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame^] | 99 | public void readStructEnd(TTransport in) throws TException {} |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 100 | |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame^] | 101 | public TField readFieldBegin(TTransport in) throws TException { |
| 102 | TField field = new TField(); |
| 103 | field.type = readByte(in); |
| 104 | if (field.type != TType.STOP) { |
| 105 | field.id = readI32(in); |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 106 | } |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame^] | 107 | return field; |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 108 | } |
| 109 | |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame^] | 110 | public void readFieldEnd(TTransport in) throws TException {} |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 111 | |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame^] | 112 | public TMap readMapBegin(TTransport in) throws TException { |
| 113 | TMap map = new TMap(); |
| 114 | map.keyType = readByte(in); |
| 115 | map.valueType = readByte(in); |
| 116 | map.size = readI32(in); |
| 117 | return map; |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame^] | 120 | public void readMapEnd(TTransport in) throws TException {} |
| 121 | |
| 122 | public TList readListBegin(TTransport in) throws TException { |
| 123 | TList list = new TList(); |
| 124 | list.elemType = readByte(in); |
| 125 | list.size = readI32(in); |
| 126 | return list; |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame^] | 129 | public void readListEnd(TTransport in) throws TException {} |
| 130 | |
| 131 | public TSet readSetBegin(TTransport in) throws TException { |
| 132 | TSet set = new TSet(); |
| 133 | set.elemType = readByte(in); |
| 134 | set.size = readI32(in); |
| 135 | return set; |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame^] | 138 | public void readSetEnd(TTransport in) throws TException {} |
| 139 | |
| 140 | byte[] bin = new byte[1]; |
| 141 | public byte readByte(TTransport in) throws TException { |
| 142 | in.readAll(bin, 0, 1); |
| 143 | return bin[0]; |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame^] | 146 | public int readU32(TTransport in) throws TException { |
| 147 | return readI32(in); |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 148 | } |
| 149 | |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame^] | 150 | byte[] i32rd = new byte[4]; |
| 151 | public int readI32(TTransport in) throws TException { |
| 152 | in.readAll(i32rd, 0, 4); |
| 153 | return |
| 154 | ((i32rd[0] & 0xff) << 24) | |
| 155 | ((i32rd[1] & 0xff) << 16) | |
| 156 | ((i32rd[2] & 0xff) << 8) | |
| 157 | ((i32rd[3] & 0xff)); |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 158 | } |
| 159 | |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame^] | 160 | public long readU64(TTransport in) throws TException { |
| 161 | return readI64(in); |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame^] | 164 | byte[] i64rd = new byte[8]; |
| 165 | public long readI64(TTransport in) throws TException { |
| 166 | in.readAll(i64rd, 0, 8); |
| 167 | return |
| 168 | ((long)(i64rd[0] & 0xff) << 56) | |
| 169 | ((long)(i64rd[1] & 0xff) << 48) | |
| 170 | ((long)(i64rd[2] & 0xff) << 40) | |
| 171 | ((long)(i64rd[3] & 0xff) << 32) | |
| 172 | ((long)(i64rd[4] & 0xff) << 24) | |
| 173 | ((long)(i64rd[5] & 0xff) << 16) | |
| 174 | ((long)(i64rd[6] & 0xff) << 8) | |
| 175 | ((long)(i64rd[7] & 0xff)); |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 176 | } |
| 177 | |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame^] | 178 | public String readString(TTransport in) throws TException { |
| 179 | int size = readI32(in); |
| 180 | byte[] buf = new byte[size]; |
| 181 | in.readAll(buf, 0, size); |
| 182 | return new String(buf); |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 183 | } |
| 184 | } |