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

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

Introduction

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

Prototype

public void writeI16(Short i16) 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;
    }/*from   www  . ja  v a  2 s. 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.plugin.thrift.ThriftRequestProperty.java

License:Apache License

public void writeTraceHeader(ThriftHeader headerKey, TProtocol oprot) throws TException {
    Object headerValue = this.thriftHeaders.get(headerKey);
    if (headerValue == null) {
        return;//from w  ww  .  jav  a2s  .c  o m
    }
    byte headerType = headerKey.getType();
    TField traceField = new TField(headerKey.name(), headerKey.getType(), headerKey.getId());
    oprot.writeFieldBegin(traceField);
    try {
        if (headerType == TType.STRING) {
            // these will be read as byte buffer although it's probably safe to just use writeString here.
            // see org.apache.thrift.protocol.TProtocolUtil.skip(TProtocol, byte, int)
            oprot.writeBinary(stringToByteBuffer((String) headerValue));
        } else if (headerType == TType.I64) {
            oprot.writeI64((Long) headerValue);
        } else if (headerType == TType.I16) {
            oprot.writeI16((Short) headerValue);
        } else if (headerType == TType.BOOL) {
            oprot.writeBool((Boolean) headerValue);
        } else {
            throw new TProtocolException("Invalid apm header type - " + headerType);
        }
    } finally {
        oprot.writeFieldEnd();
    }
}

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

License:Apache License

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

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

License:Apache License

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

From source file:com.navercorp.pinpoint.plugin.thrift.ThriftRequestProperty.java

License:Apache License

public void writeTraceHeader(ThriftHeader headerKey, TProtocol oprot) throws TException {
    Object headerValue = this.thriftHeaders.get(headerKey);
    if (headerValue == null) {
        return;/*from ww  w. j  a v  a  2 s .c o m*/
    }
    byte headerType = headerKey.getType();
    TField traceField = new TField(headerKey.name(), headerKey.getType(), headerKey.getId());
    oprot.writeFieldBegin(traceField);
    try {
        if (headerType == TType.STRING) {
            // these will be read as byte buffer although it's probably safe to just use writeString here.
            // see org.apache.thrift.protocol.TProtocolUtil.skip(TProtocol, byte, int)
            oprot.writeBinary(stringToByteBuffer((String) headerValue));
        } else if (headerType == TType.I64) {
            oprot.writeI64((Long) headerValue);
        } else if (headerType == TType.I16) {
            oprot.writeI16((Short) headerValue);
        } else if (headerType == TType.BOOL) {
            oprot.writeBool((Boolean) headerValue);
        } else {
            throw new TProtocolException("Invalid pinpoint header type - " + headerType);
        }
    } finally {
        oprot.writeFieldEnd();
    }
}

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

License:Apache License

@Override
public void write(TProtocol protocol) throws TException {
    protocol.writeFieldBegin(field);/*  w  w w  .  j  a  v a  2s  . c om*/

    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 a  v a 2s  .  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  ww  w  .  j a v  a 2  s.c  o m
                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;//w w w.  j  av a  2 s. c om
    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:mt.swift.MapSerializer.java

License:Apache License

private void write(TProtocol protocol, Object value, Type type, Field field) throws TException {
    if (type == BasicType.BINARY) {
        if (value instanceof byte[]) {
            protocol.writeBinary((byte[]) value);
        } else if (value instanceof ByteBuffer) {
            protocol.writeBinary(((ByteBuffer) value).array());
        } else {// w w  w. j  a v a2s. c  o m
            throwInvalidTypeException(field, value);
        }
    } else if (type == BasicType.BOOLEAN) {
        if (value instanceof Boolean) {
            protocol.writeBool((Boolean) value);
        } else {
            throwInvalidTypeException(field, value);
        }
    } else if (type == BasicType.BYTE) {
        if (value instanceof Byte || value instanceof Short || value instanceof Integer || value instanceof Long
                || value instanceof BigInteger) {
            Number number = (Number) value;
            if (number.shortValue() < Byte.MIN_VALUE || number.shortValue() > Byte.MAX_VALUE) {
                throwOverflowException(field, number, Byte.MIN_VALUE, Byte.MAX_VALUE);
            }

            protocol.writeByte(number.byteValue());
        } else {
            throwInvalidTypeException(field, value);
        }
    } else if (type == BasicType.I16) {
        if (value instanceof Byte || value instanceof Short || value instanceof Integer || value instanceof Long
                || value instanceof BigInteger) {
            Number number = (Number) value;
            if (number.intValue() < Short.MIN_VALUE || number.intValue() > Short.MAX_VALUE) {
                throwOverflowException(field, number, Short.MIN_VALUE, Short.MAX_VALUE);
            }

            protocol.writeI16(number.shortValue());
        } else {
            throwInvalidTypeException(field, value);
        }
    } else if (type == BasicType.I32) {
        if (value instanceof Byte || value instanceof Short || value instanceof Integer || value instanceof Long
                || value instanceof BigInteger) {
            Number number = (Number) value;
            if (number.longValue() < Integer.MIN_VALUE || number.longValue() > Integer.MAX_VALUE) {
                throwOverflowException(field, number, Integer.MIN_VALUE, Integer.MAX_VALUE);
            }

            protocol.writeI32(number.intValue());
        } else {
            throwInvalidTypeException(field, value);
        }
    } else if (type == BasicType.I64) {
        if (value instanceof Byte || value instanceof Short || value instanceof Integer
                || value instanceof Long) {
            protocol.writeI64(((Number) value).longValue());
        } else if (value instanceof BigInteger) {
            BigInteger number = (BigInteger) value;
            if (number.compareTo(BigInteger.valueOf(Long.MAX_VALUE)) > 0
                    || number.compareTo(BigInteger.valueOf(Long.MIN_VALUE)) < 0) {
                throwOverflowException(field, number, Long.MAX_VALUE, Long.MIN_VALUE);
            }

            protocol.writeI64(((Number) value).longValue());
        } else {
            throwInvalidTypeException(field, value);
        }
    } else if (type == BasicType.DOUBLE) {
        if (value instanceof Byte || value instanceof Short || value instanceof Integer || value instanceof Long
                || value instanceof Float || value instanceof Double) {
            protocol.writeDouble(((Number) value).doubleValue());
        }
        // TODO: support BigDecimal, BigInteger if they fit
        else {
            throwInvalidTypeException(field, value);
        }
    } else if (type == BasicType.STRING) {
        if (value instanceof String) {
            protocol.writeString((String) value);
        } else if (value instanceof CharSequence) {
            protocol.writeString(value.toString());
        }
        // TODO: support any object by calling toString
        else {
            throwInvalidTypeException(field, value);
        }
    } else if (type instanceof MapType) {
        Map<?, ?> mapValue = (Map<?, ?>) value;
        MapType mapType = (MapType) type;
        TMap tmap = new TMap(mapType.getKeyType().getTType(), mapType.getValueType().getTType(),
                mapValue.size());
        protocol.writeMapBegin(tmap);
        for (Map.Entry<?, ?> entry : mapValue.entrySet()) {
            write(protocol, entry.getKey(), mapType.getKeyType(), field);
            write(protocol, entry.getValue(), mapType.getValueType(), field);
        }
        protocol.writeMapEnd();
    } else if (type instanceof ListType) {
        List<?> list = (List<?>) value;
        ListType listType = (ListType) type;
        TList tlist = new TList(listType.getValueType().getTType(), list.size());
        protocol.writeListBegin(tlist);
        for (Object obj : list) {
            write(protocol, obj, listType.getValueType(), field);
        }
        protocol.writeListEnd();
    } else if (type instanceof SetType) {
        Set<?> set = (Set<?>) value;
        SetType setType = (SetType) type;
        TSet tset = new TSet(setType.getValueType().getTType(), set.size());
        protocol.writeSetBegin(tset);
        for (Object obj : set) {
            write(protocol, obj, setType.getValueType(), field);
        }
        protocol.writeSetEnd();
    } else if (type instanceof StructureType) {
        if (value instanceof Map) {
            Map<String, ?> child = (Map<String, ?>) value;
            StructureType structureType = (StructureType) type;
            String structureName = structureType.getName();
            serialize(child, structureName, protocol);
        } else {
            throwInvalidTypeException(field, value);
        }
    } else {
        throw new IllegalArgumentException(String.format("Don't know how to serialize '%s' (%s)",
                field.getName(), field.getType().getSignature()));
    }
}