Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 1 | package com.facebook.thrift.protocol; |
| 2 | |
| 3 | import com.facebook.thrift.TException; |
| 4 | import com.facebook.thrift.transport.TTransport; |
| 5 | import com.facebook.thrift.types.*; |
| 6 | |
| 7 | /** |
| 8 | * Binary protocol implementation for thrift. |
| 9 | * |
| 10 | * @author Mark Slee <mcslee@facebook.com> |
| 11 | */ |
| 12 | public 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 Slee | f3c322b | 2006-06-26 23:52:22 +0000 | [diff] [blame^] | 26 | writeI32(out, field.id); |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 27 | } |
| 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 Slee | f3c322b | 2006-06-26 23:52:22 +0000 | [diff] [blame^] | 43 | writeI32(out, map.size); |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 44 | } |
| 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 Slee | f3c322b | 2006-06-26 23:52:22 +0000 | [diff] [blame^] | 54 | writeI32(out, list.size); |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 55 | } |
| 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 Slee | f3c322b | 2006-06-26 23:52:22 +0000 | [diff] [blame^] | 65 | writeI32(out, set.size); |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 66 | } |
| 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 Slee | f3c322b | 2006-06-26 23:52:22 +0000 | [diff] [blame^] | 105 | int sent = writeI32(out, new Int32(dat.length)); |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 106 | 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 Slee | f3c322b | 2006-06-26 23:52:22 +0000 | [diff] [blame^] | 132 | field.id = new Int32(0); |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 133 | return recv; |
| 134 | } |
Mark Slee | f3c322b | 2006-06-26 23:52:22 +0000 | [diff] [blame^] | 135 | recv += readI32(in, field.id); |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 136 | 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 Slee | f3c322b | 2006-06-26 23:52:22 +0000 | [diff] [blame^] | 151 | recv += readI32(in, map.size); |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 152 | 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 Slee | f3c322b | 2006-06-26 23:52:22 +0000 | [diff] [blame^] | 165 | recv += readI32(in, list.size); |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 166 | 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 Slee | f3c322b | 2006-06-26 23:52:22 +0000 | [diff] [blame^] | 179 | recv += readI32(in, set.size); |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 180 | 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 Slee | f3c322b | 2006-06-26 23:52:22 +0000 | [diff] [blame^] | 229 | 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 Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 233 | s.value = new String(buf); |
Mark Slee | f3c322b | 2006-06-26 23:52:22 +0000 | [diff] [blame^] | 234 | return recv + size.get(); |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 235 | } |
| 236 | } |