Package com.alibaba.dubbo.common.io

Examples of com.alibaba.dubbo.common.io.UnsafeByteArrayOutputStream


            byte status = res.getStatus();
            header[3] = status;
            // set request id.
            Bytes.long2bytes(res.getId(), header, 4);

            UnsafeByteArrayOutputStream bos = new UnsafeByteArrayOutputStream(1024);
            ObjectOutput out = serialization.serialize(channel.getUrl(), bos);
            // encode response data or error message.
            if (status == Response.OK) {
                if (res.isHeartbeat()) {
                    encodeHeartbeatData(channel, out, res.getResult());
                } else {
                    encodeResponseData(channel, out, res.getResult());
                }
            } else out.writeUTF(res.getErrorMessage());
            out.flushBuffer();
            bos.flush();
            bos.close();

            byte[] data = bos.toByteArray();
            checkPayload(channel, data.length);
            Bytes.int2bytes(data.length, header, 12);
            // write
            os.write(header); // write header.
            os.write(data); // write data.
View Full Code Here


  private static final byte[] SMALL_BYTES = SMALL_STRING.getBytes(), BIG_BYTES = BIG_STREAM.getBytes();

  public void testMain() throws Exception
  {
    // write.
    UnsafeByteArrayOutputStream os = new UnsafeByteArrayOutputStream();
    DataOutput cos = new GenericDataOutput(os);
    writeTest(cos);

    // read.
    byte[] b = os.toByteArray();
    DataInput cis = new GenericDataInput(new UnsafeByteArrayInputStream(b));
    readTest(cis);
  }
View Full Code Here

    @Test
  public void testPrimaryTypeBuilder() throws Exception
  {
    System.out.println((new byte[2]).hashCode());
    Builder<String> builder = Builder.register(String.class);
    UnsafeByteArrayOutputStream os = new UnsafeByteArrayOutputStream();
    String v = "123";
    builder.writeTo(v, os);
    byte[] b = os.toByteArray();
    System.out.println(b.length+":"+Bytes.bytes2hex(b));
    v = builder.parseFrom(b);
    builder.writeTo(v, os);
    b = os.toByteArray();
    System.out.println(b.length+":"+Bytes.bytes2hex(b));
  }
View Full Code Here

    @Test
  public void testEnumBuilder() throws Exception
  {
    Builder<Type> builder = Builder.register(Type.class);
    UnsafeByteArrayOutputStream os = new UnsafeByteArrayOutputStream();
    Type v = Type.High;
    builder.writeTo(v, os);
    byte[] b = os.toByteArray();
    System.out.println(b.length+":"+Bytes.bytes2hex(b));
    v = builder.parseFrom(b);
  }
View Full Code Here

    @Test
  public void testThrowableBuilder() throws Exception
  {
    Builder<Throwable> builder = Builder.register(Throwable.class);
    Throwable th = new Throwable();
    UnsafeByteArrayOutputStream os = new UnsafeByteArrayOutputStream();
    builder.writeTo(th, os);
    byte[] b = os.toByteArray();
    System.out.println(b.length+":"+Bytes.bytes2hex(b));

    th = builder.parseFrom(b);
  }
View Full Code Here

  }

    @Test
  public void testArrayClassBuilder() throws Exception
  {
    UnsafeByteArrayOutputStream os;

    byte[] b;

    Builder<Object[]> osb = Builder.register(Object[].class);
    os = new UnsafeByteArrayOutputStream();
    osb.writeTo(new Object[]{ new String[0] }, os);
    b = os.toByteArray();

    Builder<long[]> lsb = Builder.register(long[].class);
    os = new UnsafeByteArrayOutputStream();
    lsb.writeTo(new long[]{ 1,121232,-3,4,-5,61321432413l }, os);
    lsb.writeTo(new long[]{ 1,121232,-3,4,-5,61321432413l }, os);
    lsb.writeTo(new long[]{ 1,2,3,12131314,123132313135l,-6 }, os);
    b = os.toByteArray();
    long[] ls = lsb.parseFrom(b);
    assertEquals(ls.length, 6);

    Builder<byte[]> bsb = Builder.register(byte[].class);
    os = new UnsafeByteArrayOutputStream();
    bsb.writeTo("i am a string.".getBytes(), os);
    b = os.toByteArray();

    Builder<int[][]> iisb = Builder.register(int[][].class);
    os = new UnsafeByteArrayOutputStream();
    iisb.writeTo(new int[][]{ {1,2,3,4}, {5,6,7,8}, {9,10}, {122,123,444} }, os);
    b = os.toByteArray();
    int[][] iis = iisb.parseFrom(b);
    assertEquals(iis.length, 4);

    Builder<int[][][]> iiisb = Builder.register(int[][][].class);
    os = new UnsafeByteArrayOutputStream();
    iiisb.writeTo(new int[][][]{
      {{1,2,3,4}},
      {{5,6,7,8}},
      {{122,123,444}}
    }, os);
    b = os.toByteArray();
    int[][][] iii = iiisb.parseFrom(b);
    assertEquals(iii.length, 3);
  }
View Full Code Here

  }

    @Test
  public void testObjectBuilder() throws Exception
  {
    UnsafeByteArrayOutputStream os = new UnsafeByteArrayOutputStream();
    Builder<Bean> BeanBuilder = Builder.register(Bean.class);

    Bean bean = new Bean();
    bean.name = "ql";
    bean.type = Type.High;
    bean.types = new Type[]{ Type.High, Type.High };
    BeanBuilder.writeTo(bean, os);

    byte[] b = os.toByteArray();
    System.out.println(b.length+":"+Bytes.bytes2hex(b));

    bean = BeanBuilder.parseFrom(b);
    assertNull(bean.time);
    assertEquals(bean.i, 123123);
View Full Code Here

  }

    @Test
  public void testInterfaceBuilder() throws Exception
  {
    UnsafeByteArrayOutputStream os = new UnsafeByteArrayOutputStream();
    Builder<TestDO> builder = Builder.register(TestDO.class);
    TestDO d = new TestDOImpl();
    builder.writeTo(d, os);

    byte[] b = os.toByteArray();

    d = builder.parseFrom(b);
    assertTrue(TestDO.class.isAssignableFrom(d.getClass()));
    assertEquals("name", d.getName());
    assertEquals(28, d.getArg());
View Full Code Here

  }

    @Test
  public void testGenericBuilder() throws Exception
  {
    UnsafeByteArrayOutputStream os = new UnsafeByteArrayOutputStream();
    Builder<Object> ob = Builder.register(Object.class);

    Object o = new Object();
    ob.writeTo(o, os);
    byte[] b = os.toByteArray();

    os = new UnsafeByteArrayOutputStream();
    Bean bean = new Bean();
    bean.name = "ql";
    bean.type = Type.High;
    bean.types = new Type[]{ Type.High, Type.High };
    ob.writeTo(bean, os);

    b = os.toByteArray();
    bean = (Bean)ob.parseFrom(b);
    assertEquals(bean.i, 123123);
    assertEquals(bean.ni, -12344);
    assertEquals(bean.d, 12.345);
    assertEquals(bean.nd, -12.345);
View Full Code Here

  }

    @Test
  public void testObjectArrayBuilder() throws Exception
  {
    UnsafeByteArrayOutputStream os = new UnsafeByteArrayOutputStream();
    Builder<Object[]> builder = Builder.register(Object[].class);

    Object[] obj = new Object[5];
    obj[0] = "1234";
    obj[1] = new Double(109.23);
    obj[2] = "3455";
    obj[3] = null;
    obj[4] = Boolean.TRUE;

    builder.writeTo(obj, os);
    byte[] b = os.toByteArray();
    System.out.println("Object array:"+b.length+":"+Bytes.bytes2hex(b));

    Assert.assertArrayEquals(obj, builder.parseFrom(b));
  }
View Full Code Here

TOP

Related Classes of com.alibaba.dubbo.common.io.UnsafeByteArrayOutputStream

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.