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

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

Introduction

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

Prototype

public TMemoryInputTransport(byte[] buf, int offset, int length) 

Source Link

Usage

From source file:com.linecorp.armeria.client.thrift.ThriftClientDelegate.java

License:Apache License

private Object decodeResponse(ThriftFunction method, HttpData content) throws TException {
    if (content.isEmpty()) {
        if (method.isOneway()) {
            return null;
        }//from   w w  w.jav  a  2s. c o  m
        throw new TApplicationException(TApplicationException.MISSING_RESULT);
    }

    final TMemoryInputTransport inputTransport = new TMemoryInputTransport(content.array(), content.offset(),
            content.length());
    final TProtocol inputProtocol = protocolFactory.getProtocol(inputTransport);

    final TMessage msg = inputProtocol.readMessageBegin();
    if (msg.type == TMessageType.EXCEPTION) {
        TApplicationException ex = TApplicationException.read(inputProtocol);
        inputProtocol.readMessageEnd();
        throw ex;
    }

    if (!method.name().equals(msg.name)) {
        throw new TApplicationException(TApplicationException.WRONG_METHOD_NAME, msg.name);
    }
    TBase<? extends TBase<?, ?>, TFieldIdEnum> result = method.newResult();
    result.read(inputProtocol);
    inputProtocol.readMessageEnd();

    for (TFieldIdEnum fieldIdEnum : method.exceptionFields()) {
        if (result.isSet(fieldIdEnum)) {
            throw (TException) result.getFieldValue(fieldIdEnum);
        }
    }

    TFieldIdEnum successField = method.successField();
    if (successField == null) { // void method
        return null;
    }
    if (result.isSet(successField)) {
        return result.getFieldValue(successField);
    }

    throw new TApplicationException(TApplicationException.MISSING_RESULT,
            result.getClass().getName() + '.' + successField.getFieldName());
}

From source file:com.linecorp.armeria.client.thrift.THttpClientDelegate.java

License:Apache License

private void handle(ClientRequestContext ctx, DefaultRpcResponse reply, ThriftFunction func, HttpData content)
        throws TException {

    if (func.isOneWay()) {
        handleSuccess(ctx, reply, null, null);
        return;//from   w w  w .ja v a  2  s.co m
    }

    if (content.isEmpty()) {
        throw new TApplicationException(TApplicationException.MISSING_RESULT);
    }

    final TMemoryInputTransport inputTransport = new TMemoryInputTransport(content.array(), content.offset(),
            content.length());
    final TProtocol inputProtocol = protocolFactory.getProtocol(inputTransport);

    final TMessage header = inputProtocol.readMessageBegin();
    final TApplicationException appEx = readApplicationException(func, inputProtocol, header);
    if (appEx != null) {
        handleException(ctx, reply, new ThriftReply(header, appEx), appEx);
        return;
    }

    TBase<? extends TBase<?, ?>, TFieldIdEnum> result = func.newResult();
    result.read(inputProtocol);
    inputProtocol.readMessageEnd();

    final ThriftReply rawResponseContent = new ThriftReply(header, result);

    for (TFieldIdEnum fieldIdEnum : func.exceptionFields()) {
        if (result.isSet(fieldIdEnum)) {
            final TException cause = (TException) ThriftFieldAccess.get(result, fieldIdEnum);
            handleException(ctx, reply, rawResponseContent, cause);
            return;
        }
    }

    final TFieldIdEnum successField = func.successField();
    if (successField == null) { // void method
        handleSuccess(ctx, reply, null, rawResponseContent);
        return;
    }

    if (result.isSet(successField)) {
        final Object returnValue = ThriftFieldAccess.get(result, successField);
        handleSuccess(ctx, reply, returnValue, rawResponseContent);
        return;
    }

    handleException(ctx, reply, rawResponseContent,
            new TApplicationException(TApplicationException.MISSING_RESULT,
                    result.getClass().getName() + '.' + successField.getFieldName()));
}

From source file:org.apache.accumulo.core.util.ThriftMessageUtil.java

License:Apache License

/**
 * Deserializes a message into the provided {@code instance} from {@code serialized}
 *
 * @param serialized//from  ww w  .  ja  va 2s.c o m
 *          The serialized representation of the object
 * @param instance
 *          An instance of the object to reconstitute
 * @return The reconstituted instance provided
 * @throws IOException
 *           When deserialization fails
 */
public <T extends TBase<?, ?>> T deserialize(byte[] serialized, int offset, int length, T instance)
        throws IOException {
    requireNonNull(instance);
    TCompactProtocol proto = new TCompactProtocol(new TMemoryInputTransport(serialized, offset, length));
    try {
        instance.read(proto);
    } catch (TException e) {
        throw new IOException(e);
    }
    return instance;
}