Example usage for io.netty.buffer ByteBuf getBytes

List of usage examples for io.netty.buffer ByteBuf getBytes

Introduction

In this page you can find the example usage for io.netty.buffer ByteBuf getBytes.

Prototype

public abstract int getBytes(int index, GatheringByteChannel out, int length) throws IOException;

Source Link

Document

Transfers this buffer's data to the specified channel starting at the specified absolute index .

Usage

From source file:se.sics.ktoolbox.util.identifiable.basic.IntIdSerializerTest.java

License:Open Source License

@Test
public void simpleTest() {
    Serializer serializer = Serializers.lookupSerializer(IntId.class);
    IntId original, copy;/*from   ww w .  j  av  a 2s  .co  m*/
    ByteBuf serializedOriginal, serializedCopy;

    IntIdFactory idFactory = new IntIdFactory(new Random(1234l));
    original = idFactory.randomId();
    serializedOriginal = Unpooled.buffer();
    serializer.toBinary(original, serializedOriginal);

    serializedCopy = Unpooled.buffer();
    serializedOriginal.getBytes(0, serializedCopy, serializedOriginal.readableBytes());
    copy = (IntId) serializer.fromBinary(serializedCopy, Optional.absent());

    Assert.assertEquals(original, copy);
    Assert.assertEquals(0, serializedCopy.readableBytes());
}

From source file:se.sics.ktoolbox.util.identifiable.basic.OverlayIdSerializerTest.java

License:Open Source License

@Test
public void simpleTest() {
    Serializer serializer = Serializers.lookupSerializer(OverlayId.class);
    OverlayId original, copy;//from   w  w w .j  a v a2 s . c  o m
    ByteBuf serializedOriginal, serializedCopy;

    byte ownerId = 11;
    IdentifierFactory baseIdFactory = IdentifierRegistry.lookup(BasicIdentifiers.Values.OVERLAY.toString());
    OverlayIdFactory idFactory = new OverlayIdFactory(baseIdFactory, OverlayId.BasicTypes.CROUPIER, ownerId);
    original = idFactory.randomId();
    serializedOriginal = Unpooled.buffer();
    serializer.toBinary(original, serializedOriginal);

    serializedCopy = Unpooled.buffer();
    serializedOriginal.getBytes(0, serializedCopy, serializedOriginal.readableBytes());
    copy = (OverlayId) serializer.fromBinary(serializedCopy, Optional.absent());

    Assert.assertEquals(original, copy);
    Assert.assertEquals(0, serializedCopy.readableBytes());
}

From source file:se.sics.ktoolbox.util.identifiable.basic.SimpleByteIdSerializerTest.java

License:Open Source License

@Test
public void simpleTest() {
    Serializer serializer = Serializers.lookupSerializer(SimpleByteId.class);
    SimpleByteId original, copy;//from   ww w  .j a  v  a2s .com
    ByteBuf serializedOriginal, serializedCopy;

    SimpleByteIdFactory idFactory = new SimpleByteIdFactory(new Random(1234l), 5);
    original = idFactory.randomId();
    serializedOriginal = Unpooled.buffer();
    serializer.toBinary(original, serializedOriginal);

    serializedCopy = Unpooled.buffer();
    serializedOriginal.getBytes(0, serializedCopy, serializedOriginal.readableBytes());
    copy = (SimpleByteId) serializer.fromBinary(serializedCopy, Optional.absent());

    Assert.assertEquals(original, copy);
    Assert.assertEquals(0, serializedCopy.readableBytes());
}

From source file:se.sics.ktoolbox.util.identifiable.basic.StringByteIdSerializerTest.java

License:Open Source License

@Test
public void simpleTest() {
    Serializer serializer = Serializers.lookupSerializer(StringByteId.class);
    StringByteId original, copy;/*  w w w  .ja v a  2  s.co m*/
    ByteBuf serializedOriginal, serializedCopy;

    StringByteIdFactory idFactory = new StringByteIdFactory(new Random(1234l), 6);
    original = idFactory.randomId();
    serializedOriginal = Unpooled.buffer();
    serializer.toBinary(original, serializedOriginal);

    serializedCopy = Unpooled.buffer();
    serializedOriginal.getBytes(0, serializedCopy, serializedOriginal.readableBytes());
    copy = (StringByteId) serializer.fromBinary(serializedCopy, Optional.absent());

    Assert.assertEquals(original, copy);
    Assert.assertEquals(0, serializedCopy.readableBytes());
}

From source file:se.sics.ktoolbox.util.identifiable.basic.UUIDIdSerializerTest.java

License:Open Source License

@Test
public void simpleTest() {
    Serializer serializer = Serializers.lookupSerializer(UUIDId.class);
    UUIDId original, copy;//from www.j a v  a 2 s. c  om
    ByteBuf serializedOriginal, serializedCopy;

    UUIDIdFactory idFactory = new UUIDIdFactory();
    original = idFactory.randomId();
    serializedOriginal = Unpooled.buffer();
    serializer.toBinary(original, serializedOriginal);

    serializedCopy = Unpooled.buffer();
    serializedOriginal.getBytes(0, serializedCopy, serializedOriginal.readableBytes());
    copy = (UUIDId) serializer.fromBinary(serializedCopy, Optional.absent());

    Assert.assertEquals(original, copy);
    Assert.assertEquals(0, serializedCopy.readableBytes());
}

From source file:se.sics.ktoolbox.util.network.basic.BasicAddressSerializerTest.java

License:Open Source License

@Test
public void testBasicAddress() throws UnknownHostException {
    Serializer serializer = Serializers.lookupSerializer(BasicAddress.class);
    BasicAddress original, copy;/*from w  ww. jav a 2s.  co  m*/
    ByteBuf buf;

    Identifier nodeId = IdentifierRegistry.lookup(BasicIdentifiers.Values.NODE.toString()).randomId();
    original = new BasicAddress(InetAddress.getLocalHost(), 10000, nodeId);
    buf = Unpooled.buffer();
    serializer.toBinary(original, buf);

    ByteBuf copyBuf = Unpooled.buffer();
    buf.getBytes(0, copyBuf, buf.readableBytes());
    copy = (BasicAddress) serializer.fromBinary(copyBuf, Optional.absent());

    Assert.assertEquals(original, copy);
    Assert.assertEquals(0, copyBuf.readableBytes());
}

From source file:se.sics.ktoolbox.util.network.basic.BasicContentMsgSerializerTest.java

License:Open Source License

@Test
public void testBasicContentMsg() {
    Serializer serializer = Serializers.lookupSerializer(BasicContentMsg.class);
    BasicContentMsg original, copy;//from ww w . j a v a 2s .c o  m
    ByteBuf serializedOriginal, serializedCopy;

    InetAddress localHost;
    try {
        localHost = InetAddress.getByName("localhost");
    } catch (UnknownHostException ex) {
        throw new RuntimeException(ex);
    }

    IdentifierFactory nodeIdFactory = IdentifierRegistry.lookup(BasicIdentifiers.Values.NODE.toString());
    BasicAddress basicAdr1 = new BasicAddress(localHost, 10000, nodeIdFactory.randomId());
    BasicAddress basicAdr2 = new BasicAddress(localHost, 10000, nodeIdFactory.randomId());
    BasicAddress basicAdr3 = new BasicAddress(localHost, 10000, nodeIdFactory.randomId());
    BasicAddress basicAdr4 = new BasicAddress(localHost, 10000, nodeIdFactory.randomId());

    byte ownerId = 1;
    IdentifierFactory baseOverlayIdFactory = IdentifierRegistry
            .lookup(BasicIdentifiers.Values.OVERLAY.toString());
    OverlayIdFactory overlayIdFactory = new OverlayIdFactory(baseOverlayIdFactory,
            OverlayId.BasicTypes.CROUPIER, ownerId);
    DecoratedHeader header = new DecoratedHeader(new BasicHeader(basicAdr1, basicAdr4, Transport.UDP),
            overlayIdFactory.randomId());
    TestContent content = new TestContent(5);

    original = new BasicContentMsg(header, content);
    serializedOriginal = Unpooled.buffer();
    serializer.toBinary(original, serializedOriginal);

    serializedCopy = Unpooled.buffer();
    serializedOriginal.getBytes(0, serializedCopy, serializedOriginal.readableBytes());
    copy = (BasicContentMsg) serializer.fromBinary(serializedCopy, Optional.absent());

    Assert.assertEquals(original.getHeader().getSource(), copy.getHeader().getSource());
    Assert.assertEquals(original.getHeader().getDestination(), copy.getHeader().getDestination());
    Assert.assertEquals(original.getHeader().getProtocol(), copy.getHeader().getProtocol());
    Assert.assertEquals(original.getContent(), copy.getContent());
    Assert.assertEquals(0, serializedCopy.readableBytes());
}

From source file:se.sics.ktoolbox.util.network.basic.BasicHeaderSerializerTest.java

License:Open Source License

@Test
public void testBasicAddress() throws UnknownHostException {
    Serializer serializer = Serializers.lookupSerializer(BasicHeader.class);
    BasicHeader<KAddress> original, copy;
    ByteBuf serializedOriginal, serializedCopy;

    InetAddress localHost;/* www.j  a  v a 2s.  co  m*/
    try {
        localHost = InetAddress.getByName("localhost");
    } catch (UnknownHostException ex) {
        throw new RuntimeException(ex);
    }
    IdentifierFactory nodeIdFactory = IdentifierRegistry.lookup(BasicIdentifiers.Values.NODE.toString());
    BasicAddress simpleAdr1 = new BasicAddress(localHost, 10000, nodeIdFactory.randomId());
    BasicAddress simpleAdr2 = new BasicAddress(localHost, 10000, nodeIdFactory.randomId());
    original = new BasicHeader(simpleAdr1, simpleAdr2, Transport.UDP);
    serializedOriginal = Unpooled.buffer();
    serializer.toBinary(original, serializedOriginal);

    serializedCopy = Unpooled.buffer();
    serializedOriginal.getBytes(0, serializedCopy, serializedOriginal.readableBytes());
    copy = (BasicHeader) serializer.fromBinary(serializedCopy, Optional.absent());

    Assert.assertEquals(original.getSource(), copy.getSource());
    Assert.assertEquals(original.getDestination(), copy.getDestination());
    Assert.assertEquals(original.getProtocol(), copy.getProtocol());
    Assert.assertEquals(0, serializedCopy.readableBytes());
}

From source file:se.sics.ktoolbox.util.network.basic.BasicHeaderSerializerTest.java

License:Open Source License

@Test
public void testNatAwareOpenAddress() throws UnknownHostException {
    Serializer serializer = Serializers.lookupSerializer(BasicHeader.class);
    BasicHeader<NatAwareAddressImpl> original, copy;
    ByteBuf serializedOriginal, serializedCopy;

    InetAddress localHost;//  w ww .ja  v a  2 s  . co m
    try {
        localHost = InetAddress.getByName("localhost");
    } catch (UnknownHostException ex) {
        throw new RuntimeException(ex);
    }
    IdentifierFactory nodeIdFactory = IdentifierRegistry.lookup(BasicIdentifiers.Values.NODE.toString());
    NatAwareAddressImpl simpleAdr1 = NatAwareAddressImpl
            .open(new BasicAddress(localHost, 10000, nodeIdFactory.randomId()));
    NatAwareAddressImpl simpleAdr2 = NatAwareAddressImpl
            .open(new BasicAddress(localHost, 10000, nodeIdFactory.randomId()));
    original = new BasicHeader(simpleAdr1, simpleAdr2, Transport.UDP);
    serializedOriginal = Unpooled.buffer();
    serializer.toBinary(original, serializedOriginal);

    serializedCopy = Unpooled.buffer();
    serializedOriginal.getBytes(0, serializedCopy, serializedOriginal.readableBytes());
    copy = (BasicHeader) serializer.fromBinary(serializedCopy, Optional.absent());

    Assert.assertFalse(copy.getSource().getPrivateAdr().isPresent());
    Assert.assertEquals(original.getSource().getPublicAdr(), copy.getSource().getPublicAdr());
    Assert.assertEquals(original.getSource().getNatType(), copy.getSource().getNatType());
    Assert.assertTrue(Objects.equals(original.getSource().getParents(), copy.getSource().getParents()));
    Assert.assertFalse(copy.getDestination().getPrivateAdr().isPresent());
    Assert.assertEquals(original.getDestination().getPublicAdr(), copy.getDestination().getPublicAdr());
    Assert.assertEquals(original.getDestination().getNatType(), copy.getDestination().getNatType());
    Assert.assertTrue(
            Objects.equals(original.getDestination().getParents(), copy.getDestination().getParents()));
    Assert.assertEquals(original.getProtocol(), copy.getProtocol());
    Assert.assertEquals(0, serializedCopy.readableBytes());
}

From source file:se.sics.ktoolbox.util.network.basic.DecoratedHeaderSerializerTest.java

License:Open Source License

@Test
public void test1() throws UnknownHostException {
    Serializer serializer = Serializers.lookupSerializer(DecoratedHeader.class);
    DecoratedHeader<BasicAddress> original, copy;
    ByteBuf serializedOriginal, serializedCopy;

    InetAddress localHost;/*from   w  ww. ja va 2s.c o  m*/
    try {
        localHost = InetAddress.getByName("localhost");
    } catch (UnknownHostException ex) {
        throw new RuntimeException(ex);
    }

    IdentifierFactory nodeIdFactory = IdentifierRegistry.lookup(BasicIdentifiers.Values.NODE.toString());
    BasicAddress basicAdr1 = new BasicAddress(localHost, 10000, nodeIdFactory.randomId());
    BasicAddress basicAdr2 = new BasicAddress(localHost, 10000, nodeIdFactory.randomId());

    byte ownerId = 1;
    IdentifierFactory baseOverlayIdFactory = IdentifierRegistry
            .lookup(BasicIdentifiers.Values.OVERLAY.toString());
    OverlayIdFactory overlayIdFactory = new OverlayIdFactory(baseOverlayIdFactory,
            OverlayId.BasicTypes.CROUPIER, ownerId);
    original = new DecoratedHeader(new BasicHeader(basicAdr1, basicAdr2, Transport.UDP),
            overlayIdFactory.randomId());
    serializedOriginal = Unpooled.buffer();
    serializer.toBinary(original, serializedOriginal);

    serializedCopy = Unpooled.buffer();
    serializedOriginal.getBytes(0, serializedCopy, serializedOriginal.readableBytes());
    copy = (DecoratedHeader) serializer.fromBinary(serializedCopy, Optional.absent());

    Assert.assertEquals(original.getSource(), copy.getSource());
    Assert.assertEquals(original.getDestination(), copy.getDestination());
    Assert.assertEquals(original.getProtocol(), copy.getProtocol());
    Assert.assertEquals(original.getOverlayId(), copy.getOverlayId());

    Assert.assertEquals(0, serializedCopy.readableBytes());
}