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.network.nat.NatAwareAddressImplSerializerTest.java

License:Open Source License

@Test
public void testOpenAddress() throws UnknownHostException {
    Serializer serializer = Serializers.lookupSerializer(NatAwareAddressImpl.class);
    NatAwareAddressImpl original, copy;/*ww w . j a v  a2 s.c  o m*/
    ByteBuf buf;

    IdentifierFactory nodeIdFactory = IdentifierRegistry.lookup(BasicIdentifiers.Values.NODE.toString());
    BasicAddress address = new BasicAddress(InetAddress.getLocalHost(), 10000, nodeIdFactory.randomId());
    original = NatAwareAddressImpl.open(address);
    buf = Unpooled.buffer();
    serializer.toBinary(original, buf);

    ByteBuf copyBuf = Unpooled.buffer();
    buf.getBytes(0, copyBuf, buf.readableBytes());
    copy = (NatAwareAddressImpl) serializer.fromBinary(copyBuf, Optional.absent());
    Assert.assertEquals(original.getPublicAdr(), copy.getPublicAdr());
    Assert.assertEquals(original.getNatType(), copy.getNatType());
    Assert.assertTrue(Objects.equals(original.parents, copy.parents));
    Assert.assertFalse(copy.getPrivateAdr().isPresent());
    Assert.assertEquals(0, copyBuf.readableBytes());
}

From source file:se.sics.ktoolbox.util.network.nat.NatAwareAddressImplSerializerTest.java

License:Open Source License

@Test
public void testNatedAddress() throws UnknownHostException {
    Serializer serializer = Serializers.lookupSerializer(NatAwareAddressImpl.class);
    NatAwareAddressImpl original, copy;//w  w w  . ja  v a2s .  c om
    ByteBuf buf, copyBuf;

    IdentifierFactory nodeIdFactory = IdentifierRegistry.lookup(BasicIdentifiers.Values.NODE.toString());
    Identifier nodeId = nodeIdFactory.randomId();
    BasicAddress privateAdr = new BasicAddress(InetAddress.getByName("192.100.100.2"), 10000, nodeId);
    BasicAddress publicAdr = new BasicAddress(InetAddress.getByName("193.200.200.2"), 20000, nodeId);
    NatType natType = NatType.nated(Nat.MappingPolicy.HOST_DEPENDENT, Nat.AllocationPolicy.PORT_CONTIGUITY, 1,
            Nat.FilteringPolicy.HOST_DEPENDENT, 10000);
    ArrayList<BasicAddress> parents = new ArrayList<>();
    BasicAddress parent;
    parent = new BasicAddress(InetAddress.getByName("193.200.200.3"), 10000, nodeIdFactory.randomId());
    parents.add(parent);
    parent = new BasicAddress(InetAddress.getByName("193.200.200.4"), 10001, nodeIdFactory.randomId());
    parents.add(parent);
    original = NatAwareAddressImpl.nated(privateAdr, publicAdr, natType, parents);
    buf = Unpooled.buffer();
    serializer.toBinary(original, buf);

    copyBuf = Unpooled.buffer();
    buf.getBytes(0, copyBuf, buf.readableBytes());
    copy = (NatAwareAddressImpl) serializer.fromBinary(copyBuf, Optional.absent());
    Assert.assertEquals(original.getPublicAdr(), copy.getPublicAdr());
    Assert.assertEquals(original.getNatType(), copy.getNatType());
    Assert.assertTrue(Objects.equals(original.parents, copy.parents));
    Assert.assertFalse(copy.getPrivateAdr().isPresent());
    Assert.assertEquals(0, copyBuf.readableBytes());

    copyBuf = Unpooled.buffer();
    buf.getBytes(0, copyBuf, buf.readableBytes());
    InetSocketAddress isa = new InetSocketAddress(privateAdr.getIp(), privateAdr.getPort());
    copy = (NatAwareAddressImpl) serializer.fromBinary(copyBuf, Optional.of((Object) isa));
    Assert.assertEquals(original.getPublicAdr(), copy.getPublicAdr());
    Assert.assertEquals(original.getNatType(), copy.getNatType());
    Assert.assertTrue(Objects.equals(original.parents, copy.parents));
    Assert.assertTrue(copy.getPrivateAdr().isPresent());
    Assert.assertEquals(original.getPrivateAdr().get(), copy.getPrivateAdr().get());
    Assert.assertEquals(0, copyBuf.readableBytes());
}

From source file:se.sics.ktoolbox.util.network.nat.NatTypeSerializerTest.java

License:Open Source License

@Test
public void testTraits() throws UnknownHostException {
    Serializer natTypeSerializer = Serializers.lookupSerializer(NatType.class);

    NatType original, copy;/* www  .j  a  v  a 2s  .  c o  m*/
    ByteBuf buf, copyBuf;

    buf = Unpooled.buffer();
    original = NatType.open();
    natTypeSerializer.toBinary(original, buf);

    copyBuf = Unpooled.buffer();
    buf.getBytes(0, copyBuf, buf.readableBytes());
    copy = (NatType) natTypeSerializer.fromBinary(copyBuf, Optional.absent());

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

    //****************
    buf = Unpooled.buffer();
    original = NatType.firewall();
    natTypeSerializer.toBinary(original, buf);

    copyBuf = Unpooled.buffer();
    buf.getBytes(0, copyBuf, buf.readableBytes());
    copy = (NatType) natTypeSerializer.fromBinary(copyBuf, Optional.absent());

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

    //*****************
    buf = Unpooled.buffer();
    original = NatType.udpBlocked();
    natTypeSerializer.toBinary(original, buf);

    copyBuf = Unpooled.buffer();
    buf.getBytes(0, copyBuf, buf.readableBytes());
    copy = (NatType) natTypeSerializer.fromBinary(copyBuf, Optional.absent());

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

    //*****************
    buf = Unpooled.buffer();
    original = NatType.upnp();
    natTypeSerializer.toBinary(original, buf);

    copyBuf = Unpooled.buffer();
    buf.getBytes(0, copyBuf, buf.readableBytes());
    copy = (NatType) natTypeSerializer.fromBinary(copyBuf, Optional.absent());

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

    //*****************
    buf = Unpooled.buffer();
    original = NatType.nated(Nat.MappingPolicy.HOST_DEPENDENT, Nat.AllocationPolicy.PORT_CONTIGUITY, 0,
            Nat.FilteringPolicy.HOST_DEPENDENT, 10000);
    natTypeSerializer.toBinary(original, buf);

    copyBuf = Unpooled.buffer();
    buf.getBytes(0, copyBuf, buf.readableBytes());
    copy = (NatType) natTypeSerializer.fromBinary(copyBuf, Optional.absent());

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

    //******************
    buf = Unpooled.buffer();
    original = NatType.nated(Nat.MappingPolicy.HOST_DEPENDENT, Nat.AllocationPolicy.PORT_CONTIGUITY, 0,
            Nat.FilteringPolicy.HOST_DEPENDENT, 10000);
    natTypeSerializer.toBinary(original, buf);

    copyBuf = Unpooled.buffer();
    buf.getBytes(0, copyBuf, buf.readableBytes());
    copy = (NatType) natTypeSerializer.fromBinary(copyBuf, Optional.absent());

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

    //******************
    buf = Unpooled.buffer();
    original = NatType.unknown();
    natTypeSerializer.toBinary(original, buf);

    copyBuf = Unpooled.buffer();
    buf.getBytes(0, copyBuf, buf.readableBytes());
    copy = (NatType) natTypeSerializer.fromBinary(copyBuf, Optional.absent());

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

From source file:se.sics.nstream.storage.cache.KHintSummarySerializerTest.java

License:Open Source License

@Test
public void simpleTest() {
    Serializer serializer = Serializers.lookupSerializer(KHint.Summary.class);
    KHintSummaryEC eqc = new KHintSummaryEC();
    KHint.Summary original, copy;//from  w w  w  .ja  va  2 s. c o m
    ByteBuf serializedOriginal, serializedCopy;

    Set<Integer> blocks = new TreeSet<>();
    blocks.add(0);
    blocks.add(1);
    original = new KHint.Summary(1l, blocks);
    serializedOriginal = Unpooled.buffer();
    serializer.toBinary(original, serializedOriginal);

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

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

From source file:se.sics.nstream.torrent.conn.msg.NetCloseTransferSerializerTest.java

License:Open Source License

@Test
public void simpleClose() {
    Serializer serializer = Serializers.lookupSerializer(NetCloseTransfer.class);
    NetCloseTransferEC ec = new NetCloseTransferEC();
    NetCloseTransfer original, copy;/*w  ww .j  a va2 s.  c  o  m*/
    ByteBuf serializedOriginal, serializedCopy;

    original = new NetCloseTransfer(TorrentIds.fileId(overlayIdFactory.randomId(), 0), false);
    serializedOriginal = Unpooled.buffer();
    serializer.toBinary(original, serializedOriginal);

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

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

From source file:se.sics.nstream.torrent.conn.msg.NetConnectSerializerTest.java

License:Open Source License

@Test
public void simpleReq() {
    Serializer serializer = Serializers.lookupSerializer(NetConnect.Request.class);
    NetConnectRequestEC ec = new NetConnectRequestEC();
    NetConnect.Request original, copy;
    ByteBuf serializedOriginal, serializedCopy;

    original = new NetConnect.Request(overlayIdFactory.randomId());
    serializedOriginal = Unpooled.buffer();
    serializer.toBinary(original, serializedOriginal);

    serializedCopy = Unpooled.buffer();// w  ww. j ava  2  s  .c o m
    serializedOriginal.getBytes(0, serializedCopy, serializedOriginal.readableBytes());
    copy = (NetConnect.Request) serializer.fromBinary(serializedCopy, Optional.absent());

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

From source file:se.sics.nstream.torrent.conn.msg.NetConnectSerializerTest.java

License:Open Source License

@Test
public void simpleResp() {
    Serializer serializer = Serializers.lookupSerializer(NetConnect.Response.class);
    NetConnectResponseEC ec = new NetConnectResponseEC();
    NetConnect.Response original, copy;
    ByteBuf serializedOriginal, serializedCopy;

    NetConnect.Request request = new NetConnect.Request(overlayIdFactory.randomId());
    original = request.answer(true);/* www  . ja  va  2 s. co m*/
    serializedOriginal = Unpooled.buffer();
    serializer.toBinary(original, serializedOriginal);

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

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

From source file:se.sics.nstream.torrent.conn.msg.NetDetailedStateSerializerTest.java

License:Open Source License

@Test
public void simpleReq() {
    Serializer serializer = Serializers.lookupSerializer(NetDetailedState.Request.class);
    NetDetailedStateRequestEC ec = new NetDetailedStateRequestEC();
    NetDetailedState.Request original, copy;
    ByteBuf serializedOriginal, serializedCopy;

    original = new NetDetailedState.Request(overlayIdFactory.randomId());
    serializedOriginal = Unpooled.buffer();
    serializer.toBinary(original, serializedOriginal);

    serializedCopy = Unpooled.buffer();/*from w w  w .  j  a  v  a 2s .  co  m*/
    serializedOriginal.getBytes(0, serializedCopy, serializedOriginal.readableBytes());
    copy = (NetDetailedState.Request) serializer.fromBinary(serializedCopy, Optional.absent());

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

From source file:se.sics.nstream.torrent.conn.msg.NetDetailedStateSerializerTest.java

License:Open Source License

@Test
public void simpleResp() {
    Serializer serializer = Serializers.lookupSerializer(NetDetailedState.Response.class);
    NetDetailedStateResponseEC ec = new NetDetailedStateResponseEC();
    NetDetailedState.Response original, copy;
    ByteBuf serializedOriginal, serializedCopy;

    NetDetailedState.Request request = new NetDetailedState.Request(overlayIdFactory.randomId());
    original = request.success(new MyTorrent.ManifestDef(2, new BlockDetails(5, 3, 2, 1)));
    serializedOriginal = Unpooled.buffer();
    serializer.toBinary(original, serializedOriginal);

    serializedCopy = Unpooled.buffer();// www.  j av  a 2 s .c o m
    serializedOriginal.getBytes(0, serializedCopy, serializedOriginal.readableBytes());
    copy = (NetDetailedState.Response) serializer.fromBinary(serializedCopy, Optional.absent());

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

From source file:se.sics.nstream.torrent.conn.msg.NetOpenTransferSerializerTest.java

License:Open Source License

@Test
public void simpleDefinitionRequest() {
    Serializer serializer = Serializers.lookupSerializer(NetOpenTransfer.Request.class);
    NetTransferDefRequestEC ec = new NetTransferDefRequestEC();
    NetOpenTransfer.Request original, copy;
    ByteBuf serializedOriginal, serializedCopy;

    original = new NetOpenTransfer.Request(TorrentIds.fileId(overlayIdFactory.randomId(), 1));
    serializedOriginal = Unpooled.buffer();
    serializer.toBinary(original, serializedOriginal);

    serializedCopy = Unpooled.buffer();//from w  w w  . jav a 2 s.  c o m
    serializedOriginal.getBytes(0, serializedCopy, serializedOriginal.readableBytes());
    copy = (NetOpenTransfer.Request) serializer.fromBinary(serializedCopy, Optional.absent());

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