org.apache.thrift.transport.TMemoryBuffer Java Examples

The following examples show how to use org.apache.thrift.transport.TMemoryBuffer. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MemTrans.java    From ThriftBook with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) 
        throws IOException, TTransportException, ClassNotFoundException {
    TMemoryBuffer transport = new TMemoryBuffer(4096);

    Trade trade = new Trade();
    trade.symbol = "F";
    trade.price = 13.10;
    trade.size = 2500;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(trade);
    transport.write(baos.toByteArray());

    byte[] buf = new byte[128];
    int bytes_read = transport.read(buf, 0, buf.length);
    ByteArrayInputStream bais = new ByteArrayInputStream(buf);
    ObjectInputStream ois = new ObjectInputStream(bais);
    Trade trade_read = (Trade) ois.readObject();
    System.out.println("Trade(" + bytes_read + "): " + trade_read.symbol
            + " " + trade_read.size + " @ " + trade_read.price);
}
 
Example #2
Source File: StructTypeAdapterTest.java    From Firefly with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldWriteUnion_thatThriftRead() throws Exception {
    TMemoryBuffer transport = new TMemoryBuffer(1024);
    TBinaryProtocol protocol = new TBinaryProtocol(transport);
    UnionB fireflyUnionB = new UnionB();
    OrderedStruct fireflyOrderedStruct = new OrderedStruct();
    fireflyOrderedStruct.id = 99;
    fireflyUnionB.os = fireflyOrderedStruct;
    StructTypeAdapterFactory.StructTypeAdapter structTypeAdapter = new StructTypeAdapterFactory.StructTypeAdapter(UnionB.class, thrift);
    structTypeAdapter.write(fireflyUnionB, protocol);

    com.meituan.firefly.testthrift.UnionB thriftUnionB = new com.meituan.firefly.testthrift.UnionB();
    thriftUnionB.read(protocol);

    assertThat(thriftUnionB.getSetField()).isEqualTo(com.meituan.firefly.testthrift.UnionB._Fields.OS);
    assertThat(thriftUnionB.getOs()).isNotNull();
    assertThat(thriftUnionB.getOs().getId()).isEqualTo(99);
}
 
Example #3
Source File: StructTypeAdapterTest.java    From Firefly with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldWriteStruct_thatThriftRead() throws Exception {
    TMemoryBuffer transport = new TMemoryBuffer(1024);
    TBinaryProtocol protocol = new TBinaryProtocol(transport);
    OrderedStruct fireflyOrderedStruct = new OrderedStruct();
    fireflyOrderedStruct.id = 99;

    StructTypeAdapterFactory.StructTypeAdapter structTypeAdapter = new StructTypeAdapterFactory.StructTypeAdapter(OrderedStruct.class, thrift);
    structTypeAdapter.write(fireflyOrderedStruct, protocol);
    com.meituan.firefly.testthrift.OrderedStruct thriftOrderedStruct = new com.meituan.firefly.testthrift.OrderedStruct();
    thriftOrderedStruct.read(protocol);
    assertThat(thriftOrderedStruct.id).isEqualTo(99);
}
 
Example #4
Source File: TCompactProtocolTest.java    From pinpoint with Apache License 2.0 5 votes vote down vote up
@Test
public void compactProtocolVint() throws TException {
    TMemoryBuffer tMemoryBuffer = writeVInt32(BytesUtils.zigzagToInt(64));
    logger.trace("length:{}", tMemoryBuffer.length());

    TMemoryBuffer tMemoryBuffer2 = writeVInt32(64);
    logger.trace("length:{}", tMemoryBuffer2.length());

}
 
Example #5
Source File: TradeReader.java    From ThriftBook with Apache License 2.0 5 votes vote down vote up
public static void main(String[] argv)
    throws java.io.IOException,
           java.lang.InterruptedException,
           java.util.concurrent.TimeoutException,
           TException,
           TTransportException {

    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("localhost");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();
    channel.queueDeclare(QUEUE_NAME, false, false, false, null);
    QueueingConsumer consumer = new QueueingConsumer(channel);
    channel.basicConsume(QUEUE_NAME, true, consumer);

    System.out.println("Waiting for trade reports...");
    while (true) {
        QueueingConsumer.Delivery delivery = consumer.nextDelivery();
        byte[] data = delivery.getBody();
        TMemoryBuffer trans = new TMemoryBuffer(data.length);
        trans.write(data, 0, data.length);
        TCompactProtocol proto = new TCompactProtocol(trans);
        TradeReport tr = new TradeReport();
        tr.read(proto);
        System.out.println("[" + tr.seq_num + "] " + tr.symbol + 
                           " @ " + tr.price + " x " + tr.size);
    }
}
 
Example #6
Source File: BinMem.java    From ThriftBook with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example #7
Source File: ThriftServiceTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
@BeforeEach
void before() {
    in = new TMemoryInputTransport();
    out = new TMemoryBuffer(128);

    promise = new CompletableFuture<>();
    promise2 = new CompletableFuture<>();
}
 
Example #8
Source File: ThriftJacksonSerializers.java    From armeria with Apache License 2.0 5 votes vote down vote up
private static String serializeTBaseLike(Consumer<TProtocol> writer, boolean useNamedEnums) {
    final TMemoryBuffer buffer = new TMemoryBuffer(1024);
    final TProtocolFactory factory = useNamedEnums ? ThriftProtocolFactories.TEXT_NAMED_ENUM
                                                   : ThriftProtocolFactories.TEXT;
    final TProtocol protocol = factory.getProtocol(buffer);
    writer.accept(protocol);
    return new String(buffer.getArray(), 0, buffer.length());
}
 
Example #9
Source File: StructTypeAdapterTest.java    From Firefly with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldReadUnion_thatThriftWrite() throws Exception {
    TMemoryBuffer transport = new TMemoryBuffer(1024);
    TBinaryProtocol protocol = new TBinaryProtocol(transport);
    com.meituan.firefly.testthrift.OrderedStruct thriftOrderedStruct = new com.meituan.firefly.testthrift.OrderedStruct(99);
    com.meituan.firefly.testthrift.UnionB thriftUnionB = new com.meituan.firefly.testthrift.UnionB(com.meituan.firefly.testthrift.UnionB._Fields.OS, thriftOrderedStruct);
    thriftUnionB.write(protocol);

    StructTypeAdapterFactory.StructTypeAdapter structTypeAdapter = new StructTypeAdapterFactory.StructTypeAdapter(UnionB.class, thrift);
    UnionB fireflyUnionB = (UnionB) structTypeAdapter.read(protocol);
    assertThat(fireflyUnionB).isNotNull();
    assertThat(fireflyUnionB.os).isNotNull();
    assertThat(fireflyUnionB.os.id).isEqualTo(99);
}
 
Example #10
Source File: StructTypeAdapterTest.java    From Firefly with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldWriteMixStruct_thatThriftRead() throws Exception {
    TMemoryBuffer transport = new TMemoryBuffer(1024);
    TBinaryProtocol protocol = new TBinaryProtocol(transport);
    MixStruct fireflyMixStruct = new MixStruct();
    fireflyMixStruct.id = 1;
    fireflyMixStruct.uid = 2;

    StructTypeAdapterFactory.StructTypeAdapter structTypeAdapter = new StructTypeAdapterFactory.StructTypeAdapter(MixStruct.class, thrift);
    structTypeAdapter.write(fireflyMixStruct, protocol);
    com.meituan.firefly.testthrift.MixStruct thriftMixStruct = new com.meituan.firefly.testthrift.MixStruct();
    thriftMixStruct.read(protocol);
    assertThat(thriftMixStruct.id).isEqualTo(1);
    assertThat(thriftMixStruct.uid).isEqualTo(2);
}
 
Example #11
Source File: StructTypeAdapterTest.java    From Firefly with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldReadMixStruct_thatThriftWrite() throws Exception {
    TMemoryBuffer transport = new TMemoryBuffer(1024);
    TBinaryProtocol protocol = new TBinaryProtocol(transport);
    com.meituan.firefly.testthrift.MixStruct thriftMixStruct = new com.meituan.firefly.testthrift.MixStruct(1, 2);
    thriftMixStruct.write(protocol);

    StructTypeAdapterFactory.StructTypeAdapter structTypeAdapter = new StructTypeAdapterFactory.StructTypeAdapter(MixStruct.class, thrift);
    MixStruct fireflyMixStruct = (MixStruct) structTypeAdapter.read(protocol);
    assertThat(fireflyMixStruct).isNotNull();
    assertThat(fireflyMixStruct.id).isEqualTo(1);
    assertThat(fireflyMixStruct.uid).isEqualTo(2);
}
 
Example #12
Source File: StructTypeAdapterTest.java    From Firefly with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldReadStruct_thatThriftWrite() throws Exception {
    TMemoryBuffer transport = new TMemoryBuffer(1024);
    TBinaryProtocol protocol = new TBinaryProtocol(transport);
    com.meituan.firefly.testthrift.OrderedStruct thriftOrderedStruct = new com.meituan.firefly.testthrift.OrderedStruct(99);
    thriftOrderedStruct.write(protocol);

    StructTypeAdapterFactory.StructTypeAdapter structTypeAdapter = new StructTypeAdapterFactory.StructTypeAdapter(OrderedStruct.class, thrift);
    OrderedStruct fireflyOrderedStruct = (OrderedStruct) structTypeAdapter.read(protocol);
    assertThat(fireflyOrderedStruct).isNotNull();
    assertThat(fireflyOrderedStruct.id).isEqualTo(99);
}
 
Example #13
Source File: TestZipkinSpanReceiver.java    From incubator-retired-htrace with Apache License 2.0 5 votes vote down vote up
@Override
public void send(List<byte[]> spans) throws IOException {
  for (byte[] message : spans) {
    TMemoryBuffer transport = new TMemoryBuffer(message.length);
    try {
      transport.write(message);
      com.twitter.zipkin.gen.Span zSpan = new com.twitter.zipkin.gen.Span();
      zSpan.read(new TBinaryProtocol(transport));
      receivedSpans.add(zSpan);
    } catch (TException e) {
      throw new IOException(e);
    }
  }
}
 
Example #14
Source File: ZKAccessControl.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
static byte[] serialize(AccessControlEntry ace) throws IOException {
    TMemoryBuffer transport = new TMemoryBuffer(BUFFER_SIZE);
    TJSONProtocol protocol = new TJSONProtocol(transport);
    try {
        ace.write(protocol);
        transport.flush();
        return transport.toString(UTF_8.name()).getBytes(UTF_8);
    } catch (TException e) {
        throw new IOException("Failed to serialize access control entry : ", e);
    } catch (UnsupportedEncodingException uee) {
        throw new IOException("Failed to serialize acesss control entry : ", uee);
    }
}
 
Example #15
Source File: ServiceTest.java    From ikasoa with MIT License 5 votes vote down vote up
@Test
public void testThriftBaseReadWrite() {
	TBinaryProtocol protocol = new TBinaryProtocol(new TMemoryBuffer(16));
	ArgsThriftBase args = new ArgsThriftBase(TestConstants.TEST_STRING);
	ResultThriftBase result = new ResultThriftBase();
	try {
		args.write(protocol);
		result.read(protocol);
		assertEquals(args.getStr(), result.getStr(), TestConstants.TEST_STRING);
	} catch (TException e) {
		fail();
	}
}
 
Example #16
Source File: ZKAccessControl.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
static byte[] serialize(AccessControlEntry ace) throws IOException {
    TMemoryBuffer transport = new TMemoryBuffer(BUFFER_SIZE);
    TJSONProtocol protocol = new TJSONProtocol(transport);
    try {
        ace.write(protocol);
        transport.flush();
        return transport.toString(UTF_8.name()).getBytes(UTF_8);
    } catch (TException e) {
        throw new IOException("Failed to serialize access control entry : ", e);
    } catch (UnsupportedEncodingException uee) {
        throw new IOException("Failed to serialize acesss control entry : ", uee);
    }
}
 
Example #17
Source File: StreamLoad.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
public byte[] serialize() throws IOException {
    TMemoryBuffer transport = new TMemoryBuffer(BUFFER_SIZE);
    TJSONProtocol protocol = new TJSONProtocol(transport);
    try {
        toThrift().write(protocol);
        transport.flush();
        return transport.toString(UTF_8.name()).getBytes(UTF_8);
    } catch (TException e) {
        throw new IOException("Failed to serialize stream load : ", e);
    } catch (UnsupportedEncodingException uee) {
        throw new IOException("Failed to serialize stream load : ", uee);
    }
}
 
Example #18
Source File: ServerLoad.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
public byte[] serialize() throws IOException {
    TMemoryBuffer transport = new TMemoryBuffer(BUFFER_SIZE);
    TJSONProtocol protocol = new TJSONProtocol(transport);
    try {
        toThrift().write(protocol);
        transport.flush();
        return transport.toString(UTF_8.name()).getBytes(UTF_8);
    } catch (TException e) {
        throw new IOException("Failed to serialize server load : ", e);
    } catch (UnsupportedEncodingException uee) {
        throw new IOException("Failed to serialize server load : ", uee);
    }
}
 
Example #19
Source File: ScribeSender.java    From zipkin-finagle with Apache License 2.0 5 votes vote down vote up
/** This doesn't use thrift sequence ids because scrooge doesn't */
@Override protected ThriftClientRequest makeRequest(List<byte[]> spans) throws TException {
  int encodedSize = InternalScribeCodec.messageSizeInBytes(category, spans);
  TMemoryBuffer mem = new TMemoryBuffer(encodedSize);
  TBinaryProtocol prot = new TBinaryProtocol(mem);
  InternalScribeCodec.writeLogRequest(category, spans, 0, prot);
  return new ThriftClientRequest(mem.getArray(), false);
}
 
Example #20
Source File: ThriftIDLSerializer.java    From octo-rpc with Apache License 2.0 5 votes vote down vote up
private byte[] doSerializeRequest(DefaultRequest request) throws Exception {
    RpcInvocation rpcInvocation = request.getData();
    TMessage message = new TMessage(rpcInvocation.getMethod().getName(), TMessageType.CALL, request.getSeqToInt());

    TMemoryBuffer memoryBuffer = new TMemoryBuffer(INITIAL_BYTE_ARRAY_SIZE);
    TBinaryProtocol protocol = new TBinaryProtocol(memoryBuffer);

    protocol.writeMessageBegin(message);
    serializeArguments(rpcInvocation, protocol);
    protocol.writeMessageEnd();

    protocol.getTransport().flush();
    return getActualBytes(memoryBuffer);
}
 
Example #21
Source File: TCompactProtocolTest.java    From pinpoint with Apache License 2.0 4 votes vote down vote up
private TMemoryBuffer writeVInt32(int i) throws TException {
    TMemoryBuffer tMemoryBuffer = new TMemoryBuffer(10);
    TCompactProtocol tCompactProtocol = new TCompactProtocol(tMemoryBuffer);
    tCompactProtocol.writeI32(i);
    return tMemoryBuffer;
}
 
Example #22
Source File: ThriftIDLSerializer.java    From octo-rpc with Apache License 2.0 4 votes vote down vote up
private byte[] getActualBytes(TMemoryBuffer memoryBuffer) {
    byte[] actualBytes = new byte[memoryBuffer.length()];
    System.arraycopy(memoryBuffer.getArray(), 0, actualBytes, 0, memoryBuffer.length());
    return actualBytes;
}