Example usage for org.apache.thrift.transport TMemoryBuffer TMemoryBuffer

List of usage examples for org.apache.thrift.transport TMemoryBuffer TMemoryBuffer

Introduction

In this page you can find the example usage for org.apache.thrift.transport TMemoryBuffer TMemoryBuffer.

Prototype

public TMemoryBuffer(int size) 

Source Link

Document

Create a TMemoryBuffer with an initial buffer size of size.

Usage

From source file:com.baidu.oped.apm.common.util.BytesUtilsTest.java

License:Apache License

private TMemoryBuffer writeVInt32(int i) throws TException {
    TMemoryBuffer tMemoryBuffer = new TMemoryBuffer(10);
    TCompactProtocol tCompactProtocol = new TCompactProtocol(tMemoryBuffer);
    tCompactProtocol.writeI32(i);/*www.j  ava  2  s  .c o  m*/
    return tMemoryBuffer;
}

From source file:com.cloudera.flume.handlers.thrift.TestTStatsTransport.java

License:Apache License

/**
 * Tests writes/* w  ww .  ja  v  a 2s  .  c om*/
 */
@Test
public void testTStatsWritten() throws TTransportException {
    TMemoryBuffer mb = new TMemoryBuffer(1 << 20); // 1 MB memory buffer
    TStatsTransport stats = new TStatsTransport(mb);

    stats.write(new byte[100]);
    Assert.assertEquals(stats.getBytesRead(), 0);
    Assert.assertEquals(stats.getBytesWritten(), 100);

    stats.write(new byte[42]);
    Assert.assertEquals(stats.getBytesRead(), 0);
    Assert.assertEquals(stats.getBytesWritten(), 142);
}

From source file:com.cloudera.flume.handlers.thrift.TestTStatsTransport.java

License:Apache License

/**
 * Does a write to fill the buffer and then tests reads.
 *//* w  w  w  . j  a  va2  s . co m*/
@Test
public void testTStatsRead() throws TTransportException {
    TMemoryBuffer mb = new TMemoryBuffer(1 << 20); // 1 MB memory buffer
    TStatsTransport stats = new TStatsTransport(mb);

    stats.write(new byte[200]);

    stats.read(new byte[100], 0, 100);
    Assert.assertEquals(stats.getBytesRead(), 100);
    Assert.assertEquals(stats.getBytesWritten(), 200);

    stats.read(new byte[42], 0, 42);
    Assert.assertEquals(stats.getBytesRead(), 142);
    Assert.assertEquals(stats.getBytesWritten(), 200);

    // buffer can be under filled but says by how much
    int count = stats.read(new byte[100], 0, 100);
    Assert.assertEquals(58, count);
    Assert.assertEquals(stats.getBytesRead(), 200);
    Assert.assertEquals(stats.getBytesWritten(), 200);
}

From source file:com.facebook.swift.codec.AbstractThriftCodecManagerTest.java

License:Apache License

private <T> T testRoundTripSerialize(ThriftCodec<T> readCodec, ThriftCodec<T> writeCodec, Type structType,
        T structInstance, TProtocolFactory protocolFactory) throws Exception {
    ThriftCatalog readCatalog = readCodecManager.getCatalog();
    ThriftStructMetadata readMetadata = readCatalog.getThriftStructMetadata(structType);
    assertNotNull(readMetadata);/*from www.j av a  2 s  .c  o  m*/

    ThriftCatalog writeCatalog = writeCodecManager.getCatalog();
    ThriftStructMetadata writeMetadata = writeCatalog.getThriftStructMetadata(structType);
    assertNotNull(writeMetadata);

    TMemoryBuffer transport = new TMemoryBuffer(10 * 1024);
    TProtocol protocol = protocolFactory.getProtocol(transport);
    writeCodec.write(structInstance, protocol);

    T copy = readCodec.read(protocol);
    assertNotNull(copy);
    assertEquals(copy, structInstance);

    return copy;
}

From source file:com.facebook.swift.codec.guice.TestThriftCodecModule.java

License:Apache License

public static <T> void testRoundTripSerialize(ThriftCodec<T> codec, T value) throws Exception {
    // write value
    TMemoryBuffer transport = new TMemoryBuffer(10 * 1024);
    TCompactProtocol protocol = new TCompactProtocol(transport);
    codec.write(value, protocol);//from ww  w .  j a  va  2 s.co m

    // read value back
    T copy = codec.read(protocol);
    assertNotNull(copy);

    // verify they are the same
    assertEquals(copy, value);
}

From source file:com.facebook.swift.codec.TestThriftCodecManager.java

License:Apache License

private <T> void testRoundTripSerialize(ThriftType type, T value, TProtocolFactory protocolFactory)
        throws Exception {
    // write value
    TMemoryBuffer transport = new TMemoryBuffer(10 * 1024);
    TProtocol protocol = protocolFactory.getProtocol(transport);
    codecManager.write(type, value, protocol);

    // read value back
    T copy = (T) codecManager.read(type, protocol);
    assertNotNull(copy);/*from   w  ww . j  ava 2s. c  om*/

    // verify they are the same
    assertEquals(copy, value);
}

From source file:com.kromatik.dasshy.server.thrift.JsonSerializer.java

License:Open Source License

@Override
public void write(final T t, final OutputStream outputStream) {
    byte[] bytesEntity;
    TMemoryBuffer memoryBuffer = null;/*from w w  w.  j  a v a 2s.  co m*/
    try {
        memoryBuffer = new TMemoryBuffer(1);
        t.write(new TSimpleJSONProtocol(memoryBuffer));
        memoryBuffer.flush();

        bytesEntity = memoryBuffer.toString("UTF-8").getBytes("UTF-8");

        outputStream.write(bytesEntity);

    } catch (IOException e) {
        throw new SerializationException("Cannot write base entity", e);
    } catch (final Exception e) {
        throw new SerializationException(
                "Failed to serialise Thrift entity to Simple JSON format. Thrift entity toString(): '"
                        + ((t == null) ? "null" : t.toString()) + "'",
                e);
    } finally {
        if (memoryBuffer != null) {
            memoryBuffer.close();
        }
    }
}

From source file:com.kromatik.dasshy.server.thrift.TUtils.java

License:Open Source License

/**
 * Serialize the thrift entity using Json protocol
 *
 * @param tEntity thrift entity/*w  w  w  .  j  av a 2  s. c om*/
 * @return byte[]
 * @throws TException
 */
public static byte[] serializeJson(final TBase tEntity) throws TException {
    final TMemoryBuffer memoryBuffer = new TMemoryBuffer(1);
    tEntity.write(new TJSONProtocol(memoryBuffer));
    memoryBuffer.flush();
    try {
        return memoryBuffer.toString("UTF-8").getBytes("UTF-8");
    } catch (UnsupportedEncodingException e) {
        throw new TException(e);
    } finally {
        memoryBuffer.close();
    }
}

From source file:com.kromatik.dasshy.server.thrift.TUtils.java

License:Open Source License

/**
 * Deserialize into thrift entity using JSON protocol
 *
 * @param bytesEntity byte[] to deserialize
 * @param tBase       thrift entity//from   w  w  w . j a  va  2  s.  c  om
 * @throws TException
 */
public static void deserializeJson(byte[] bytesEntity, final TBase tBase) throws TException {
    final TMemoryBuffer memoryBuffer = new TMemoryBuffer(bytesEntity.length);
    memoryBuffer.write(bytesEntity);
    tBase.read(new TJSONProtocol(memoryBuffer));
    memoryBuffer.close();
}

From source file:com.kromatik.dasshy.server.thrift.TUtils.java

License:Open Source License

/**
 * Serialize the thrift entity using Compact protocol
 *
 * @param tEntity thrift entity//w ww .  j  a  va  2 s .  com
 * @return byte[]
 * @throws TException
 */
public static byte[] serializeCompact(final TBase tEntity) throws TException {
    final TMemoryBuffer memoryBuffer = new TMemoryBuffer(1);
    tEntity.write(new TCompactProtocol(memoryBuffer));
    memoryBuffer.flush();
    try {
        return memoryBuffer.getArray();
    } finally {
        memoryBuffer.close();
    }
}