From: Mark Slee Date: Tue, 27 Nov 2007 21:54:38 +0000 (+0000) Subject: Clean up the TSerializer X-Git-Tag: 0.2.0~1103 X-Git-Url: https://source.supwisdom.com/gerrit/gitweb?a=commitdiff_plain;h=fe6ed0dff423a405fabd61e4bef3e490506ba2ba;p=common%2Fthrift.git Clean up the TSerializer Summary: Nested Transport subclass wasn't necessary, add comments Reviewed By: dreiss Test Plan: Same as before, test/java/TestClient git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@665369 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/lib/java/src/TSerializer.java b/lib/java/src/TSerializer.java index 2fb3794d..0606d9b4 100644 --- a/lib/java/src/TSerializer.java +++ b/lib/java/src/TSerializer.java @@ -16,56 +16,84 @@ import com.facebook.thrift.transport.TIOStreamTransport; import com.facebook.thrift.transport.TTransport; /** - * Generic utility for easily serializing objects into a byte array. + * Generic utility for easily serializing objects into a byte array or Java + * String. * * @author Mark Slee */ public class TSerializer { - private static class TByteArrayTransport extends TIOStreamTransport { + /** + * This is the byte array that data is actually serialized into + */ + private final ByteArrayOutputStream baos_ = new ByteArrayOutputStream(); - private final ByteArrayOutputStream baos_ = new ByteArrayOutputStream(); - - public TByteArrayTransport() { - outputStream_ = baos_; - } - - public byte[] get() { - return baos_.toByteArray(); - } - - public void reset() { - baos_.reset(); - } - } + /** + * This transport wraps that byte array + */ + private final TIOStreamTransport transport_ = new TIOStreamTransport(baos_); + /** + * Internal protocol used for serializing objects. + */ private TProtocol protocol_; - private final TByteArrayTransport transport_ = new TByteArrayTransport(); - + /** + * Create a new TSerializer that uses the TBinaryProtocol by default. + * + * @param protocolFactory Factory to create a protocol + */ public TSerializer() { this(new TBinaryProtocol.Factory()); } + /** + * Create a new TSerializer. It will use the TProtocol specified by the + * factory that is passed in. + * + * @param protocolFactory Factory to create a protocol + */ public TSerializer(TProtocolFactory protocolFactory) { protocol_ = protocolFactory.getProtocol(transport_); } + /** + * Serialize the Thrift object into a byte array. The process is simple, + * just clear the byte array output, write the object into it, and grab the + * raw bytes. + * + * @param base The object to serialize + * @return Serialized object in byte[] format + */ public byte[] serialize(TBase base) throws TException { - transport_.reset(); + baos_.reset(); base.write(protocol_); - byte[] data = transport_.get(); - return data; + return baos_.toByteArray(); } + /** + * Serialize the Thrift object into a Java string, using a specified + * character set for encoding. + * + * @param base The object to serialize + * @param charset Valid JVM charset + * @return Serialized object as a String + */ public String toString(TBase base, String charset) throws TException { try { return new String(serialize(base), charset); } catch (UnsupportedEncodingException uex) { - throw new TException("JVM DOES NOT SUPPORT ENCODING"); + throw new TException("JVM DOES NOT SUPPORT ENCODING: " + charset); } } + /** + * Serialize the Thrift object into a Java string, using the default JVM + * charset encoding. + * + * @param base The object to serialize + * @return Serialized object as a String + */ public String toString(TBase base) throws TException { return new String(serialize(base)); }