Java Code Examples for org.apache.thrift.protocol.TProtocol#writeI16()

The following examples show how to use org.apache.thrift.protocol.TProtocol#writeI16() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ThriftRequestProperty.java    From pinpoint with Apache License 2.0 6 votes vote down vote up
public void writeTraceHeader(ThriftHeader headerKey, TProtocol oprot) throws TException {
    Object headerValue = this.thriftHeaders.get(headerKey);
    if (headerValue == null) {
        return;
    }
    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();
    }
}
 
Example 2
Source File: MultiServiceProcessor.java    From dubbo-2.6.5 with Apache License 2.0 4 votes vote down vote up
@Override
public boolean process(TProtocol in, TProtocol out) throws TException {

    short magic = in.readI16();

    if (magic != ThriftCodec.MAGIC) {
        logger.error("Unsupported magic " + magic);
        return false;
    }

    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("Could not find processor for service " + serviceName);
        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;

}
 
Example 3
Source File: MultiServiceProcessor.java    From dubbox with Apache License 2.0 4 votes vote down vote up
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;
        }

        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;

    }
 
Example 4
Source File: MultiServiceProcessor.java    From dubbox-hystrix with Apache License 2.0 4 votes vote down vote up
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;
        }

        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;

    }
 
Example 5
Source File: MultiServiceProcessor.java    From dubbox with Apache License 2.0 4 votes vote down vote up
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;
        }

        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;

    }
 
Example 6
Source File: TypeAdapter.java    From Firefly with Apache License 2.0 4 votes vote down vote up
@Override
public void write(Short aShort, TProtocol protocol) throws TException {
    protocol.writeI16(aShort);
}
 
Example 7
Source File: MultiServiceProcessor.java    From dubbox with Apache License 2.0 4 votes vote down vote up
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;
        }

        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;

    }
 
Example 8
Source File: ProtocolReadToWrite.java    From parquet-mr with Apache License 2.0 4 votes vote down vote up
void readOneValue(TProtocol in, TProtocol out, byte type)
    throws TException {
  switch (type) {
  case TType.LIST:
    readOneList(in, out);
    break;
  case TType.MAP:
    readOneMap(in, out);
    break;
  case TType.SET:
    readOneSet(in, out);
    break;
  case TType.STRUCT:
    readOneStruct(in, out);
    break;
  case TType.STOP:
    break;
  case TType.BOOL:
    out.writeBool(in.readBool());
    break;
  case TType.BYTE:
    out.writeByte(in.readByte());
    break;
  case TType.DOUBLE:
    out.writeDouble(in.readDouble());
    break;
  case TType.I16:
    out.writeI16(in.readI16());
    break;
  case TType.ENUM: // same as i32 => actually never seen in the protocol layer as enums are written as a i32 field
  case TType.I32:
    out.writeI32(in.readI32());
    break;
  case TType.I64:
    out.writeI64(in.readI64());
    break;
  case TType.STRING:
    out.writeBinary(in.readBinary());
    break;
  case TType.VOID:
    break;
  default:
    throw new TException("Unknown type: " + type);
  }
}
 
Example 9
Source File: ThriftSampleData.java    From incubator-pinot with Apache License 2.0 4 votes vote down vote up
public void write(TProtocol oprot, ThriftSampleData struct)
    throws TException {
  struct.validate();
  oprot.writeStructBegin(ThriftSampleData.STRUCT_DESC);
  if (struct.isSetId()) {
    oprot.writeFieldBegin(ThriftSampleData.ID_FIELD_DESC);
    oprot.writeI32(struct.id);
    oprot.writeFieldEnd();
  }

  if (struct.name != null && struct.isSetName()) {
    oprot.writeFieldBegin(ThriftSampleData.NAME_FIELD_DESC);
    oprot.writeString(struct.name);
    oprot.writeFieldEnd();
  }

  if (struct.isSetCreated_at()) {
    oprot.writeFieldBegin(ThriftSampleData.CREATED_AT_FIELD_DESC);
    oprot.writeI64(struct.created_at);
    oprot.writeFieldEnd();
  }

  if (struct.isSetActive()) {
    oprot.writeFieldBegin(ThriftSampleData.ACTIVE_FIELD_DESC);
    oprot.writeBool(struct.active);
    oprot.writeFieldEnd();
  }

  Iterator var3;
  if (struct.groups != null) {
    oprot.writeFieldBegin(ThriftSampleData.GROUPS_FIELD_DESC);
    oprot.writeListBegin(new TList((byte) 6, struct.groups.size()));
    var3 = struct.groups.iterator();

    while (var3.hasNext()) {
      short _iter10 = ((Short) var3.next()).shortValue();
      oprot.writeI16(_iter10);
    }

    oprot.writeListEnd();
    oprot.writeFieldEnd();
  }

  if (struct.map_values != null) {
    oprot.writeFieldBegin(ThriftSampleData.MAP_VALUES_FIELD_DESC);
    oprot.writeMapBegin(new TMap((byte) 11, (byte) 10, struct.map_values.size()));
    var3 = struct.map_values.entrySet().iterator();

    while (var3.hasNext()) {
      Entry<String, Long> _iter11 = (Entry) var3.next();
      oprot.writeString((String) _iter11.getKey());
      oprot.writeI64(((Long) _iter11.getValue()).longValue());
    }

    oprot.writeMapEnd();
    oprot.writeFieldEnd();
  }

  if (struct.set_values != null) {
    oprot.writeFieldBegin(ThriftSampleData.SET_VALUES_FIELD_DESC);
    oprot.writeSetBegin(new TSet((byte) 11, struct.set_values.size()));
    var3 = struct.set_values.iterator();

    while (var3.hasNext()) {
      String _iter12 = (String) var3.next();
      oprot.writeString(_iter12);
    }

    oprot.writeSetEnd();
    oprot.writeFieldEnd();
  }

  oprot.writeFieldStop();
  oprot.writeStructEnd();
}