// There are actually 2 buffers in play here - the BufferedOutputStream prevents thrift from
// causing a call to deflate() on every encoded primitive. The DeflaterOutputStream buffer
// allows the underlying Deflater to operate on a larger chunk at a time without stopping to
// copy the intermediate compressed output to outBytes.
// See http://bugs.java.com/bugdatabase/view_bug.do?bug_id=4986239
TTransport transport = new TIOStreamTransport(
new BufferedOutputStream(
new DeflaterOutputStream(outBytes, new Deflater(DEFLATE_LEVEL), DEFLATER_BUFFER_SIZE),
DEFLATER_BUFFER_SIZE));
TProtocol protocol = PROTOCOL_FACTORY.getProtocol(transport);
tBase.write(protocol);
transport.close();
return outBytes.toByteArray();
} catch (TException e) {
throw new CodingException("Failed to serialize: " + tBase, e);
}
}