blob: b6431bffa19eabb9f905047fd635ecc9866e2c8f [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 {
17 public TProtocol[] getIOProtocols(TTransport in, TTransport out) {
18 TProtocol[] io = new TProtocol[2];
19 io[0] = io[1] = new TBinaryProtocol(in, out);
20 return io;
21 }
Mark Slee78f58e22006-09-02 04:17:07 +000022 }
23
Mark Slee456b7a82006-10-25 20:53:37 +000024 /**
25 * Constructor
26 */
27 public TBinaryProtocol(TTransport in, TTransport out) {
28 super(in, out);
Mark Slee83c52a82006-06-07 06:51:18 +000029 }
30
Mark Slee456b7a82006-10-25 20:53:37 +000031 public void writeMessageBegin(TMessage message) throws TException {
32 writeString(message.name);
33 writeByte(message.type);
34 writeI32(message.seqid);
Mark Slee83c52a82006-06-07 06:51:18 +000035 }
36
Mark Slee456b7a82006-10-25 20:53:37 +000037 public void writeMessageEnd() {}
38
39 public void writeStructBegin(TStruct struct) {}
40
41 public void writeStructEnd() {}
42
43 public void writeFieldBegin(TField field) throws TException {
44 writeByte(field.type);
45 writeI16(field.id);
Mark Slee83c52a82006-06-07 06:51:18 +000046 }
47
Mark Slee456b7a82006-10-25 20:53:37 +000048 public void writeFieldEnd() {}
Mark Slee530fd662006-08-09 00:05:18 +000049
Mark Slee456b7a82006-10-25 20:53:37 +000050 public void writeFieldStop() throws TException {
51 writeByte(TType.STOP);
Mark Slee83c52a82006-06-07 06:51:18 +000052 }
53
Mark Slee456b7a82006-10-25 20:53:37 +000054 public void writeMapBegin(TMap map) throws TException {
55 writeByte(map.keyType);
56 writeByte(map.valueType);
57 writeI32(map.size);
Mark Slee83c52a82006-06-07 06:51:18 +000058 }
59
Mark Slee456b7a82006-10-25 20:53:37 +000060 public void writeMapEnd() {}
Mark Slee530fd662006-08-09 00:05:18 +000061
Mark Slee456b7a82006-10-25 20:53:37 +000062 public void writeListBegin(TList list) throws TException {
63 writeByte(list.elemType);
64 writeI32(list.size);
Mark Slee78f58e22006-09-02 04:17:07 +000065 }
66
Mark Slee456b7a82006-10-25 20:53:37 +000067 public void writeListEnd() {}
68
69 public void writeSetBegin(TSet set) throws TException {
70 writeByte(set.elemType);
71 writeI32(set.size);
72 }
73
74 public void writeSetEnd() {}
75
76 public void writeBool(boolean b) throws TException {
77 writeByte(b ? (byte)1 : (byte)0);
78 }
79
80 private byte [] bout = new byte[1];
81 public void writeByte(byte b) throws TException {
Mark Slee530fd662006-08-09 00:05:18 +000082 bout[0] = b;
Mark Slee456b7a82006-10-25 20:53:37 +000083 outputTransport_.write(bout, 0, 1);
Mark Slee83c52a82006-06-07 06:51:18 +000084 }
85
Mark Slee456b7a82006-10-25 20:53:37 +000086 private byte[] i16out = new byte[2];
87 public void writeI16(short i16) throws TException {
Mark Slee78f58e22006-09-02 04:17:07 +000088 i16out[0] = (byte)(0xff & (i16 >> 8));
89 i16out[1] = (byte)(0xff & (i16));
Mark Slee456b7a82006-10-25 20:53:37 +000090 outputTransport_.write(i16out, 0, 2);
Mark Slee83c52a82006-06-07 06:51:18 +000091 }
92
Mark Slee456b7a82006-10-25 20:53:37 +000093 private byte[] i32out = new byte[4];
94 public void writeI32(int i32) throws TException {
Mark Slee530fd662006-08-09 00:05:18 +000095 i32out[0] = (byte)(0xff & (i32 >> 24));
96 i32out[1] = (byte)(0xff & (i32 >> 16));
97 i32out[2] = (byte)(0xff & (i32 >> 8));
98 i32out[3] = (byte)(0xff & (i32));
Mark Slee456b7a82006-10-25 20:53:37 +000099 outputTransport_.write(i32out, 0, 4);
Mark Slee83c52a82006-06-07 06:51:18 +0000100 }
101
Mark Slee456b7a82006-10-25 20:53:37 +0000102 private byte[] i64out = new byte[8];
103 public void writeI64(long i64) throws TException {
Mark Slee530fd662006-08-09 00:05:18 +0000104 i64out[0] = (byte)(0xff & (i64 >> 56));
105 i64out[1] = (byte)(0xff & (i64 >> 48));
106 i64out[2] = (byte)(0xff & (i64 >> 40));
107 i64out[3] = (byte)(0xff & (i64 >> 32));
108 i64out[4] = (byte)(0xff & (i64 >> 24));
109 i64out[5] = (byte)(0xff & (i64 >> 16));
110 i64out[6] = (byte)(0xff & (i64 >> 8));
111 i64out[7] = (byte)(0xff & (i64));
Mark Slee456b7a82006-10-25 20:53:37 +0000112 outputTransport_.write(i64out, 0, 8);
Mark Slee83c52a82006-06-07 06:51:18 +0000113 }
114
Mark Slee456b7a82006-10-25 20:53:37 +0000115 public void writeDouble(double dub) throws TException {
116 writeI64(Double.doubleToLongBits(dub));
Mark Sleec98d0502006-09-06 02:42:25 +0000117 }
118
Mark Slee456b7a82006-10-25 20:53:37 +0000119 public void writeString(String str) throws TException {
Mark Slee530fd662006-08-09 00:05:18 +0000120 byte[] dat = str.getBytes();
Mark Slee456b7a82006-10-25 20:53:37 +0000121 writeI32(dat.length);
122 outputTransport_.write(dat, 0, dat.length);
Mark Slee83c52a82006-06-07 06:51:18 +0000123 }
124
125 /**
126 * Reading methods.
127 */
128
Mark Slee456b7a82006-10-25 20:53:37 +0000129 public TMessage readMessageBegin() throws TException {
Mark Slee78f58e22006-09-02 04:17:07 +0000130 TMessage message = new TMessage();
Mark Slee456b7a82006-10-25 20:53:37 +0000131 message.name = readString();
132 message.type = readByte();
133 message.seqid = readI32();
Mark Slee78f58e22006-09-02 04:17:07 +0000134 return message;
135 }
136
Mark Slee456b7a82006-10-25 20:53:37 +0000137 public void readMessageEnd() {}
Mark Slee78f58e22006-09-02 04:17:07 +0000138
Mark Slee456b7a82006-10-25 20:53:37 +0000139 public TStruct readStructBegin() {
Mark Slee530fd662006-08-09 00:05:18 +0000140 return new TStruct();
Mark Slee83c52a82006-06-07 06:51:18 +0000141 }
142
Mark Slee456b7a82006-10-25 20:53:37 +0000143 public void readStructEnd() {}
Mark Slee83c52a82006-06-07 06:51:18 +0000144
Mark Slee456b7a82006-10-25 20:53:37 +0000145 public TField readFieldBegin() throws TException {
Mark Slee530fd662006-08-09 00:05:18 +0000146 TField field = new TField();
Mark Slee456b7a82006-10-25 20:53:37 +0000147 field.type = readByte();
Mark Slee530fd662006-08-09 00:05:18 +0000148 if (field.type != TType.STOP) {
Mark Slee456b7a82006-10-25 20:53:37 +0000149 field.id = readI16();
Mark Slee83c52a82006-06-07 06:51:18 +0000150 }
Mark Slee530fd662006-08-09 00:05:18 +0000151 return field;
Mark Slee83c52a82006-06-07 06:51:18 +0000152 }
153
Mark Slee456b7a82006-10-25 20:53:37 +0000154 public void readFieldEnd() {}
Mark Slee83c52a82006-06-07 06:51:18 +0000155
Mark Slee456b7a82006-10-25 20:53:37 +0000156 public TMap readMapBegin() throws TException {
Mark Slee530fd662006-08-09 00:05:18 +0000157 TMap map = new TMap();
Mark Slee456b7a82006-10-25 20:53:37 +0000158 map.keyType = readByte();
159 map.valueType = readByte();
160 map.size = readI32();
Mark Slee530fd662006-08-09 00:05:18 +0000161 return map;
Mark Slee83c52a82006-06-07 06:51:18 +0000162 }
163
Mark Slee456b7a82006-10-25 20:53:37 +0000164 public void readMapEnd() {}
Mark Slee530fd662006-08-09 00:05:18 +0000165
Mark Slee456b7a82006-10-25 20:53:37 +0000166 public TList readListBegin() throws TException {
Mark Slee530fd662006-08-09 00:05:18 +0000167 TList list = new TList();
Mark Slee456b7a82006-10-25 20:53:37 +0000168 list.elemType = readByte();
169 list.size = readI32();
Mark Slee530fd662006-08-09 00:05:18 +0000170 return list;
Mark Slee83c52a82006-06-07 06:51:18 +0000171 }
172
Mark Slee456b7a82006-10-25 20:53:37 +0000173 public void readListEnd() {}
Mark Slee530fd662006-08-09 00:05:18 +0000174
Mark Slee456b7a82006-10-25 20:53:37 +0000175 public TSet readSetBegin() throws TException {
Mark Slee530fd662006-08-09 00:05:18 +0000176 TSet set = new TSet();
Mark Slee456b7a82006-10-25 20:53:37 +0000177 set.elemType = readByte();
178 set.size = readI32();
Mark Slee530fd662006-08-09 00:05:18 +0000179 return set;
Mark Slee83c52a82006-06-07 06:51:18 +0000180 }
181
Mark Slee456b7a82006-10-25 20:53:37 +0000182 public void readSetEnd() {}
Mark Slee530fd662006-08-09 00:05:18 +0000183
Mark Slee456b7a82006-10-25 20:53:37 +0000184 public boolean readBool() throws TException {
185 return (readByte() == 1);
Mark Slee78f58e22006-09-02 04:17:07 +0000186 }
187
Mark Slee456b7a82006-10-25 20:53:37 +0000188 private byte[] bin = new byte[1];
189 public byte readByte() throws TException {
190 inputTransport_.readAll(bin, 0, 1);
Mark Slee530fd662006-08-09 00:05:18 +0000191 return bin[0];
Mark Slee83c52a82006-06-07 06:51:18 +0000192 }
193
Mark Slee456b7a82006-10-25 20:53:37 +0000194 private byte[] i16rd = new byte[2];
195 public short readI16() throws TException {
196 inputTransport_.readAll(i16rd, 0, 2);
Mark Slee78f58e22006-09-02 04:17:07 +0000197 return
198 (short)
199 (((i16rd[0] & 0xff) << 8) |
200 ((i16rd[1] & 0xff)));
Mark Slee83c52a82006-06-07 06:51:18 +0000201 }
202
Mark Slee456b7a82006-10-25 20:53:37 +0000203 private byte[] i32rd = new byte[4];
204 public int readI32() throws TException {
205 inputTransport_.readAll(i32rd, 0, 4);
Mark Slee530fd662006-08-09 00:05:18 +0000206 return
207 ((i32rd[0] & 0xff) << 24) |
208 ((i32rd[1] & 0xff) << 16) |
209 ((i32rd[2] & 0xff) << 8) |
210 ((i32rd[3] & 0xff));
Mark Slee83c52a82006-06-07 06:51:18 +0000211 }
Mark Slee78f58e22006-09-02 04:17:07 +0000212
Mark Slee456b7a82006-10-25 20:53:37 +0000213 private byte[] i64rd = new byte[8];
214 public long readI64() throws TException {
215 inputTransport_.readAll(i64rd, 0, 8);
Mark Slee530fd662006-08-09 00:05:18 +0000216 return
217 ((long)(i64rd[0] & 0xff) << 56) |
218 ((long)(i64rd[1] & 0xff) << 48) |
219 ((long)(i64rd[2] & 0xff) << 40) |
220 ((long)(i64rd[3] & 0xff) << 32) |
221 ((long)(i64rd[4] & 0xff) << 24) |
222 ((long)(i64rd[5] & 0xff) << 16) |
223 ((long)(i64rd[6] & 0xff) << 8) |
224 ((long)(i64rd[7] & 0xff));
Mark Slee83c52a82006-06-07 06:51:18 +0000225 }
226
Mark Slee456b7a82006-10-25 20:53:37 +0000227 public double readDouble() throws TException {
228 return Double.longBitsToDouble(readI64());
Mark Sleec98d0502006-09-06 02:42:25 +0000229 }
230
Mark Slee456b7a82006-10-25 20:53:37 +0000231 public String readString() throws TException {
232 int size = readI32();
Mark Slee530fd662006-08-09 00:05:18 +0000233 byte[] buf = new byte[size];
Mark Slee456b7a82006-10-25 20:53:37 +0000234 inputTransport_.readAll(buf, 0, size);
Mark Slee530fd662006-08-09 00:05:18 +0000235 return new String(buf);
Mark Slee83c52a82006-06-07 06:51:18 +0000236 }
237}