// Copyright (c) 2006- Facebook
// Distributed under the Thrift Software License
//
// See accompanying file LICENSE or visit the Thrift site at:
// http://developers.facebook.com/thrift/

package com.facebook.thrift;

import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;

import com.facebook.thrift.protocol.TBinaryProtocol;
import com.facebook.thrift.protocol.TProtocol;
import com.facebook.thrift.protocol.TProtocolFactory;
import com.facebook.thrift.transport.TIOStreamTransport;
import com.facebook.thrift.transport.TTransport;

/**
 * Generic utility for easily serializing objects into a byte array.
 *
 * @author Mark Slee <mcslee@facebook.com>
 */
public class TSerializer {

  private static class TByteArrayTransport extends TIOStreamTransport {

    private final ByteArrayOutputStream baos_ = new ByteArrayOutputStream();

    public TByteArrayTransport() {
      outputStream_ = baos_;
    }

    public byte[] get() {
      return baos_.toByteArray();
    }

    public void reset() {
      baos_.reset();
    }
  }

  private TProtocol protocol_;

  private final TByteArrayTransport transport_ = new TByteArrayTransport();

  public TSerializer() {
    this(new TBinaryProtocol.Factory());
  }

  public TSerializer(TProtocolFactory protocolFactory) {
    protocol_ = protocolFactory.getProtocol(transport_);
  }

  public byte[] serialize(TBase base) throws TException {
    transport_.reset();
    base.write(protocol_);
    byte[] data = transport_.get();
    return data;
  }

  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");
    }
  }

  public String toString(TBase base) throws TException {
    return new String(serialize(base));
  }
}


@@ -628,16 +628,34 @@
 te array
+ or Java%0A * String
 .%0A *%0A * 
@@ -732,81 +732,84 @@
 %0A%0A  
-private static class TByteA
+/**%0A   * This is the byte a
 rray
-Transport extends TIOStreamTransport %7B%0A%0A  
+ that data is actually serialized into%0A   */%0A
   pr
@@ -886,500 +886,1027 @@
 %0A%0A  
-  public TByteArrayTransport() %7B%0A      outputStream_ = baos_;%0A    %7D%0A%0A    public byte%5B%5D get() %7B%0A      return baos_.toByteArray(
+/**%0A   * This transport wraps that byte array%0A   */%0A  private final TIOStreamTransport transport_ = new TIOStreamTransport(baos_
 );%0A
+%0A
   
-  %7D%0A%0A    public void reset() %7B%0A      baos_.reset();%0A    %7D%0A  %7D%0A%0A  private TProtocol protocol_;%0A%0A  private final TByteArrayTransport transport_ = new TByteArrayTransport();%0A%0A  public TSerializer() %7B%0A    this(new TBinaryProtocol.Factory());%0A  %7D%0A%0A  public TSerializer(TProtocolFactory protocolFactory) %7B%0A    protocol_ = protocolFactory.getProtocol(transport_);%0A  %7D%0A
+/**%0A   * Internal protocol used for serializing objects.%0A   */%0A  private TProtocol protocol_;%0A%0A  /**%0A   * Create a new TSerializer that uses the TBinaryProtocol by default.%0A   *%0A   * @param protocolFactory Factory to create a protocol%0A   */%0A  public TSerializer() %7B%0A    this(new TBinaryProtocol.Factory());%0A  %7D%0A%0A  /**%0A   * Create a new TSerializer. It will use the TProtocol specified by the%0A   * factory that is passed in.%0A   *%0A   * @param protocolFactory Factory to create a protocol%0A   */%0A  public TSerializer(TProtocolFactory protocolFactory) %7B%0A    protocol_ = protocolFactory.getProtocol(transport_);%0A  %7D%0A%0A  /**%0A   * Serialize the Thrift object into a byte array. The process is simple,%0A   * just clear the byte array output, write the object into it, and grab the%0A   * raw bytes.%0A   *%0A   * @param base The object to serialize%0A   * @return Serialized object in byte%5B%5D format%0A   */
 %0A  p
@@ -1964,25 +1964,20 @@
 n %7B%0A    
-transport
+baos
 _.reset(
@@ -2014,61 +2014,283 @@
     
-byte%5B%5D data = transport_.get();%0A    return data;%0A  %7D%0A
+return baos_.toByteArray();%0A  %7D%0A%0A  /**%0A   * Serialize the Thrift object into a Java string, using a specified%0A   * character set for encoding.%0A   *%0A   * @param base The object to serialize%0A   * @param charset Valid JVM charset%0A   * @return Serialized object as a String%0A   */
 %0A  p
@@ -2530,22 +2530,233 @@
 DING
-%22);%0A    %7D%0A  %7D%0A
+: %22 + charset);%0A    %7D%0A  %7D%0A%0A  /**%0A   * Serialize the Thrift object into a Java string, using the default JVM%0A   * charset encoding.%0A   *%0A   * @param base The object to serialize%0A   * @return Serialized object as a String%0A   */
 %0A  p

Clean up the TSerializer