public static void main(String[] args) throws TException { TMemoryBuffer trans = new TMemoryBuffer(4096); TProtocol proto = new TBinaryProtocol(trans); proto.writeString("Hello Thrift Serialization"); System.out.println("Wrote " + trans.length() + " bytes to the TMemoryBuffer"); String strMsg = proto.readString(); System.out.println("Recovered string: " + strMsg); }
public String toJSON() { TMemoryBuffer trans = new TMemoryBuffer(1024); TJSONProtocol protocol = new TJSONProtocol(trans); try { _analyzerDefinition.write(protocol); } catch (TException e) { throw new RuntimeException(e); } trans.close(); byte[] array = trans.getArray(); return new String(array, 0, trans.length()); }
public static <T extends TBase> void testDeserialization( TProtocolFactory factory, T object, Class<T> klass) throws Exception { TMemoryBuffer buf = new TMemoryBuffer(0); object.write(factory.getProtocol(buf)); byte[] serialized = new byte[100 * 1024]; buf.read(serialized, 0, 100 * 1024); long startTime = System.currentTimeMillis(); for (int i = 0; i < HOW_MANY; i++) { T o2 = klass.newInstance(); o2.read(factory.getProtocol(new TMemoryInputTransport(serialized))); } long endTime = System.currentTimeMillis(); System.out.println("Deserialization test time: " + (endTime - startTime) + " ms"); }
private static String writeThriftObjectAsTText(Consumer<TProtocol> writer) { TMemoryBuffer buffer = new TMemoryBuffer(1024); TProtocol protocol = new TTextProtocol.Factory().getProtocol(buffer); writer.accept(protocol); return new String(buffer.getArray(), 0, buffer.length()); }