blob: 0606d9b4b8ff2573a69e79529872e4e5a97c123a [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.
43 *
44 * @param protocolFactory Factory to create a protocol
45 */
Mark Slee844ac122007-11-27 08:38:52 +000046 public TSerializer() {
47 this(new TBinaryProtocol.Factory());
48 }
49
Mark Sleefe6ed0d2007-11-27 21:54:38 +000050 /**
51 * Create a new TSerializer. It will use the TProtocol specified by the
52 * factory that is passed in.
53 *
54 * @param protocolFactory Factory to create a protocol
55 */
Mark Slee844ac122007-11-27 08:38:52 +000056 public TSerializer(TProtocolFactory protocolFactory) {
57 protocol_ = protocolFactory.getProtocol(transport_);
58 }
59
Mark Sleefe6ed0d2007-11-27 21:54:38 +000060 /**
61 * Serialize the Thrift object into a byte array. The process is simple,
62 * just clear the byte array output, write the object into it, and grab the
63 * raw bytes.
64 *
65 * @param base The object to serialize
66 * @return Serialized object in byte[] format
67 */
Mark Slee844ac122007-11-27 08:38:52 +000068 public byte[] serialize(TBase base) throws TException {
Mark Sleefe6ed0d2007-11-27 21:54:38 +000069 baos_.reset();
Mark Slee844ac122007-11-27 08:38:52 +000070 base.write(protocol_);
Mark Sleefe6ed0d2007-11-27 21:54:38 +000071 return baos_.toByteArray();
Mark Slee844ac122007-11-27 08:38:52 +000072 }
73
Mark Sleefe6ed0d2007-11-27 21:54:38 +000074 /**
75 * Serialize the Thrift object into a Java string, using a specified
76 * character set for encoding.
77 *
78 * @param base The object to serialize
79 * @param charset Valid JVM charset
80 * @return Serialized object as a String
81 */
Mark Slee844ac122007-11-27 08:38:52 +000082 public String toString(TBase base, String charset) throws TException {
83 try {
84 return new String(serialize(base), charset);
85 } catch (UnsupportedEncodingException uex) {
Mark Sleefe6ed0d2007-11-27 21:54:38 +000086 throw new TException("JVM DOES NOT SUPPORT ENCODING: " + charset);
Mark Slee844ac122007-11-27 08:38:52 +000087 }
88 }
89
Mark Sleefe6ed0d2007-11-27 21:54:38 +000090 /**
91 * Serialize the Thrift object into a Java string, using the default JVM
92 * charset encoding.
93 *
94 * @param base The object to serialize
95 * @return Serialized object as a String
96 */
Mark Slee844ac122007-11-27 08:38:52 +000097 public String toString(TBase base) throws TException {
98 return new String(serialize(base));
99 }
100}
101