]>
Commit | Line | Data |
---|---|---|
f67539c2 TL |
1 | /* |
2 | * Licensed to the Apache Software Foundation (ASF) under one | |
3 | * or more contributor license agreements. See the NOTICE file | |
4 | * distributed with this work for additional information | |
5 | * regarding copyright ownership. The ASF licenses this file | |
6 | * to you under the Apache License, Version 2.0 (the | |
7 | * "License"); you may not use this file except in compliance | |
8 | * with the License. You may obtain a copy of the License at | |
9 | * | |
10 | * http://www.apache.org/licenses/LICENSE-2.0 | |
11 | * | |
12 | * Unless required by applicable law or agreed to in writing, | |
13 | * software distributed under the License is distributed on an | |
14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
15 | * KIND, either express or implied. See the License for the | |
16 | * specific language governing permissions and limitations | |
17 | * under the License. | |
18 | */ | |
19 | ||
20 | package org.apache.thrift; | |
21 | ||
22 | import java.io.UnsupportedEncodingException; | |
23 | import java.nio.ByteBuffer; | |
24 | ||
25 | import org.apache.thrift.protocol.TBinaryProtocol; | |
26 | import org.apache.thrift.protocol.TField; | |
27 | import org.apache.thrift.protocol.TProtocol; | |
28 | import org.apache.thrift.protocol.TProtocolFactory; | |
29 | import org.apache.thrift.protocol.TProtocolUtil; | |
30 | import org.apache.thrift.protocol.TType; | |
31 | import org.apache.thrift.transport.TMemoryInputTransport; | |
32 | ||
33 | /** | |
34 | * Generic utility for easily deserializing objects from a byte array or Java | |
35 | * String. | |
36 | * | |
37 | */ | |
38 | public class TDeserializer { | |
39 | private final TProtocol protocol_; | |
40 | private final TMemoryInputTransport trans_; | |
41 | ||
42 | /** | |
43 | * Create a new TDeserializer that uses the TBinaryProtocol by default. | |
44 | */ | |
45 | public TDeserializer() { | |
46 | this(new TBinaryProtocol.Factory()); | |
47 | } | |
48 | ||
49 | /** | |
50 | * Create a new TDeserializer. It will use the TProtocol specified by the | |
51 | * factory that is passed in. | |
52 | * | |
53 | * @param protocolFactory Factory to create a protocol | |
54 | */ | |
55 | public TDeserializer(TProtocolFactory protocolFactory) { | |
56 | trans_ = new TMemoryInputTransport(); | |
57 | protocol_ = protocolFactory.getProtocol(trans_); | |
58 | } | |
59 | ||
60 | /** | |
61 | * Deserialize the Thrift object from a byte array. | |
62 | * | |
63 | * @param base The object to read into | |
64 | * @param bytes The array to read from | |
65 | */ | |
66 | public void deserialize(TBase base, byte[] bytes) throws TException { | |
67 | deserialize(base, bytes, 0, bytes.length); | |
68 | } | |
69 | ||
70 | /** | |
71 | * Deserialize the Thrift object from a byte array. | |
72 | * | |
73 | * @param base The object to read into | |
74 | * @param bytes The array to read from | |
75 | * @param offset The offset into {@code bytes} | |
76 | * @param length The length to read from {@code bytes} | |
77 | */ | |
78 | public void deserialize(TBase base, byte[] bytes, int offset, int length) throws TException { | |
79 | try { | |
80 | trans_.reset(bytes, offset, length); | |
81 | base.read(protocol_); | |
82 | } finally { | |
83 | trans_.clear(); | |
84 | protocol_.reset(); | |
85 | } | |
86 | } | |
87 | ||
88 | /** | |
89 | * Deserialize the Thrift object from a Java string, using a specified | |
90 | * character set for decoding. | |
91 | * | |
92 | * @param base The object to read into | |
93 | * @param data The string to read from | |
94 | * @param charset Valid JVM charset | |
95 | */ | |
96 | public void deserialize(TBase base, String data, String charset) throws TException { | |
97 | try { | |
98 | deserialize(base, data.getBytes(charset)); | |
99 | } catch (UnsupportedEncodingException uex) { | |
100 | throw new TException("JVM DOES NOT SUPPORT ENCODING: " + charset); | |
101 | } finally { | |
102 | protocol_.reset(); | |
103 | } | |
104 | } | |
105 | ||
106 | /** | |
107 | * Deserialize only a single Thrift object (addressed by recursively using field id) | |
108 | * from a byte record. | |
109 | * @param tb The object to read into | |
110 | * @param bytes The serialized object to read from | |
111 | * @param fieldIdPathFirst First of the FieldId's that define a path tb | |
112 | * @param fieldIdPathRest The rest FieldId's that define a path tb | |
113 | * @throws TException | |
114 | */ | |
115 | public void partialDeserialize(TBase tb, byte[] bytes, TFieldIdEnum fieldIdPathFirst, TFieldIdEnum ... fieldIdPathRest) throws TException { | |
116 | try { | |
117 | if (locateField(bytes, fieldIdPathFirst, fieldIdPathRest) != null) { | |
118 | // if this line is reached, iprot will be positioned at the start of tb. | |
119 | tb.read(protocol_); | |
120 | } | |
121 | } catch (Exception e) { | |
122 | throw new TException(e); | |
123 | } finally { | |
124 | trans_.clear(); | |
125 | protocol_.reset(); | |
126 | } | |
127 | } | |
128 | ||
129 | /** | |
130 | * Deserialize only a boolean field (addressed by recursively using field id) | |
131 | * from a byte record. | |
132 | * @param bytes The serialized object to read from | |
133 | * @param fieldIdPathFirst First of the FieldId's that define a path to a boolean field | |
134 | * @param fieldIdPathRest The rest FieldId's that define a path to a boolean field | |
135 | * @throws TException | |
136 | */ | |
137 | public Boolean partialDeserializeBool(byte[] bytes, TFieldIdEnum fieldIdPathFirst, TFieldIdEnum ... fieldIdPathRest) throws TException { | |
138 | return (Boolean) partialDeserializeField(TType.BOOL, bytes, fieldIdPathFirst, fieldIdPathRest); | |
139 | } | |
140 | ||
141 | /** | |
142 | * Deserialize only a byte field (addressed by recursively using field id) | |
143 | * from a byte record. | |
144 | * @param bytes The serialized object to read from | |
145 | * @param fieldIdPathFirst First of the FieldId's that define a path to a byte field | |
146 | * @param fieldIdPathRest The rest FieldId's that define a path to a byte field | |
147 | * @throws TException | |
148 | */ | |
149 | public Byte partialDeserializeByte(byte[] bytes, TFieldIdEnum fieldIdPathFirst, TFieldIdEnum ... fieldIdPathRest) throws TException { | |
150 | return (Byte) partialDeserializeField(TType.BYTE, bytes, fieldIdPathFirst, fieldIdPathRest); | |
151 | } | |
152 | ||
153 | /** | |
154 | * Deserialize only a double field (addressed by recursively using field id) | |
155 | * from a byte record. | |
156 | * @param bytes The serialized object to read from | |
157 | * @param fieldIdPathFirst First of the FieldId's that define a path to a double field | |
158 | * @param fieldIdPathRest The rest FieldId's that define a path to a double field | |
159 | * @throws TException | |
160 | */ | |
161 | public Double partialDeserializeDouble(byte[] bytes, TFieldIdEnum fieldIdPathFirst, TFieldIdEnum ... fieldIdPathRest) throws TException { | |
162 | return (Double) partialDeserializeField(TType.DOUBLE, bytes, fieldIdPathFirst, fieldIdPathRest); | |
163 | } | |
164 | ||
165 | /** | |
166 | * Deserialize only an i16 field (addressed by recursively using field id) | |
167 | * from a byte record. | |
168 | * @param bytes The serialized object to read from | |
169 | * @param fieldIdPathFirst First of the FieldId's that define a path to an i16 field | |
170 | * @param fieldIdPathRest The rest FieldId's that define a path to an i16 field | |
171 | * @throws TException | |
172 | */ | |
173 | public Short partialDeserializeI16(byte[] bytes, TFieldIdEnum fieldIdPathFirst, TFieldIdEnum ... fieldIdPathRest) throws TException { | |
174 | return (Short) partialDeserializeField(TType.I16, bytes, fieldIdPathFirst, fieldIdPathRest); | |
175 | } | |
176 | ||
177 | /** | |
178 | * Deserialize only an i32 field (addressed by recursively using field id) | |
179 | * from a byte record. | |
180 | * @param bytes The serialized object to read from | |
181 | * @param fieldIdPathFirst First of the FieldId's that define a path to an i32 field | |
182 | * @param fieldIdPathRest The rest FieldId's that define a path to an i32 field | |
183 | * @throws TException | |
184 | */ | |
185 | public Integer partialDeserializeI32(byte[] bytes, TFieldIdEnum fieldIdPathFirst, TFieldIdEnum ... fieldIdPathRest) throws TException { | |
186 | return (Integer) partialDeserializeField(TType.I32, bytes, fieldIdPathFirst, fieldIdPathRest); | |
187 | } | |
188 | ||
189 | /** | |
190 | * Deserialize only an i64 field (addressed by recursively using field id) | |
191 | * from a byte record. | |
192 | * @param bytes The serialized object to read from | |
193 | * @param fieldIdPathFirst First of the FieldId's that define a path to an i64 field | |
194 | * @param fieldIdPathRest The rest FieldId's that define a path to an i64 field | |
195 | * @throws TException | |
196 | */ | |
197 | public Long partialDeserializeI64(byte[] bytes, TFieldIdEnum fieldIdPathFirst, TFieldIdEnum ... fieldIdPathRest) throws TException { | |
198 | return (Long) partialDeserializeField(TType.I64, bytes, fieldIdPathFirst, fieldIdPathRest); | |
199 | } | |
200 | ||
201 | /** | |
202 | * Deserialize only a string field (addressed by recursively using field id) | |
203 | * from a byte record. | |
204 | * @param bytes The serialized object to read from | |
205 | * @param fieldIdPathFirst First of the FieldId's that define a path to a string field | |
206 | * @param fieldIdPathRest The rest FieldId's that define a path to a string field | |
207 | * @throws TException | |
208 | */ | |
209 | public String partialDeserializeString(byte[] bytes, TFieldIdEnum fieldIdPathFirst, TFieldIdEnum ... fieldIdPathRest) throws TException { | |
210 | return (String) partialDeserializeField(TType.STRING, bytes, fieldIdPathFirst, fieldIdPathRest); | |
211 | } | |
212 | ||
213 | /** | |
214 | * Deserialize only a binary field (addressed by recursively using field id) | |
215 | * from a byte record. | |
216 | * @param bytes The serialized object to read from | |
217 | * @param fieldIdPathFirst First of the FieldId's that define a path to a binary field | |
218 | * @param fieldIdPathRest The rest FieldId's that define a path to a binary field | |
219 | * @throws TException | |
220 | */ | |
221 | public ByteBuffer partialDeserializeByteArray(byte[] bytes, TFieldIdEnum fieldIdPathFirst, TFieldIdEnum ... fieldIdPathRest) throws TException { | |
222 | // TType does not have binary, so we use the arbitrary num 100 | |
223 | return (ByteBuffer) partialDeserializeField((byte)100, bytes, fieldIdPathFirst, fieldIdPathRest); | |
224 | } | |
225 | ||
226 | /** | |
227 | * Deserialize only the id of the field set in a TUnion (addressed by recursively using field id) | |
228 | * from a byte record. | |
229 | * @param bytes The serialized object to read from | |
230 | * @param fieldIdPathFirst First of the FieldId's that define a path to a TUnion | |
231 | * @param fieldIdPathRest The rest FieldId's that define a path to a TUnion | |
232 | * @throws TException | |
233 | */ | |
234 | public Short partialDeserializeSetFieldIdInUnion(byte[] bytes, TFieldIdEnum fieldIdPathFirst, TFieldIdEnum ... fieldIdPathRest) throws TException { | |
235 | try { | |
236 | TField field = locateField(bytes, fieldIdPathFirst, fieldIdPathRest); | |
237 | if (field != null){ | |
238 | protocol_.readStructBegin(); // The Union | |
239 | return protocol_.readFieldBegin().id; // The field set in the union | |
240 | } | |
241 | return null; | |
242 | } catch (Exception e) { | |
243 | throw new TException(e); | |
244 | } finally { | |
245 | trans_.clear(); | |
246 | protocol_.reset(); | |
247 | } | |
248 | } | |
249 | ||
250 | private Object partialDeserializeField(byte ttype, byte[] bytes, TFieldIdEnum fieldIdPathFirst, TFieldIdEnum ... fieldIdPathRest) throws TException { | |
251 | try { | |
252 | TField field = locateField(bytes, fieldIdPathFirst, fieldIdPathRest); | |
253 | if (field != null) { | |
254 | if (ttype == field.type) { | |
255 | // if this point is reached, iprot will be positioned at the start of | |
256 | // the field | |
257 | switch (ttype) { | |
258 | case TType.BOOL: | |
259 | return protocol_.readBool(); | |
260 | case TType.BYTE: | |
261 | return protocol_.readByte(); | |
262 | case TType.DOUBLE: | |
263 | return protocol_.readDouble(); | |
264 | case TType.I16: | |
265 | return protocol_.readI16(); | |
266 | case TType.I32: | |
267 | return protocol_.readI32(); | |
268 | case TType.I64: | |
269 | return protocol_.readI64(); | |
270 | case TType.STRING: | |
271 | return protocol_.readString(); | |
272 | default: | |
273 | return null; | |
274 | } | |
275 | } | |
276 | // hack to differentiate between string and binary | |
277 | if (ttype == 100 && field.type == TType.STRING) { | |
278 | return protocol_.readBinary(); | |
279 | } | |
280 | } | |
281 | return null; | |
282 | } catch (Exception e) { | |
283 | throw new TException(e); | |
284 | } finally { | |
285 | trans_.clear(); | |
286 | protocol_.reset(); | |
287 | } | |
288 | } | |
289 | ||
290 | private TField locateField(byte[] bytes, TFieldIdEnum fieldIdPathFirst, TFieldIdEnum ... fieldIdPathRest) throws TException { | |
291 | trans_.reset(bytes); | |
292 | ||
293 | TFieldIdEnum[] fieldIdPath = new TFieldIdEnum[fieldIdPathRest.length + 1]; | |
294 | fieldIdPath[0] = fieldIdPathFirst; | |
295 | System.arraycopy(fieldIdPathRest, 0, fieldIdPath, 1, fieldIdPathRest.length); | |
296 | ||
297 | // index into field ID path being currently searched for | |
298 | int curPathIndex = 0; | |
299 | ||
300 | // this will be the located field, or null if it is not located | |
301 | TField field = null; | |
302 | ||
303 | protocol_.readStructBegin(); | |
304 | ||
305 | while (curPathIndex < fieldIdPath.length) { | |
306 | field = protocol_.readFieldBegin(); | |
307 | // we can stop searching if we either see a stop or we go past the field | |
308 | // id we're looking for (since fields should now be serialized in asc | |
309 | // order). | |
310 | if (field.type == TType.STOP || field.id > fieldIdPath[curPathIndex].getThriftFieldId()) { | |
311 | return null; | |
312 | } | |
313 | ||
314 | if (field.id != fieldIdPath[curPathIndex].getThriftFieldId()) { | |
315 | // Not the field we're looking for. Skip field. | |
316 | TProtocolUtil.skip(protocol_, field.type); | |
317 | protocol_.readFieldEnd(); | |
318 | } else { | |
319 | // This field is the next step in the path. Step into field. | |
320 | curPathIndex++; | |
321 | if (curPathIndex < fieldIdPath.length) { | |
322 | protocol_.readStructBegin(); | |
323 | } | |
324 | } | |
325 | } | |
326 | return field; | |
327 | } | |
328 | ||
329 | /** | |
330 | * Deserialize the Thrift object from a Java string, using the default JVM | |
331 | * charset encoding. | |
332 | * | |
333 | * @param base The object to read into | |
334 | * @param data The string to read from | |
335 | */ | |
336 | public void fromString(TBase base, String data) throws TException { | |
337 | deserialize(base, data.getBytes()); | |
338 | } | |
339 | } |