Mark Slee | 7eb0d63 | 2007-03-01 00:00:27 +0000 | [diff] [blame] | 1 | // Copyright (c) 2006- Facebook |
| 2 | // Distributed under the Thrift Software License |
| 3 | // |
| 4 | // See accompanying file LICENSE or visit the Thrift site at: |
| 5 | // http://developers.facebook.com/thrift/ |
| 6 | |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 7 | package com.facebook.thrift.protocol; |
| 8 | |
| 9 | import com.facebook.thrift.TException; |
| 10 | import com.facebook.thrift.transport.TTransport; |
David Reiss | 4a15eb7 | 2007-12-14 22:14:47 +0000 | [diff] [blame] | 11 | import com.facebook.thrift.transport.TTransportException; |
| 12 | |
Mark Slee | 6eb5892 | 2007-11-07 00:48:47 +0000 | [diff] [blame] | 13 | import java.io.UnsupportedEncodingException; |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 14 | |
| 15 | /** |
| 16 | * Binary protocol implementation for thrift. |
| 17 | * |
| 18 | * @author Mark Slee <mcslee@facebook.com> |
| 19 | */ |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 20 | public class TBinaryProtocol extends TProtocol { |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 21 | |
Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 22 | protected static final int VERSION_MASK = 0xffff0000; |
| 23 | protected static final int VERSION_1 = 0x80010000; |
| 24 | |
| 25 | protected boolean strictRead_ = false; |
| 26 | protected boolean strictWrite_ = true; |
| 27 | |
David Reiss | 4a15eb7 | 2007-12-14 22:14:47 +0000 | [diff] [blame] | 28 | protected int readLength_; |
| 29 | protected boolean checkReadLength_ = false; |
| 30 | |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 31 | /** |
| 32 | * Factory |
| 33 | */ |
| 34 | public static class Factory implements TProtocolFactory { |
Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 35 | protected boolean strictRead_ = false; |
| 36 | protected boolean strictWrite_ = true; |
Mark Slee | 6eb5892 | 2007-11-07 00:48:47 +0000 | [diff] [blame] | 37 | |
Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 38 | public Factory() { |
Mark Slee | a1d4cab | 2007-11-20 04:58:16 +0000 | [diff] [blame] | 39 | this(false, true); |
Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | public Factory(boolean strictRead, boolean strictWrite) { |
| 43 | strictRead_ = strictRead; |
| 44 | strictWrite_ = strictWrite; |
| 45 | } |
| 46 | |
Aditya Agarwal | 5a42958 | 2007-02-06 02:51:15 +0000 | [diff] [blame] | 47 | public TProtocol getProtocol(TTransport trans) { |
Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 48 | return new TBinaryProtocol(trans, strictRead_, strictWrite_); |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 49 | } |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 52 | /** |
| 53 | * Constructor |
| 54 | */ |
Aditya Agarwal | 5a42958 | 2007-02-06 02:51:15 +0000 | [diff] [blame] | 55 | public TBinaryProtocol(TTransport trans) { |
Mark Slee | a1d4cab | 2007-11-20 04:58:16 +0000 | [diff] [blame] | 56 | this(trans, false, true); |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 59 | public TBinaryProtocol(TTransport trans, boolean strictRead, boolean strictWrite) { |
| 60 | super(trans); |
| 61 | strictRead_ = strictRead; |
| 62 | strictWrite_ = strictWrite; |
| 63 | } |
| 64 | |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 65 | public void writeMessageBegin(TMessage message) throws TException { |
Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 66 | if (strictWrite_) { |
| 67 | int version = VERSION_1 | message.type; |
| 68 | writeI32(version); |
| 69 | writeString(message.name); |
| 70 | writeI32(message.seqid); |
| 71 | } else { |
| 72 | writeString(message.name); |
| 73 | writeByte(message.type); |
| 74 | writeI32(message.seqid); |
| 75 | } |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 78 | public void writeMessageEnd() {} |
| 79 | |
| 80 | public void writeStructBegin(TStruct struct) {} |
| 81 | |
| 82 | public void writeStructEnd() {} |
| 83 | |
| 84 | public void writeFieldBegin(TField field) throws TException { |
| 85 | writeByte(field.type); |
| 86 | writeI16(field.id); |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 89 | public void writeFieldEnd() {} |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame] | 90 | |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 91 | public void writeFieldStop() throws TException { |
| 92 | writeByte(TType.STOP); |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 93 | } |
| 94 | |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 95 | public void writeMapBegin(TMap map) throws TException { |
| 96 | writeByte(map.keyType); |
| 97 | writeByte(map.valueType); |
| 98 | writeI32(map.size); |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 101 | public void writeMapEnd() {} |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame] | 102 | |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 103 | public void writeListBegin(TList list) throws TException { |
| 104 | writeByte(list.elemType); |
| 105 | writeI32(list.size); |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 108 | public void writeListEnd() {} |
| 109 | |
| 110 | public void writeSetBegin(TSet set) throws TException { |
| 111 | writeByte(set.elemType); |
| 112 | writeI32(set.size); |
| 113 | } |
| 114 | |
| 115 | public void writeSetEnd() {} |
| 116 | |
| 117 | public void writeBool(boolean b) throws TException { |
| 118 | writeByte(b ? (byte)1 : (byte)0); |
| 119 | } |
| 120 | |
| 121 | private byte [] bout = new byte[1]; |
| 122 | public void writeByte(byte b) throws TException { |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame] | 123 | bout[0] = b; |
Aditya Agarwal | 5a42958 | 2007-02-06 02:51:15 +0000 | [diff] [blame] | 124 | trans_.write(bout, 0, 1); |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 127 | private byte[] i16out = new byte[2]; |
| 128 | public void writeI16(short i16) throws TException { |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 129 | i16out[0] = (byte)(0xff & (i16 >> 8)); |
| 130 | i16out[1] = (byte)(0xff & (i16)); |
Aditya Agarwal | 5a42958 | 2007-02-06 02:51:15 +0000 | [diff] [blame] | 131 | trans_.write(i16out, 0, 2); |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 134 | private byte[] i32out = new byte[4]; |
| 135 | public void writeI32(int i32) throws TException { |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame] | 136 | i32out[0] = (byte)(0xff & (i32 >> 24)); |
| 137 | i32out[1] = (byte)(0xff & (i32 >> 16)); |
| 138 | i32out[2] = (byte)(0xff & (i32 >> 8)); |
| 139 | i32out[3] = (byte)(0xff & (i32)); |
Aditya Agarwal | 5a42958 | 2007-02-06 02:51:15 +0000 | [diff] [blame] | 140 | trans_.write(i32out, 0, 4); |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 143 | private byte[] i64out = new byte[8]; |
| 144 | public void writeI64(long i64) throws TException { |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame] | 145 | i64out[0] = (byte)(0xff & (i64 >> 56)); |
| 146 | i64out[1] = (byte)(0xff & (i64 >> 48)); |
| 147 | i64out[2] = (byte)(0xff & (i64 >> 40)); |
| 148 | i64out[3] = (byte)(0xff & (i64 >> 32)); |
| 149 | i64out[4] = (byte)(0xff & (i64 >> 24)); |
| 150 | i64out[5] = (byte)(0xff & (i64 >> 16)); |
| 151 | i64out[6] = (byte)(0xff & (i64 >> 8)); |
| 152 | i64out[7] = (byte)(0xff & (i64)); |
Aditya Agarwal | 5a42958 | 2007-02-06 02:51:15 +0000 | [diff] [blame] | 153 | trans_.write(i64out, 0, 8); |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 156 | public void writeDouble(double dub) throws TException { |
| 157 | writeI64(Double.doubleToLongBits(dub)); |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 158 | } |
| 159 | |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 160 | public void writeString(String str) throws TException { |
Mark Slee | 6eb5892 | 2007-11-07 00:48:47 +0000 | [diff] [blame] | 161 | try { |
| 162 | byte[] dat = str.getBytes("UTF-8"); |
| 163 | writeI32(dat.length); |
| 164 | trans_.write(dat, 0, dat.length); |
| 165 | } catch (UnsupportedEncodingException uex) { |
| 166 | throw new TException("JVM DOES NOT SUPPORT UTF-8"); |
| 167 | } |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 168 | } |
| 169 | |
Mark Slee | 8d725a2 | 2007-04-13 01:57:12 +0000 | [diff] [blame] | 170 | public void writeBinary(byte[] bin) throws TException { |
| 171 | writeI32(bin.length); |
| 172 | trans_.write(bin, 0, bin.length); |
| 173 | } |
| 174 | |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 175 | /** |
| 176 | * Reading methods. |
| 177 | */ |
| 178 | |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 179 | public TMessage readMessageBegin() throws TException { |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 180 | TMessage message = new TMessage(); |
Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 181 | |
| 182 | int size = readI32(); |
| 183 | if (size < 0) { |
| 184 | int version = size & VERSION_MASK; |
| 185 | if (version != VERSION_1) { |
| 186 | throw new TProtocolException(TProtocolException.BAD_VERSION, "Bad version in readMessageBegin"); |
| 187 | } |
David Reiss | 5ddd2f1 | 2007-12-17 09:49:10 +0000 | [diff] [blame] | 188 | message.type = (byte)(size & 0x000000ff); |
Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 189 | message.name = readString(); |
| 190 | message.seqid = readI32(); |
| 191 | } else { |
| 192 | if (strictRead_) { |
| 193 | throw new TProtocolException(TProtocolException.BAD_VERSION, "Missing version in readMessageBegin, old client?"); |
| 194 | } |
| 195 | message.name = readStringBody(size); |
| 196 | message.type = readByte(); |
| 197 | message.seqid = readI32(); |
| 198 | } |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 199 | return message; |
| 200 | } |
| 201 | |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 202 | public void readMessageEnd() {} |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 203 | |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 204 | public TStruct readStructBegin() { |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame] | 205 | return new TStruct(); |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 206 | } |
| 207 | |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 208 | public void readStructEnd() {} |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 209 | |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 210 | public TField readFieldBegin() throws TException { |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame] | 211 | TField field = new TField(); |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 212 | field.type = readByte(); |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame] | 213 | if (field.type != TType.STOP) { |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 214 | field.id = readI16(); |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 215 | } |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame] | 216 | return field; |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 217 | } |
Mark Slee | 6eb5892 | 2007-11-07 00:48:47 +0000 | [diff] [blame] | 218 | |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 219 | public void readFieldEnd() {} |
Mark Slee | 6eb5892 | 2007-11-07 00:48:47 +0000 | [diff] [blame] | 220 | |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 221 | public TMap readMapBegin() throws TException { |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame] | 222 | TMap map = new TMap(); |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 223 | map.keyType = readByte(); |
| 224 | map.valueType = readByte(); |
| 225 | map.size = readI32(); |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame] | 226 | return map; |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 227 | } |
| 228 | |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 229 | public void readMapEnd() {} |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame] | 230 | |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 231 | public TList readListBegin() throws TException { |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame] | 232 | TList list = new TList(); |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 233 | list.elemType = readByte(); |
| 234 | list.size = readI32(); |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame] | 235 | return list; |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 236 | } |
| 237 | |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 238 | public void readListEnd() {} |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame] | 239 | |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 240 | public TSet readSetBegin() throws TException { |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame] | 241 | TSet set = new TSet(); |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 242 | set.elemType = readByte(); |
| 243 | set.size = readI32(); |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame] | 244 | return set; |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 245 | } |
| 246 | |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 247 | public void readSetEnd() {} |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame] | 248 | |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 249 | public boolean readBool() throws TException { |
| 250 | return (readByte() == 1); |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 253 | private byte[] bin = new byte[1]; |
| 254 | public byte readByte() throws TException { |
David Reiss | 4a15eb7 | 2007-12-14 22:14:47 +0000 | [diff] [blame] | 255 | readAll(bin, 0, 1); |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame] | 256 | return bin[0]; |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 257 | } |
| 258 | |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 259 | private byte[] i16rd = new byte[2]; |
| 260 | public short readI16() throws TException { |
David Reiss | 4a15eb7 | 2007-12-14 22:14:47 +0000 | [diff] [blame] | 261 | readAll(i16rd, 0, 2); |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 262 | return |
| 263 | (short) |
| 264 | (((i16rd[0] & 0xff) << 8) | |
| 265 | ((i16rd[1] & 0xff))); |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 266 | } |
| 267 | |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 268 | private byte[] i32rd = new byte[4]; |
| 269 | public int readI32() throws TException { |
David Reiss | 4a15eb7 | 2007-12-14 22:14:47 +0000 | [diff] [blame] | 270 | readAll(i32rd, 0, 4); |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame] | 271 | return |
| 272 | ((i32rd[0] & 0xff) << 24) | |
| 273 | ((i32rd[1] & 0xff) << 16) | |
| 274 | ((i32rd[2] & 0xff) << 8) | |
| 275 | ((i32rd[3] & 0xff)); |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 276 | } |
Mark Slee | 6eb5892 | 2007-11-07 00:48:47 +0000 | [diff] [blame] | 277 | |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 278 | private byte[] i64rd = new byte[8]; |
| 279 | public long readI64() throws TException { |
David Reiss | 4a15eb7 | 2007-12-14 22:14:47 +0000 | [diff] [blame] | 280 | readAll(i64rd, 0, 8); |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame] | 281 | return |
| 282 | ((long)(i64rd[0] & 0xff) << 56) | |
| 283 | ((long)(i64rd[1] & 0xff) << 48) | |
| 284 | ((long)(i64rd[2] & 0xff) << 40) | |
| 285 | ((long)(i64rd[3] & 0xff) << 32) | |
| 286 | ((long)(i64rd[4] & 0xff) << 24) | |
| 287 | ((long)(i64rd[5] & 0xff) << 16) | |
| 288 | ((long)(i64rd[6] & 0xff) << 8) | |
| 289 | ((long)(i64rd[7] & 0xff)); |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 290 | } |
| 291 | |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 292 | public double readDouble() throws TException { |
| 293 | return Double.longBitsToDouble(readI64()); |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 294 | } |
| 295 | |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 296 | public String readString() throws TException { |
| 297 | int size = readI32(); |
Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 298 | return readStringBody(size); |
| 299 | } |
| 300 | |
| 301 | public String readStringBody(int size) throws TException { |
Mark Slee | 6eb5892 | 2007-11-07 00:48:47 +0000 | [diff] [blame] | 302 | try { |
David Reiss | 4a15eb7 | 2007-12-14 22:14:47 +0000 | [diff] [blame] | 303 | checkReadLength(size); |
Mark Slee | 6eb5892 | 2007-11-07 00:48:47 +0000 | [diff] [blame] | 304 | byte[] buf = new byte[size]; |
| 305 | trans_.readAll(buf, 0, size); |
| 306 | return new String(buf, "UTF-8"); |
| 307 | } catch (UnsupportedEncodingException uex) { |
| 308 | throw new TException("JVM DOES NOT SUPPORT UTF-8"); |
| 309 | } |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 310 | } |
Mark Slee | 8d725a2 | 2007-04-13 01:57:12 +0000 | [diff] [blame] | 311 | |
| 312 | public byte[] readBinary() throws TException { |
| 313 | int size = readI32(); |
David Reiss | 4a15eb7 | 2007-12-14 22:14:47 +0000 | [diff] [blame] | 314 | checkReadLength(size); |
Mark Slee | 8d725a2 | 2007-04-13 01:57:12 +0000 | [diff] [blame] | 315 | byte[] buf = new byte[size]; |
| 316 | trans_.readAll(buf, 0, size); |
| 317 | return buf; |
| 318 | } |
| 319 | |
David Reiss | 4a15eb7 | 2007-12-14 22:14:47 +0000 | [diff] [blame] | 320 | private int readAll(byte[] buf, int off, int len) throws TException { |
| 321 | checkReadLength(len); |
| 322 | return trans_.readAll(buf, off, len); |
| 323 | } |
| 324 | |
| 325 | public void setReadLength(int readLength) { |
| 326 | readLength_ = readLength; |
| 327 | checkReadLength_ = true; |
| 328 | } |
| 329 | |
| 330 | protected void checkReadLength(int length) throws TException { |
| 331 | if (checkReadLength_) { |
| 332 | readLength_ -= length; |
| 333 | if (readLength_ < 0) { |
| 334 | throw new TException("Message length exceeded: " + length); |
| 335 | } |
| 336 | } |
| 337 | } |
| 338 | |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 339 | } |