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

The following examples show how to use org.apache.thrift.protocol.TProtocol#writeString() . 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: ThriftProtocolAgentIntercept.java    From java-specialagent with Apache License 2.0 6 votes vote down vote up
public static void writeFieldStop(final Object protocol) throws TException {
  if (injected.get())
    return;

  final TProtocol tProtocol = (TProtocol)protocol;
  final Span span = spanHolder.get();
  if (span == null)
    return;

  final Map<String,String> map = new HashMap<>();
  GlobalTracer.get().inject(span.context(), Builtin.TEXT_MAP, new TextMapAdapter(map));

  tProtocol.writeFieldBegin(new TField("span", TType.MAP, SPAN_FIELD_ID));
  tProtocol.writeMapBegin(new TMap(TType.STRING, TType.STRING, map.size()));
  for (final Entry<String,String> entry : map.entrySet()) {
    tProtocol.writeString(entry.getKey());
    tProtocol.writeString(entry.getValue());
  }

  tProtocol.writeMapEnd();
  tProtocol.writeFieldEnd();
  injected.set(true);
}
 
Example 2
Source File: AbstractThriftBase.java    From ikasoa with MIT License 5 votes vote down vote up
/**
 * 写入操作
 */
@Override
public void write(TProtocol oprot) throws TException {
	if (!StringUtil.equals("org.apache.thrift.scheme.StandardScheme", oprot.getScheme().getName()))
		throw new TApplicationException("Service scheme must be 'org.apache.thrift.scheme.StandardScheme' !");
	oprot.writeStructBegin(getTStruct());
	if (ObjectUtil.isNotNull(str)) {
		oprot.writeFieldBegin(new TField("value", TType.STRING, (short) 0));
		oprot.writeString(str);
		oprot.writeFieldEnd();
	}
	oprot.writeFieldStop();
	oprot.writeStructEnd();
}
 
Example 3
Source File: CompFileWrite.java    From ThriftBook with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws TException {
    TTransport trans = new TSimpleFileTransport("data.comp", false, true);
    TProtocol proto = new TCompactProtocol(trans);			

    Trade trade = new Trade();
    trade.symbol = "F";
    trade.price = 13.10;
    trade.size = 2500;

    proto.writeStructBegin(new TStruct());

    proto.writeFieldBegin(new TField("symbol", 
                                     TType.STRING, 
                                     (short) 1));
    proto.writeString(trade.symbol);
    proto.writeFieldEnd();

    proto.writeFieldBegin(new TField("price", 
                                     TType.DOUBLE, 
                                     (short) 2));
    proto.writeDouble(trade.price);
    proto.writeFieldEnd();

    proto.writeFieldBegin(new TField("size", 
                                     TType.I32, 
                                     (short) 3));
    proto.writeI32(trade.size);
    proto.writeFieldEnd();

    proto.writeFieldStop();
    proto.writeStructEnd();
    
    System.out.println("Wrote trade to file");
}
 
Example 4
Source File: BinMem.java    From ThriftBook with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws TException {
    TMemoryBuffer trans = new TMemoryBuffer(4096);
    TProtocol proto = new TBinaryProtocol(trans);

    proto.writeString("Hello Thrift Serialization");
    System.out.println("Wrote " + trans.length() + " bytes to the TMemoryBuffer");

    String strMsg = proto.readString();
    System.out.println("Recovered string: " + strMsg);
}
 
Example 5
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 6
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 7
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 8
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 9
Source File: TypeAdapter.java    From Firefly with Apache License 2.0 4 votes vote down vote up
@Override
public void write(String s, TProtocol protocol) throws TException {
    protocol.writeString(s);
}
 
Example 10
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 11
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();
}