Example usage for org.apache.thrift.protocol TTupleProtocol readByte

List of usage examples for org.apache.thrift.protocol TTupleProtocol readByte

Introduction

In this page you can find the example usage for org.apache.thrift.protocol TTupleProtocol readByte.

Prototype

public byte readByte() throws TException 

Source Link

Document

Read a single byte off the wire.

Usage

From source file:net.morimekta.providence.thrift.TTupleProtocolSerializer.java

License:Apache License

private Object readTypedValue(PDescriptor type, TTupleProtocol protocol)
        throws TException, SerializerException {
    switch (type.getType()) {
    case BOOL:/*from ww  w. j  av  a 2s .c  o m*/
        return protocol.readBool();
    case BYTE:
        return protocol.readByte();
    case I16:
        return protocol.readI16();
    case I32:
        return protocol.readI32();
    case I64:
        return protocol.readI64();
    case DOUBLE:
        return protocol.readDouble();
    case BINARY: {
        ByteBuffer buffer = protocol.readBinary();
        return Binary.wrap(buffer.array());
    }
    case STRING:
        return protocol.readString();
    case ENUM: {
        PEnumDescriptor<?> et = (PEnumDescriptor<?>) type;
        PEnumBuilder<?> eb = et.builder();
        final int value = protocol.readI32();
        eb.setByValue(value);
        if (readStrict && !eb.valid()) {
            throw new SerializerException("Invalid enum value " + value + " for " + et.getQualifiedName());
        }
        return eb.build();
    }
    case MESSAGE:
        return readMessage(protocol, (PMessageDescriptor<?, ?>) type);
    case LIST: {
        int lSize = protocol.readI32();
        @SuppressWarnings("unchecked")
        PList<Object> lDesc = (PList<Object>) type;
        PDescriptor liDesc = lDesc.itemDescriptor();

        PList.Builder<Object> list = lDesc.builder();
        for (int i = 0; i < lSize; ++i) {
            list.add(readTypedValue(liDesc, protocol));
        }

        return list.build();
    }
    case SET: {
        int sSize = protocol.readI32();
        @SuppressWarnings("unchecked")
        PSet<Object> sDesc = (PSet<Object>) type;
        PDescriptor siDesc = sDesc.itemDescriptor();

        PSet.Builder<Object> set = sDesc.builder();
        for (int i = 0; i < sSize; ++i) {
            set.add(readTypedValue(siDesc, protocol));
        }

        return set.build();
    }
    case MAP: {
        int mSize = protocol.readI32();
        @SuppressWarnings("unchecked")
        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 < mSize; ++i) {
            Object key = readTypedValue(mkDesc, protocol);
            Object val = readTypedValue(miDesc, protocol);
            map.put(key, val);
        }

        protocol.readMapEnd();
        return map.build();
    }
    default:
        throw new SerializerException("Unsupported protocol field type: " + type.getType());
    }
}