Java Code Examples for org.apache.thrift.protocol.TType#STRUCT

The following examples show how to use org.apache.thrift.protocol.TType#STRUCT . 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: MockResult.java    From thrift-mock with Apache License 2.0 5 votes vote down vote up
private void readSuccessStruct(org.apache.thrift.protocol.TField schemeField,
                               org.apache.thrift.protocol.TProtocol iprot,
                               MockResult struct) throws org.apache.thrift.TException {
  if (schemeField.type == TType.STRUCT) {
    try {
      struct.success = struct.success.getClass().newInstance();
    } catch (Exception e) {
      throw new IllegalArgumentException("init object failed:" + struct.success.getClass() + e);
    }
    struct.success.read(iprot);
    struct.setSuccessIsSet(true);
  } else {
    org.apache.thrift.protocol.TProtocolUtil.skip(iprot, schemeField.type);
  }
}
 
Example 2
Source File: TSimpleJSONProtocol.java    From nettythrift with Apache License 2.0 4 votes vote down vote up
/**
 * Reading methods.
 */
public TMessage readMessageBegin() throws TException {
	byte[] buf = new byte[256];
	ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
	while (true) {
		int readLen = trans_.read(buf, 0, buf.length);
		if (readLen == 0) {
			break;
		}
		out.write(buf, 0, readLen);
		if (readLen < buf.length) {
			break;
		}
	}
	String sb = null;
	try {
		buf = out.toByteArray();
		sb = new String(buf, "UTF-8");
	} catch (UnsupportedEncodingException e1) {
		e1.printStackTrace();
	}
	// System.out.println("读取完毕: sb=" + sb);
	// TODO JSON 格式的检查
	if (sb.charAt(0) != '[' || sb.charAt(sb.length() - 1) != ']') {
		throw new TProtocolException(TProtocolException.INVALID_DATA, "bad format!");
	}
	JSONArray jsonArray = new JSONArray(sb);
	TMessage msg = new TMessage(jsonArray.getString(0), (byte) jsonArray.getInt(1), jsonArray.getInt(2));
	// System.out.println(msg + ", jsonArray.len = " + jsonArray.length());

	if (jsonArray.length() > 3) {
		if (argsTBaseClass == null) {
			try {
				argsTBaseClass = guessTBaseClassByMethodName(msg.name);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		if (argsTBaseClass == null) {
			// throw new
			// TProtocolException(TApplicationException.UNKNOWN_METHOD,
			// "Invalid method name: '" + msg.name + "'");
			return new TMessage(msg.name, TMessageType.EXCEPTION, msg.seqid);
		}
		@SuppressWarnings("unchecked")
		StructMetaData meta = new StructMetaData(TType.STRUCT, argsTBaseClass);
		msgStruct = new BaseArray(meta, (ArrayJson) jsonArray.get(3));
	}
	return msg;
}
 
Example 3
Source File: BaseArray.java    From nettythrift with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
public BaseArray(FieldValueMetaData meta, ArrayJson obj) {
	this.obj = obj;
	this.metaData = meta;
	ARRAY_SIZE = obj.arraySize();
	int addStep = 1;
	if (meta.type == TType.STRUCT) {
		StructMetaData sm = (StructMetaData) meta;
		Map<TFieldIdEnum, FieldMetaData> map = (Map<TFieldIdEnum, FieldMetaData>) FieldMetaData
				.getStructMetaDataMap(sm.structClass);

		if (obj instanceof JSONObject) {
			this.fieldIndex = 1;
			addStep = 2;
			if (map != null && map.size() > 0) {
				elementMetas = new HashMap<String, Map.Entry<TFieldIdEnum, FieldMetaData>>(map.size());
				for (Map.Entry<TFieldIdEnum, FieldMetaData> m : map.entrySet()) {
					TFieldIdEnum k = m.getKey();
					// fieldName <-> metaData
					elementMetas.put(k.getFieldName(), m);
					// id <-> metaData
					elementMetas.put(String.valueOf(k.getThriftFieldId()), m);
				}
			}
			String fieldName = obj.getString(0);
			if (!useId && fieldName.length() > 0) {
				char c0 = fieldName.charAt(0);
				useId = c0 >= '0' && c0 <= '9';
			}
		} else {
			elementMetaArr = map.entrySet().toArray(new Map.Entry[0]);
			Arrays.sort(elementMetaArr, new Comparator<Map.Entry<TFieldIdEnum, FieldMetaData>>() {
				@Override
				public int compare(Entry<TFieldIdEnum, FieldMetaData> o1, Entry<TFieldIdEnum, FieldMetaData> o2) {
					return o1.getKey().getThriftFieldId() - o2.getKey().getThriftFieldId();
				}
			});
		}
	}
	this.addStep = addStep;
}
 
Example 4
Source File: ProtocolReadToWrite.java    From parquet-mr with Apache License 2.0 4 votes vote down vote up
void readOneValue(TProtocol in, TProtocol out, byte type)
    throws TException {
  switch (type) {
  case TType.LIST:
    readOneList(in, out);
    break;
  case TType.MAP:
    readOneMap(in, out);
    break;
  case TType.SET:
    readOneSet(in, out);
    break;
  case TType.STRUCT:
    readOneStruct(in, out);
    break;
  case TType.STOP:
    break;
  case TType.BOOL:
    out.writeBool(in.readBool());
    break;
  case TType.BYTE:
    out.writeByte(in.readByte());
    break;
  case TType.DOUBLE:
    out.writeDouble(in.readDouble());
    break;
  case TType.I16:
    out.writeI16(in.readI16());
    break;
  case TType.ENUM: // same as i32 => actually never seen in the protocol layer as enums are written as a i32 field
  case TType.I32:
    out.writeI32(in.readI32());
    break;
  case TType.I64:
    out.writeI64(in.readI64());
    break;
  case TType.STRING:
    out.writeBinary(in.readBinary());
    break;
  case TType.VOID:
    break;
  default:
    throw new TException("Unknown type: " + type);
  }
}
 
Example 5
Source File: BufferedProtocolReadToWrite.java    From parquet-mr with Apache License 2.0 4 votes vote down vote up
/**
 * @return true when all value is consumed, false when some values is ignored due to the field is not defined in expectedType
 * @throws TException
 */
private boolean readOneValue(TProtocol in, byte type, List<Action> buffer, ThriftType expectedType) throws TException {
  if (expectedType != null && expectedType.getType().getSerializedThriftType() != type) {
    throw new DecodingSchemaMismatchException("the data type does not match the expected thrift structure: expected " + expectedType + " got " + typeName(type));
  }
  boolean hasFieldsIgnored = false;
  switch (type) {
  case TType.LIST:
    hasFieldsIgnored = readOneList(in, buffer, (ListType)expectedType);
    break;
  case TType.MAP:
    hasFieldsIgnored = readOneMap(in, buffer, (MapType)expectedType);
    break;
  case TType.SET:
    hasFieldsIgnored = readOneSet(in, buffer, (SetType)expectedType);
    break;
  case TType.STRUCT:
    hasFieldsIgnored = readOneStruct(in, buffer, (StructType)expectedType);
    break;
  case TType.STOP:
    break;
  case TType.BOOL:
    final boolean bool = in.readBool();
    writeBoolAction(buffer, bool);
    break;
  case TType.BYTE:
    final byte b = in.readByte();
    writeByteAction(buffer, b);
    break;
  case TType.DOUBLE:
    final double d = in.readDouble();
    writeDoubleAction(buffer, d);
    break;
  case TType.I16:
    final short s = in.readI16();
    writeShortAction(buffer, s);
    break;
  case TType.ENUM: // same as i32 => actually never seen in the protocol layer as enums are written as a i32 field
  case TType.I32:
    final int i = in.readI32();
    checkEnum(expectedType,i);
    writeIntAction(buffer, i);
    break;
  case TType.I64:
    final long l = in.readI64();
    writeLongAction(buffer, l);
    break;
  case TType.STRING:
    final ByteBuffer bin = in.readBinary();
    writeStringAction(buffer, bin);
    break;
  case TType.VOID:
    break;
  default:
    throw new TException("Unknown type: " + type);
  }
  return hasFieldsIgnored;
}