[강운덕] [LUCYSUS-1744] commit 안된게 있어 추가.

git-svn-id: http://svn.bds.nhncorp.com/pe/hippo-tomcat-profiler/trunk@2078 84d0f5b1-2673-498c-a247-62c4ff18d310
This commit is contained in:
Woonduk Kang
2013-08-02 02:23:49 +00:00
parent 52d16c5be5
commit 6878c6aee3
@@ -0,0 +1,114 @@
package com.nhn.pinpoint.profiler.io;
import com.nhn.pinpoint.common.dto.Header;
import com.nhn.pinpoint.common.util.BytesUtils;
import org.apache.thrift.TBase;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TCompactProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.protocol.TProtocolFactory;
import org.apache.thrift.transport.TIOStreamTransport;
import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;
/**
* Generic utility for easily serializing objects into a byte array or Java
* String.
*/
public class SafeHeaderTBaseSerializer {
/**
* This is the byte array that data is actually serialized into
*/
// udp 패킷 사이즈에 최대 맞춤.
private final ByteArrayOutputStream baos_ = new ByteArrayOutputStream(1024 * 4);
/**
* This transport wraps that byte array
*/
private final TIOStreamTransport transport_ = new TIOStreamTransport(baos_);
/**
* Internal protocol used for serializing objects.
*/
private TProtocol protocol_;
/**
* Create a new TSerializer that uses the TBinaryProtocol by default.
*/
public SafeHeaderTBaseSerializer() {
this(new TCompactProtocol.Factory(), new DefaultTBaseLocator());
}
private TBaseLocator locator;
/**
* 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 SafeHeaderTBaseSerializer(TProtocolFactory protocolFactory, TBaseLocator locator) {
protocol_ = protocolFactory.getProtocol(transport_);
this.locator = locator;
}
/**
* 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 {
final Header header = locator.headerLookup(base);
baos_.reset();
writeHeader(header);
base.write(protocol_);
return baos_.toByteArray();
}
public int getInterBufferSize() {
return baos_.size();
}
private void writeHeader(Header header) throws TException {
protocol_.writeByte(header.getSignature());
protocol_.writeByte(header.getVersion());
// 프로토콜 변경에 관계 없이 고정 사이즈의 데이터로 인코딩 하도록 변경.
short type = header.getType();
protocol_.writeByte(BytesUtils.writeShort1(type));
protocol_.writeByte(BytesUtils.writeShort2(type));
}
/**
* 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: " + 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));
}
}