Subversion Repositories SmartDukaan

Rev

Rev 30 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
30 ashish 1
/*
2
 * Licensed to the Apache Software Foundation (ASF) under one
3
 * or more contributor license agreements. See the NOTICE file
4
 * distributed with this work for additional information
5
 * regarding copyright ownership. The ASF licenses this file
6
 * to you under the Apache License, Version 2.0 (the
7
 * "License"); you may not use this file except in compliance
8
 * with the License. You may obtain a copy of the License at
9
 *
10
 *   http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing,
13
 * software distributed under the License is distributed on an
14
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15
 * KIND, either express or implied. See the License for the
16
 * specific language governing permissions and limitations
17
 * under the License.
18
 */
19
 
20
package org.apache.thrift;
21
 
22
import java.io.ByteArrayOutputStream;
23
import java.io.UnsupportedEncodingException;
24
 
25
import org.apache.thrift.protocol.TBinaryProtocol;
26
import org.apache.thrift.protocol.TProtocol;
27
import org.apache.thrift.protocol.TProtocolFactory;
28
import org.apache.thrift.transport.TIOStreamTransport;
29
 
30
/**
31
 * Generic utility for easily serializing objects into a byte array or Java
32
 * String.
33
 *
34
 */
35
public class TSerializer {
36
 
37
  /**
38
   * This is the byte array that data is actually serialized into
39
   */
40
  private final ByteArrayOutputStream baos_ = new ByteArrayOutputStream();
41
 
42
  /**
43
   * This transport wraps that byte array
44
   */
45
  private final TIOStreamTransport transport_ = new TIOStreamTransport(baos_);
46
 
47
  /**
48
   * Internal protocol used for serializing objects.
49
   */
50
  private TProtocol protocol_;
51
 
52
  /**
53
   * Create a new TSerializer that uses the TBinaryProtocol by default.
54
   */
55
  public TSerializer() {
56
    this(new TBinaryProtocol.Factory());
57
  }
58
 
59
  /**
60
   * Create a new TSerializer. It will use the TProtocol specified by the
61
   * factory that is passed in.
62
   *
63
   * @param protocolFactory Factory to create a protocol
64
   */
65
  public TSerializer(TProtocolFactory protocolFactory) {
66
    protocol_ = protocolFactory.getProtocol(transport_);
67
  }
68
 
69
  /**
70
   * Serialize the Thrift object into a byte array. The process is simple,
71
   * just clear the byte array output, write the object into it, and grab the
72
   * raw bytes.
73
   *
74
   * @param base The object to serialize
75
   * @return Serialized object in byte[] format
76
   */
77
  public byte[] serialize(TBase base) throws TException {
78
    baos_.reset();
79
    base.write(protocol_);
80
    return baos_.toByteArray();
81
  }
82
 
83
  /**
84
   * Serialize the Thrift object into a Java string, using a specified
85
   * character set for encoding.
86
   *
87
   * @param base The object to serialize
88
   * @param charset Valid JVM charset
89
   * @return Serialized object as a String
90
   */
91
  public String toString(TBase base, String charset) throws TException {
92
    try {
93
      return new String(serialize(base), charset);
94
    } catch (UnsupportedEncodingException uex) {
95
      throw new TException("JVM DOES NOT SUPPORT ENCODING: " + charset);
96
    }
97
  }
98
 
99
  /**
100
   * Serialize the Thrift object into a Java string, using the default JVM
101
   * charset encoding.
102
   *
103
   * @param base The object to serialize
104
   * @return Serialized object as a String
105
   */
106
  public String toString(TBase base) throws TException {
107
    return new String(serialize(base));
108
  }
109
}
110