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

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

Introduction

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

Prototype

public void writeI16(short i16) throws TException 

Source Link

Document

Write an I16 as a zigzag varint.

Usage

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

License:Apache License

private void writeMessage(PMessage<?, ?> message, TTupleProtocol protocol)
        throws TException, SerializerException {
    PMessageDescriptor<?, ?> descriptor = message.descriptor();
    if (descriptor.getVariant() == PMessageVariant.UNION) {
        PField fld = ((PUnion<?, ?>) message).unionField();
        protocol.writeI16((short) fld.getKey());
        writeTypedValue(message.get(fld.getKey()), fld.getDescriptor(), protocol);
    } else {// w  ww.j av  a 2 s .c  om
        PField[] fields = descriptor.getFields();
        Arrays.sort(fields, (a, b) -> Integer.compare(a.getKey(), b.getKey()));
        int numOptionals = countOptionals(fields);
        BitSet optionals = new BitSet();
        if (numOptionals > 0) {
            int optionalPos = 0;
            for (PField fld : fields) {
                if (fld.getRequirement() != PRequirement.REQUIRED) {
                    if (message.has(fld.getKey())) {
                        optionals.set(optionalPos);
                    }
                    ++optionalPos;
                }
            }
        }

        boolean shouldWriteOptionals = true;
        int optionalPos = 0;

        for (PField fld : fields) {
            if (fld.getRequirement() == PRequirement.REQUIRED) {
                writeTypedValue(message.get(fld.getKey()), fld.getDescriptor(), protocol);
            } else {
                // Write the optionals bitset at the position of the first
                // non-required field.
                if (shouldWriteOptionals) {
                    protocol.writeBitSet(optionals, numOptionals);
                    shouldWriteOptionals = false;
                }
                if (optionals.get(optionalPos)) {
                    writeTypedValue(message.get(fld.getKey()), fld.getDescriptor(), protocol);
                }
                ++optionalPos;
            }
        }
    }
}

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

License:Apache License

private void writeTypedValue(Object item, PDescriptor type, TTupleProtocol protocol)
        throws TException, SerializerException {
    switch (type.getType()) {
    case BOOL:/* ww  w  .j  a  v  a  2s  .co  m*/
        protocol.writeBool((Boolean) item);
        break;
    case BYTE:
        protocol.writeByte((Byte) item);
        break;
    case I16:
        protocol.writeI16((Short) item);
        break;
    case I32:
        protocol.writeI32((Integer) item);
        break;
    case I64:
        protocol.writeI64((Long) item);
        break;
    case DOUBLE:
        protocol.writeDouble((Double) item);
        break;
    case STRING:
        protocol.writeString((String) item);
        break;
    case BINARY:
        protocol.writeBinary(((Binary) item).getByteBuffer());
        break;
    case ENUM:
        PEnumValue<?> value = (PEnumValue<?>) item;
        protocol.writeI32(value.getValue());
        break;
    case MESSAGE:
        writeMessage((PMessage<?, ?>) item, protocol);
        break;
    case LIST:
        PList<?> lType = (PList<?>) type;
        List<?> list = (List<?>) item;
        protocol.writeI32(list.size());
        for (Object i : list) {
            writeTypedValue(i, lType.itemDescriptor(), protocol);
        }
        break;
    case SET:
        PSet<?> sType = (PSet<?>) type;
        Set<?> set = (Set<?>) item;
        protocol.writeI32(set.size());
        for (Object i : set) {
            writeTypedValue(i, sType.itemDescriptor(), protocol);
        }
        break;
    case MAP:
        PMap<?, ?> mType = (PMap<?, ?>) type;
        Map<?, ?> map = (Map<?, ?>) item;
        protocol.writeI32(map.size());

        for (Map.Entry<?, ?> entry : map.entrySet()) {
            writeTypedValue(entry.getKey(), mType.keyDescriptor(), protocol);
            writeTypedValue(entry.getValue(), mType.itemDescriptor(), protocol);
        }

        protocol.writeMapEnd();
        break;
    default:
        break;
    }
}