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

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

Introduction

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

Prototype

public short readI16() throws TException 

Source Link

Document

Read an i16 from the wire as a zigzag varint.

Usage

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

License:Apache License

private <Message extends PMessage<Message, Field>, Field extends PField> Message readMessage(
        TTupleProtocol protocol, PMessageDescriptor<Message, Field> descriptor)
        throws SerializerException, TException {
    PMessageBuilder<Message, Field> builder = descriptor.builder();

    if (descriptor.getVariant() == PMessageVariant.UNION) {
        int fieldId = protocol.readI16();
        PField fld = descriptor.getField(fieldId);
        builder.set(fld.getKey(), readTypedValue(fld.getDescriptor(), protocol));
    } else {//from w  w w .jav  a2s  . c o m
        PField[] fields = descriptor.getFields();
        int numOptionals = countOptionals(fields);

        BitSet optionals = null;
        int optionalPos = 0;
        for (PField fld : fields) {
            if (fld.getRequirement() == PRequirement.REQUIRED) {
                builder.set(fld.getKey(), readTypedValue(fld.getDescriptor(), protocol));
            } else {
                if (optionals == null) {
                    optionals = protocol.readBitSet(numOptionals);
                }
                if (optionals.get(optionalPos)) {
                    builder.set(fld.getKey(), readTypedValue(fld.getDescriptor(), protocol));
                }
                ++optionalPos;
            }
        }
    }

    if (readStrict) {
        try {
            builder.validate();
        } catch (IllegalStateException e) {
            throw new SerializerException(e, e.getMessage());
        }
    }

    return builder.build();
}

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   www .j a v  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());
    }
}