Example usage for org.apache.thrift.protocol TProtocol writeByte

List of usage examples for org.apache.thrift.protocol TProtocol writeByte

Introduction

In this page you can find the example usage for org.apache.thrift.protocol TProtocol writeByte.

Prototype

public void writeByte(Byte b) throws TException 

Source Link

Usage

From source file:com.alibaba.dubbo.rpc.protocol.thrift.ext.MultiServiceProcessor.java

License:Open Source License

public boolean process(TProtocol in, TProtocol out) throws TException {

    short magic = in.readI16();

    if (magic != ThriftCodec.MAGIC) {
        logger.error(new StringBuilder(24).append("Unsupported magic ").append(magic).toString());
        return false;
    }// w ww  .ja  v  a 2s.c o m

    in.readI32();
    in.readI16();
    byte version = in.readByte();
    String serviceName = in.readString();
    long id = in.readI64();

    ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);

    TIOStreamTransport transport = new TIOStreamTransport(bos);

    TProtocol protocol = protocolFactory.getProtocol(transport);

    TProcessor processor = processorMap.get(serviceName);

    if (processor == null) {
        logger.error(new StringBuilder(32).append("Could not find processor for service ").append(serviceName)
                .toString());
        return false;
    }

    // todo if exception
    boolean result = processor.process(in, protocol);

    ByteArrayOutputStream header = new ByteArrayOutputStream(512);

    TIOStreamTransport headerTransport = new TIOStreamTransport(header);

    TProtocol headerProtocol = protocolFactory.getProtocol(headerTransport);

    headerProtocol.writeI16(magic);
    headerProtocol.writeI32(Integer.MAX_VALUE);
    headerProtocol.writeI16(Short.MAX_VALUE);
    headerProtocol.writeByte(version);
    headerProtocol.writeString(serviceName);
    headerProtocol.writeI64(id);
    headerProtocol.getTransport().flush();

    out.writeI16(magic);
    out.writeI32(bos.size() + header.size());
    out.writeI16((short) (0xffff & header.size()));
    out.writeByte(version);
    out.writeString(serviceName);
    out.writeI64(id);

    out.getTransport().write(bos.toByteArray());
    out.getTransport().flush();

    return result;

}

From source file:com.baidu.oped.apm.thrift.io.ChunkHeaderBufferedTBaseSerializer.java

License:Apache License

private void writeHeader(final TProtocol protocol, final Header header) throws TException {
    protocol.writeByte(header.getSignature());
    protocol.writeByte(header.getVersion());
    short type = header.getType();
    protocol.writeByte(BytesUtils.writeShort1(type));
    protocol.writeByte(BytesUtils.writeShort2(type));
}

From source file:com.ebay.nest.io.sede.dynamic_type.DynamicSerDeTypeByte.java

License:Apache License

@Override
public void serialize(Object o, ObjectInspector oi, TProtocol oprot)
        throws TException, SerDeException, NoSuchFieldException, IllegalAccessException {
    ByteObjectInspector poi = (ByteObjectInspector) oi;
    oprot.writeByte(poi.get(o));
}

From source file:com.facebook.swift.codec.internal.builtin.ByteThriftCodec.java

License:Apache License

@Override
public void write(Byte value, TProtocol protocol) throws Exception {
    Preconditions.checkNotNull(value, "value is null");
    Preconditions.checkNotNull(protocol, "protocol is null");
    protocol.writeByte(value);
}

From source file:com.navercorp.pinpoint.thrift.io.HeaderUtils.java

License:Apache License

public static void writeHeader(TProtocol protocol, Header header) throws TException {
    protocol.writeByte(header.getSignature());
    protocol.writeByte(header.getVersion());
    // fixed size regardless protocol
    short type = header.getType();
    protocol.writeByte(BytesUtils.writeShort1(type));
    protocol.writeByte(BytesUtils.writeShort2(type));
}

From source file:com.ning.metrics.serialization.thrift.ThriftFieldImpl.java

License:Apache License

@Override
public void write(TProtocol protocol) throws TException {
    protocol.writeFieldBegin(field);/* ww  w .j av  a2s  .  c  o  m*/

    switch (field.type) {
    case TType.BOOL:
        protocol.writeBool(dataItem.getBoolean());
        break;
    case TType.BYTE:
        protocol.writeByte(dataItem.getByte());
        break;
    case TType.I16:
        protocol.writeI16(dataItem.getShort());
        break;
    case TType.I32:
        protocol.writeI32(dataItem.getInteger());
        break;
    case TType.I64:
        protocol.writeI64(dataItem.getLong());
        break;
    case TType.STRING:
        protocol.writeString(dataItem.getString());
        break;
    case TType.DOUBLE:
        protocol.writeDouble(dataItem.getDouble());
        break;
    default:
        throw new IllegalArgumentException(String.format("unsupported thrift type %s", field.type));
    }
    protocol.writeFieldEnd();
}

From source file:ezbake.query.basequeryableprocedure.ColumnDataValues.java

License:Apache License

@Override
protected void standardSchemeWriteValue(org.apache.thrift.protocol.TProtocol oprot)
        throws org.apache.thrift.TException {
    switch (setField_) {
    case BOOL_VALS:
        List<Boolean> bool_vals = (List<Boolean>) value_; {
        oprot.writeListBegin(/*from   w w w .j av a  2 s. c  om*/
                new org.apache.thrift.protocol.TList(org.apache.thrift.protocol.TType.BOOL, bool_vals.size()));
        for (boolean _iter32 : bool_vals) {
            oprot.writeBool(_iter32);
        }
        oprot.writeListEnd();
    }
        return;
    case BYTE_VALS:
        List<Byte> byte_vals = (List<Byte>) value_; {
        oprot.writeListBegin(
                new org.apache.thrift.protocol.TList(org.apache.thrift.protocol.TType.BYTE, byte_vals.size()));
        for (byte _iter33 : byte_vals) {
            oprot.writeByte(_iter33);
        }
        oprot.writeListEnd();
    }
        return;
    case SHORT_VALS:
        List<Short> short_vals = (List<Short>) value_; {
        oprot.writeListBegin(
                new org.apache.thrift.protocol.TList(org.apache.thrift.protocol.TType.I16, short_vals.size()));
        for (short _iter34 : short_vals) {
            oprot.writeI16(_iter34);
        }
        oprot.writeListEnd();
    }
        return;
    case INT_VALS:
        List<Integer> int_vals = (List<Integer>) value_; {
        oprot.writeListBegin(
                new org.apache.thrift.protocol.TList(org.apache.thrift.protocol.TType.I32, int_vals.size()));
        for (int _iter35 : int_vals) {
            oprot.writeI32(_iter35);
        }
        oprot.writeListEnd();
    }
        return;
    case LONG_VALS:
        List<Long> long_vals = (List<Long>) value_; {
        oprot.writeListBegin(
                new org.apache.thrift.protocol.TList(org.apache.thrift.protocol.TType.I64, long_vals.size()));
        for (long _iter36 : long_vals) {
            oprot.writeI64(_iter36);
        }
        oprot.writeListEnd();
    }
        return;
    case DOUBLE_VALS:
        List<Double> double_vals = (List<Double>) value_; {
        oprot.writeListBegin(new org.apache.thrift.protocol.TList(org.apache.thrift.protocol.TType.DOUBLE,
                double_vals.size()));
        for (double _iter37 : double_vals) {
            oprot.writeDouble(_iter37);
        }
        oprot.writeListEnd();
    }
        return;
    case STRING_VALS:
        List<String> string_vals = (List<String>) value_; {
        oprot.writeListBegin(new org.apache.thrift.protocol.TList(org.apache.thrift.protocol.TType.STRING,
                string_vals.size()));
        for (String _iter38 : string_vals) {
            oprot.writeString(_iter38);
        }
        oprot.writeListEnd();
    }
        return;
    case BINARY_VALS:
        List<ByteBuffer> binary_vals = (List<ByteBuffer>) value_; {
        oprot.writeListBegin(new org.apache.thrift.protocol.TList(org.apache.thrift.protocol.TType.STRING,
                binary_vals.size()));
        for (ByteBuffer _iter39 : binary_vals) {
            oprot.writeBinary(_iter39);
        }
        oprot.writeListEnd();
    }
        return;
    default:
        throw new IllegalStateException("Cannot write union with unknown field " + setField_);
    }
}

From source file:ezbake.query.basequeryableprocedure.ColumnDataValues.java

License:Apache License

@Override
protected void tupleSchemeWriteValue(org.apache.thrift.protocol.TProtocol oprot)
        throws org.apache.thrift.TException {
    switch (setField_) {
    case BOOL_VALS:
        List<Boolean> bool_vals = (List<Boolean>) value_; {
        oprot.writeListBegin(//from   w ww .  j  a v  a  2  s  .c om
                new org.apache.thrift.protocol.TList(org.apache.thrift.protocol.TType.BOOL, bool_vals.size()));
        for (boolean _iter64 : bool_vals) {
            oprot.writeBool(_iter64);
        }
        oprot.writeListEnd();
    }
        return;
    case BYTE_VALS:
        List<Byte> byte_vals = (List<Byte>) value_; {
        oprot.writeListBegin(
                new org.apache.thrift.protocol.TList(org.apache.thrift.protocol.TType.BYTE, byte_vals.size()));
        for (byte _iter65 : byte_vals) {
            oprot.writeByte(_iter65);
        }
        oprot.writeListEnd();
    }
        return;
    case SHORT_VALS:
        List<Short> short_vals = (List<Short>) value_; {
        oprot.writeListBegin(
                new org.apache.thrift.protocol.TList(org.apache.thrift.protocol.TType.I16, short_vals.size()));
        for (short _iter66 : short_vals) {
            oprot.writeI16(_iter66);
        }
        oprot.writeListEnd();
    }
        return;
    case INT_VALS:
        List<Integer> int_vals = (List<Integer>) value_; {
        oprot.writeListBegin(
                new org.apache.thrift.protocol.TList(org.apache.thrift.protocol.TType.I32, int_vals.size()));
        for (int _iter67 : int_vals) {
            oprot.writeI32(_iter67);
        }
        oprot.writeListEnd();
    }
        return;
    case LONG_VALS:
        List<Long> long_vals = (List<Long>) value_; {
        oprot.writeListBegin(
                new org.apache.thrift.protocol.TList(org.apache.thrift.protocol.TType.I64, long_vals.size()));
        for (long _iter68 : long_vals) {
            oprot.writeI64(_iter68);
        }
        oprot.writeListEnd();
    }
        return;
    case DOUBLE_VALS:
        List<Double> double_vals = (List<Double>) value_; {
        oprot.writeListBegin(new org.apache.thrift.protocol.TList(org.apache.thrift.protocol.TType.DOUBLE,
                double_vals.size()));
        for (double _iter69 : double_vals) {
            oprot.writeDouble(_iter69);
        }
        oprot.writeListEnd();
    }
        return;
    case STRING_VALS:
        List<String> string_vals = (List<String>) value_; {
        oprot.writeListBegin(new org.apache.thrift.protocol.TList(org.apache.thrift.protocol.TType.STRING,
                string_vals.size()));
        for (String _iter70 : string_vals) {
            oprot.writeString(_iter70);
        }
        oprot.writeListEnd();
    }
        return;
    case BINARY_VALS:
        List<ByteBuffer> binary_vals = (List<ByteBuffer>) value_; {
        oprot.writeListBegin(new org.apache.thrift.protocol.TList(org.apache.thrift.protocol.TType.STRING,
                binary_vals.size()));
        for (ByteBuffer _iter71 : binary_vals) {
            oprot.writeBinary(_iter71);
        }
        oprot.writeListEnd();
    }
        return;
    default:
        throw new IllegalStateException("Cannot write union with unknown field " + setField_);
    }
}

From source file:ezbake.thrift.utils.xml2thrift.util.ThriftUtils.java

License:Apache License

private static void writeSingleFieldNoTag(TProtocol proto, Field field, Object value) throws TException {
    switch (field.getType()) {

    case TType.BOOL:
        proto.writeBool((Boolean) value);
        break;//from w  w  w . j  a va2s.c o  m
    case TType.BYTE:
        proto.writeByte((Byte) value);
        break;
    case TType.I16:
        proto.writeI16((Short) value);
        break;
    case TType.I32:
        proto.writeI32((Integer) value);
        break;
    case TType.ENUM:
        proto.writeI32(((TEnum) value).getValue());
        break;
    case TType.I64:
        proto.writeI64((Long) value);
        break;
    case TType.DOUBLE:
        proto.writeDouble((Double) value);
        break;
    case TType.STRING: {
        if (value instanceof String) {
            proto.writeString((String) value);
        } else {
            proto.writeBinary((ByteBuffer) value);
        }
    }
        break;
    case TType.STRUCT:
        ((TBase<?, ?>) value).write(proto);
        break;

    default:
        throw new IllegalArgumentException("Unexpected type : " + field.getType());
    }
}

From source file:ezbake.thrift.utils.xml2thrift.util.ThriftUtils.java

License:Apache License

/**
 * Serializes a single field of a thrift struct.
 *
 * @throws TException/*from  w  w  w.j  ava 2s.c o m*/
 */
public static void writeFieldNoTag(TProtocol proto, Field field, Object value) throws TException {
    if (value == null) {
        return;
    }

    Field innerField = null;

    switch (field.getType()) {

    case TType.LIST:
        innerField = field.getListElemField();
        break;
    case TType.SET:
        innerField = field.getSetElemField();
        break;
    case TType.MAP:
        innerField = field.getMapKeyField();
        break;

    default:
        writeSingleFieldNoTag(proto, field, value);
        return;
    }

    // a map or a collection:

    if (field.getType() == TType.MAP) {

        Field valueField = field.getMapValueField();
        Map<?, ?> map = (Map<?, ?>) value;

        proto.writeByte(innerField.getType());
        proto.writeByte(valueField.getType());
        proto.writeI32(map.size());

        for (Entry<?, ?> entry : map.entrySet()) {
            writeSingleFieldNoTag(proto, innerField, entry.getKey());
            writeSingleFieldNoTag(proto, valueField, entry.getValue());
        }

    } else { // SET or LIST

        Collection<?> coll = (Collection<?>) value;

        proto.writeByte(innerField.getType());
        proto.writeI32(coll.size());

        for (Object v : coll) {
            writeSingleFieldNoTag(proto, innerField, v);
        }

    }
}