blob: a7590058e02151ada8b49b146017e4c34f0964fd [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 Slee530fd662006-08-09 00:05:18 +000012 public void writeStructBegin(TTransport out, TStruct struct) throws TException {}
13
14 public void writeStructEnd(TTransport out) throws TException {}
15
16 public void writeFieldBegin(TTransport out, TField field) throws TException {
17 writeByte(out, field.type);
18 writeI32(out, field.id);
Mark Slee83c52a82006-06-07 06:51:18 +000019 }
20
Mark Slee530fd662006-08-09 00:05:18 +000021 public void writeFieldEnd(TTransport out) throws TException {}
22
23 public void writeFieldStop(TTransport out) throws TException {
24 writeByte(out, TType.STOP);
Mark Slee83c52a82006-06-07 06:51:18 +000025 }
26
Mark Slee530fd662006-08-09 00:05:18 +000027 public void writeMapBegin(TTransport out, TMap map) throws TException {
28 writeByte(out, map.keyType);
29 writeByte(out, map.valueType);
30 writeI32(out, map.size);
Mark Slee83c52a82006-06-07 06:51:18 +000031 }
32
Mark Slee530fd662006-08-09 00:05:18 +000033 public void writeMapEnd(TTransport out) throws TException {}
34
35 public void writeListBegin(TTransport out, TList list) throws TException {
36 writeByte(out, list.elemType);
37 writeI32(out, list.size);
Mark Slee83c52a82006-06-07 06:51:18 +000038 }
39
Mark Slee530fd662006-08-09 00:05:18 +000040 public void writeListEnd(TTransport out) throws TException {}
41
42 public void writeSetBegin(TTransport out, TSet set) throws TException {
43 writeByte(out, set.elemType);
44 writeI32(out, set.size);
Mark Slee83c52a82006-06-07 06:51:18 +000045 }
46
Mark Slee530fd662006-08-09 00:05:18 +000047 public void writeSetEnd(TTransport out) throws TException {}
48
49 byte[] bout = new byte[1];
50 public void writeByte(TTransport out, byte b) throws TException {
51 bout[0] = b;
52 out.write(bout, 0, 1);
Mark Slee83c52a82006-06-07 06:51:18 +000053 }
54
Mark Slee530fd662006-08-09 00:05:18 +000055 public void writeU32(TTransport out, int u32) throws TException {
56 writeI32(out, u32);
Mark Slee83c52a82006-06-07 06:51:18 +000057 }
58
Mark Slee530fd662006-08-09 00:05:18 +000059 byte[] i32out = new byte[4];
60 public void writeI32(TTransport out, int i32) throws TException {
61 i32out[0] = (byte)(0xff & (i32 >> 24));
62 i32out[1] = (byte)(0xff & (i32 >> 16));
63 i32out[2] = (byte)(0xff & (i32 >> 8));
64 i32out[3] = (byte)(0xff & (i32));
65 out.write(i32out, 0, 4);
Mark Slee83c52a82006-06-07 06:51:18 +000066 }
67
Mark Slee530fd662006-08-09 00:05:18 +000068 public void writeU64(TTransport out, long u64) throws TException {
69 writeI64(out, u64);
Mark Slee83c52a82006-06-07 06:51:18 +000070 }
71
Mark Slee530fd662006-08-09 00:05:18 +000072 byte[] i64out = new byte[8];
73 public void writeI64(TTransport out, long i64) throws TException {
74 i64out[0] = (byte)(0xff & (i64 >> 56));
75 i64out[1] = (byte)(0xff & (i64 >> 48));
76 i64out[2] = (byte)(0xff & (i64 >> 40));
77 i64out[3] = (byte)(0xff & (i64 >> 32));
78 i64out[4] = (byte)(0xff & (i64 >> 24));
79 i64out[5] = (byte)(0xff & (i64 >> 16));
80 i64out[6] = (byte)(0xff & (i64 >> 8));
81 i64out[7] = (byte)(0xff & (i64));
82 out.write(i64out, 0, 8);
Mark Slee83c52a82006-06-07 06:51:18 +000083 }
84
Mark Slee530fd662006-08-09 00:05:18 +000085 public void writeString(TTransport out, String str) throws TException {
86 byte[] dat = str.getBytes();
87 writeI32(out, dat.length);
Mark Slee83c52a82006-06-07 06:51:18 +000088 out.write(dat, 0, dat.length);
Mark Slee83c52a82006-06-07 06:51:18 +000089 }
90
91 /**
92 * Reading methods.
93 */
94
Mark Slee530fd662006-08-09 00:05:18 +000095 public TStruct readStructBegin(TTransport in) throws TException {
96 return new TStruct();
Mark Slee83c52a82006-06-07 06:51:18 +000097 }
98
Mark Slee530fd662006-08-09 00:05:18 +000099 public void readStructEnd(TTransport in) throws TException {}
Mark Slee83c52a82006-06-07 06:51:18 +0000100
Mark Slee530fd662006-08-09 00:05:18 +0000101 public TField readFieldBegin(TTransport in) throws TException {
102 TField field = new TField();
103 field.type = readByte(in);
104 if (field.type != TType.STOP) {
105 field.id = readI32(in);
Mark Slee83c52a82006-06-07 06:51:18 +0000106 }
Mark Slee530fd662006-08-09 00:05:18 +0000107 return field;
Mark Slee83c52a82006-06-07 06:51:18 +0000108 }
109
Mark Slee530fd662006-08-09 00:05:18 +0000110 public void readFieldEnd(TTransport in) throws TException {}
Mark Slee83c52a82006-06-07 06:51:18 +0000111
Mark Slee530fd662006-08-09 00:05:18 +0000112 public TMap readMapBegin(TTransport in) throws TException {
113 TMap map = new TMap();
114 map.keyType = readByte(in);
115 map.valueType = readByte(in);
116 map.size = readI32(in);
117 return map;
Mark Slee83c52a82006-06-07 06:51:18 +0000118 }
119
Mark Slee530fd662006-08-09 00:05:18 +0000120 public void readMapEnd(TTransport in) throws TException {}
121
122 public TList readListBegin(TTransport in) throws TException {
123 TList list = new TList();
124 list.elemType = readByte(in);
125 list.size = readI32(in);
126 return list;
Mark Slee83c52a82006-06-07 06:51:18 +0000127 }
128
Mark Slee530fd662006-08-09 00:05:18 +0000129 public void readListEnd(TTransport in) throws TException {}
130
131 public TSet readSetBegin(TTransport in) throws TException {
132 TSet set = new TSet();
133 set.elemType = readByte(in);
134 set.size = readI32(in);
135 return set;
Mark Slee83c52a82006-06-07 06:51:18 +0000136 }
137
Mark Slee530fd662006-08-09 00:05:18 +0000138 public void readSetEnd(TTransport in) throws TException {}
139
140 byte[] bin = new byte[1];
141 public byte readByte(TTransport in) throws TException {
142 in.readAll(bin, 0, 1);
143 return bin[0];
Mark Slee83c52a82006-06-07 06:51:18 +0000144 }
145
Mark Slee530fd662006-08-09 00:05:18 +0000146 public int readU32(TTransport in) throws TException {
147 return readI32(in);
Mark Slee83c52a82006-06-07 06:51:18 +0000148 }
149
Mark Slee530fd662006-08-09 00:05:18 +0000150 byte[] i32rd = new byte[4];
151 public int readI32(TTransport in) throws TException {
152 in.readAll(i32rd, 0, 4);
153 return
154 ((i32rd[0] & 0xff) << 24) |
155 ((i32rd[1] & 0xff) << 16) |
156 ((i32rd[2] & 0xff) << 8) |
157 ((i32rd[3] & 0xff));
Mark Slee83c52a82006-06-07 06:51:18 +0000158 }
159
Mark Slee530fd662006-08-09 00:05:18 +0000160 public long readU64(TTransport in) throws TException {
161 return readI64(in);
Mark Slee83c52a82006-06-07 06:51:18 +0000162 }
163
Mark Slee530fd662006-08-09 00:05:18 +0000164 byte[] i64rd = new byte[8];
165 public long readI64(TTransport in) throws TException {
166 in.readAll(i64rd, 0, 8);
167 return
168 ((long)(i64rd[0] & 0xff) << 56) |
169 ((long)(i64rd[1] & 0xff) << 48) |
170 ((long)(i64rd[2] & 0xff) << 40) |
171 ((long)(i64rd[3] & 0xff) << 32) |
172 ((long)(i64rd[4] & 0xff) << 24) |
173 ((long)(i64rd[5] & 0xff) << 16) |
174 ((long)(i64rd[6] & 0xff) << 8) |
175 ((long)(i64rd[7] & 0xff));
Mark Slee83c52a82006-06-07 06:51:18 +0000176 }
177
Mark Slee530fd662006-08-09 00:05:18 +0000178 public String readString(TTransport in) throws TException {
179 int size = readI32(in);
180 byte[] buf = new byte[size];
181 in.readAll(buf, 0, size);
182 return new String(buf);
Mark Slee83c52a82006-06-07 06:51:18 +0000183 }
184}