blob: 4ba094174b9289a5a54b40ab40b265adc4e077ba [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
129 /**
130 * Reading methods.
131 */
132
Mark Slee456b7a82006-10-25 20:53:37 +0000133 public TMessage readMessageBegin() throws TException {
Mark Slee78f58e22006-09-02 04:17:07 +0000134 TMessage message = new TMessage();
Mark Slee456b7a82006-10-25 20:53:37 +0000135 message.name = readString();
136 message.type = readByte();
137 message.seqid = readI32();
Mark Slee78f58e22006-09-02 04:17:07 +0000138 return message;
139 }
140
Mark Slee456b7a82006-10-25 20:53:37 +0000141 public void readMessageEnd() {}
Mark Slee78f58e22006-09-02 04:17:07 +0000142
Mark Slee456b7a82006-10-25 20:53:37 +0000143 public TStruct readStructBegin() {
Mark Slee530fd662006-08-09 00:05:18 +0000144 return new TStruct();
Mark Slee83c52a82006-06-07 06:51:18 +0000145 }
146
Mark Slee456b7a82006-10-25 20:53:37 +0000147 public void readStructEnd() {}
Mark Slee83c52a82006-06-07 06:51:18 +0000148
Mark Slee456b7a82006-10-25 20:53:37 +0000149 public TField readFieldBegin() throws TException {
Mark Slee530fd662006-08-09 00:05:18 +0000150 TField field = new TField();
Mark Slee456b7a82006-10-25 20:53:37 +0000151 field.type = readByte();
Mark Slee530fd662006-08-09 00:05:18 +0000152 if (field.type != TType.STOP) {
Mark Slee456b7a82006-10-25 20:53:37 +0000153 field.id = readI16();
Mark Slee83c52a82006-06-07 06:51:18 +0000154 }
Mark Slee530fd662006-08-09 00:05:18 +0000155 return field;
Mark Slee83c52a82006-06-07 06:51:18 +0000156 }
157
Mark Slee456b7a82006-10-25 20:53:37 +0000158 public void readFieldEnd() {}
Mark Slee83c52a82006-06-07 06:51:18 +0000159
Mark Slee456b7a82006-10-25 20:53:37 +0000160 public TMap readMapBegin() throws TException {
Mark Slee530fd662006-08-09 00:05:18 +0000161 TMap map = new TMap();
Mark Slee456b7a82006-10-25 20:53:37 +0000162 map.keyType = readByte();
163 map.valueType = readByte();
164 map.size = readI32();
Mark Slee530fd662006-08-09 00:05:18 +0000165 return map;
Mark Slee83c52a82006-06-07 06:51:18 +0000166 }
167
Mark Slee456b7a82006-10-25 20:53:37 +0000168 public void readMapEnd() {}
Mark Slee530fd662006-08-09 00:05:18 +0000169
Mark Slee456b7a82006-10-25 20:53:37 +0000170 public TList readListBegin() throws TException {
Mark Slee530fd662006-08-09 00:05:18 +0000171 TList list = new TList();
Mark Slee456b7a82006-10-25 20:53:37 +0000172 list.elemType = readByte();
173 list.size = readI32();
Mark Slee530fd662006-08-09 00:05:18 +0000174 return list;
Mark Slee83c52a82006-06-07 06:51:18 +0000175 }
176
Mark Slee456b7a82006-10-25 20:53:37 +0000177 public void readListEnd() {}
Mark Slee530fd662006-08-09 00:05:18 +0000178
Mark Slee456b7a82006-10-25 20:53:37 +0000179 public TSet readSetBegin() throws TException {
Mark Slee530fd662006-08-09 00:05:18 +0000180 TSet set = new TSet();
Mark Slee456b7a82006-10-25 20:53:37 +0000181 set.elemType = readByte();
182 set.size = readI32();
Mark Slee530fd662006-08-09 00:05:18 +0000183 return set;
Mark Slee83c52a82006-06-07 06:51:18 +0000184 }
185
Mark Slee456b7a82006-10-25 20:53:37 +0000186 public void readSetEnd() {}
Mark Slee530fd662006-08-09 00:05:18 +0000187
Mark Slee456b7a82006-10-25 20:53:37 +0000188 public boolean readBool() throws TException {
189 return (readByte() == 1);
Mark Slee78f58e22006-09-02 04:17:07 +0000190 }
191
Mark Slee456b7a82006-10-25 20:53:37 +0000192 private byte[] bin = new byte[1];
193 public byte readByte() throws TException {
Aditya Agarwal5a429582007-02-06 02:51:15 +0000194 trans_.readAll(bin, 0, 1);
Mark Slee530fd662006-08-09 00:05:18 +0000195 return bin[0];
Mark Slee83c52a82006-06-07 06:51:18 +0000196 }
197
Mark Slee456b7a82006-10-25 20:53:37 +0000198 private byte[] i16rd = new byte[2];
199 public short readI16() throws TException {
Aditya Agarwal5a429582007-02-06 02:51:15 +0000200 trans_.readAll(i16rd, 0, 2);
Mark Slee78f58e22006-09-02 04:17:07 +0000201 return
202 (short)
203 (((i16rd[0] & 0xff) << 8) |
204 ((i16rd[1] & 0xff)));
Mark Slee83c52a82006-06-07 06:51:18 +0000205 }
206
Mark Slee456b7a82006-10-25 20:53:37 +0000207 private byte[] i32rd = new byte[4];
208 public int readI32() throws TException {
Aditya Agarwal5a429582007-02-06 02:51:15 +0000209 trans_.readAll(i32rd, 0, 4);
Mark Slee530fd662006-08-09 00:05:18 +0000210 return
211 ((i32rd[0] & 0xff) << 24) |
212 ((i32rd[1] & 0xff) << 16) |
213 ((i32rd[2] & 0xff) << 8) |
214 ((i32rd[3] & 0xff));
Mark Slee83c52a82006-06-07 06:51:18 +0000215 }
Mark Slee78f58e22006-09-02 04:17:07 +0000216
Mark Slee456b7a82006-10-25 20:53:37 +0000217 private byte[] i64rd = new byte[8];
218 public long readI64() throws TException {
Aditya Agarwal5a429582007-02-06 02:51:15 +0000219 trans_.readAll(i64rd, 0, 8);
Mark Slee530fd662006-08-09 00:05:18 +0000220 return
221 ((long)(i64rd[0] & 0xff) << 56) |
222 ((long)(i64rd[1] & 0xff) << 48) |
223 ((long)(i64rd[2] & 0xff) << 40) |
224 ((long)(i64rd[3] & 0xff) << 32) |
225 ((long)(i64rd[4] & 0xff) << 24) |
226 ((long)(i64rd[5] & 0xff) << 16) |
227 ((long)(i64rd[6] & 0xff) << 8) |
228 ((long)(i64rd[7] & 0xff));
Mark Slee83c52a82006-06-07 06:51:18 +0000229 }
230
Mark Slee456b7a82006-10-25 20:53:37 +0000231 public double readDouble() throws TException {
232 return Double.longBitsToDouble(readI64());
Mark Sleec98d0502006-09-06 02:42:25 +0000233 }
234
Mark Slee456b7a82006-10-25 20:53:37 +0000235 public String readString() throws TException {
236 int size = readI32();
Mark Slee530fd662006-08-09 00:05:18 +0000237 byte[] buf = new byte[size];
Aditya Agarwal5a429582007-02-06 02:51:15 +0000238 trans_.readAll(buf, 0, size);
Mark Slee530fd662006-08-09 00:05:18 +0000239 return new String(buf);
Mark Slee83c52a82006-06-07 06:51:18 +0000240 }
241}