Hello,
I currently have a use-case where I want to serialize multiple thrift objects
(of the same type and using the same TProtocol) and later on deserialize those
bytes back into a collection of thrift objects. I found the TSerializer and
TDeserializer classes in libthrift that handles serializing and deserialization
from a single thrift object to bytes or vice-versa but was wondering if there
was support for serializing/deserializing to an input/output stream?
I found a way to do it by using the TTransport and TProtocol classes directly
in the example below, but was wondering if this was the best way to do it or if
there are other thrift apis better suited for this kind of operation.
SampleThrift thrift1 = // a thrift object
SampleThrift thrift2 = // another thrift object of the same class as
thrift1
TSerializer serializer = new TSerializer(new
TCompactProtocol.Factory());
final ByteArrayOutputStream os = new ByteArrayOutputStream();
final byte[] serialize1 = serializer.serialize(thrift1);
os.write(serialize1);
final byte[] serialize2 = serializer.serialize(thrift2);
os.write(serialize2);
final byte[] output = os.toByteArray();
final TMemoryInputTransport transport = new
TMemoryInputTransport(output);
final TProtocol protocol = new
TCompactProtocol.Factory().getProtocol(transport);
boolean next = true;
do {
TBase read = new SampleThrift();
read.read(protocol);
System.out.println("read = " + read);
// TODO add the object to a collection
if (transport.getBytesRemainingInBuffer() <= 0) {
next = false;
}
} while (next);
Thanks,
Brian
CONFIDENTIALITY NOTICE This message and any included attachments are from
Cerner Corporation and are intended only for the addressee. The information
contained in this message is confidential and may constitute inside or
non-public information under international, federal, or state securities laws.
Unauthorized forwarding, printing, copying, distribution, or use of such
information is strictly prohibited and may be unlawful. If you are not the
addressee, please promptly delete this message and notify the sender of the
delivery error by e-mail or you may call Cerner's corporate offices in Kansas
City, Missouri, U.S.A at (+1) (816)221-1024.