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

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

Introduction

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

Prototype

public void writeString(String str) throws TException 

Source Link

Document

Write a string to the wire with a varint size preceding.

Usage

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 av a  2 s.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;
    }
}