Package org.apache.thrift.transport

Examples of org.apache.thrift.transport.TMemoryBuffer


 
  public void testTupleProtocolSerialization () throws Exception {
    TestUnion union = new TestUnion(TestUnion._Fields.I32_FIELD, 25);
    union.setI32_set(Collections.singleton(42));

    TMemoryBuffer buf = new TMemoryBuffer(0);
    TProtocol proto = new TTupleProtocol(buf);

    union.write(proto);

    TestUnion u2 = new TestUnion();

    u2.read(proto);

    assertEquals(u2, union);

    StructWithAUnion swau = new StructWithAUnion(u2);

    buf = new TMemoryBuffer(0);
    proto = new TBinaryProtocol(buf);

    swau.write(proto);

    StructWithAUnion swau2 = new StructWithAUnion();
    assertFalse(swau2.equals(swau));
    swau2.read(proto);
    assertEquals(swau2, swau);

    // this should NOT throw an exception.
    buf = new TMemoryBuffer(0);
    proto = new TTupleProtocol(buf);

    swau.write(proto);
    new Empty().read(proto);
  }
View Full Code Here


       
        private String thriftRequest(byte[] input){
            try{
           
                //Input
                TMemoryBuffer inbuffer = new TMemoryBuffer(input.length);          
                inbuffer.write(input);             
                TProtocol  inprotocol   = new TJSONProtocol(inbuffer);                  
               
                //Output
                TMemoryBuffer outbuffer = new TMemoryBuffer(100);          
                TProtocol outprotocol   = new TJSONProtocol(outbuffer);
               
                TProcessor processor = new ThriftTest.Processor(new TestHandler());     
                processor.process(inprotocol, outprotocol);
               
                byte[] output = new byte[outbuffer.length()];
                outbuffer.readAll(output, 0, output.length);
           
                return new String(output,"UTF-8");
            }catch(Throwable t){
                return "Error:"+t.getMessage();
            }
View Full Code Here

   
    System.out.println("Serialization test time: " + (endTime - startTime) + " ms");
  }
 
  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)));
View Full Code Here

       
        private String thriftRequest(byte[] input){
            try{
           
                //Input
                TMemoryBuffer inbuffer = new TMemoryBuffer(input.length);          
                inbuffer.write(input);             
                TProtocol  inprotocol   = new TJSONProtocol(inbuffer);                  
               
                //Output
                TMemoryBuffer outbuffer = new TMemoryBuffer(100);          
                TProtocol outprotocol   = new TJSONProtocol(outbuffer);
               
                TProcessor processor = new Calculator.Processor(new CalculatorHandler());     
                processor.process(inprotocol, outprotocol);
               
                byte[] output = new byte[outbuffer.length()];
                outbuffer.readAll(output, 0, output.length);
           
                return new String(output,"UTF-8");
            }catch(Throwable t){
                return "Error:"+t.getMessage();
            }
View Full Code Here

  /**
   * Initialize buffers.
   * @throws TException if buffer initialization fails
   */
  protected void prepareMethodCall() throws TException {
    TMemoryBuffer memoryBuffer = new TMemoryBuffer(INITIAL_MEMORY_BUFFER_SIZE);
    TProtocol protocol = protocolFactory.getProtocol(memoryBuffer);
    write_args(protocol);

    int length = memoryBuffer.length();
    frameBuffer = ByteBuffer.wrap(memoryBuffer.getArray(), 0, length);

    TFramedTransport.encodeFrameSize(length, sizeBufferArray);
    sizeBuffer = ByteBuffer.wrap(sizeBufferArray);
  }
View Full Code Here

    }

    StorageDescriptor sd = new StorageDescriptor();
    try {
      // replace with THRIFT-138
      TMemoryBuffer buffer = new TMemoryBuffer(1024);
      TBinaryProtocol prot = new TBinaryProtocol(buffer);
      tbl.getTTable().getSd().write(prot);

      sd.read(prot);
    } catch (TException e) {
View Full Code Here

  /**
   * Tests writes
   */
  @Test
  public void testTStatsWritten() throws TTransportException {
    TMemoryBuffer mb = new TMemoryBuffer(1 << 20); // 1 MB memory buffer
    TStatsTransport stats = new TStatsTransport(mb);

    stats.write(new byte[100]);
    Assert.assertEquals(stats.getBytesRead(), 0);
    Assert.assertEquals(stats.getBytesWritten(), 100);
View Full Code Here

  /**
   * Does a write to fill the buffer and then tests reads.
   */
  @Test
  public void testTStatsRead() throws TTransportException {
    TMemoryBuffer mb = new TMemoryBuffer(1 << 20); // 1 MB memory buffer
    TStatsTransport stats = new TStatsTransport(mb);

    stats.write(new byte[200]);

    stats.read(new byte[100], 0, 100);
View Full Code Here

    }
  }

  public String toThriftJSONString() throws IOException {
    org.apache.hadoop.hive.ql.plan.api.Query q = getQueryPlan();
    TMemoryBuffer tmb = new TMemoryBuffer(q.toString().length() * 5);
    TJSONProtocol oprot = new TJSONProtocol(tmb);
    try {
      q.write(oprot);
    } catch (TException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      return q.toString();
    }
    return tmb.toString("UTF-8");
  }
View Full Code Here

    return tmb.toString("UTF-8");
  }

  public String toBinaryString() throws IOException {
    org.apache.hadoop.hive.ql.plan.api.Query q = getQueryPlan();
    TMemoryBuffer tmb = new TMemoryBuffer(q.toString().length() * 5);
    TBinaryProtocol oprot = new TBinaryProtocol(tmb);
    try {
      q.write(oprot);
    } catch (TException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      return q.toString();
    }
    byte[] buf = new byte[tmb.length()];
    tmb.read(buf, 0, tmb.length());
    return new String(buf);
    // return getQueryPlan().toString();

  }
View Full Code Here

TOP

Related Classes of org.apache.thrift.transport.TMemoryBuffer

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.