blob: d90decefbabca97381221a2c452cb6c91d1ec86d [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 Slee530fd662006-08-09 00:05:18 +000098 public void writeString(TTransport out, String str) throws TException {
99 byte[] dat = str.getBytes();
100 writeI32(out, dat.length);
Mark Slee83c52a82006-06-07 06:51:18 +0000101 out.write(dat, 0, dat.length);
Mark Slee83c52a82006-06-07 06:51:18 +0000102 }
103
104 /**
105 * Reading methods.
106 */
107
Mark Slee78f58e22006-09-02 04:17:07 +0000108 public TMessage readMessageBegin(TTransport in) throws TException {
109 TMessage message = new TMessage();
110 message.name = readString(in);
111 message.type = readByte(in);
112 message.seqid = readI32(in);
113 return message;
114 }
115
116 public void readMessageEnd(TTransport in) throws TException {}
117
Mark Slee530fd662006-08-09 00:05:18 +0000118 public TStruct readStructBegin(TTransport in) throws TException {
119 return new TStruct();
Mark Slee83c52a82006-06-07 06:51:18 +0000120 }
121
Mark Slee530fd662006-08-09 00:05:18 +0000122 public void readStructEnd(TTransport in) throws TException {}
Mark Slee83c52a82006-06-07 06:51:18 +0000123
Mark Slee530fd662006-08-09 00:05:18 +0000124 public TField readFieldBegin(TTransport in) throws TException {
125 TField field = new TField();
126 field.type = readByte(in);
127 if (field.type != TType.STOP) {
Mark Slee78f58e22006-09-02 04:17:07 +0000128 field.id = readI16(in);
Mark Slee83c52a82006-06-07 06:51:18 +0000129 }
Mark Slee530fd662006-08-09 00:05:18 +0000130 return field;
Mark Slee83c52a82006-06-07 06:51:18 +0000131 }
132
Mark Slee530fd662006-08-09 00:05:18 +0000133 public void readFieldEnd(TTransport in) throws TException {}
Mark Slee83c52a82006-06-07 06:51:18 +0000134
Mark Slee530fd662006-08-09 00:05:18 +0000135 public TMap readMapBegin(TTransport in) throws TException {
136 TMap map = new TMap();
137 map.keyType = readByte(in);
138 map.valueType = readByte(in);
139 map.size = readI32(in);
140 return map;
Mark Slee83c52a82006-06-07 06:51:18 +0000141 }
142
Mark Slee530fd662006-08-09 00:05:18 +0000143 public void readMapEnd(TTransport in) throws TException {}
144
145 public TList readListBegin(TTransport in) throws TException {
146 TList list = new TList();
147 list.elemType = readByte(in);
148 list.size = readI32(in);
149 return list;
Mark Slee83c52a82006-06-07 06:51:18 +0000150 }
151
Mark Slee530fd662006-08-09 00:05:18 +0000152 public void readListEnd(TTransport in) throws TException {}
153
154 public TSet readSetBegin(TTransport in) throws TException {
155 TSet set = new TSet();
156 set.elemType = readByte(in);
157 set.size = readI32(in);
158 return set;
Mark Slee83c52a82006-06-07 06:51:18 +0000159 }
160
Mark Slee530fd662006-08-09 00:05:18 +0000161 public void readSetEnd(TTransport in) throws TException {}
162
Mark Slee78f58e22006-09-02 04:17:07 +0000163 public boolean readBool(TTransport in) throws TException {
164 return (readByte(in) == 1);
165 }
166
Mark Slee530fd662006-08-09 00:05:18 +0000167 byte[] bin = new byte[1];
168 public byte readByte(TTransport in) throws TException {
169 in.readAll(bin, 0, 1);
170 return bin[0];
Mark Slee83c52a82006-06-07 06:51:18 +0000171 }
172
Mark Slee78f58e22006-09-02 04:17:07 +0000173 byte[] i16rd = new byte[2];
174 public short readI16(TTransport in) throws TException {
175 in.readAll(i16rd, 0, 2);
176 return
177 (short)
178 (((i16rd[0] & 0xff) << 8) |
179 ((i16rd[1] & 0xff)));
Mark Slee83c52a82006-06-07 06:51:18 +0000180 }
181
Mark Slee530fd662006-08-09 00:05:18 +0000182 byte[] i32rd = new byte[4];
183 public int readI32(TTransport in) throws TException {
184 in.readAll(i32rd, 0, 4);
185 return
186 ((i32rd[0] & 0xff) << 24) |
187 ((i32rd[1] & 0xff) << 16) |
188 ((i32rd[2] & 0xff) << 8) |
189 ((i32rd[3] & 0xff));
Mark Slee83c52a82006-06-07 06:51:18 +0000190 }
Mark Slee78f58e22006-09-02 04:17:07 +0000191
Mark Slee530fd662006-08-09 00:05:18 +0000192 byte[] i64rd = new byte[8];
193 public long readI64(TTransport in) throws TException {
194 in.readAll(i64rd, 0, 8);
195 return
196 ((long)(i64rd[0] & 0xff) << 56) |
197 ((long)(i64rd[1] & 0xff) << 48) |
198 ((long)(i64rd[2] & 0xff) << 40) |
199 ((long)(i64rd[3] & 0xff) << 32) |
200 ((long)(i64rd[4] & 0xff) << 24) |
201 ((long)(i64rd[5] & 0xff) << 16) |
202 ((long)(i64rd[6] & 0xff) << 8) |
203 ((long)(i64rd[7] & 0xff));
Mark Slee83c52a82006-06-07 06:51:18 +0000204 }
205
Mark Slee530fd662006-08-09 00:05:18 +0000206 public String readString(TTransport in) throws TException {
207 int size = readI32(in);
208 byte[] buf = new byte[size];
209 in.readAll(buf, 0, size);
210 return new String(buf);
Mark Slee83c52a82006-06-07 06:51:18 +0000211 }
212}