Mark Slee | 844ac12 | 2007-11-27 08:38:52 +0000 | [diff] [blame] | 1 | // 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 | |
| 7 | package com.facebook.thrift; |
| 8 | |
| 9 | import java.io.ByteArrayOutputStream; |
| 10 | import java.io.UnsupportedEncodingException; |
| 11 | |
| 12 | import com.facebook.thrift.protocol.TBinaryProtocol; |
| 13 | import com.facebook.thrift.protocol.TProtocol; |
| 14 | import com.facebook.thrift.protocol.TProtocolFactory; |
| 15 | import com.facebook.thrift.transport.TIOStreamTransport; |
| 16 | import com.facebook.thrift.transport.TTransport; |
| 17 | |
| 18 | /** |
Mark Slee | fe6ed0d | 2007-11-27 21:54:38 +0000 | [diff] [blame] | 19 | * Generic utility for easily serializing objects into a byte array or Java |
| 20 | * String. |
Mark Slee | 844ac12 | 2007-11-27 08:38:52 +0000 | [diff] [blame] | 21 | * |
| 22 | * @author Mark Slee <mcslee@facebook.com> |
| 23 | */ |
| 24 | public class TSerializer { |
| 25 | |
Mark Slee | fe6ed0d | 2007-11-27 21:54:38 +0000 | [diff] [blame] | 26 | /** |
| 27 | * This is the byte array that data is actually serialized into |
| 28 | */ |
| 29 | private final ByteArrayOutputStream baos_ = new ByteArrayOutputStream(); |
Mark Slee | 844ac12 | 2007-11-27 08:38:52 +0000 | [diff] [blame] | 30 | |
Mark Slee | fe6ed0d | 2007-11-27 21:54:38 +0000 | [diff] [blame] | 31 | /** |
| 32 | * This transport wraps that byte array |
| 33 | */ |
| 34 | private final TIOStreamTransport transport_ = new TIOStreamTransport(baos_); |
Mark Slee | 844ac12 | 2007-11-27 08:38:52 +0000 | [diff] [blame] | 35 | |
Mark Slee | fe6ed0d | 2007-11-27 21:54:38 +0000 | [diff] [blame] | 36 | /** |
| 37 | * Internal protocol used for serializing objects. |
| 38 | */ |
Mark Slee | 844ac12 | 2007-11-27 08:38:52 +0000 | [diff] [blame] | 39 | private TProtocol protocol_; |
| 40 | |
Mark Slee | fe6ed0d | 2007-11-27 21:54:38 +0000 | [diff] [blame] | 41 | /** |
| 42 | * Create a new TSerializer that uses the TBinaryProtocol by default. |
Mark Slee | fe6ed0d | 2007-11-27 21:54:38 +0000 | [diff] [blame] | 43 | */ |
Mark Slee | 844ac12 | 2007-11-27 08:38:52 +0000 | [diff] [blame] | 44 | public TSerializer() { |
| 45 | this(new TBinaryProtocol.Factory()); |
| 46 | } |
| 47 | |
Mark Slee | fe6ed0d | 2007-11-27 21:54:38 +0000 | [diff] [blame] | 48 | /** |
| 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 Slee | 844ac12 | 2007-11-27 08:38:52 +0000 | [diff] [blame] | 54 | public TSerializer(TProtocolFactory protocolFactory) { |
| 55 | protocol_ = protocolFactory.getProtocol(transport_); |
| 56 | } |
| 57 | |
Mark Slee | fe6ed0d | 2007-11-27 21:54:38 +0000 | [diff] [blame] | 58 | /** |
| 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 Slee | 844ac12 | 2007-11-27 08:38:52 +0000 | [diff] [blame] | 66 | public byte[] serialize(TBase base) throws TException { |
Mark Slee | fe6ed0d | 2007-11-27 21:54:38 +0000 | [diff] [blame] | 67 | baos_.reset(); |
Mark Slee | 844ac12 | 2007-11-27 08:38:52 +0000 | [diff] [blame] | 68 | base.write(protocol_); |
Mark Slee | fe6ed0d | 2007-11-27 21:54:38 +0000 | [diff] [blame] | 69 | return baos_.toByteArray(); |
Mark Slee | 844ac12 | 2007-11-27 08:38:52 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Mark Slee | fe6ed0d | 2007-11-27 21:54:38 +0000 | [diff] [blame] | 72 | /** |
| 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 Slee | 844ac12 | 2007-11-27 08:38:52 +0000 | [diff] [blame] | 80 | public String toString(TBase base, String charset) throws TException { |
| 81 | try { |
| 82 | return new String(serialize(base), charset); |
| 83 | } catch (UnsupportedEncodingException uex) { |
Mark Slee | fe6ed0d | 2007-11-27 21:54:38 +0000 | [diff] [blame] | 84 | throw new TException("JVM DOES NOT SUPPORT ENCODING: " + charset); |
Mark Slee | 844ac12 | 2007-11-27 08:38:52 +0000 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | |
Mark Slee | fe6ed0d | 2007-11-27 21:54:38 +0000 | [diff] [blame] | 88 | /** |
| 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 Slee | 844ac12 | 2007-11-27 08:38:52 +0000 | [diff] [blame] | 95 | public String toString(TBase base) throws TException { |
| 96 | return new String(serialize(base)); |
| 97 | } |
| 98 | } |
| 99 | |