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);
}
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);
}
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());
}
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;
}
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);
}
}
Aggregations