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

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

Introduction

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

Prototype

public String toString(Charset charset) 

Source Link

Document

Output the contents of the memory buffer as a String, using the supplied encoding

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 {//  w w  w  .  j  a v  a  2  s  .  c  o  m
        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  ww  . j a  v 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.twitter.distributedlog.acl.ZKAccessControl.java

License:Apache License

static byte[] serialize(AccessControlEntry ace) throws IOException {
    TMemoryBuffer transport = new TMemoryBuffer(BUFFER_SIZE);
    TJSONProtocol protocol = new TJSONProtocol(transport);
    try {//from   www  .j a va2  s .  c  o  m
        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);
    }
}

From source file:com.twitter.distributedlog.metadata.BKDLConfig.java

License:Apache License

String serialize(BKDLConfigFormat configFormat) {
    TMemoryBuffer transport = new TMemoryBuffer(BUFFER_SIZE);
    TJSONProtocol protocol = new TJSONProtocol(transport);
    try {//ww w  . jav a  2 s  . c  o  m
        configFormat.write(protocol);
        transport.flush();
        return transport.toString("UTF-8");
    } catch (TException e) {
        throw new RuntimeException("Failed to serialize BKDLConfig : ", e);
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException("Failed to serialize BKDLConfig : ", e);
    }
}

From source file:org.apache.distributedlog.service.placement.ServerLoad.java

License:Apache License

public byte[] serialize() throws IOException {
    TMemoryBuffer transport = new TMemoryBuffer(BUFFER_SIZE);
    TJSONProtocol protocol = new TJSONProtocol(transport);
    try {/*w  w w .  ja  va 2s  .c  o m*/
        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);
    }
}

From source file:org.apache.distributedlog.service.placement.StreamLoad.java

License:Apache License

public byte[] serialize() throws IOException {
    TMemoryBuffer transport = new TMemoryBuffer(BUFFER_SIZE);
    TJSONProtocol protocol = new TJSONProtocol(transport);
    try {/*from ww  w  .  jav a 2s. c  o  m*/
        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);
    }
}

From source file:org.apache.hadoop.hive.metastore.hbase.HBaseReadWrite.java

License:Apache License

private String dumpThriftObject(TBase obj) throws TException, UnsupportedEncodingException {
    TMemoryBuffer buf = new TMemoryBuffer(1000);
    TProtocol protocol = new TSimpleJSONProtocol(buf);
    obj.write(protocol);/*from www.j a  v  a 2 s  .  c  o m*/
    return buf.toString("UTF-8");
}

From source file:org.apache.hadoop.hive.ql.QueryPlan.java

License:Apache License

public String toThriftJSONString() throws IOException {
    org.apache.hadoop.hive.ql.plan.api.Query q = getQueryPlan();
    TMemoryBuffer tmb = new TMemoryBuffer(q.toString().length() * 5);
    TJSONProtocol oprot = new TJSONProtocol(tmb);
    try {/* w w w.ja va2s.  c om*/
        q.write(oprot);
    } catch (TException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return q.toString();
    }
    return tmb.toString("UTF-8");
}

From source file:tech.sirwellington.alchemy.thrift.ThriftMemory.java

License:Apache License

/**
 * This function properly extracts the Buffer from the {@link  TMemoryBuffer}, since the built-in {@link TMemoryBuffer#getArray()
 * } is broken.//from w w  w . j  a  va  2s  . c  o m
 *
 * @param buffer
 *
 * @return A properly formatted buffer, without the trailing characters.
 *
 * @throws UnsupportedEncodingException
 */
public static byte[] readBufferFrom(TMemoryBuffer buffer) throws UnsupportedEncodingException {
    checkThat(buffer).is(notNull());

    return buffer.toString(UTF_8.name()).getBytes(UTF_8);
}