blob: e9dd8824f43183bc7844c12ee56694965b478431 [file] [log] [blame]
Mark Slee844ac122007-11-27 08:38:52 +00001// 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
7package com.facebook.thrift;
8
9import java.io.ByteArrayOutputStream;
10import java.io.UnsupportedEncodingException;
11
12import com.facebook.thrift.protocol.TBinaryProtocol;
13import com.facebook.thrift.protocol.TProtocol;
14import com.facebook.thrift.protocol.TProtocolFactory;
15import com.facebook.thrift.transport.TIOStreamTransport;
16import com.facebook.thrift.transport.TTransport;
17
18/**
Mark Sleefe6ed0d2007-11-27 21:54:38 +000019 * Generic utility for easily serializing objects into a byte array or Java
20 * String.
Mark Slee844ac122007-11-27 08:38:52 +000021 *
22 * @author Mark Slee <mcslee@facebook.com>
23 */
24public class TSerializer {
25
Mark Sleefe6ed0d2007-11-27 21:54:38 +000026 /**
27 * This is the byte array that data is actually serialized into
28 */
29 private final ByteArrayOutputStream baos_ = new ByteArrayOutputStream();
Mark Slee844ac122007-11-27 08:38:52 +000030
Mark Sleefe6ed0d2007-11-27 21:54:38 +000031 /**
32 * This transport wraps that byte array
33 */
34 private final TIOStreamTransport transport_ = new TIOStreamTransport(baos_);
Mark Slee844ac122007-11-27 08:38:52 +000035
Mark Sleefe6ed0d2007-11-27 21:54:38 +000036 /**
37 * Internal protocol used for serializing objects.
38 */
Mark Slee844ac122007-11-27 08:38:52 +000039 private TProtocol protocol_;
40
Mark Sleefe6ed0d2007-11-27 21:54:38 +000041 /**
42 * Create a new TSerializer that uses the TBinaryProtocol by default.
Mark Sleefe6ed0d2007-11-27 21:54:38 +000043 */
Mark Slee844ac122007-11-27 08:38:52 +000044 public TSerializer() {
45 this(new TBinaryProtocol.Factory());
46 }
47
Mark Sleefe6ed0d2007-11-27 21:54:38 +000048 /**
49 * Create a new TSerializer. It will use the TProtocol specified by the
50 * factory that is passed in.
51 *
52 * @param protocolFactory Factory to create a protocol
53 */
Mark Slee844ac122007-11-27 08:38:52 +000054 public TSerializer(TProtocolFactory protocolFactory) {
55 protocol_ = protocolFactory.getProtocol(transport_);
56 }
57
Mark Sleefe6ed0d2007-11-27 21:54:38 +000058 /**
59 * Serialize the Thrift object into a byte array. The process is simple,
60 * just clear the byte array output, write the object into it, and grab the
61 * raw bytes.
62 *
63 * @param base The object to serialize
64 * @return Serialized object in byte[] format
65 */
Mark Slee844ac122007-11-27 08:38:52 +000066 public byte[] serialize(TBase base) throws TException {
Mark Sleefe6ed0d2007-11-27 21:54:38 +000067 baos_.reset();
Mark Slee844ac122007-11-27 08:38:52 +000068 base.write(protocol_);
Mark Sleefe6ed0d2007-11-27 21:54:38 +000069 return baos_.toByteArray();
Mark Slee844ac122007-11-27 08:38:52 +000070 }
71
Mark Sleefe6ed0d2007-11-27 21:54:38 +000072 /**
73 * Serialize the Thrift object into a Java string, using a specified
74 * character set for encoding.
75 *
76 * @param base The object to serialize
77 * @param charset Valid JVM charset
78 * @return Serialized object as a String
79 */
Mark Slee844ac122007-11-27 08:38:52 +000080 public String toString(TBase base, String charset) throws TException {
81 try {
82 return new String(serialize(base), charset);
83 } catch (UnsupportedEncodingException uex) {
Mark Sleefe6ed0d2007-11-27 21:54:38 +000084 throw new TException("JVM DOES NOT SUPPORT ENCODING: " + charset);
Mark Slee844ac122007-11-27 08:38:52 +000085 }
86 }
87
Mark Sleefe6ed0d2007-11-27 21:54:38 +000088 /**
89 * Serialize the Thrift object into a Java string, using the default JVM
90 * charset encoding.
91 *
92 * @param base The object to serialize
93 * @return Serialized object as a String
94 */
Mark Slee844ac122007-11-27 08:38:52 +000095 public String toString(TBase base) throws TException {
96 return new String(serialize(base));
97 }
98}
99