| 30 |
ashish |
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.ByteArrayInputStream;
|
|
|
23 |
import java.io.UnsupportedEncodingException;
|
|
|
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.TIOStreamTransport;
|
|
|
32 |
import org.apache.thrift.TFieldIdEnum;
|
|
|
33 |
|
|
|
34 |
/**
|
|
|
35 |
* Generic utility for easily deserializing objects from a byte array or Java
|
|
|
36 |
* String.
|
|
|
37 |
*
|
|
|
38 |
*/
|
|
|
39 |
public class TDeserializer {
|
|
|
40 |
private final TProtocolFactory protocolFactory_;
|
|
|
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 |
protocolFactory_ = protocolFactory;
|
|
|
57 |
}
|
|
|
58 |
|
|
|
59 |
/**
|
|
|
60 |
* Deserialize the Thrift object from a byte array.
|
|
|
61 |
*
|
|
|
62 |
* @param base The object to read into
|
|
|
63 |
* @param bytes The array to read from
|
|
|
64 |
*/
|
|
|
65 |
public void deserialize(TBase base, byte[] bytes) throws TException {
|
|
|
66 |
base.read(
|
|
|
67 |
protocolFactory_.getProtocol(
|
|
|
68 |
new TIOStreamTransport(
|
|
|
69 |
new ByteArrayInputStream(bytes))));
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
/**
|
|
|
73 |
* Deserialize the Thrift object from a Java string, using a specified
|
|
|
74 |
* character set for decoding.
|
|
|
75 |
*
|
|
|
76 |
* @param base The object to read into
|
|
|
77 |
* @param data The string to read from
|
|
|
78 |
* @param charset Valid JVM charset
|
|
|
79 |
*/
|
|
|
80 |
public void deserialize(TBase base, String data, String charset) throws TException {
|
|
|
81 |
try {
|
|
|
82 |
deserialize(base, data.getBytes(charset));
|
|
|
83 |
} catch (UnsupportedEncodingException uex) {
|
|
|
84 |
throw new TException("JVM DOES NOT SUPPORT ENCODING: " + charset);
|
|
|
85 |
}
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
/**
|
|
|
89 |
* Deserialize only a single Thrift object (addressed by recursively using field id)
|
|
|
90 |
* from a byte record.
|
|
|
91 |
* @param record The object to read from
|
|
|
92 |
* @param tb The object to read into
|
|
|
93 |
* @param fieldIdPath The FieldId's that define a path tb
|
|
|
94 |
* @throws TException
|
|
|
95 |
*/
|
|
|
96 |
public void partialDeserialize(TBase tb, byte[] bytes, TFieldIdEnum ... fieldIdPath) throws TException {
|
|
|
97 |
// if there are no elements in the path, then the user is looking for the
|
|
|
98 |
// regular deserialize method
|
|
|
99 |
// TODO: it might be nice not to have to do this check every time to save
|
|
|
100 |
// some performance.
|
|
|
101 |
if (fieldIdPath.length == 0) {
|
|
|
102 |
deserialize(tb, bytes);
|
|
|
103 |
return;
|
|
|
104 |
}
|
|
|
105 |
|
|
|
106 |
TProtocol iprot = protocolFactory_.getProtocol(
|
|
|
107 |
new TIOStreamTransport(
|
|
|
108 |
new ByteArrayInputStream(bytes)));
|
|
|
109 |
|
|
|
110 |
// index into field ID path being currently searched for
|
|
|
111 |
int curPathIndex = 0;
|
|
|
112 |
|
|
|
113 |
iprot.readStructBegin();
|
|
|
114 |
|
|
|
115 |
while (curPathIndex < fieldIdPath.length) {
|
|
|
116 |
TField field = iprot.readFieldBegin();
|
|
|
117 |
// we can stop searching if we either see a stop or we go past the field
|
|
|
118 |
// id we're looking for (since fields should now be serialized in asc
|
|
|
119 |
// order).
|
|
|
120 |
if (field.type == TType.STOP || field.id > fieldIdPath[curPathIndex].getThriftFieldId()) {
|
|
|
121 |
return;
|
|
|
122 |
}
|
|
|
123 |
|
|
|
124 |
if (field.id != fieldIdPath[curPathIndex].getThriftFieldId()) {
|
|
|
125 |
// Not the field we're looking for. Skip field.
|
|
|
126 |
TProtocolUtil.skip(iprot, field.type);
|
|
|
127 |
iprot.readFieldEnd();
|
|
|
128 |
} else {
|
|
|
129 |
// This field is the next step in the path. Step into field.
|
|
|
130 |
curPathIndex++;
|
|
|
131 |
if (curPathIndex < fieldIdPath.length) {
|
|
|
132 |
iprot.readStructBegin();
|
|
|
133 |
}
|
|
|
134 |
}
|
|
|
135 |
}
|
|
|
136 |
|
|
|
137 |
// when this line is reached, iprot will be positioned at the start of tb.
|
|
|
138 |
tb.read(iprot);
|
|
|
139 |
}
|
|
|
140 |
|
|
|
141 |
/**
|
|
|
142 |
* Deserialize the Thrift object from a Java string, using the default JVM
|
|
|
143 |
* charset encoding.
|
|
|
144 |
*
|
|
|
145 |
* @param base The object to read into
|
|
|
146 |
* @param data The string to read from
|
|
|
147 |
*/
|
|
|
148 |
public void toString(TBase base, String data) throws TException {
|
|
|
149 |
deserialize(base, data.getBytes());
|
|
|
150 |
}
|
|
|
151 |
}
|
|
|
152 |
|