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

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

Introduction

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

Prototype

@Override
    public void close() 

Source Link

Usage

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;
    try {/*from   w  w w  .  ja  v  a 2  s . c om*/
        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/*from  w w  w  .  j  a va 2s  .  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//  ww  w.  ja v  a2s  . c  o  m
 * @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/*from ww w.  j  a va 2  s .c o m*/
 * @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();
    }
}

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

License:Open Source License

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

From source file:com.nearinfinity.blur.analysis.BlurAnalyzer.java

License:Apache License

public String toJSON() {
    TMemoryBuffer trans = new TMemoryBuffer(1024);
    TJSONProtocol protocol = new TJSONProtocol(trans);
    try {/* w ww . j  a  v  a  2 s  .  com*/
        _analyzerDefinition.write(protocol);
    } catch (TException e) {
        throw new RuntimeException(e);
    }
    trans.close();
    byte[] array = trans.getArray();
    return new String(array, 0, trans.length());
}

From source file:com.nearinfinity.blur.utils.BlurUtil.java

License:Apache License

public static byte[] read(TBase<?, ?> base) {
    if (base == null) {
        return null;
    }// w  w  w  . j av  a  2 s  . c  o m
    TMemoryBuffer trans = new TMemoryBuffer(1024);
    TJSONProtocol protocol = new TJSONProtocol(trans);
    try {
        base.write(protocol);
    } catch (TException e) {
        throw new RuntimeException(e);
    }
    trans.close();
    byte[] buf = new byte[trans.length()];
    System.arraycopy(trans.getArray(), 0, buf, 0, trans.length());
    return buf;
}

From source file:com.nearinfinity.blur.utils.BlurUtil.java

License:Apache License

public static void write(byte[] data, TBase<?, ?> base) {
    nullCheck(null, "Data cannot be null.");
    TMemoryBuffer trans = new TMemoryBuffer(1024);
    TJSONProtocol protocol = new TJSONProtocol(trans);
    try {/*from w ww .  j  a v  a  2  s  .  c  o  m*/
        trans.write(data);
        base.read(protocol);
    } catch (TException e) {
        throw new RuntimeException(e);
    }
    trans.close();
}