org.apache.thrift.transport.TMemoryInputTransport Java Examples

The following examples show how to use org.apache.thrift.transport.TMemoryInputTransport. 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: ThriftIDLSerializer.java    From octo-rpc with Apache License 2.0 6 votes vote down vote up
@Override
protected Object deserialize4OctoThrift(byte[] buff, Object obj) throws Exception {
    TMemoryInputTransport transport = new TMemoryInputTransport(buff);
    TBinaryProtocol protocol = new TBinaryProtocol(transport);
    TMessage message = protocol.readMessageBegin();

    Object result;
    if (obj instanceof DefaultRequest) {
        result = doDeserializeRequest((DefaultRequest) obj, message, protocol);
    } else if (obj instanceof DefaultResponse) {
        result = doDeserializeResponse((DefaultResponse) obj, message, protocol);
    } else {
        throw new ProtocolException("Thrift octoProtocol object type is invalid, it should not happen.");
    }
    protocol.readMessageEnd();
    return result;
}
 
Example #2
Source File: ServerLoad.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
public static ServerLoad deserialize(byte[] data) throws IOException {
    org.apache.distributedlog.service.placement.thrift.ServerLoad tServerLoad =
        new org.apache.distributedlog.service.placement.thrift.ServerLoad();
    TMemoryInputTransport transport = new TMemoryInputTransport(data);
    TJSONProtocol protocol = new TJSONProtocol(transport);
    try {
        tServerLoad.read(protocol);
        ServerLoad serverLoad = new ServerLoad(tServerLoad.getServer());
        if (tServerLoad.isSetStreams()) {
            for (org.apache.distributedlog.service.placement.thrift.StreamLoad tStreamLoad :
                tServerLoad.getStreams()) {
                serverLoad.addStream(new StreamLoad(tStreamLoad.getStream(), tStreamLoad.getLoad()));
            }
        }
        return serverLoad;
    } catch (TException e) {
        throw new IOException("Failed to deserialize server load : ", e);
    }
}
 
Example #3
Source File: CallBack.java    From ikasoa with MIT License 5 votes vote down vote up
@Override
public String getResult() throws TException {
	if (getState() != TAsyncMethodCall.State.RESPONSE_READ)
		throw new IllegalStateException("Method call not finished !");
	return (new ServiceClientImpl(
			client.getProtocolFactory().getProtocol(new TMemoryInputTransport(getFrameBuffer().array()))))
					.recvGet();
}
 
Example #4
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 #5
Source File: ThriftTest.java    From Firefly with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldCreateService() {
    TestService service = thrift.create(TestService.class, new Thrift.SimpleTProtocolFactory() {
        @Override
        public TProtocol get() {
            return new TBinaryProtocol(new TMemoryInputTransport(new byte[]{}));
        }
    });
    assertThat(service).isNotNull();
}
 
Example #6
Source File: Utils.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
public static Message parseMessage(byte[] data) throws TException {
    Message msg = new Message();
    TMemoryInputTransport transport = new TMemoryInputTransport(data);
    TBinaryProtocol protocol = new TBinaryProtocol(transport);
    msg.read(protocol);
    return msg;
}
 
Example #7
Source File: ZKAccessControl.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
static AccessControlEntry deserialize(String zkPath, byte[] data) throws IOException {
    if (data.length == 0) {
        return DEFAULT_ACCESS_CONTROL_ENTRY;
    }

    AccessControlEntry ace = new AccessControlEntry();
    TMemoryInputTransport transport = new TMemoryInputTransport(data);
    TJSONProtocol protocol = new TJSONProtocol(transport);
    try {
        ace.read(protocol);
    } catch (TException e) {
        throw new CorruptedAccessControlException(zkPath, e);
    }
    return ace;
}
 
Example #8
Source File: QuasarTokenDecoder.java    From warp10-platform with Apache License 2.0 5 votes vote down vote up
/**
 * Deserialize the given byte array into any type of Thrift tokens
 * This method avoid an explicit cast on the deserialized token
 * @param base The Thrift instance
 * @param bytes the serialized thrift token
 */
private void deserializeThriftToken(TBase<?, ?> base, byte[] bytes) throws TException {
  // Thrift deserialization
  TMemoryInputTransport trans_ = new TMemoryInputTransport();
  TProtocol protocol_ = new TCompactProtocol.Factory().getProtocol(trans_);
  try {
    trans_.reset(bytes);
    // TRASH THE 8 fist bytes (SIP HASH)
    trans_.consumeBuffer(8);
    base.read(protocol_);
  } finally {
    trans_.clear();
    protocol_.reset();
  }
}
 
Example #9
Source File: Utils.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
public static Message parseMessage(byte[] data) throws TException {
    Message msg = new Message();
    TMemoryInputTransport transport = new TMemoryInputTransport(data);
    TBinaryProtocol protocol = new TBinaryProtocol(transport);
    msg.read(protocol);
    return msg;
}
 
Example #10
Source File: ZKAccessControl.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
static AccessControlEntry deserialize(String zkPath, byte[] data) throws IOException {
    if (data.length == 0) {
        return DEFAULT_ACCESS_CONTROL_ENTRY;
    }

    AccessControlEntry ace = new AccessControlEntry();
    TMemoryInputTransport transport = new TMemoryInputTransport(data);
    TJSONProtocol protocol = new TJSONProtocol(transport);
    try {
        ace.read(protocol);
    } catch (TException e) {
        throw new CorruptedAccessControlException(zkPath, e);
    }
    return ace;
}
 
Example #11
Source File: StreamLoad.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
public static StreamLoad deserialize(byte[] data) throws IOException {
    org.apache.distributedlog.service.placement.thrift.StreamLoad tStreamLoad =
        new org.apache.distributedlog.service.placement.thrift.StreamLoad();
    TMemoryInputTransport transport = new TMemoryInputTransport(data);
    TJSONProtocol protocol = new TJSONProtocol(transport);
    try {
        tStreamLoad.read(protocol);
        return new StreamLoad(tStreamLoad.getStream(), tStreamLoad.getLoad());
    } catch (TException e) {
        throw new IOException("Failed to deserialize stream load : ", e);
    }
}
 
Example #12
Source File: ScribeSender.java    From zipkin-finagle with Apache License 2.0 5 votes vote down vote up
@Override public Future<Void> apply(byte[] responseBytes) {
  TBinaryProtocol iprot = new TBinaryProtocol(new TMemoryInputTransport(responseBytes));
  try {
    if (InternalScribeCodec.readLogResponse(0, iprot)) {
      return Future.Void();
    } else {
      return Future.exception(new IllegalStateException("try later"));
    }
  } catch (Exception e) {
    return Future.exception(e);
  }
}
 
Example #13
Source File: ThriftIDLSerializer.java    From octo-rpc with Apache License 2.0 5 votes vote down vote up
@Override
protected Object deserialize4Thrift(byte[] buff, Class<?> iface, Map<String, Object> attachments) throws Exception {
    TraceTimeline timeline = TraceTimeline.newRecord(CommonUtil.objectToBool(attachments.get(Constants.TRACE_IS_RECORD_TIMELINE), false),
            TraceTimeline.DECODE_START_TS);

    Object obj = null;
    TMemoryInputTransport transport = new TMemoryInputTransport(buff);
    TBinaryProtocol protocol = new TBinaryProtocol(transport);
    TMessage message = protocol.readMessageBegin();
    if (message.type == TMessageType.CALL) {
        DefaultRequest request = new DefaultRequest(Long.valueOf(message.seqid));
        request.setServiceInterface(iface);
        RpcInvocation rpcInvocation = doDeserializeRequest(request, message, protocol);
        request.setData(rpcInvocation);
        obj = request;

        rpcInvocation.putAttachment(Constants.TRACE_TIMELINE, timeline);
    } else if (message.type == TMessageType.REPLY || message.type == TMessageType.EXCEPTION) {
        DefaultResponse response = new DefaultResponse(Long.valueOf(message.seqid));
        response.setServiceInterface(iface);
        try {
            RpcResult rpcResult = doDeserializeResponse(response, message, protocol);
            response.setResult(rpcResult);
        } catch (Exception e) {
            response.setException(e);
        }

        obj = response;
        if (response.getRequest() != null && response.getRequest().getData() != null) {
            TraceTimeline.copyRecord(timeline, response.getRequest().getData());
        }
    } else {
        throw new ProtocolException("Thrift deserialize message type is invalid");
    }
    protocol.readMessageEnd();

    MetaUtil.recordTraceInfoInDecode(buff, obj);
    return obj;
}
 
Example #14
Source File: ThriftCodec.java    From singer with Apache License 2.0 4 votes vote down vote up
public PrefixedDeserializer() {
  transport = new TMemoryInputTransport();
  protocol = new TCompactProtocol(transport);
}
 
Example #15
Source File: BKDLConfig.java    From distributedlog with Apache License 2.0 4 votes vote down vote up
@Override
public void deserialize(byte[] data) throws IOException {
    BKDLConfigFormat configFormat = new BKDLConfigFormat();
    TMemoryInputTransport transport = new TMemoryInputTransport(data);
    TJSONProtocol protocol = new TJSONProtocol(transport);
    try {
        configFormat.read(protocol);
    } catch (TException e) {
        throw new IOException("Failed to deserialize data '"
                + new String(data, UTF_8) + "' : ", e);
    }
    // bookkeeper cluster settings
    if (configFormat.isSetBkZkServers()) {
        bkZkServersForWriter = configFormat.getBkZkServers();
    }
    if (configFormat.isSetBkZkServersForReader()) {
        bkZkServersForReader = configFormat.getBkZkServersForReader();
    } else {
        bkZkServersForReader = bkZkServersForWriter;
    }
    if (configFormat.isSetBkLedgersPath()) {
        bkLedgersPath = configFormat.getBkLedgersPath();
    }
    // dl zookeeper cluster settings
    if (configFormat.isSetDlZkServersForWriter()) {
        dlZkServersForWriter = configFormat.getDlZkServersForWriter();
    }
    if (configFormat.isSetDlZkServersForReader()) {
        dlZkServersForReader = configFormat.getDlZkServersForReader();
    } else {
        dlZkServersForReader = dlZkServersForWriter;
    }
    // dl settings
    sanityCheckTxnID = !configFormat.isSetSanityCheckTxnID() || configFormat.isSanityCheckTxnID();
    encodeRegionID = configFormat.isSetEncodeRegionID() && configFormat.isEncodeRegionID();
    if (configFormat.isSetAclRootPath()) {
        aclRootPath = configFormat.getAclRootPath();
    }

    if (configFormat.isSetFirstLogSegmentSeqNo()) {
        firstLogSegmentSeqNo = configFormat.getFirstLogSegmentSeqNo();
    }
    isFederatedNamespace = configFormat.isSetFederatedNamespace() && configFormat.isFederatedNamespace();

    // Validate the settings
    if (null == bkZkServersForWriter || null == bkZkServersForReader || null == bkLedgersPath
            || null == dlZkServersForWriter || null == dlZkServersForReader) {
        throw new IOException("Missing zk/bk settings in BKDL Config : " + new String(data, UTF_8));
    }
}
 
Example #16
Source File: BKDLConfig.java    From distributedlog with Apache License 2.0 4 votes vote down vote up
@Override
public void deserialize(byte[] data) throws IOException {
    BKDLConfigFormat configFormat = new BKDLConfigFormat();
    TMemoryInputTransport transport = new TMemoryInputTransport(data);
    TJSONProtocol protocol = new TJSONProtocol(transport);
    try {
        configFormat.read(protocol);
    } catch (TException e) {
        throw new IOException("Failed to deserialize data '" +
                new String(data, UTF_8) + "' : ", e);
    }
    // bookkeeper cluster settings
    if (configFormat.isSetBkZkServers()) {
        bkZkServersForWriter = configFormat.getBkZkServers();
    }
    if (configFormat.isSetBkZkServersForReader()) {
        bkZkServersForReader = configFormat.getBkZkServersForReader();
    } else {
        bkZkServersForReader = bkZkServersForWriter;
    }
    if (configFormat.isSetBkLedgersPath()) {
        bkLedgersPath = configFormat.getBkLedgersPath();
    }
    // dl zookeeper cluster settings
    if (configFormat.isSetDlZkServersForWriter()) {
        dlZkServersForWriter = configFormat.getDlZkServersForWriter();
    }
    if (configFormat.isSetDlZkServersForReader()) {
        dlZkServersForReader = configFormat.getDlZkServersForReader();
    } else {
        dlZkServersForReader = dlZkServersForWriter;
    }
    // dl settings
    sanityCheckTxnID = !configFormat.isSetSanityCheckTxnID() || configFormat.isSanityCheckTxnID();
    encodeRegionID = configFormat.isSetEncodeRegionID() && configFormat.isEncodeRegionID();
    if (configFormat.isSetAclRootPath()) {
        aclRootPath = configFormat.getAclRootPath();
    }

    if (configFormat.isSetFirstLogSegmentSeqNo()) {
        firstLogSegmentSeqNo = configFormat.getFirstLogSegmentSeqNo();
    }
    isFederatedNamespace = configFormat.isSetFederatedNamespace() && configFormat.isFederatedNamespace();

    // Validate the settings
    if (null == bkZkServersForWriter || null == bkZkServersForReader || null == bkLedgersPath ||
            null == dlZkServersForWriter || null == dlZkServersForReader) {
        throw new IOException("Missing zk/bk settings in BKDL Config : " + new String(data, UTF_8));
    }
}
 
Example #17
Source File: ChunkHeaderTBaseDeserializer.java    From pinpoint with Apache License 2.0 4 votes vote down vote up
ChunkHeaderTBaseDeserializer(TProtocolFactory protocolFactory, TypeLocator<TBase<?, ?>> locator) {
    this.trans = new TMemoryInputTransport();
    this.protocol = protocolFactory.getProtocol(trans);
    this.locator = locator;
}
 
Example #18
Source File: HeaderTBaseDeserializer.java    From pinpoint with Apache License 2.0 2 votes vote down vote up
/**
 * Create a new TDeserializer. It will use the TProtocol specified by the
 * factory that is passed in.
 *
 * @param protocolFactory Factory to create a protocol
 */
HeaderTBaseDeserializer(TProtocolFactory protocolFactory, TypeLocator<TBase<?, ?>> locator) {
    this.trans = new TMemoryInputTransport();
    this.protocol = protocolFactory.getProtocol(trans);
    this.locator = locator;
}