Example usage for org.apache.thrift.protocol TProtocolUtil guessProtocolFactory

List of usage examples for org.apache.thrift.protocol TProtocolUtil guessProtocolFactory

Introduction

In this page you can find the example usage for org.apache.thrift.protocol TProtocolUtil guessProtocolFactory.

Prototype

public static TProtocolFactory guessProtocolFactory(byte[] data, TProtocolFactory fallback) 

Source Link

Document

Attempt to determine the protocol used to serialize some data.

Usage

From source file:io.druid.data.input.thrift.ThriftDeserialization.java

License:Apache License

/**
 * Deserializes byte-array into thrift object.
 * <p>//from  ww  w.j  a va2  s  .c o  m
 * Supporting binary, compact and json protocols,
 * and the byte array could be or not be encoded by Base64.
 *
 * @param bytes     the byte-array to deserialize
 * @param thriftObj the output thrift object
 *
 * @return the output thrift object, or null if error occurs
 */
public static <T extends TBase> T detectAndDeserialize(final byte[] bytes, final T thriftObj)
        throws TException {
    Preconditions.checkNotNull(thriftObj);
    try {
        final byte[] src = decodeB64IfNeeded(bytes);
        final TProtocolFactory protocolFactory = TProtocolUtil.guessProtocolFactory(src, null);
        Preconditions.checkNotNull(protocolFactory);
        if (protocolFactory instanceof TCompactProtocol.Factory) {
            DESERIALIZER_COMPACT.get().deserialize(thriftObj, src);
        } else if (protocolFactory instanceof TBinaryProtocol.Factory) {
            DESERIALIZER_BINARY.get().deserialize(thriftObj, src);
        } else {
            DESERIALIZER_JSON.get().deserialize(thriftObj, src);
        }
    } catch (final IllegalArgumentException e) {
        throw new TException(e);
    }
    return thriftObj;
}