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
10 * http://www.apache.org/licenses/LICENSE-2.0
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
20 package org
.apache
.thrift
;
22 import java
.io
.ByteArrayOutputStream
;
23 import java
.io
.UnsupportedEncodingException
;
25 import org
.apache
.thrift
.protocol
.TBinaryProtocol
;
26 import org
.apache
.thrift
.protocol
.TProtocol
;
27 import org
.apache
.thrift
.protocol
.TProtocolFactory
;
28 import org
.apache
.thrift
.transport
.TIOStreamTransport
;
31 * Generic utility for easily serializing objects into a byte array or Java
35 public class TSerializer
{
38 * This is the byte array that data is actually serialized into
40 private final ByteArrayOutputStream baos_
= new ByteArrayOutputStream();
43 * This transport wraps that byte array
45 private final TIOStreamTransport transport_
= new TIOStreamTransport(baos_
);
48 * Internal protocol used for serializing objects.
50 private TProtocol protocol_
;
53 * Create a new TSerializer that uses the TBinaryProtocol by default.
55 public TSerializer() {
56 this(new TBinaryProtocol
.Factory());
60 * Create a new TSerializer. It will use the TProtocol specified by the
61 * factory that is passed in.
63 * @param protocolFactory Factory to create a protocol
65 public TSerializer(TProtocolFactory protocolFactory
) {
66 protocol_
= protocolFactory
.getProtocol(transport_
);
70 * Serialize the Thrift object into a byte array. The process is simple,
71 * just clear the byte array output, write the object into it, and grab the
74 * @param base The object to serialize
75 * @return Serialized object in byte[] format
77 public byte[] serialize(TBase base
) throws TException
{
79 base
.write(protocol_
);
80 return baos_
.toByteArray();
84 * Serialize the Thrift object into a Java string, using a specified
85 * character set for encoding.
87 * @param base The object to serialize
88 * @param charset Valid JVM charset
89 * @return Serialized object as a String
91 public String
toString(TBase base
, String charset
) throws TException
{
93 return new String(serialize(base
), charset
);
94 } catch (UnsupportedEncodingException uex
) {
95 throw new TException("JVM DOES NOT SUPPORT ENCODING: " + charset
);
100 * Serialize the Thrift object into a Java string, using the default JVM
103 * @param base The object to serialize
104 * @return Serialized object as a String
106 public String
toString(TBase base
) throws TException
{
107 return new String(serialize(base
));