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.TProtocolFactory;
27 import org.apache.thrift.transport.TIOStreamTransport;
28 
29 /**
30  * Generic utility for easily deserializing objects from a byte array or Java
31  * String.
32  *
33  */
34 public class TDeserializer {
35   private final TProtocolFactory protocolFactory_;
36 
37   /**
38    * Create a new TDeserializer that uses the TBinaryProtocol by default.
39    */
TDeserializer()40   public TDeserializer() {
41     this(new TBinaryProtocol.Factory());
42   }
43 
44   /**
45    * Create a new TDeserializer. It will use the TProtocol specified by the
46    * factory that is passed in.
47    *
48    * @param protocolFactory Factory to create a protocol
49    */
TDeserializer(TProtocolFactory protocolFactory)50   public TDeserializer(TProtocolFactory protocolFactory) {
51     protocolFactory_ = protocolFactory;
52   }
53 
54   /**
55    * Deserialize the Thrift object from a byte array.
56    *
57    * @param base The object to read into
58    * @param bytes The array to read from
59    */
deserialize(TBase base, byte[] bytes)60   public void deserialize(TBase base, byte[] bytes) throws TException {
61     base.read(
62         protocolFactory_.getProtocol(
63           new TIOStreamTransport(
64             new ByteArrayInputStream(bytes))));
65   }
66 
67   /**
68    * Deserialize the Thrift object from a Java string, using a specified
69    * character set for decoding.
70    *
71    * @param base The object to read into
72    * @param data The string to read from
73    * @param charset Valid JVM charset
74    */
deserialize(TBase base, String data, String charset)75   public void deserialize(TBase base, String data, String charset) throws TException {
76     try {
77       deserialize(base, data.getBytes(charset));
78     } catch (UnsupportedEncodingException uex) {
79       throw new TException("JVM DOES NOT SUPPORT ENCODING: " + charset);
80     }
81   }
82 
83   /**
84    * Deerialize the Thrift object from a Java string, using the default JVM
85    * charset encoding.
86    *
87    * @param base The object to read into
88    * @param data The string to read from
89    * @return Serialized object as a String
90    */
toString(TBase base, String data)91   public void toString(TBase base, String data) throws TException {
92     deserialize(base, data.getBytes());
93   }
94 }
95 
96