Beispiel #1
0
  public static void main(String[] args) throws Exception {
    if (args.length != 2) {
      System.out.println(
          "usage: java -cp build/classes org.apache.thrift.test.ReadStruct filename proto_factory_class");
      System.out.println(
          "Read in an instance of CompactProtocolTestStruct from 'file', making sure that it is equivalent to Fixtures.compactProtoTestStruct. Use a protocol from 'proto_factory_class'.");
    }

    TTransport trans =
        new TIOStreamTransport(new BufferedInputStream(new FileInputStream(args[0])));

    TProtocolFactory factory = (TProtocolFactory) Class.forName(args[1]).newInstance();

    TProtocol proto = factory.getProtocol(trans);

    CompactProtoTestStruct cpts = new CompactProtoTestStruct();

    for (CompactProtoTestStruct._Fields fid : CompactProtoTestStruct.metaDataMap.keySet()) {
      cpts.setFieldValue(fid, null);
    }

    cpts.read(proto);

    if (cpts.equals(Fixtures.compactProtoTestStruct)) {
      System.out.println("Object verified successfully!");
    } else {
      System.out.println("Object failed verification!");
      System.out.println("Expected: " + Fixtures.compactProtoTestStruct + " but got " + cpts);
    }
  }
 public static void testPartialDeserialize(
     TProtocolFactory protocolFactory,
     TBase input,
     TBase output,
     TBase expected,
     TFieldIdEnum... fieldIdPath)
     throws TException {
   byte[] record = new TSerializer(protocolFactory).serialize(input);
   TDeserializer deserializer = new TDeserializer(protocolFactory);
   for (int i = 0; i < 2; i++) {
     TBase outputCopy = output.deepCopy();
     deserializer.partialDeserialize(outputCopy, record, fieldIdPath);
     assertEquals(
         "on attempt "
             + i
             + ", with "
             + protocolFactory.toString()
             + ", expected "
             + expected
             + " but got "
             + outputCopy,
         expected,
         outputCopy);
   }
 }
  public static <T extends TBase> void testDeserialization(
      TProtocolFactory factory, T object, Class<T> klass) throws Exception {
    TMemoryBuffer buf = new TMemoryBuffer(0);
    object.write(factory.getProtocol(buf));
    byte[] serialized = new byte[100 * 1024];
    buf.read(serialized, 0, 100 * 1024);

    long startTime = System.currentTimeMillis();
    for (int i = 0; i < HOW_MANY; i++) {
      T o2 = klass.newInstance();
      o2.read(factory.getProtocol(new TMemoryInputTransport(serialized)));
    }
    long endTime = System.currentTimeMillis();

    System.out.println("Deserialization test time: " + (endTime - startTime) + " ms");
  }
  public static void testSerialization(TProtocolFactory factory, TBase object) throws Exception {
    TTransport trans =
        new TTransport() {
          public void write(byte[] bin, int x, int y) throws TTransportException {}

          public int read(byte[] bin, int x, int y) throws TTransportException {
            return 0;
          }

          public void close() {}

          public void open() {}

          public boolean isOpen() {
            return true;
          }
        };

    TProtocol proto = factory.getProtocol(trans);

    long startTime = System.currentTimeMillis();
    for (int i = 0; i < HOW_MANY; i++) {
      object.write(proto);
    }
    long endTime = System.currentTimeMillis();

    System.out.println("Serialization test time: " + (endTime - startTime) + " ms");
  }
Beispiel #5
0
  public void write(final TBase<?, ?> base) throws TException {
    final TBaseStreamNode node = new TBaseStreamNode(transport);
    node.setClassName(base.getClass().getName());
    node.setBeginPosition(transport.getBufferPosition());

    final TProtocol protocol = protocolFactory.getProtocol(transport);
    base.write(protocol);

    node.setEndPosition(transport.getBufferPosition());
    nodes.add(node);
  }
    @Override
    public T makeObject() throws Exception {
      String thriftClassName = StringUtils.split(serviceNode.getServiceFace(), '$')[0];
      Class<T> clientClazz = (Class<T>) ClassHelper.forName(thriftClassName + "$Client");
      TTransport transport = thriftClient.connectSync(serviceNode.getSocketAddress());

      TMultiplexedProtocol protocol =
          new TMultiplexedProtocol(factory.getProtocol(transport), header);
      Constructor<T> cons = clientClazz.getConstructor(TProtocol.class);
      T client = cons.newInstance(protocol);
      transports.put(client, transport);
      LOG.debug(
          "ServiceNode: {}, finish makeObject client : {}, transport : {}",
          serviceNode.getZnodeName(),
          client,
          transport);
      return client;
    }
 @Override
 public TProtocol getProtocol(TTransport tTransport) {
   return factory.getProtocol(tTransport);
 }