Example usage for org.apache.thrift.protocol TCompactProtocol TCompactProtocol

List of usage examples for org.apache.thrift.protocol TCompactProtocol TCompactProtocol

Introduction

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

Prototype

public TCompactProtocol(TTransport transport) 

Source Link

Document

Create a TCompactProtocol.

Usage

From source file:boxalino.client.SDK.BxClient.java

private Client getP13n(int timeout, boolean useCurlIfAvailable) throws IOException,
        UnsupportedEncodingException, TTransportException, URISyntaxException, MalformedURLException {
    try {//ww  w . j av  a 2  s . co  m
        //default start
        if (timeout == 0) {
            timeout = 2;
        }
        //default end
        useCurlIfAvailable = false;
        THttpClient transport = null;
        if (useCurlIfAvailable) {

        } else {
            try {
                transport = new THttpClient(
                        new URI(String.format("%s://%s%s", this.schema, this.host, this.uri)).toURL()
                                .toString());
            } catch (TTransportException ex) {
                throw ex;
            }
        }
        transport.setCustomHeader("Authorization", "Basic " + Base64.getEncoder()
                .encodeToString((this.p13n_username + ':' + this.p13n_password).getBytes("UTF-8")));
        Client client = new Client(new TCompactProtocol(transport));
        transport.open();
        return client;
    } catch (UncheckedIOException ex) {
        throw ex.getCause();
    }
}

From source file:com.baidu.oped.apm.common.util.BytesUtilsTest.java

License:Apache License

private TMemoryBuffer writeVInt32(int i) throws TException {
    TMemoryBuffer tMemoryBuffer = new TMemoryBuffer(10);
    TCompactProtocol tCompactProtocol = new TCompactProtocol(tMemoryBuffer);
    tCompactProtocol.writeI32(i);// w  ww. j  a  va 2 s. co  m
    return tMemoryBuffer;
}

From source file:com.baidu.oped.apm.thrift.io.ReplaceListCompactProtocolTest.java

License:Apache License

@Test
public void replace() throws Exception {
    List<ByteArrayOutput> nodes01 = new ArrayList<ByteArrayOutput>();
    final AtomicInteger writeTo01 = new AtomicInteger(0);
    nodes01.add(new ByteArrayOutput() {
        public void writeTo(OutputStream out) throws IOException {
            writeTo01.incrementAndGet();
        }/*w w w.  ja  va  2  s .  co m*/
    });

    final AtomicInteger writeTo02 = new AtomicInteger(0);
    List<ByteArrayOutput> nodes02 = new ArrayList<ByteArrayOutput>();
    nodes02.add(new ByteArrayOutput() {
        public void writeTo(OutputStream out) throws IOException {
            writeTo02.incrementAndGet();
        }
    });

    ByteArrayOutputStreamTransport transport = new ByteArrayOutputStreamTransport(new ByteArrayOutputStream());
    TReplaceListProtocol protocol01 = new TReplaceListProtocol(new TCompactProtocol(transport));
    protocol01.addReplaceField("spanEventList", nodes01);
    span.write(protocol01);
    assertEquals(1, writeTo01.get());

    TReplaceListProtocol protocol02 = new TReplaceListProtocol(new TCompactProtocol(transport));
    protocol02.addReplaceField("spanEventList", nodes02);
    span.write(protocol02);
    assertEquals(1, writeTo02.get());
}

From source file:com.devwebsphere.TestAPIs.java

License:Open Source License

@BeforeClass
static public void initialize() throws TTransportException {
    TTransport port = new TSocket("localhost", 9100);
    TFramedTransport transport = new TFramedTransport(port);
    TCompactProtocol protocol = new TCompactProtocol(transport);
    client = new WxsGatewayService.Client(protocol);
    transport.open();/*from  w  ww .j  av a 2 s  .co  m*/
}

From source file:com.facebook.buck.distributed.ThriftOverHttp.java

License:Apache License

public ThriftOverHttp(String uri, Encoding type) throws TTransportException {
    transport = new THttpClient(uri);
    transport.setReadTimeout(READ_TIMEOUT_MS);
    transport.setCustomHeader("X-Thrift-Protocol", type.toString());
    transport.open();//from   w w  w.  j  a va  2s  .c  om
    switch (type) {
    case json:
        proto = new TJSONProtocol(transport);
        break;
    case compact:
        proto = new TCompactProtocol(transport);
        break;
    case binary:
    default:
        proto = new TBinaryProtocol(transport);
        break;
    }
}

From source file:com.facebook.swift.codec.guice.TestThriftCodecModule.java

License:Apache License

public static <T> void testRoundTripSerialize(ThriftCodec<T> codec, T value) throws Exception {
    // write value
    TMemoryBuffer transport = new TMemoryBuffer(10 * 1024);
    TCompactProtocol protocol = new TCompactProtocol(transport);
    codec.write(value, protocol);/*from  www .j  a  v  a 2s.  co  m*/

    // read value back
    T copy = codec.read(protocol);
    assertNotNull(copy);

    // verify they are the same
    assertEquals(copy, value);
}

From source file:com.freetest.framework.thrift.ThriftClientTest.java

License:Open Source License

/**
 * @param args/*from  www.j a va2 s  . c  o m*/
 */
public static void main(String[] args) {

    TTransport transport = new TSocket("127.0.0.1", 8888);

    TProtocol protocol = new TCompactProtocol(transport);

    TestService.Client client = new TestService.Client(protocol);
    try {
        transport.open();

        String response = client.doRequest("nihao...hello");

        System.out.println(response);
    } catch (TTransportException e) {
        e.printStackTrace();
    } catch (TException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

From source file:com.jwsphere.accumulo.DeflateTransportCompression.java

License:Apache License

@Override
public List<TKeyValue> compress(List<? extends KeyValue> source) {
    ScanResult result = new ScanResult(toThrift(source), false);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DeflaterOutputStream deflateStream = new DeflaterOutputStream(baos);
    TTransport transport = new TIOStreamTransport(deflateStream);
    TCompactProtocol proto = new TCompactProtocol(transport);
    try {//  www  .j  a  va 2  s . c o m
        transport.open();
        result.write(proto);
    } catch (TException e) {
        throw new RuntimeException(e);
    } finally {
        transport.close();
    }
    byte[] bytes = baos.toByteArray();
    return Collections.singletonList(new TKeyValue(new TKey(), ByteBuffer.wrap(bytes)));
}

From source file:com.jwsphere.accumulo.DeflateTransportCompression.java

License:Apache License

@Override
public List<KeyValue> decompress(List<TKeyValue> data) {
    if (data.size() != 1) {
        throw new IllegalArgumentException("Unexpected number of results.");
    }/*from   w w  w .  ja  v  a 2 s .  co  m*/
    byte[] bytes = data.get(0).getValue();
    ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
    InflaterInputStream inflateStream = new InflaterInputStream(bais);
    TTransport transport = new TIOStreamTransport(inflateStream);
    TCompactProtocol proto = new TCompactProtocol(transport);
    ScanResult result = new ScanResult();
    try {
        transport.open();
        result.read(proto);
    } catch (TException e) {
        throw new RuntimeException(e);
    } finally {
        transport.close();
    }
    List<TKeyValue> decoded = result.getResults();
    return fromThrift(decoded);
}

From source file:com.jwsphere.accumulo.TransportCompression.java

License:Apache License

private long computeSize(List<TKeyValue> data) {
    ScanResult result = new ScanResult(data, false);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    TTransport transport = new TIOStreamTransport(baos);
    TCompactProtocol proto = new TCompactProtocol(transport);
    try {//from w  w  w.j av  a2 s  .com
        result.write(proto);
        transport.flush();
    } catch (TException e) {
        throw new RuntimeException(e);
    }
    return baos.toByteArray().length;
}