Search in sources :

Example 21 with TIOStreamTransport

use of org.apache.thrift.transport.TIOStreamTransport in project hive by apache.

the class ThriftByteStreamTypedSerDe method init.

private void init(TProtocolFactory inFactory, TProtocolFactory outFactory) throws Exception {
    outTransport = new TIOStreamTransport(bos);
    inTransport = new TIOStreamTransport(bis);
    outProtocol = outFactory.getProtocol(outTransport);
    inProtocol = inFactory.getProtocol(inTransport);
}
Also used : TIOStreamTransport(org.apache.thrift.transport.TIOStreamTransport)

Example 22 with TIOStreamTransport

use of org.apache.thrift.transport.TIOStreamTransport in project commons by twitter.

the class TTextProtocolTest method tTextProtocolReadWriteTest.

/**
   * Read in (deserialize) a thrift message in TTextProtocol format
   * from a file on disk, then serialize it back out to a string.
   * Finally, deserialize that string and compare to the original
   * message.
   * @throws IOException
   */
@Test
public void tTextProtocolReadWriteTest() throws IOException, TException {
    // Deserialize the file contents into a thrift message.
    ByteArrayInputStream bais1 = new ByteArrayInputStream(fileContents.getBytes());
    TTextProtocolTestMsg msg1 = new TTextProtocolTestMsg();
    msg1.read(new TTextProtocol(new TIOStreamTransport(bais1)));
    assertEquals(testMsg(), msg1);
    // Serialize that thrift message out to a byte array
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    msg1.write(new TTextProtocol(new TIOStreamTransport(baos)));
    byte[] bytes = baos.toByteArray();
    // Deserialize that string back to a thrift message.
    ByteArrayInputStream bais2 = new ByteArrayInputStream(bytes);
    TTextProtocolTestMsg msg2 = new TTextProtocolTestMsg();
    msg2.read(new TTextProtocol(new TIOStreamTransport(bais2)));
    assertEquals(msg1, msg2);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) TIOStreamTransport(org.apache.thrift.transport.TIOStreamTransport) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 23 with TIOStreamTransport

use of org.apache.thrift.transport.TIOStreamTransport in project commons by twitter.

the class TTextProtocolTest method tTextProtocolWriteUnionTest.

// For TUnion structure, TTextProtocol can only handle serialization, but not deserialization.
// Because when deserialization, we loose the context of which thrift class we are currently at.
// Specifically, because we rely on the callstack to determine which structure is currently being
// parsed, but TUnion actually implements of read/write. So when the parser comes to any TUnion,
// it only knows TUnion from the stack, but not the specific thrift struct.
// So here we only test serialization, not the deserialization part.
@Test
public void tTextProtocolWriteUnionTest() throws IOException, TException {
    TTextProtocolTestMsgUnion msg = new TTextProtocolTestMsgUnion();
    msg.setU(TestUnion.f2(2));
    // Serialize that thrift message with union out to a byte array
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    msg.write(new TTextProtocol(new TIOStreamTransport(baos)));
    String expectedMsg = "{\n" + "  \"u\": {\n" + "    \"f2\": 2\n" + "  }\n" + "}";
    assertEquals(expectedMsg, baos.toString());
}
Also used : TIOStreamTransport(org.apache.thrift.transport.TIOStreamTransport) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 24 with TIOStreamTransport

use of org.apache.thrift.transport.TIOStreamTransport in project commons by twitter.

the class ThriftCodec method deserialize.

@Override
public T deserialize(InputStream source) throws IOException {
    Preconditions.checkNotNull(source);
    T template = templateSupplier.get();
    try {
        template.read(protocolFactory.apply(new TIOStreamTransport(source, null)));
    } catch (TException e) {
        throw new IOException("Problem de-serializing thrift struct from stream", e);
    }
    return template;
}
Also used : TException(org.apache.thrift.TException) TIOStreamTransport(org.apache.thrift.transport.TIOStreamTransport) IOException(java.io.IOException)

Example 25 with TIOStreamTransport

use of org.apache.thrift.transport.TIOStreamTransport in project commons by twitter.

the class ThriftCodec method serialize.

@Override
public void serialize(T item, OutputStream sink) throws IOException {
    Preconditions.checkNotNull(item);
    Preconditions.checkNotNull(sink);
    try {
        item.write(protocolFactory.apply(new TIOStreamTransport(null, sink)));
    } catch (TException e) {
        throw new IOException("Problem serializing thrift struct: " + item, e);
    }
}
Also used : TException(org.apache.thrift.TException) TIOStreamTransport(org.apache.thrift.transport.TIOStreamTransport) IOException(java.io.IOException)

Aggregations

TIOStreamTransport (org.apache.thrift.transport.TIOStreamTransport)33 TException (org.apache.thrift.TException)13 TBinaryProtocol (org.apache.thrift.protocol.TBinaryProtocol)11 TProtocol (org.apache.thrift.protocol.TProtocol)10 ByteArrayOutputStream (java.io.ByteArrayOutputStream)9 Test (org.junit.Test)9 TMessage (org.apache.thrift.protocol.TMessage)8 TTransport (org.apache.thrift.transport.TTransport)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 TCompactProtocol (org.apache.thrift.protocol.TCompactProtocol)7 Request (com.alibaba.dubbo.remoting.exchange.Request)6 Demo (com.alibaba.dubbo.rpc.gen.thrift.Demo)6 IOException (java.io.IOException)6 ChannelBuffer (com.alibaba.dubbo.remoting.buffer.ChannelBuffer)5 RpcResult (com.alibaba.dubbo.rpc.RpcResult)5 RandomAccessByteArrayOutputStream (com.alibaba.dubbo.rpc.protocol.thrift.io.RandomAccessByteArrayOutputStream)5 URL (com.alibaba.dubbo.common.URL)4 Channel (com.alibaba.dubbo.remoting.Channel)4 Response (com.alibaba.dubbo.remoting.exchange.Response)4 RpcException (com.alibaba.dubbo.rpc.RpcException)3