Example usage for io.netty.buffer Unpooled directBuffer

List of usage examples for io.netty.buffer Unpooled directBuffer

Introduction

In this page you can find the example usage for io.netty.buffer Unpooled directBuffer.

Prototype

public static ByteBuf directBuffer() 

Source Link

Document

Creates a new big-endian direct buffer with reasonably small initial capacity, which expands its capacity boundlessly on demand.

Usage

From source file:se.sics.kompics.network.netty.serialization.SerializationTest.java

License:Open Source License

@Test
public void serializableTest() {
    SomeSerializable some = new SomeSerializable();
    ByteBuf buf = Unpooled.directBuffer();
    Serializers.toBinary(some, buf);// w ww.j  a va 2 s.  c  om
    System.out.println(
            "SomeSerializable: " + ByteBufUtil.hexDump(buf) + " : " + ByteBufUtil.hexDump(buf).length());
    SomeSerializable someRes = (SomeSerializable) Serializers.fromBinary(buf, Optional.absent());
    assertEquals(some.getField(), someRes.getField());
}

From source file:se.sics.kompics.network.netty.serialization.SerializationTest.java

License:Open Source License

@Test
public void parentSerializableTest() {
    SomeSerializable someI = new SomeSerializable();
    ParentSome some = new ParentSome(someI);
    ByteBuf buf = Unpooled.directBuffer();
    Serializers.toBinary(some, buf);//  w w w .  j a  va 2s.  c  o  m
    System.out.println("SomeParent: " + ByteBufUtil.hexDump(buf) + " : " + ByteBufUtil.hexDump(buf).length());
    ParentSome someRes = (ParentSome) Serializers.fromBinary(buf, Optional.absent());
    assertEquals(some.getMySer().getField(), someRes.getMySer().getField());
}

From source file:se.sics.kompics.network.netty.serialization.SerializationTest.java

License:Open Source License

@Test
public void addressTest() {
    try {//from   w  w w .j  av  a 2s  . co  m
        Address addr = new Address(InetAddress.getByName("127.0.0.1"), 1234, new byte[] { 1, 2, 3, 4 });
        Address hostAddr = addr.hostAddress();
        ByteBuf buf = Unpooled.directBuffer();
        Serializers.toBinary(addr, buf);
        System.out.println("Address: " + ByteBufUtil.hexDump(buf));
        Address someRes = (Address) Serializers.fromBinary(buf, Optional.absent());
        assertEquals(addr, someRes);

        buf.clear();

        Serializers.toBinary(hostAddr, buf);
        System.out.println("HostAddress: " + ByteBufUtil.hexDump(buf));
        Address someHostRes = (Address) Serializers.fromBinary(buf, Optional.absent());
        assertEquals(hostAddr, someHostRes);

        buf.release();
    } catch (UnknownHostException ex) {
        Assert.fail(ex.getMessage());
    }
}

From source file:se.sics.kompics.network.netty.serialization.SerializationTest.java

License:Open Source License

@Test
public void disambTest() {
    for (Transport proto : Transport.values()) {
        try {//w  w  w.j  a va2 s.  c  o  m
            Address src = new Address(InetAddress.getByName("127.0.0.1"), 1234, new byte[] { 1, 2, 3, 4 });
            Address dst = new Address(InetAddress.getByName("127.0.0.1"), 5678, new byte[] { 5, 6, 7, 8 });
            DisambiguateConnection.Req req = new DisambiguateConnection.Req(src, dst, proto, 1234, 5678);
            DisambiguateConnection.Resp resp = new DisambiguateConnection.Resp(src, dst, proto, 1234, 5678,
                    9876);
            ByteBuf buf = Unpooled.directBuffer();
            Serializers.toBinary(req, buf);
            System.out.println("DisambReq: " + ByteBufUtil.hexDump(buf));
            Object someRes = Serializers.fromBinary(buf, Optional.absent());
            Assert.assertNotNull(someRes);
            someRes = null;
            buf.clear();
            Serializers.toBinary(resp, buf);
            System.out.println("DisambResp: " + ByteBufUtil.hexDump(buf));
            someRes = Serializers.fromBinary(buf, Optional.absent());
            Assert.assertNotNull(someRes);
        } catch (UnknownHostException ex) {
            Assert.fail(ex.getMessage());
        }
    }
}

From source file:se.sics.kompics.network.netty.serialization.SerializationTest.java

License:Open Source License

@Test
public void uuidTest() {
    ByteBuf buf = Unpooled.directBuffer();
    UUID orig = UUID.randomUUID();
    Serializers.toBinary(orig, buf);//from w ww  . jav a  2 s.  c  om
    System.out.println("UUID: " + ByteBufUtil.hexDump(buf));
    UUID copy = (UUID) Serializers.fromBinary(buf, Optional.absent());
    Assert.assertEquals(orig, copy);
}

From source file:se.sics.kompics.network.netty.serialization.SerializationTest.java

License:Open Source License

@Test
public void avroTest() throws AvroSerializer.KeyExistsException, AvroSerializer.InvalidKeyException {
    Serializers.register(new AvroSerializer(), "avroS");
    AvroSerializer.register(15, SomeAvro.class);
    Serializers.register(ParentSomeAvro.class, "avroS");
    AvroSerializer.register(16, SomeGeneratedAvro.class);
    SomeAvro some = new SomeAvro();
    ByteBuf buf = Unpooled.directBuffer();
    Serializers.toBinary(some, buf);/*w  ww  .  ja  va2s  .  c o  m*/
    System.out.println(
            "AVRO - SomeAvro: " + ByteBufUtil.hexDump(buf) + " : " + ByteBufUtil.hexDump(buf).length());
    SomeAvro someRes = (SomeAvro) Serializers.fromBinary(buf, Optional.absent());
    assertEquals(some.getField(), someRes.getField());
    //
    buf.clear();
    //
    ParentSomeAvro someP = new ParentSomeAvro(some);
    Serializers.toBinary(someP, buf);
    System.out.println(
            "AVRO - ParentSomeAvro: " + ByteBufUtil.hexDump(buf) + " : " + ByteBufUtil.hexDump(buf).length());
    ParentSomeAvro somePRes = (ParentSomeAvro) Serializers.fromBinary(buf, Optional.absent());
    assertEquals(someP.getMySer().getField(), somePRes.getMySer().getField());
    //
    buf.clear();
    //
    SomeGeneratedAvro sga = SomeGeneratedAvro.newBuilder().setSomeNumber(1234).build();
    Serializers.toBinary(sga, buf);
    System.out.println("AVRO - SomeGeneratedAvro: " + ByteBufUtil.hexDump(buf) + " : "
            + ByteBufUtil.hexDump(buf).length());
    SomeGeneratedAvro sgaR = (SomeGeneratedAvro) Serializers.fromBinary(buf, Optional.absent());
    assertEquals(sga.getSomeNumber(), sgaR.getSomeNumber());
    //
    buf.release();
}