List of usage examples for org.apache.thrift.protocol TProtocol readI16
public abstract short readI16() throws TException;
From source file:com.alibaba.dubbo.rpc.protocol.thrift.ext.MultiServiceProcessor.java
License:Open Source License
public boolean process(TProtocol in, TProtocol out) throws TException { short magic = in.readI16(); if (magic != ThriftCodec.MAGIC) { logger.error(new StringBuilder(24).append("Unsupported magic ").append(magic).toString()); return false; }/*from w w w.jav a 2s . co m*/ in.readI32(); in.readI16(); byte version = in.readByte(); String serviceName = in.readString(); long id = in.readI64(); ByteArrayOutputStream bos = new ByteArrayOutputStream(1024); TIOStreamTransport transport = new TIOStreamTransport(bos); TProtocol protocol = protocolFactory.getProtocol(transport); TProcessor processor = processorMap.get(serviceName); if (processor == null) { logger.error(new StringBuilder(32).append("Could not find processor for service ").append(serviceName) .toString()); return false; } // todo if exception boolean result = processor.process(in, protocol); ByteArrayOutputStream header = new ByteArrayOutputStream(512); TIOStreamTransport headerTransport = new TIOStreamTransport(header); TProtocol headerProtocol = protocolFactory.getProtocol(headerTransport); headerProtocol.writeI16(magic); headerProtocol.writeI32(Integer.MAX_VALUE); headerProtocol.writeI16(Short.MAX_VALUE); headerProtocol.writeByte(version); headerProtocol.writeString(serviceName); headerProtocol.writeI64(id); headerProtocol.getTransport().flush(); out.writeI16(magic); out.writeI32(bos.size() + header.size()); out.writeI16((short) (0xffff & header.size())); out.writeByte(version); out.writeString(serviceName); out.writeI64(id); out.getTransport().write(bos.toByteArray()); out.getTransport().flush(); return result; }
From source file:com.alibaba.dubbo.rpc.protocol.thrift.ThriftCodec.java
License:Open Source License
private Object decode(TProtocol protocol) throws IOException { // version// ww w . j a v a 2s . c o m String serviceName; long id; TMessage message; try { protocol.readI16(); protocol.readByte(); serviceName = protocol.readString(); id = protocol.readI64(); message = protocol.readMessageBegin(); } catch (TException e) { throw new IOException(e.getMessage(), e); } if (message.type == TMessageType.CALL) { RpcInvocation result = new RpcInvocation(); result.setAttachment(Constants.INTERFACE_KEY, serviceName); result.setMethodName(message.name); String argsClassName = ExtensionLoader.getExtensionLoader(ClassNameGenerator.class) .getExtension(ThriftClassNameGenerator.NAME).generateArgsClassName(serviceName, message.name); if (StringUtils.isEmpty(argsClassName)) { throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, "The specified interface name incorrect."); } Class clazz = cachedClass.get(argsClassName); if (clazz == null) { try { clazz = ClassHelper.forNameWithThreadContextClassLoader(argsClassName); cachedClass.putIfAbsent(argsClassName, clazz); } catch (ClassNotFoundException e) { throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e); } } TBase args; try { args = (TBase) clazz.newInstance(); } catch (InstantiationException e) { throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e); } catch (IllegalAccessException e) { throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e); } try { args.read(protocol); protocol.readMessageEnd(); } catch (TException e) { throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e); } List<Object> parameters = new ArrayList<Object>(); List<Class<?>> parameterTypes = new ArrayList<Class<?>>(); int index = 1; while (true) { TFieldIdEnum fieldIdEnum = args.fieldForId(index++); if (fieldIdEnum == null) { break; } String fieldName = fieldIdEnum.getFieldName(); String getMethodName = ThriftUtils.generateGetMethodName(fieldName); Method getMethod; try { getMethod = clazz.getMethod(getMethodName); } catch (NoSuchMethodException e) { throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e); } parameterTypes.add(getMethod.getReturnType()); try { parameters.add(getMethod.invoke(args)); } catch (IllegalAccessException e) { throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e); } catch (InvocationTargetException e) { throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e); } } result.setArguments(parameters.toArray()); result.setParameterTypes(parameterTypes.toArray(new Class[parameterTypes.size()])); Request request = new Request(id); request.setData(result); cachedRequest.putIfAbsent(id, RequestData.create(message.seqid, serviceName, message.name)); return request; } else if (message.type == TMessageType.EXCEPTION) { TApplicationException exception; try { exception = TApplicationException.read(protocol); protocol.readMessageEnd(); } catch (TException e) { throw new IOException(e.getMessage(), e); } RpcResult result = new RpcResult(); result.setException(new RpcException(exception.getMessage())); Response response = new Response(); response.setResult(result); response.setId(id); return response; } else if (message.type == TMessageType.REPLY) { String resultClassName = ExtensionLoader.getExtensionLoader(ClassNameGenerator.class) .getExtension(ThriftClassNameGenerator.NAME).generateResultClassName(serviceName, message.name); if (StringUtils.isEmpty(resultClassName)) { throw new IllegalArgumentException(new StringBuilder(32) .append("Could not infer service result class name from service name ").append(serviceName) .append(", the service name you specified may not generated by thrift idl compiler") .toString()); } Class<?> clazz = cachedClass.get(resultClassName); if (clazz == null) { try { clazz = ClassHelper.forNameWithThreadContextClassLoader(resultClassName); cachedClass.putIfAbsent(resultClassName, clazz); } catch (ClassNotFoundException e) { throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e); } } TBase<?, ? extends TFieldIdEnum> result; try { result = (TBase<?, ?>) clazz.newInstance(); } catch (InstantiationException e) { throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e); } catch (IllegalAccessException e) { throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e); } try { result.read(protocol); protocol.readMessageEnd(); } catch (TException e) { throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e); } Object realResult = null; int index = 0; while (true) { TFieldIdEnum fieldIdEnum = result.fieldForId(index++); if (fieldIdEnum == null) { break; } Field field; try { field = clazz.getDeclaredField(fieldIdEnum.getFieldName()); field.setAccessible(true); } catch (NoSuchFieldException e) { throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e); } try { realResult = field.get(result); } catch (IllegalAccessException e) { throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e); } if (realResult != null) { break; } } Response response = new Response(); response.setId(id); RpcResult rpcResult = new RpcResult(); if (realResult instanceof Throwable) { rpcResult.setException((Throwable) realResult); } else { rpcResult.setValue(realResult); } response.setResult(rpcResult); return response; } else { // Impossible throw new IOException(); } }
From source file:com.ebay.nest.io.sede.dynamic_type.DynamicSerDeTypei16.java
License:Apache License
@Override public Object deserialize(Object reuse, TProtocol iprot) throws SerDeException, TException, IllegalAccessException { int val = iprot.readI16(); if (val == 0 && iprot instanceof com.ebay.nest.io.sede.thrift.WriteNullsProtocol && ((com.ebay.nest.io.sede.thrift.WriteNullsProtocol) iprot).lastPrimitiveWasNull()) { return null; }//from w w w . j ava 2s . c o m return Integer.valueOf(val); }
From source file:com.facebook.swift.codec.internal.builtin.ShortThriftCodec.java
License:Apache License
@Override public Short read(TProtocol protocol) throws Exception { Preconditions.checkNotNull(protocol, "protocol is null"); return protocol.readI16(); }
From source file:com.ning.metrics.serialization.thrift.item.DataItemDeserializer.java
License:Apache License
public DataItem fromThrift(TProtocol protocol, TField field) throws TException { final DataItem dataItem; switch (field.type) { case TType.BOOL: dataItem = new BooleanDataItem(protocol.readBool()); break;/*from w w w. j a v a 2 s. c om*/ case TType.BYTE: dataItem = new ByteDataItem(protocol.readByte()); break; case TType.I16: dataItem = new ShortDataItem(protocol.readI16()); break; case TType.I32: dataItem = new IntegerDataItem(protocol.readI32()); break; case TType.I64: dataItem = new LongDataItem(protocol.readI64()); break; case TType.DOUBLE: dataItem = new DoubleDataItem(protocol.readDouble()); break; case TType.STRING: dataItem = new StringDataItem(protocol.readString()); //TODO: we only allow strings, this won't if data is binary (let presentation layer deal with this) break; default: throw new IllegalArgumentException(String.format("Unknown type %d", field.type)); } return dataItem; }
From source file:ezbake.query.basequeryableprocedure.ColumnDataValues.java
License:Apache License
@Override protected Object standardSchemeReadValue(org.apache.thrift.protocol.TProtocol iprot, org.apache.thrift.protocol.TField field) throws org.apache.thrift.TException { _Fields setField = _Fields.findByThriftId(field.id); if (setField != null) { switch (setField) { case BOOL_VALS: if (field.type == BOOL_VALS_FIELD_DESC.type) { List<Boolean> bool_vals; {//from w w w . ja va 2 s. co m org.apache.thrift.protocol.TList _list8 = iprot.readListBegin(); bool_vals = new ArrayList<Boolean>(_list8.size); for (int _i9 = 0; _i9 < _list8.size; ++_i9) { boolean _elem10; _elem10 = iprot.readBool(); bool_vals.add(_elem10); } iprot.readListEnd(); } return bool_vals; } else { org.apache.thrift.protocol.TProtocolUtil.skip(iprot, field.type); return null; } case BYTE_VALS: if (field.type == BYTE_VALS_FIELD_DESC.type) { List<Byte> byte_vals; { org.apache.thrift.protocol.TList _list11 = iprot.readListBegin(); byte_vals = new ArrayList<Byte>(_list11.size); for (int _i12 = 0; _i12 < _list11.size; ++_i12) { byte _elem13; _elem13 = iprot.readByte(); byte_vals.add(_elem13); } iprot.readListEnd(); } return byte_vals; } else { org.apache.thrift.protocol.TProtocolUtil.skip(iprot, field.type); return null; } case SHORT_VALS: if (field.type == SHORT_VALS_FIELD_DESC.type) { List<Short> short_vals; { org.apache.thrift.protocol.TList _list14 = iprot.readListBegin(); short_vals = new ArrayList<Short>(_list14.size); for (int _i15 = 0; _i15 < _list14.size; ++_i15) { short _elem16; _elem16 = iprot.readI16(); short_vals.add(_elem16); } iprot.readListEnd(); } return short_vals; } else { org.apache.thrift.protocol.TProtocolUtil.skip(iprot, field.type); return null; } case INT_VALS: if (field.type == INT_VALS_FIELD_DESC.type) { List<Integer> int_vals; { org.apache.thrift.protocol.TList _list17 = iprot.readListBegin(); int_vals = new ArrayList<Integer>(_list17.size); for (int _i18 = 0; _i18 < _list17.size; ++_i18) { int _elem19; _elem19 = iprot.readI32(); int_vals.add(_elem19); } iprot.readListEnd(); } return int_vals; } else { org.apache.thrift.protocol.TProtocolUtil.skip(iprot, field.type); return null; } case LONG_VALS: if (field.type == LONG_VALS_FIELD_DESC.type) { List<Long> long_vals; { org.apache.thrift.protocol.TList _list20 = iprot.readListBegin(); long_vals = new ArrayList<Long>(_list20.size); for (int _i21 = 0; _i21 < _list20.size; ++_i21) { long _elem22; _elem22 = iprot.readI64(); long_vals.add(_elem22); } iprot.readListEnd(); } return long_vals; } else { org.apache.thrift.protocol.TProtocolUtil.skip(iprot, field.type); return null; } case DOUBLE_VALS: if (field.type == DOUBLE_VALS_FIELD_DESC.type) { List<Double> double_vals; { org.apache.thrift.protocol.TList _list23 = iprot.readListBegin(); double_vals = new ArrayList<Double>(_list23.size); for (int _i24 = 0; _i24 < _list23.size; ++_i24) { double _elem25; _elem25 = iprot.readDouble(); double_vals.add(_elem25); } iprot.readListEnd(); } return double_vals; } else { org.apache.thrift.protocol.TProtocolUtil.skip(iprot, field.type); return null; } case STRING_VALS: if (field.type == STRING_VALS_FIELD_DESC.type) { List<String> string_vals; { org.apache.thrift.protocol.TList _list26 = iprot.readListBegin(); string_vals = new ArrayList<String>(_list26.size); for (int _i27 = 0; _i27 < _list26.size; ++_i27) { String _elem28; _elem28 = iprot.readString(); string_vals.add(_elem28); } iprot.readListEnd(); } return string_vals; } else { org.apache.thrift.protocol.TProtocolUtil.skip(iprot, field.type); return null; } case BINARY_VALS: if (field.type == BINARY_VALS_FIELD_DESC.type) { List<ByteBuffer> binary_vals; { org.apache.thrift.protocol.TList _list29 = iprot.readListBegin(); binary_vals = new ArrayList<ByteBuffer>(_list29.size); for (int _i30 = 0; _i30 < _list29.size; ++_i30) { ByteBuffer _elem31; _elem31 = iprot.readBinary(); binary_vals.add(_elem31); } iprot.readListEnd(); } return binary_vals; } else { org.apache.thrift.protocol.TProtocolUtil.skip(iprot, field.type); return null; } default: throw new IllegalStateException( "setField wasn't null, but didn't match any of the case statements!"); } } else { org.apache.thrift.protocol.TProtocolUtil.skip(iprot, field.type); return null; } }
From source file:ezbake.query.basequeryableprocedure.ColumnDataValues.java
License:Apache License
@Override protected Object tupleSchemeReadValue(org.apache.thrift.protocol.TProtocol iprot, short fieldID) throws org.apache.thrift.TException { _Fields setField = _Fields.findByThriftId(fieldID); if (setField != null) { switch (setField) { case BOOL_VALS: List<Boolean> bool_vals; { org.apache.thrift.protocol.TList _list40 = iprot.readListBegin(); bool_vals = new ArrayList<Boolean>(_list40.size); for (int _i41 = 0; _i41 < _list40.size; ++_i41) { boolean _elem42; _elem42 = iprot.readBool(); bool_vals.add(_elem42);/*from w w w.j av a2 s . c o m*/ } iprot.readListEnd(); } return bool_vals; case BYTE_VALS: List<Byte> byte_vals; { org.apache.thrift.protocol.TList _list43 = iprot.readListBegin(); byte_vals = new ArrayList<Byte>(_list43.size); for (int _i44 = 0; _i44 < _list43.size; ++_i44) { byte _elem45; _elem45 = iprot.readByte(); byte_vals.add(_elem45); } iprot.readListEnd(); } return byte_vals; case SHORT_VALS: List<Short> short_vals; { org.apache.thrift.protocol.TList _list46 = iprot.readListBegin(); short_vals = new ArrayList<Short>(_list46.size); for (int _i47 = 0; _i47 < _list46.size; ++_i47) { short _elem48; _elem48 = iprot.readI16(); short_vals.add(_elem48); } iprot.readListEnd(); } return short_vals; case INT_VALS: List<Integer> int_vals; { org.apache.thrift.protocol.TList _list49 = iprot.readListBegin(); int_vals = new ArrayList<Integer>(_list49.size); for (int _i50 = 0; _i50 < _list49.size; ++_i50) { int _elem51; _elem51 = iprot.readI32(); int_vals.add(_elem51); } iprot.readListEnd(); } return int_vals; case LONG_VALS: List<Long> long_vals; { org.apache.thrift.protocol.TList _list52 = iprot.readListBegin(); long_vals = new ArrayList<Long>(_list52.size); for (int _i53 = 0; _i53 < _list52.size; ++_i53) { long _elem54; _elem54 = iprot.readI64(); long_vals.add(_elem54); } iprot.readListEnd(); } return long_vals; case DOUBLE_VALS: List<Double> double_vals; { org.apache.thrift.protocol.TList _list55 = iprot.readListBegin(); double_vals = new ArrayList<Double>(_list55.size); for (int _i56 = 0; _i56 < _list55.size; ++_i56) { double _elem57; _elem57 = iprot.readDouble(); double_vals.add(_elem57); } iprot.readListEnd(); } return double_vals; case STRING_VALS: List<String> string_vals; { org.apache.thrift.protocol.TList _list58 = iprot.readListBegin(); string_vals = new ArrayList<String>(_list58.size); for (int _i59 = 0; _i59 < _list58.size; ++_i59) { String _elem60; _elem60 = iprot.readString(); string_vals.add(_elem60); } iprot.readListEnd(); } return string_vals; case BINARY_VALS: List<ByteBuffer> binary_vals; { org.apache.thrift.protocol.TList _list61 = iprot.readListBegin(); binary_vals = new ArrayList<ByteBuffer>(_list61.size); for (int _i62 = 0; _i62 < _list61.size; ++_i62) { ByteBuffer _elem63; _elem63 = iprot.readBinary(); binary_vals.add(_elem63); } iprot.readListEnd(); } return binary_vals; default: throw new IllegalStateException( "setField wasn't null, but didn't match any of the case statements!"); } } else { throw new TProtocolException("Couldn't find a field with field id " + fieldID); } }
From source file:ezbake.thrift.utils.xml2thrift.util.ThriftUtils.java
License:Apache License
private static Object readSingleFieldNoTag(TProtocol proto, Field field) throws TException { switch (field.getType()) { case TType.BOOL: return proto.readBool(); case TType.BYTE: return proto.readByte(); case TType.I16: return proto.readI16(); case TType.I32: return proto.readI32(); case TType.ENUM: return field.getEnumValueOf(proto.readI32()); case TType.I64: return proto.readI64(); case TType.DOUBLE: return proto.readDouble(); case TType.STRING: return field.isBuffer() ? proto.readBinary() : proto.readString(); case TType.STRUCT: TBase<?, ?> tObj = field.gettStructDescriptor().newThriftObject(); tObj.read(proto);// w ww.ja v a2 s . c o m return tObj; default: throw new IllegalArgumentException("Unexpected type : " + field.getType()); } }
From source file:net.morimekta.providence.thrift.TProtocolSerializer.java
License:Apache License
private Object readTypedValue(byte tType, PDescriptor type, TProtocol protocol) throws TException, SerializerException { switch (tType) { case TType.BOOL: return protocol.readBool(); case TType.BYTE: return protocol.readByte(); case TType.I16: return protocol.readI16(); case TType.I32: if (PType.ENUM == type.getType()) { PEnumDescriptor<?> et = (PEnumDescriptor<?>) type; PEnumBuilder<?> eb = et.builder(); int value = protocol.readI32(); eb.setByValue(value);/* w w w .j a va2 s . co m*/ if (!eb.valid() && readStrict) { throw new SerializerException("Invalid enum value " + value + " for " + et.getQualifiedName()); } return eb.build(); } else { return protocol.readI32(); } case TType.I64: return protocol.readI64(); case TType.DOUBLE: return protocol.readDouble(); case TType.STRING: if (type == PPrimitive.BINARY) { ByteBuffer buffer = protocol.readBinary(); return Binary.wrap(buffer.array()); } return protocol.readString(); case TType.STRUCT: return readMessage(protocol, (PMessageDescriptor<?, ?>) type); case TType.LIST: TList listInfo = protocol.readListBegin(); PList<Object> lDesc = (PList<Object>) type; PDescriptor liDesc = lDesc.itemDescriptor(); PList.Builder<Object> list = lDesc.builder(); for (int i = 0; i < listInfo.size; ++i) { list.add(readTypedValue(listInfo.elemType, liDesc, protocol)); } protocol.readListEnd(); return list.build(); case TType.SET: TSet setInfo = protocol.readSetBegin(); PSet<Object> sDesc = (PSet<Object>) type; PDescriptor siDesc = sDesc.itemDescriptor(); PSet.Builder<Object> set = sDesc.builder(); for (int i = 0; i < setInfo.size; ++i) { set.add(readTypedValue(setInfo.elemType, siDesc, protocol)); } protocol.readSetEnd(); return set.build(); case TType.MAP: TMap mapInfo = protocol.readMapBegin(); PMap<Object, Object> mDesc = (PMap<Object, Object>) type; PDescriptor mkDesc = mDesc.keyDescriptor(); PDescriptor miDesc = mDesc.itemDescriptor(); PMap.Builder<Object, Object> map = mDesc.builder(); for (int i = 0; i < mapInfo.size; ++i) { Object key = readTypedValue(mapInfo.keyType, mkDesc, protocol); Object val = readTypedValue(mapInfo.valueType, miDesc, protocol); map.put(key, val); } protocol.readMapEnd(); return map.build(); default: throw new SerializerException("Unsupported protocol field type: " + tType); } }
From source file:org.apache.dubbo.rpc.protocol.thrift.ext.MultiServiceProcessor.java
License:Apache License
@Override public boolean process(TProtocol in, TProtocol out) throws TException { short magic = in.readI16(); if (magic != ThriftCodec.MAGIC) { logger.error("Unsupported magic " + magic); return false; }/*from w w w . jav a 2 s . c o m*/ in.readI32(); in.readI16(); byte version = in.readByte(); String serviceName = in.readString(); long id = in.readI64(); ByteArrayOutputStream bos = new ByteArrayOutputStream(1024); TIOStreamTransport transport = new TIOStreamTransport(bos); TProtocol protocol = protocolFactory.getProtocol(transport); TProcessor processor = processorMap.get(serviceName); if (processor == null) { logger.error("Could not find processor for service " + serviceName); return false; } // todo if exception boolean result = processor.process(in, protocol); ByteArrayOutputStream header = new ByteArrayOutputStream(512); TIOStreamTransport headerTransport = new TIOStreamTransport(header); TProtocol headerProtocol = protocolFactory.getProtocol(headerTransport); headerProtocol.writeI16(magic); headerProtocol.writeI32(Integer.MAX_VALUE); headerProtocol.writeI16(Short.MAX_VALUE); headerProtocol.writeByte(version); headerProtocol.writeString(serviceName); headerProtocol.writeI64(id); headerProtocol.getTransport().flush(); out.writeI16(magic); out.writeI32(bos.size() + header.size()); out.writeI16((short) (0xffff & header.size())); out.writeByte(version); out.writeString(serviceName); out.writeI64(id); out.getTransport().write(bos.toByteArray()); out.getTransport().flush(); return result; }