Example usage for org.apache.thrift.protocol TProtocolException INVALID_DATA

List of usage examples for org.apache.thrift.protocol TProtocolException INVALID_DATA

Introduction

In this page you can find the example usage for org.apache.thrift.protocol TProtocolException INVALID_DATA.

Prototype

int INVALID_DATA

To view the source code for org.apache.thrift.protocol TProtocolException INVALID_DATA.

Click Source Link

Usage

From source file:io.nettythrift.protocol.TSimpleJSONProtocol.java

License:Apache License

/**
 * Reading methods.//from  w w w  .ja va 2  s  .  c o  m
 */
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;
}