Example usage for org.apache.thrift.transport TMemoryInputTransport TMemoryInputTransport

List of usage examples for org.apache.thrift.transport TMemoryInputTransport TMemoryInputTransport

Introduction

In this page you can find the example usage for org.apache.thrift.transport TMemoryInputTransport TMemoryInputTransport.

Prototype

public TMemoryInputTransport(byte[] buf) 

Source Link

Usage

From source file:com.linecorp.armeria.server.thrift.THttp2Client.java

License:Apache License

@Override
public void flush() throws TTransportException {
    THttp2ClientInitializer initHandler = new THttp2ClientInitializer();

    Bootstrap b = new Bootstrap();
    b.group(group);// w  ww .ja va  2 s  . c  o  m
    b.channel(NioSocketChannel.class);
    b.handler(initHandler);

    Channel ch = null;
    try {
        ch = b.connect(host, port).syncUninterruptibly().channel();
        THttp2ClientHandler handler = initHandler.clientHandler;

        // Wait until HTTP/2 upgrade is finished.
        assertTrue(handler.settingsPromise.await(5, TimeUnit.SECONDS));
        handler.settingsPromise.get();

        // Send a Thrift request.
        FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, path,
                Unpooled.wrappedBuffer(out.getArray(), 0, out.length()));
        request.headers().add(HttpHeaderNames.HOST, host);
        request.headers().set(ExtensionHeaderNames.SCHEME.text(), uri.getScheme());
        ch.writeAndFlush(request).sync();

        // Wait until the Thrift response is received.
        assertTrue(handler.responsePromise.await(5, TimeUnit.SECONDS));
        ByteBuf response = handler.responsePromise.get();

        // Pass the received Thrift response to the Thrift client.
        final byte[] array = new byte[response.readableBytes()];
        response.readBytes(array);
        in = new TMemoryInputTransport(array);
        response.release();
    } catch (Exception e) {
        throw new TTransportException(TTransportException.UNKNOWN, e);
    } finally {
        if (ch != null) {
            ch.close();
        }
    }
}

From source file:com.nearinfinity.blur.analysis.BlurAnalyzer.java

License:Apache License

public static BlurAnalyzer create(InputStream inputStream) throws IOException {
    TMemoryInputTransport trans = new TMemoryInputTransport(getBytes(inputStream));
    TJSONProtocol protocol = new TJSONProtocol(trans);
    AnalyzerDefinition analyzerDefinition = new AnalyzerDefinition();
    try {/*from  w  w w  .  j  av  a 2 s. co  m*/
        analyzerDefinition.read(protocol);
    } catch (TException e) {
        throw new RuntimeException(e);
    }
    trans.close();
    return new BlurAnalyzer(analyzerDefinition);
}

From source file:com.nearinfinity.blur.manager.clusterstatus.ZookeeperClusterStatus.java

License:Apache License

@SuppressWarnings("unchecked")
private <T extends TBase<?, ?>> T fromBytes(byte[] data, Class<T> clazz) {
    try {/*from www  . j  a va  2 s.c  om*/
        if (data == null) {
            return null;
        }
        TBase<?, ?> base = clazz.newInstance();
        TMemoryInputTransport trans = new TMemoryInputTransport(data);
        TJSONProtocol protocol = new TJSONProtocol(trans);
        base.read(protocol);
        trans.close();
        return (T) base;
    } catch (InstantiationException e) {
        throw new RuntimeException(e);
    } catch (IllegalAccessException e) {
        throw new RuntimeException(e);
    } catch (TException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.twitter.distributedlog.acl.ZKAccessControl.java

License:Apache License

static AccessControlEntry deserialize(String zkPath, byte[] data) throws IOException {
    if (data.length == 0) {
        return DEFAULT_ACCESS_CONTROL_ENTRY;
    }/*from  w w w.  j av  a 2s .  co  m*/

    AccessControlEntry ace = new AccessControlEntry();
    TMemoryInputTransport transport = new TMemoryInputTransport(data);
    TJSONProtocol protocol = new TJSONProtocol(transport);
    try {
        ace.read(protocol);
    } catch (TException e) {
        throw new CorruptedAccessControlException(zkPath, e);
    }
    return ace;
}

From source file:com.twitter.distributedlog.benchmark.Utils.java

License:Apache License

public static Message parseMessage(byte[] data) throws TException {
    Message msg = new Message();
    TMemoryInputTransport transport = new TMemoryInputTransport(data);
    TBinaryProtocol protocol = new TBinaryProtocol(transport);
    msg.read(protocol);/*from ww w  . jav  a2  s.  com*/
    return msg;
}

From source file:com.twitter.distributedlog.metadata.BKDLConfig.java

License:Apache License

@Override
public void deserialize(byte[] data) throws IOException {
    BKDLConfigFormat configFormat = new BKDLConfigFormat();
    TMemoryInputTransport transport = new TMemoryInputTransport(data);
    TJSONProtocol protocol = new TJSONProtocol(transport);
    try {/* www.  j ava2 s . c o m*/
        configFormat.read(protocol);
    } catch (TException e) {
        throw new IOException("Failed to deserialize data '" + new String(data, UTF_8) + "' : ", e);
    }
    // bookkeeper cluster settings
    if (configFormat.isSetBkZkServers()) {
        bkZkServersForWriter = configFormat.getBkZkServers();
    }
    if (configFormat.isSetBkZkServersForReader()) {
        bkZkServersForReader = configFormat.getBkZkServersForReader();
    } else {
        bkZkServersForReader = bkZkServersForWriter;
    }
    if (configFormat.isSetBkLedgersPath()) {
        bkLedgersPath = configFormat.getBkLedgersPath();
    }
    // dl zookeeper cluster settings
    if (configFormat.isSetDlZkServersForWriter()) {
        dlZkServersForWriter = configFormat.getDlZkServersForWriter();
    }
    if (configFormat.isSetDlZkServersForReader()) {
        dlZkServersForReader = configFormat.getDlZkServersForReader();
    } else {
        dlZkServersForReader = dlZkServersForWriter;
    }
    // dl settings
    sanityCheckTxnID = !configFormat.isSetSanityCheckTxnID() || configFormat.isSanityCheckTxnID();
    encodeRegionID = configFormat.isSetEncodeRegionID() && configFormat.isEncodeRegionID();
    if (configFormat.isSetAclRootPath()) {
        aclRootPath = configFormat.getAclRootPath();
    }

    if (configFormat.isSetFirstLogSegmentSeqNo()) {
        firstLogSegmentSeqNo = configFormat.getFirstLogSegmentSeqNo();
    }
    isFederatedNamespace = configFormat.isSetFederatedNamespace() && configFormat.isFederatedNamespace();

    // Validate the settings
    if (null == bkZkServersForWriter || null == bkZkServersForReader || null == bkLedgersPath
            || null == dlZkServersForWriter || null == dlZkServersForReader) {
        throw new IOException("Missing zk/bk settings in BKDL Config : " + new String(data, UTF_8));
    }
}

From source file:io.zipkin.internal.ThriftCodec.java

License:Apache License

private static <T> T read(ThriftAdapter<T> adapter, byte[] bytes) {
    try {//from  ww  w  .  j a v a  2  s  . com
        return adapter.read(new TBinaryProtocol(new TMemoryInputTransport(bytes)));
    } catch (Exception e) {
        return null;
    }
}

From source file:org.apache.accumulo.core.trace.TraceFormatter.java

License:Apache License

public static RemoteSpan getRemoteSpan(Entry<Key, Value> entry) {
    TMemoryInputTransport transport = new TMemoryInputTransport(entry.getValue().get());
    TCompactProtocol protocol = new TCompactProtocol(transport);
    RemoteSpan span = new RemoteSpan();
    try {/*from www.  j a v a 2  s  . c om*/
        span.read(protocol);
    } catch (TException ex) {
        throw new RuntimeException(ex);
    }
    return span;
}

From source file:org.apache.distributedlog.service.placement.ServerLoad.java

License:Apache License

public static ServerLoad deserialize(byte[] data) throws IOException {
    org.apache.distributedlog.service.placement.thrift.ServerLoad tServerLoad = new org.apache.distributedlog.service.placement.thrift.ServerLoad();
    TMemoryInputTransport transport = new TMemoryInputTransport(data);
    TJSONProtocol protocol = new TJSONProtocol(transport);
    try {//www  . j a v a 2s . co  m
        tServerLoad.read(protocol);
        ServerLoad serverLoad = new ServerLoad(tServerLoad.getServer());
        if (tServerLoad.isSetStreams()) {
            for (org.apache.distributedlog.service.placement.thrift.StreamLoad tStreamLoad : tServerLoad
                    .getStreams()) {
                serverLoad.addStream(new StreamLoad(tStreamLoad.getStream(), tStreamLoad.getLoad()));
            }
        }
        return serverLoad;
    } catch (TException e) {
        throw new IOException("Failed to deserialize server load : ", e);
    }
}

From source file:org.apache.distributedlog.service.placement.StreamLoad.java

License:Apache License

public static StreamLoad deserialize(byte[] data) throws IOException {
    org.apache.distributedlog.service.placement.thrift.StreamLoad tStreamLoad = new org.apache.distributedlog.service.placement.thrift.StreamLoad();
    TMemoryInputTransport transport = new TMemoryInputTransport(data);
    TJSONProtocol protocol = new TJSONProtocol(transport);
    try {//from ww  w . java  2 s  . com
        tStreamLoad.read(protocol);
        return new StreamLoad(tStreamLoad.getStream(), tStreamLoad.getLoad());
    } catch (TException e) {
        throw new IOException("Failed to deserialize stream load : ", e);
    }
}