Java Code Examples for org.apache.thrift.TBase#read()

The following examples show how to use org.apache.thrift.TBase#read() . 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: TServiceClientNoPrint.java    From rpc-benchmark with Apache License 2.0 6 votes vote down vote up
@Override
protected void receiveBase(TBase<?, ?> result, String methodName) throws TException {
	TMessage msg = iprot_.readMessageBegin();
	if (msg.type == TMessageType.EXCEPTION) {
		TApplicationException x = new TApplicationException();
		x.read(iprot_);
		iprot_.readMessageEnd();
		throw x;
	}
	// System.out.format("Received %d%n", msg.seqid);
	if (msg.seqid != seqid_) {
		throw new TApplicationException(TApplicationException.BAD_SEQUENCE_ID, String.format(
				"%s failed: out of sequence response: expected %d but got %d", methodName, seqid_, msg.seqid));
	}
	result.read(iprot_);
	iprot_.readMessageEnd();
}
 
Example 2
Source File: HeaderTBaseDeserializer.java    From pinpoint with Apache License 2.0 6 votes vote down vote up
private Message<TBase<?, ?>> readInternal() throws TException {
    final HeaderReader reader = newHeaderReader();
    final Header header = readHeader(reader);
    final HeaderEntity headerEntity = readHeaderEntity(reader, header);
    skipHeaderOffset(reader);


    final TBase<?, ?> base = locator.bodyLookup(header.getType());
    if (base == null) {
        throw new TException("base must not be null type:" + header.getType());
    }

    base.read(protocol);

    return new DefaultMessage<TBase<?, ?>>(header, headerEntity, base);
}
 
Example 3
Source File: ThriftCodec.java    From singer with Apache License 2.0 5 votes vote down vote up
/**
 * Deserialize the Thrift object from a byte array.
 *
 * @param base The object to read into
 * @param bytes The array to read from
 */
public void deserialize(TBase base, byte[] bytes) throws TException {
  try {
    if (bytes.length == 0) {
      return;
    }
    if (bytes[0] == PrefixedSerializer.SECRET_BYTE) {
      if (bytes.length == 1) {
        throw new TException("Unknown prefixed protocol with byte length 1.");
      }
      switch (bytes[1]) {
        case PrefixedSerializer.COMPACT_PROTOCOL_BYTE:
          transport.reset(bytes, 2, bytes.length - 2);
          base.read(protocol);
          break;
        default:
          throw new TException("Unknown protocol with byte: " + bytes[1]);
      }
    } else {
      // Default to TBinaryProtocol decoder.
      getInstance().decoder.get().deserialize(base, bytes);
    }
  } finally {
    transport.reset(null, 0, 0);
    protocol.reset();
  }
}
 
Example 4
Source File: ThriftIDLSerializer.java    From octo-rpc with Apache License 2.0 5 votes vote down vote up
private void deserializeArguments(String ifaceName, String methodName, TProtocol protocol, List<Object> arguments, List<Class<?>> parameterTypes) {
    String argsClassName = ThriftUtil.generateArgsClassName(
            ifaceName, methodName);
    TBase argsClassObj = getClazzInstance(argsClassName);
    try {
        argsClassObj.read(protocol);
    } catch (TException e) {
        throw new ProtocolException("Thrift deserialize arguments failed.", e);
    }

    String argsFieldsClassName = ThriftUtil.generateIDLFieldsClassName(argsClassName);
    Class<?> argsFieldsClazz = getClazz(argsFieldsClassName);

    if (argsFieldsClazz.getEnumConstants() == null) {
        return;
    }
    for (Object fieldEnum : argsFieldsClazz.getEnumConstants()) {
        TFieldIdEnum fieldIdEnum = (TFieldIdEnum) fieldEnum;
        if (fieldIdEnum == null) {
            continue;
        }
        Object argument = argsClassObj.getFieldValue(fieldIdEnum);
        String fieldName = fieldIdEnum.getFieldName();
        Method getMethod = ThriftUtil.obtainGetMethod(argsClassObj.getClass(), fieldName);
        if (BYTE_ARRAY_CLASS_NAME.equals(getMethod.getReturnType().getName())) {
            parameterTypes.add(ByteBuffer.class);
            arguments.add(ByteBuffer.wrap((byte[]) argument));
        } else {
            parameterTypes.add(getMethod.getReturnType());
            arguments.add(argument);
        }
    }
}
 
Example 5
Source File: ThriftIDLSerializer.java    From octo-rpc with Apache License 2.0 5 votes vote down vote up
private Object deserializeResult(TProtocol protocol, String ifaceName, String methodName) {
    String resultClassName = ThriftUtil.generateResultClassName(ifaceName, methodName);
    TBase resultClassObj = getClazzInstance(resultClassName);
    try {
        resultClassObj.read(protocol);
    } catch (TException e) {
        throw new ProtocolException("Thrift deserialize result failed.", e);
    }
    Object realResult = null;
    String resultFieldsClassName = ThriftUtil.generateIDLFieldsClassName(resultClassObj.getClass().getName());
    Class<?> resultFieldsClazz = getClazz(resultFieldsClassName);
    Object[] resultFieldsEnums = resultFieldsClazz.getEnumConstants();
    if (resultFieldsEnums == null) {
        return realResult;
    }
    // 避免基本类型默认值导致异常返回被忽略, 从后面开始获取值
    for (int i = resultFieldsEnums.length - 1; i >= 0; i--) {
        TFieldIdEnum fieldIdEnum = (TFieldIdEnum) resultFieldsEnums[i];
        if (fieldIdEnum == null) {
            continue;
        }
        Object fieldValue = resultClassObj.getFieldValue(fieldIdEnum);
        if (fieldValue != null) {
            if (BYTE_ARRAY_CLASS_NAME.equals(fieldValue.getClass().getName())) {
                fieldValue = ByteBuffer.wrap((byte[]) fieldValue);
            }
            realResult = fieldValue;
            break;
        }
    }
    return realResult;
}
 
Example 6
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 7
Source File: ThriftCoder.java    From beam with Apache License 2.0 5 votes vote down vote up
/**
 * Decodes a value of type {@code T} from the given input stream using provided {@link
 * ThriftCoder#protocolFactory}. Returns the decoded value.
 *
 * @param inStream stream of input values to be decoded
 * @throws IOException if reading from the {@code InputStream} fails for some reason
 * @throws CoderException if the value could not be decoded for some reason
 * @return {@link TBase} decoded object
 */
@Override
public T decode(InputStream inStream) throws CoderException, IOException {
  try {
    TProtocol protocol = protocolFactory.getProtocol(new TIOStreamTransport(inStream));
    TBase<?, ?> value = (TBase<?, ?>) type.getDeclaredConstructor().newInstance();
    value.read(protocol);
    return (T) value;
  } catch (Exception te) {
    throw new CoderException("Could not read value. Error: " + te.getMessage());
  }
}
 
Example 8
Source File: TestProtocolReadToWrite.java    From parquet-mr with Apache License 2.0 5 votes vote down vote up
private void writeReadCompare(TBase<?, ?> a)
        throws TException, InstantiationException, IllegalAccessException {
  ProtocolPipe[] pipes = {new ProtocolReadToWrite(), new BufferedProtocolReadToWrite(ThriftSchemaConverter.toStructType((Class<TBase<?, ?>>)a.getClass()))};
  for (ProtocolPipe p : pipes) {
    final ByteArrayOutputStream in = new ByteArrayOutputStream();
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    a.write(protocol(in));
    p.readOne(protocol(new ByteArrayInputStream(in.toByteArray())), protocol(out));
    TBase<?, ?> b = a.getClass().newInstance();
    b.read(protocol(new ByteArrayInputStream(out.toByteArray())));

    assertEquals(p.getClass().getSimpleName(), a, b);
  }
}
 
Example 9
Source File: ChunkHeaderTBaseDeserializer.java    From pinpoint with Apache License 2.0 5 votes vote down vote up
private Message<TBase<?, ?>> readInternal() throws TException {
    final HeaderReader reader = newHeaderReader();
    final Header header = readHeader(reader);
    final HeaderEntity headerEntity = readHeaderEntity(reader, header);
    skipHeaderOffset(reader);


    final TBase<?, ?> base = locator.bodyLookup(header.getType());
    if (base == null) {
        throw new TException("base must not be null type:" + header.getType());
    }
    base.read(protocol);
    return new DefaultMessage<TBase<?, ?>>(header, headerEntity, base);
}
 
Example 10
Source File: ThriftUtil.java    From buck with Apache License 2.0 5 votes vote down vote up
public static void deserialize(ThriftProtocol protocol, InputStream source, TBase<?, ?> dest)
    throws ThriftException {
  try (TIOStreamTransport responseTransport = new TIOStreamTransport(source)) {
    TProtocol responseProtocol = newProtocolInstance(protocol, responseTransport);
    dest.read(responseProtocol);
  } catch (TException e) {
    throw new ThriftException(e);
  }
}
 
Example 11
Source File: ThriftUtil.java    From buck with Apache License 2.0 5 votes vote down vote up
/** Deserialize a message from a ByteBuffer. */
public static void deserialize(ThriftProtocol protocol, ByteBuffer source, TBase<?, ?> dest)
    throws ThriftException {
  try (TTransport responseTransport = new TByteBuffer(source)) {
    TProtocol responseProtocol = newProtocolInstance(protocol, responseTransport);
    dest.read(responseProtocol);
  } catch (TException e) {
    throw new ThriftException(e);
  }
}