blob: b2e77d930f7f85a41064408a7ea2c42ece151a81 [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;
5import com.facebook.thrift.types.*;
6
7/**
8 * Binary protocol implementation for thrift.
9 *
10 * @author Mark Slee <mcslee@facebook.com>
11 */
12public class TBinaryProtocol implements TProtocol {
13 public int writeStructBegin (TTransport out,
14 TStruct struct) throws TException {
15 return 0;
16 }
17
18 public int writeStructEnd (TTransport out) throws TException {
19 return 0;
20 }
21
22 public int writeFieldBegin (TTransport out,
23 TField field) throws TException {
24 return
25 writeByte(out, field.type.getCode()) +
Mark Sleef3c322b2006-06-26 23:52:22 +000026 writeI32(out, field.id);
Mark Slee83c52a82006-06-07 06:51:18 +000027 }
28
29 public int writeFieldEnd (TTransport out) throws TException {
30 return 0;
31 }
32
33 public int writeFieldStop (TTransport out) throws TException {
34 return
35 writeByte(out, TType.STOP.getCode());
36 }
37
38 public int writeMapBegin (TTransport out,
39 TMap map) throws TException {
40 return
41 writeByte(out, map.keyType.getCode()) +
42 writeByte(out, map.valueType.getCode()) +
Mark Sleef3c322b2006-06-26 23:52:22 +000043 writeI32(out, map.size);
Mark Slee83c52a82006-06-07 06:51:18 +000044 }
45
46 public int writeMapEnd (TTransport out) throws TException {
47 return 0;
48 }
49
50 public int writeListBegin (TTransport out,
51 TList list) throws TException {
52 return
53 writeByte(out, list.elemType.getCode()) +
Mark Sleef3c322b2006-06-26 23:52:22 +000054 writeI32(out, list.size);
Mark Slee83c52a82006-06-07 06:51:18 +000055 }
56
57 public int writeListEnd (TTransport out) throws TException {
58 return 0;
59 }
60
61 public int writeSetBegin (TTransport out,
62 TSet set) throws TException {
63 return
64 writeByte(out, set.elemType.getCode()) +
Mark Sleef3c322b2006-06-26 23:52:22 +000065 writeI32(out, set.size);
Mark Slee83c52a82006-06-07 06:51:18 +000066 }
67
68 public int writeSetEnd (TTransport out) throws TException {
69 return 0;
70 }
71
72 public int writeByte (TTransport out,
73 UInt8 b) throws TException {
74 out.write(b.data(), 0, 1);
75 return 1;
76 }
77
78 public int writeU32 (TTransport out,
79 UInt32 u32) throws TException {
80 out.write(u32.data(), 0, 4);
81 return 4;
82 }
83
84 public int writeI32 (TTransport out,
85 Int32 i32) throws TException {
86 out.write(i32.data(), 0, 4);
87 return 4;
88 }
89
90 public int writeU64 (TTransport out,
91 UInt64 u64) throws TException {
92 out.write(u64.data(), 0, 8);
93 return 8;
94 }
95
96 public int writeI64 (TTransport out,
97 Int64 i64) throws TException {
98 out.write(i64.data(), 0, 8);
99 return 8;
100 }
101
102 public int writeString (TTransport out,
103 TString str) throws TException {
104 byte[] dat = str.value.getBytes();
Mark Sleef3c322b2006-06-26 23:52:22 +0000105 int sent = writeI32(out, new Int32(dat.length));
Mark Slee83c52a82006-06-07 06:51:18 +0000106 out.write(dat, 0, dat.length);
107 return sent + dat.length;
108 }
109
110 /**
111 * Reading methods.
112 */
113
114 public int readStructBegin (TTransport in,
115 TStruct struct) throws TException {
116 struct.name = "";
117 return 0;
118 }
119
120 public int readStructEnd (TTransport in) throws TException {
121 return 0;
122 }
123
124 public int readFieldBegin (TTransport in,
125 TField field) throws TException {
126 int recv = 0;
127 UInt8 t = new UInt8();
128
129 recv += readByte(in, t);
130 field.type = TType.getType(t);
131 if (field.type.equals(TType.STOP)) {
Mark Sleef3c322b2006-06-26 23:52:22 +0000132 field.id = new Int32(0);
Mark Slee83c52a82006-06-07 06:51:18 +0000133 return recv;
134 }
Mark Sleef3c322b2006-06-26 23:52:22 +0000135 recv += readI32(in, field.id);
Mark Slee83c52a82006-06-07 06:51:18 +0000136 return recv;
137 }
138
139 public int readFieldEnd (TTransport in) throws TException {
140 return 0;
141 }
142
143 public int readMapBegin (TTransport in,
144 TMap map) throws TException {
145 int recv = 0;
146 UInt8 t = new UInt8();
147 recv += readByte(in, t);
148 map.keyType = TType.getType(t);
149 recv += readByte(in, t);
150 map.valueType = TType.getType(t);
Mark Sleef3c322b2006-06-26 23:52:22 +0000151 recv += readI32(in, map.size);
Mark Slee83c52a82006-06-07 06:51:18 +0000152 return recv;
153 }
154
155 public int readMapEnd (TTransport in) throws TException {
156 return 0;
157 }
158
159 public int readListBegin (TTransport in,
160 TList list) throws TException {
161 int recv = 0;
162 UInt8 t = new UInt8();
163 recv += readByte(in, t);
164 list.elemType = TType.getType(t);
Mark Sleef3c322b2006-06-26 23:52:22 +0000165 recv += readI32(in, list.size);
Mark Slee83c52a82006-06-07 06:51:18 +0000166 return recv;
167 }
168
169 public int readListEnd (TTransport in) throws TException {
170 return 0;
171 }
172
173 public int readSetBegin (TTransport in,
174 TSet set) throws TException {
175 int recv = 0;
176 UInt8 t = new UInt8();
177 recv += readByte(in, t);
178 set.elemType = TType.getType(t);
Mark Sleef3c322b2006-06-26 23:52:22 +0000179 recv += readI32(in, set.size);
Mark Slee83c52a82006-06-07 06:51:18 +0000180 return recv;
181 }
182
183 public int readSetEnd (TTransport in) throws TException {
184 return 0;
185 }
186
187 public int readByte (TTransport in,
188 UInt8 b) throws TException {
189 byte[] buf = new byte[1];
190 in.readAll(buf, 0, 1);
191 b.read(buf, 0);
192 return 1;
193 }
194
195 public int readU32 (TTransport in,
196 UInt32 u32) throws TException {
197 byte[] buf = new byte[4];
198 in.readAll(buf, 0, 4);
199 u32.read(buf, 0);
200 return 4;
201 }
202
203 public int readI32 (TTransport in,
204 Int32 i32) throws TException {
205 byte[] buf = new byte[4];
206 in.readAll(buf, 0, 4);
207 i32.read(buf, 0);
208 return 4;
209 }
210
211 public int readU64 (TTransport in,
212 UInt64 u64) throws TException {
213 byte[] buf = new byte[8];
214 in.readAll(buf, 0, 8);
215 u64.read(buf, 0);
216 return 8;
217 }
218
219 public int readI64 (TTransport in,
220 Int64 i64) throws TException {
221 byte[] buf = new byte[8];
222 in.readAll(buf, 0, 8);
223 i64.read(buf, 0);
224 return 8;
225 }
226
227 public int readString (TTransport in,
228 TString s) throws TException {
Mark Sleef3c322b2006-06-26 23:52:22 +0000229 Int32 size = new Int32();
230 int recv = readI32(in, size);
231 byte[] buf = new byte[size.get()];
232 in.readAll(buf, 0, size.get());
Mark Slee83c52a82006-06-07 06:51:18 +0000233 s.value = new String(buf);
Mark Sleef3c322b2006-06-26 23:52:22 +0000234 return recv + size.get();
Mark Slee83c52a82006-06-07 06:51:18 +0000235 }
236}