blob: be02a3ed2537672def5127f1f34350920d71f3e9 [file] [log] [blame]
Mark Slee7eb0d632007-03-01 00:00:27 +00001// Copyright (c) 2006- Facebook
2// Distributed under the Thrift Software License
3//
4// See accompanying file LICENSE or visit the Thrift site at:
5// http://developers.facebook.com/thrift/
6
Mark Slee83c52a82006-06-07 06:51:18 +00007package com.facebook.thrift.protocol;
8
9import com.facebook.thrift.TException;
10import com.facebook.thrift.transport.TTransport;
Mark Slee83c52a82006-06-07 06:51:18 +000011
12/**
13 * Binary protocol implementation for thrift.
14 *
15 * @author Mark Slee <mcslee@facebook.com>
16 */
Mark Slee456b7a82006-10-25 20:53:37 +000017public class TBinaryProtocol extends TProtocol {
Mark Slee78f58e22006-09-02 04:17:07 +000018
Mark Slee456b7a82006-10-25 20:53:37 +000019 /**
20 * Factory
21 */
22 public static class Factory implements TProtocolFactory {
Aditya Agarwal5a429582007-02-06 02:51:15 +000023 public TProtocol getProtocol(TTransport trans) {
24 return new TBinaryProtocol(trans);
Mark Slee456b7a82006-10-25 20:53:37 +000025 }
Mark Slee78f58e22006-09-02 04:17:07 +000026 }
27
Mark Slee456b7a82006-10-25 20:53:37 +000028 /**
29 * Constructor
30 */
Aditya Agarwal5a429582007-02-06 02:51:15 +000031 public TBinaryProtocol(TTransport trans) {
32 super(trans);
Mark Slee83c52a82006-06-07 06:51:18 +000033 }
34
Mark Slee456b7a82006-10-25 20:53:37 +000035 public void writeMessageBegin(TMessage message) throws TException {
36 writeString(message.name);
37 writeByte(message.type);
38 writeI32(message.seqid);
Mark Slee83c52a82006-06-07 06:51:18 +000039 }
40
Mark Slee456b7a82006-10-25 20:53:37 +000041 public void writeMessageEnd() {}
42
43 public void writeStructBegin(TStruct struct) {}
44
45 public void writeStructEnd() {}
46
47 public void writeFieldBegin(TField field) throws TException {
48 writeByte(field.type);
49 writeI16(field.id);
Mark Slee83c52a82006-06-07 06:51:18 +000050 }
51
Mark Slee456b7a82006-10-25 20:53:37 +000052 public void writeFieldEnd() {}
Mark Slee530fd662006-08-09 00:05:18 +000053
Mark Slee456b7a82006-10-25 20:53:37 +000054 public void writeFieldStop() throws TException {
55 writeByte(TType.STOP);
Mark Slee83c52a82006-06-07 06:51:18 +000056 }
57
Mark Slee456b7a82006-10-25 20:53:37 +000058 public void writeMapBegin(TMap map) throws TException {
59 writeByte(map.keyType);
60 writeByte(map.valueType);
61 writeI32(map.size);
Mark Slee83c52a82006-06-07 06:51:18 +000062 }
63
Mark Slee456b7a82006-10-25 20:53:37 +000064 public void writeMapEnd() {}
Mark Slee530fd662006-08-09 00:05:18 +000065
Mark Slee456b7a82006-10-25 20:53:37 +000066 public void writeListBegin(TList list) throws TException {
67 writeByte(list.elemType);
68 writeI32(list.size);
Mark Slee78f58e22006-09-02 04:17:07 +000069 }
70
Mark Slee456b7a82006-10-25 20:53:37 +000071 public void writeListEnd() {}
72
73 public void writeSetBegin(TSet set) throws TException {
74 writeByte(set.elemType);
75 writeI32(set.size);
76 }
77
78 public void writeSetEnd() {}
79
80 public void writeBool(boolean b) throws TException {
81 writeByte(b ? (byte)1 : (byte)0);
82 }
83
84 private byte [] bout = new byte[1];
85 public void writeByte(byte b) throws TException {
Mark Slee530fd662006-08-09 00:05:18 +000086 bout[0] = b;
Aditya Agarwal5a429582007-02-06 02:51:15 +000087 trans_.write(bout, 0, 1);
Mark Slee83c52a82006-06-07 06:51:18 +000088 }
89
Mark Slee456b7a82006-10-25 20:53:37 +000090 private byte[] i16out = new byte[2];
91 public void writeI16(short i16) throws TException {
Mark Slee78f58e22006-09-02 04:17:07 +000092 i16out[0] = (byte)(0xff & (i16 >> 8));
93 i16out[1] = (byte)(0xff & (i16));
Aditya Agarwal5a429582007-02-06 02:51:15 +000094 trans_.write(i16out, 0, 2);
Mark Slee83c52a82006-06-07 06:51:18 +000095 }
96
Mark Slee456b7a82006-10-25 20:53:37 +000097 private byte[] i32out = new byte[4];
98 public void writeI32(int i32) throws TException {
Mark Slee530fd662006-08-09 00:05:18 +000099 i32out[0] = (byte)(0xff & (i32 >> 24));
100 i32out[1] = (byte)(0xff & (i32 >> 16));
101 i32out[2] = (byte)(0xff & (i32 >> 8));
102 i32out[3] = (byte)(0xff & (i32));
Aditya Agarwal5a429582007-02-06 02:51:15 +0000103 trans_.write(i32out, 0, 4);
Mark Slee83c52a82006-06-07 06:51:18 +0000104 }
105
Mark Slee456b7a82006-10-25 20:53:37 +0000106 private byte[] i64out = new byte[8];
107 public void writeI64(long i64) throws TException {
Mark Slee530fd662006-08-09 00:05:18 +0000108 i64out[0] = (byte)(0xff & (i64 >> 56));
109 i64out[1] = (byte)(0xff & (i64 >> 48));
110 i64out[2] = (byte)(0xff & (i64 >> 40));
111 i64out[3] = (byte)(0xff & (i64 >> 32));
112 i64out[4] = (byte)(0xff & (i64 >> 24));
113 i64out[5] = (byte)(0xff & (i64 >> 16));
114 i64out[6] = (byte)(0xff & (i64 >> 8));
115 i64out[7] = (byte)(0xff & (i64));
Aditya Agarwal5a429582007-02-06 02:51:15 +0000116 trans_.write(i64out, 0, 8);
Mark Slee83c52a82006-06-07 06:51:18 +0000117 }
118
Mark Slee456b7a82006-10-25 20:53:37 +0000119 public void writeDouble(double dub) throws TException {
120 writeI64(Double.doubleToLongBits(dub));
Mark Sleec98d0502006-09-06 02:42:25 +0000121 }
122
Mark Slee456b7a82006-10-25 20:53:37 +0000123 public void writeString(String str) throws TException {
Mark Slee530fd662006-08-09 00:05:18 +0000124 byte[] dat = str.getBytes();
Mark Slee456b7a82006-10-25 20:53:37 +0000125 writeI32(dat.length);
Aditya Agarwal5a429582007-02-06 02:51:15 +0000126 trans_.write(dat, 0, dat.length);
Mark Slee83c52a82006-06-07 06:51:18 +0000127 }
128
Mark Slee8d725a22007-04-13 01:57:12 +0000129 public void writeBinary(byte[] bin) throws TException {
130 writeI32(bin.length);
131 trans_.write(bin, 0, bin.length);
132 }
133
Mark Slee83c52a82006-06-07 06:51:18 +0000134 /**
135 * Reading methods.
136 */
137
Mark Slee456b7a82006-10-25 20:53:37 +0000138 public TMessage readMessageBegin() throws TException {
Mark Slee78f58e22006-09-02 04:17:07 +0000139 TMessage message = new TMessage();
Mark Slee456b7a82006-10-25 20:53:37 +0000140 message.name = readString();
141 message.type = readByte();
142 message.seqid = readI32();
Mark Slee78f58e22006-09-02 04:17:07 +0000143 return message;
144 }
145
Mark Slee456b7a82006-10-25 20:53:37 +0000146 public void readMessageEnd() {}
Mark Slee78f58e22006-09-02 04:17:07 +0000147
Mark Slee456b7a82006-10-25 20:53:37 +0000148 public TStruct readStructBegin() {
Mark Slee530fd662006-08-09 00:05:18 +0000149 return new TStruct();
Mark Slee83c52a82006-06-07 06:51:18 +0000150 }
151
Mark Slee456b7a82006-10-25 20:53:37 +0000152 public void readStructEnd() {}
Mark Slee83c52a82006-06-07 06:51:18 +0000153
Mark Slee456b7a82006-10-25 20:53:37 +0000154 public TField readFieldBegin() throws TException {
Mark Slee530fd662006-08-09 00:05:18 +0000155 TField field = new TField();
Mark Slee456b7a82006-10-25 20:53:37 +0000156 field.type = readByte();
Mark Slee530fd662006-08-09 00:05:18 +0000157 if (field.type != TType.STOP) {
Mark Slee456b7a82006-10-25 20:53:37 +0000158 field.id = readI16();
Mark Slee83c52a82006-06-07 06:51:18 +0000159 }
Mark Slee530fd662006-08-09 00:05:18 +0000160 return field;
Mark Slee83c52a82006-06-07 06:51:18 +0000161 }
162
Mark Slee456b7a82006-10-25 20:53:37 +0000163 public void readFieldEnd() {}
Mark Slee83c52a82006-06-07 06:51:18 +0000164
Mark Slee456b7a82006-10-25 20:53:37 +0000165 public TMap readMapBegin() throws TException {
Mark Slee530fd662006-08-09 00:05:18 +0000166 TMap map = new TMap();
Mark Slee456b7a82006-10-25 20:53:37 +0000167 map.keyType = readByte();
168 map.valueType = readByte();
169 map.size = readI32();
Mark Slee530fd662006-08-09 00:05:18 +0000170 return map;
Mark Slee83c52a82006-06-07 06:51:18 +0000171 }
172
Mark Slee456b7a82006-10-25 20:53:37 +0000173 public void readMapEnd() {}
Mark Slee530fd662006-08-09 00:05:18 +0000174
Mark Slee456b7a82006-10-25 20:53:37 +0000175 public TList readListBegin() throws TException {
Mark Slee530fd662006-08-09 00:05:18 +0000176 TList list = new TList();
Mark Slee456b7a82006-10-25 20:53:37 +0000177 list.elemType = readByte();
178 list.size = readI32();
Mark Slee530fd662006-08-09 00:05:18 +0000179 return list;
Mark Slee83c52a82006-06-07 06:51:18 +0000180 }
181
Mark Slee456b7a82006-10-25 20:53:37 +0000182 public void readListEnd() {}
Mark Slee530fd662006-08-09 00:05:18 +0000183
Mark Slee456b7a82006-10-25 20:53:37 +0000184 public TSet readSetBegin() throws TException {
Mark Slee530fd662006-08-09 00:05:18 +0000185 TSet set = new TSet();
Mark Slee456b7a82006-10-25 20:53:37 +0000186 set.elemType = readByte();
187 set.size = readI32();
Mark Slee530fd662006-08-09 00:05:18 +0000188 return set;
Mark Slee83c52a82006-06-07 06:51:18 +0000189 }
190
Mark Slee456b7a82006-10-25 20:53:37 +0000191 public void readSetEnd() {}
Mark Slee530fd662006-08-09 00:05:18 +0000192
Mark Slee456b7a82006-10-25 20:53:37 +0000193 public boolean readBool() throws TException {
194 return (readByte() == 1);
Mark Slee78f58e22006-09-02 04:17:07 +0000195 }
196
Mark Slee456b7a82006-10-25 20:53:37 +0000197 private byte[] bin = new byte[1];
198 public byte readByte() throws TException {
Aditya Agarwal5a429582007-02-06 02:51:15 +0000199 trans_.readAll(bin, 0, 1);
Mark Slee530fd662006-08-09 00:05:18 +0000200 return bin[0];
Mark Slee83c52a82006-06-07 06:51:18 +0000201 }
202
Mark Slee456b7a82006-10-25 20:53:37 +0000203 private byte[] i16rd = new byte[2];
204 public short readI16() throws TException {
Aditya Agarwal5a429582007-02-06 02:51:15 +0000205 trans_.readAll(i16rd, 0, 2);
Mark Slee78f58e22006-09-02 04:17:07 +0000206 return
207 (short)
208 (((i16rd[0] & 0xff) << 8) |
209 ((i16rd[1] & 0xff)));
Mark Slee83c52a82006-06-07 06:51:18 +0000210 }
211
Mark Slee456b7a82006-10-25 20:53:37 +0000212 private byte[] i32rd = new byte[4];
213 public int readI32() throws TException {
Aditya Agarwal5a429582007-02-06 02:51:15 +0000214 trans_.readAll(i32rd, 0, 4);
Mark Slee530fd662006-08-09 00:05:18 +0000215 return
216 ((i32rd[0] & 0xff) << 24) |
217 ((i32rd[1] & 0xff) << 16) |
218 ((i32rd[2] & 0xff) << 8) |
219 ((i32rd[3] & 0xff));
Mark Slee83c52a82006-06-07 06:51:18 +0000220 }
Mark Slee78f58e22006-09-02 04:17:07 +0000221
Mark Slee456b7a82006-10-25 20:53:37 +0000222 private byte[] i64rd = new byte[8];
223 public long readI64() throws TException {
Aditya Agarwal5a429582007-02-06 02:51:15 +0000224 trans_.readAll(i64rd, 0, 8);
Mark Slee530fd662006-08-09 00:05:18 +0000225 return
226 ((long)(i64rd[0] & 0xff) << 56) |
227 ((long)(i64rd[1] & 0xff) << 48) |
228 ((long)(i64rd[2] & 0xff) << 40) |
229 ((long)(i64rd[3] & 0xff) << 32) |
230 ((long)(i64rd[4] & 0xff) << 24) |
231 ((long)(i64rd[5] & 0xff) << 16) |
232 ((long)(i64rd[6] & 0xff) << 8) |
233 ((long)(i64rd[7] & 0xff));
Mark Slee83c52a82006-06-07 06:51:18 +0000234 }
235
Mark Slee456b7a82006-10-25 20:53:37 +0000236 public double readDouble() throws TException {
237 return Double.longBitsToDouble(readI64());
Mark Sleec98d0502006-09-06 02:42:25 +0000238 }
239
Mark Slee456b7a82006-10-25 20:53:37 +0000240 public String readString() throws TException {
241 int size = readI32();
Mark Slee530fd662006-08-09 00:05:18 +0000242 byte[] buf = new byte[size];
Aditya Agarwal5a429582007-02-06 02:51:15 +0000243 trans_.readAll(buf, 0, size);
Mark Slee530fd662006-08-09 00:05:18 +0000244 return new String(buf);
Mark Slee83c52a82006-06-07 06:51:18 +0000245 }
Mark Slee8d725a22007-04-13 01:57:12 +0000246
247 public byte[] readBinary() throws TException {
248 int size = readI32();
249 byte[] buf = new byte[size];
250 trans_.readAll(buf, 0, size);
251 return buf;
252 }
253
Mark Slee83c52a82006-06-07 06:51:18 +0000254}