blob: 4d6c34595529fbd9255c9681f0f07434fd4d6744 [file] [log] [blame]
Mark Slee83c52a82006-06-07 06:51:18 +00001package com.facebook.thrift.protocol;
2
3import com.facebook.thrift.TException;
4import com.facebook.thrift.transport.TTransport;
Mark Slee83c52a82006-06-07 06:51:18 +00005
6/**
7 * Binary protocol implementation for thrift.
8 *
9 * @author Mark Slee <mcslee@facebook.com>
10 */
11public class TBinaryProtocol implements TProtocol {
Mark Slee78f58e22006-09-02 04:17:07 +000012
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 Slee530fd662006-08-09 00:05:18 +000022 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 Slee78f58e22006-09-02 04:17:07 +000028 writeI16(out, field.id);
Mark Slee83c52a82006-06-07 06:51:18 +000029 }
30
Mark Slee530fd662006-08-09 00:05:18 +000031 public void writeFieldEnd(TTransport out) throws TException {}
32
33 public void writeFieldStop(TTransport out) throws TException {
34 writeByte(out, TType.STOP);
Mark Slee83c52a82006-06-07 06:51:18 +000035 }
36
Mark Slee530fd662006-08-09 00:05:18 +000037 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 Slee83c52a82006-06-07 06:51:18 +000041 }
42
Mark Slee530fd662006-08-09 00:05:18 +000043 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 Slee83c52a82006-06-07 06:51:18 +000048 }
49
Mark Slee530fd662006-08-09 00:05:18 +000050 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 Slee83c52a82006-06-07 06:51:18 +000055 }
56
Mark Slee530fd662006-08-09 00:05:18 +000057 public void writeSetEnd(TTransport out) throws TException {}
58
Mark Slee78f58e22006-09-02 04:17:07 +000059 public void writeBool(TTransport out, boolean b) throws TException {
60 writeByte(out, b ? (byte)1 : (byte)0);
61 }
62
Mark Slee530fd662006-08-09 00:05:18 +000063 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 Slee83c52a82006-06-07 06:51:18 +000067 }
68
Mark Slee78f58e22006-09-02 04:17:07 +000069 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 Slee83c52a82006-06-07 06:51:18 +000074 }
75
Mark Slee530fd662006-08-09 00:05:18 +000076 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 Slee83c52a82006-06-07 06:51:18 +000083 }
84
Mark Slee530fd662006-08-09 00:05:18 +000085 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 Slee83c52a82006-06-07 06:51:18 +000096 }
97
Mark Sleec98d0502006-09-06 02:42:25 +000098 public void writeDouble(TTransport out, double dub) throws TException {
99 writeI64(out, Double.doubleToLongBits(dub));
100 }
101
Mark Slee530fd662006-08-09 00:05:18 +0000102 public void writeString(TTransport out, String str) throws TException {
103 byte[] dat = str.getBytes();
104 writeI32(out, dat.length);
Mark Slee83c52a82006-06-07 06:51:18 +0000105 out.write(dat, 0, dat.length);
Mark Slee83c52a82006-06-07 06:51:18 +0000106 }
107
108 /**
109 * Reading methods.
110 */
111
Mark Slee78f58e22006-09-02 04:17:07 +0000112 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 Slee530fd662006-08-09 00:05:18 +0000122 public TStruct readStructBegin(TTransport in) throws TException {
123 return new TStruct();
Mark Slee83c52a82006-06-07 06:51:18 +0000124 }
125
Mark Slee530fd662006-08-09 00:05:18 +0000126 public void readStructEnd(TTransport in) throws TException {}
Mark Slee83c52a82006-06-07 06:51:18 +0000127
Mark Slee530fd662006-08-09 00:05:18 +0000128 public TField readFieldBegin(TTransport in) throws TException {
129 TField field = new TField();
130 field.type = readByte(in);
131 if (field.type != TType.STOP) {
Mark Slee78f58e22006-09-02 04:17:07 +0000132 field.id = readI16(in);
Mark Slee83c52a82006-06-07 06:51:18 +0000133 }
Mark Slee530fd662006-08-09 00:05:18 +0000134 return field;
Mark Slee83c52a82006-06-07 06:51:18 +0000135 }
136
Mark Slee530fd662006-08-09 00:05:18 +0000137 public void readFieldEnd(TTransport in) throws TException {}
Mark Slee83c52a82006-06-07 06:51:18 +0000138
Mark Slee530fd662006-08-09 00:05:18 +0000139 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 Slee83c52a82006-06-07 06:51:18 +0000145 }
146
Mark Slee530fd662006-08-09 00:05:18 +0000147 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 Slee83c52a82006-06-07 06:51:18 +0000154 }
155
Mark Slee530fd662006-08-09 00:05:18 +0000156 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 Slee83c52a82006-06-07 06:51:18 +0000163 }
164
Mark Slee530fd662006-08-09 00:05:18 +0000165 public void readSetEnd(TTransport in) throws TException {}
166
Mark Slee78f58e22006-09-02 04:17:07 +0000167 public boolean readBool(TTransport in) throws TException {
168 return (readByte(in) == 1);
169 }
170
Mark Slee530fd662006-08-09 00:05:18 +0000171 byte[] bin = new byte[1];
172 public byte readByte(TTransport in) throws TException {
173 in.readAll(bin, 0, 1);
174 return bin[0];
Mark Slee83c52a82006-06-07 06:51:18 +0000175 }
176
Mark Slee78f58e22006-09-02 04:17:07 +0000177 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 Slee83c52a82006-06-07 06:51:18 +0000184 }
185
Mark Slee530fd662006-08-09 00:05:18 +0000186 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 Slee83c52a82006-06-07 06:51:18 +0000194 }
Mark Slee78f58e22006-09-02 04:17:07 +0000195
Mark Slee530fd662006-08-09 00:05:18 +0000196 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 Slee83c52a82006-06-07 06:51:18 +0000208 }
209
Mark Sleec98d0502006-09-06 02:42:25 +0000210 public double readDouble(TTransport in) throws TException {
211 return Double.longBitsToDouble(readI64(in));
212 }
213
Mark Slee530fd662006-08-09 00:05:18 +0000214 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 Slee83c52a82006-06-07 06:51:18 +0000219 }
220}