blob: 08f6e5579c7e2e7e07db397ddf19543b35c7292a [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 */
Mark Slee456b7a82006-10-25 20:53:37 +000011public class TBinaryProtocol extends TProtocol {
Mark Slee78f58e22006-09-02 04:17:07 +000012
Mark Slee456b7a82006-10-25 20:53:37 +000013 /**
14 * Factory
15 */
16 public static class Factory implements TProtocolFactory {
Aditya Agarwal5a429582007-02-06 02:51:15 +000017 public TProtocol getProtocol(TTransport trans) {
18 return new TBinaryProtocol(trans);
Mark Slee456b7a82006-10-25 20:53:37 +000019 }
Mark Slee78f58e22006-09-02 04:17:07 +000020 }
21
Mark Slee456b7a82006-10-25 20:53:37 +000022 /**
23 * Constructor
24 */
Aditya Agarwal5a429582007-02-06 02:51:15 +000025 public TBinaryProtocol(TTransport trans) {
26 super(trans);
Mark Slee83c52a82006-06-07 06:51:18 +000027 }
28
Mark Slee456b7a82006-10-25 20:53:37 +000029 public void writeMessageBegin(TMessage message) throws TException {
30 writeString(message.name);
31 writeByte(message.type);
32 writeI32(message.seqid);
Mark Slee83c52a82006-06-07 06:51:18 +000033 }
34
Mark Slee456b7a82006-10-25 20:53:37 +000035 public void writeMessageEnd() {}
36
37 public void writeStructBegin(TStruct struct) {}
38
39 public void writeStructEnd() {}
40
41 public void writeFieldBegin(TField field) throws TException {
42 writeByte(field.type);
43 writeI16(field.id);
Mark Slee83c52a82006-06-07 06:51:18 +000044 }
45
Mark Slee456b7a82006-10-25 20:53:37 +000046 public void writeFieldEnd() {}
Mark Slee530fd662006-08-09 00:05:18 +000047
Mark Slee456b7a82006-10-25 20:53:37 +000048 public void writeFieldStop() throws TException {
49 writeByte(TType.STOP);
Mark Slee83c52a82006-06-07 06:51:18 +000050 }
51
Mark Slee456b7a82006-10-25 20:53:37 +000052 public void writeMapBegin(TMap map) throws TException {
53 writeByte(map.keyType);
54 writeByte(map.valueType);
55 writeI32(map.size);
Mark Slee83c52a82006-06-07 06:51:18 +000056 }
57
Mark Slee456b7a82006-10-25 20:53:37 +000058 public void writeMapEnd() {}
Mark Slee530fd662006-08-09 00:05:18 +000059
Mark Slee456b7a82006-10-25 20:53:37 +000060 public void writeListBegin(TList list) throws TException {
61 writeByte(list.elemType);
62 writeI32(list.size);
Mark Slee78f58e22006-09-02 04:17:07 +000063 }
64
Mark Slee456b7a82006-10-25 20:53:37 +000065 public void writeListEnd() {}
66
67 public void writeSetBegin(TSet set) throws TException {
68 writeByte(set.elemType);
69 writeI32(set.size);
70 }
71
72 public void writeSetEnd() {}
73
74 public void writeBool(boolean b) throws TException {
75 writeByte(b ? (byte)1 : (byte)0);
76 }
77
78 private byte [] bout = new byte[1];
79 public void writeByte(byte b) throws TException {
Mark Slee530fd662006-08-09 00:05:18 +000080 bout[0] = b;
Aditya Agarwal5a429582007-02-06 02:51:15 +000081 trans_.write(bout, 0, 1);
Mark Slee83c52a82006-06-07 06:51:18 +000082 }
83
Mark Slee456b7a82006-10-25 20:53:37 +000084 private byte[] i16out = new byte[2];
85 public void writeI16(short i16) throws TException {
Mark Slee78f58e22006-09-02 04:17:07 +000086 i16out[0] = (byte)(0xff & (i16 >> 8));
87 i16out[1] = (byte)(0xff & (i16));
Aditya Agarwal5a429582007-02-06 02:51:15 +000088 trans_.write(i16out, 0, 2);
Mark Slee83c52a82006-06-07 06:51:18 +000089 }
90
Mark Slee456b7a82006-10-25 20:53:37 +000091 private byte[] i32out = new byte[4];
92 public void writeI32(int i32) throws TException {
Mark Slee530fd662006-08-09 00:05:18 +000093 i32out[0] = (byte)(0xff & (i32 >> 24));
94 i32out[1] = (byte)(0xff & (i32 >> 16));
95 i32out[2] = (byte)(0xff & (i32 >> 8));
96 i32out[3] = (byte)(0xff & (i32));
Aditya Agarwal5a429582007-02-06 02:51:15 +000097 trans_.write(i32out, 0, 4);
Mark Slee83c52a82006-06-07 06:51:18 +000098 }
99
Mark Slee456b7a82006-10-25 20:53:37 +0000100 private byte[] i64out = new byte[8];
101 public void writeI64(long i64) throws TException {
Mark Slee530fd662006-08-09 00:05:18 +0000102 i64out[0] = (byte)(0xff & (i64 >> 56));
103 i64out[1] = (byte)(0xff & (i64 >> 48));
104 i64out[2] = (byte)(0xff & (i64 >> 40));
105 i64out[3] = (byte)(0xff & (i64 >> 32));
106 i64out[4] = (byte)(0xff & (i64 >> 24));
107 i64out[5] = (byte)(0xff & (i64 >> 16));
108 i64out[6] = (byte)(0xff & (i64 >> 8));
109 i64out[7] = (byte)(0xff & (i64));
Aditya Agarwal5a429582007-02-06 02:51:15 +0000110 trans_.write(i64out, 0, 8);
Mark Slee83c52a82006-06-07 06:51:18 +0000111 }
112
Mark Slee456b7a82006-10-25 20:53:37 +0000113 public void writeDouble(double dub) throws TException {
114 writeI64(Double.doubleToLongBits(dub));
Mark Sleec98d0502006-09-06 02:42:25 +0000115 }
116
Mark Slee456b7a82006-10-25 20:53:37 +0000117 public void writeString(String str) throws TException {
Mark Slee530fd662006-08-09 00:05:18 +0000118 byte[] dat = str.getBytes();
Mark Slee456b7a82006-10-25 20:53:37 +0000119 writeI32(dat.length);
Aditya Agarwal5a429582007-02-06 02:51:15 +0000120 trans_.write(dat, 0, dat.length);
Mark Slee83c52a82006-06-07 06:51:18 +0000121 }
122
123 /**
124 * Reading methods.
125 */
126
Mark Slee456b7a82006-10-25 20:53:37 +0000127 public TMessage readMessageBegin() throws TException {
Mark Slee78f58e22006-09-02 04:17:07 +0000128 TMessage message = new TMessage();
Mark Slee456b7a82006-10-25 20:53:37 +0000129 message.name = readString();
130 message.type = readByte();
131 message.seqid = readI32();
Mark Slee78f58e22006-09-02 04:17:07 +0000132 return message;
133 }
134
Mark Slee456b7a82006-10-25 20:53:37 +0000135 public void readMessageEnd() {}
Mark Slee78f58e22006-09-02 04:17:07 +0000136
Mark Slee456b7a82006-10-25 20:53:37 +0000137 public TStruct readStructBegin() {
Mark Slee530fd662006-08-09 00:05:18 +0000138 return new TStruct();
Mark Slee83c52a82006-06-07 06:51:18 +0000139 }
140
Mark Slee456b7a82006-10-25 20:53:37 +0000141 public void readStructEnd() {}
Mark Slee83c52a82006-06-07 06:51:18 +0000142
Mark Slee456b7a82006-10-25 20:53:37 +0000143 public TField readFieldBegin() throws TException {
Mark Slee530fd662006-08-09 00:05:18 +0000144 TField field = new TField();
Mark Slee456b7a82006-10-25 20:53:37 +0000145 field.type = readByte();
Mark Slee530fd662006-08-09 00:05:18 +0000146 if (field.type != TType.STOP) {
Mark Slee456b7a82006-10-25 20:53:37 +0000147 field.id = readI16();
Mark Slee83c52a82006-06-07 06:51:18 +0000148 }
Mark Slee530fd662006-08-09 00:05:18 +0000149 return field;
Mark Slee83c52a82006-06-07 06:51:18 +0000150 }
151
Mark Slee456b7a82006-10-25 20:53:37 +0000152 public void readFieldEnd() {}
Mark Slee83c52a82006-06-07 06:51:18 +0000153
Mark Slee456b7a82006-10-25 20:53:37 +0000154 public TMap readMapBegin() throws TException {
Mark Slee530fd662006-08-09 00:05:18 +0000155 TMap map = new TMap();
Mark Slee456b7a82006-10-25 20:53:37 +0000156 map.keyType = readByte();
157 map.valueType = readByte();
158 map.size = readI32();
Mark Slee530fd662006-08-09 00:05:18 +0000159 return map;
Mark Slee83c52a82006-06-07 06:51:18 +0000160 }
161
Mark Slee456b7a82006-10-25 20:53:37 +0000162 public void readMapEnd() {}
Mark Slee530fd662006-08-09 00:05:18 +0000163
Mark Slee456b7a82006-10-25 20:53:37 +0000164 public TList readListBegin() throws TException {
Mark Slee530fd662006-08-09 00:05:18 +0000165 TList list = new TList();
Mark Slee456b7a82006-10-25 20:53:37 +0000166 list.elemType = readByte();
167 list.size = readI32();
Mark Slee530fd662006-08-09 00:05:18 +0000168 return list;
Mark Slee83c52a82006-06-07 06:51:18 +0000169 }
170
Mark Slee456b7a82006-10-25 20:53:37 +0000171 public void readListEnd() {}
Mark Slee530fd662006-08-09 00:05:18 +0000172
Mark Slee456b7a82006-10-25 20:53:37 +0000173 public TSet readSetBegin() throws TException {
Mark Slee530fd662006-08-09 00:05:18 +0000174 TSet set = new TSet();
Mark Slee456b7a82006-10-25 20:53:37 +0000175 set.elemType = readByte();
176 set.size = readI32();
Mark Slee530fd662006-08-09 00:05:18 +0000177 return set;
Mark Slee83c52a82006-06-07 06:51:18 +0000178 }
179
Mark Slee456b7a82006-10-25 20:53:37 +0000180 public void readSetEnd() {}
Mark Slee530fd662006-08-09 00:05:18 +0000181
Mark Slee456b7a82006-10-25 20:53:37 +0000182 public boolean readBool() throws TException {
183 return (readByte() == 1);
Mark Slee78f58e22006-09-02 04:17:07 +0000184 }
185
Mark Slee456b7a82006-10-25 20:53:37 +0000186 private byte[] bin = new byte[1];
187 public byte readByte() throws TException {
Aditya Agarwal5a429582007-02-06 02:51:15 +0000188 trans_.readAll(bin, 0, 1);
Mark Slee530fd662006-08-09 00:05:18 +0000189 return bin[0];
Mark Slee83c52a82006-06-07 06:51:18 +0000190 }
191
Mark Slee456b7a82006-10-25 20:53:37 +0000192 private byte[] i16rd = new byte[2];
193 public short readI16() throws TException {
Aditya Agarwal5a429582007-02-06 02:51:15 +0000194 trans_.readAll(i16rd, 0, 2);
Mark Slee78f58e22006-09-02 04:17:07 +0000195 return
196 (short)
197 (((i16rd[0] & 0xff) << 8) |
198 ((i16rd[1] & 0xff)));
Mark Slee83c52a82006-06-07 06:51:18 +0000199 }
200
Mark Slee456b7a82006-10-25 20:53:37 +0000201 private byte[] i32rd = new byte[4];
202 public int readI32() throws TException {
Aditya Agarwal5a429582007-02-06 02:51:15 +0000203 trans_.readAll(i32rd, 0, 4);
Mark Slee530fd662006-08-09 00:05:18 +0000204 return
205 ((i32rd[0] & 0xff) << 24) |
206 ((i32rd[1] & 0xff) << 16) |
207 ((i32rd[2] & 0xff) << 8) |
208 ((i32rd[3] & 0xff));
Mark Slee83c52a82006-06-07 06:51:18 +0000209 }
Mark Slee78f58e22006-09-02 04:17:07 +0000210
Mark Slee456b7a82006-10-25 20:53:37 +0000211 private byte[] i64rd = new byte[8];
212 public long readI64() throws TException {
Aditya Agarwal5a429582007-02-06 02:51:15 +0000213 trans_.readAll(i64rd, 0, 8);
Mark Slee530fd662006-08-09 00:05:18 +0000214 return
215 ((long)(i64rd[0] & 0xff) << 56) |
216 ((long)(i64rd[1] & 0xff) << 48) |
217 ((long)(i64rd[2] & 0xff) << 40) |
218 ((long)(i64rd[3] & 0xff) << 32) |
219 ((long)(i64rd[4] & 0xff) << 24) |
220 ((long)(i64rd[5] & 0xff) << 16) |
221 ((long)(i64rd[6] & 0xff) << 8) |
222 ((long)(i64rd[7] & 0xff));
Mark Slee83c52a82006-06-07 06:51:18 +0000223 }
224
Mark Slee456b7a82006-10-25 20:53:37 +0000225 public double readDouble() throws TException {
226 return Double.longBitsToDouble(readI64());
Mark Sleec98d0502006-09-06 02:42:25 +0000227 }
228
Mark Slee456b7a82006-10-25 20:53:37 +0000229 public String readString() throws TException {
230 int size = readI32();
Mark Slee530fd662006-08-09 00:05:18 +0000231 byte[] buf = new byte[size];
Aditya Agarwal5a429582007-02-06 02:51:15 +0000232 trans_.readAll(buf, 0, size);
Mark Slee530fd662006-08-09 00:05:18 +0000233 return new String(buf);
Mark Slee83c52a82006-06-07 06:51:18 +0000234 }
235}