Example usage for io.netty.buffer Unpooled buffer

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

Introduction

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

Prototype

public static ByteBuf buffer() 

Source Link

Document

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

Usage

From source file:com.corundumstudio.socketio.parser.EncoderErrorPacketTest.java

License:Apache License

@Test
public void testEncodeWithReason() throws IOException {
    Packet packet = new Packet(PacketType.ERROR);
    packet.setReason(ErrorReason.TRANSPORT_NOT_SUPPORTED);
    ByteBuf result = Unpooled.buffer();
    encoder.encodePacket(packet, result);
    Assert.assertEquals("7:::0", result.toString(CharsetUtil.UTF_8));
}

From source file:com.corundumstudio.socketio.parser.EncoderErrorPacketTest.java

License:Apache License

@Test
public void testEncodeWithReasonAndAdvice() throws IOException {
    Packet packet = new Packet(PacketType.ERROR);
    packet.setReason(ErrorReason.UNAUTHORIZED);
    packet.setAdvice(ErrorAdvice.RECONNECT);
    ByteBuf result = Unpooled.buffer();
    encoder.encodePacket(packet, result);
    Assert.assertEquals("7:::2+0", result.toString(CharsetUtil.UTF_8));
}

From source file:com.corundumstudio.socketio.parser.EncoderErrorPacketTest.java

License:Apache License

@Test
public void testEncodeWithEndpoint() throws IOException {
    Packet packet = new Packet(PacketType.ERROR);
    packet.setEndpoint("/woot");
    ByteBuf result = Unpooled.buffer();
    encoder.encodePacket(packet, result);
    Assert.assertEquals("7::/woot", result.toString(CharsetUtil.UTF_8));
}

From source file:com.corundumstudio.socketio.parser.EncoderEventPacketTest.java

License:Apache License

@Test
public void testEncode() throws IOException {
    Packet packet = new Packet(PacketType.EVENT);
    packet.setName("woot");
    ByteBuf result = Unpooled.buffer();
    //        encoder.encodePacket(packet, result);
    Assert.assertEquals("5:::{\"name\":\"woot\"}", result.toString(CharsetUtil.UTF_8));
}

From source file:com.corundumstudio.socketio.parser.EncoderEventPacketTest.java

License:Apache License

@Test
public void testEncodeWithMessageIdAndAck() throws IOException {
    Packet packet = new Packet(PacketType.EVENT);
    //        packet.setId(1L);
    //        packet.setAck(Packet.ACK_DATA);
    packet.setName("tobi");
    ByteBuf result = Unpooled.buffer();
    //        encoder.encodePacket(packet, result);
    Assert.assertEquals("5:1+::{\"name\":\"tobi\"}", result.toString(CharsetUtil.UTF_8));
}

From source file:com.corundumstudio.socketio.parser.EncoderEventPacketTest.java

License:Apache License

@Test
public void testEncodeWithData() throws IOException {
    Packet packet = new Packet(PacketType.EVENT);
    packet.setName("edwald");
    //        packet.setArgs(Arrays.asList(Collections.singletonMap("a", "b"), 2, "3"));
    ByteBuf result = Unpooled.buffer();
    //        encoder.encodePacket(packet, result);
    Assert.assertEquals("5:::{\"name\":\"edwald\",\"args\":[{\"a\":\"b\"},2,\"3\"]}",
            result.toString(CharsetUtil.UTF_8));
}

From source file:com.corundumstudio.socketio.parser.EncoderJsonPacketTest.java

License:Apache License

@Test
public void testEncode() throws IOException {
    Packet packet = new Packet(PacketType.JSON);
    packet.setData("2");
    ByteBuf result = Unpooled.buffer();
    encoder.encodePacket(packet, result);
    Assert.assertEquals("4:::\"2\"", result.toString(CharsetUtil.UTF_8));
}

From source file:com.corundumstudio.socketio.parser.EncoderJsonPacketTest.java

License:Apache License

@Test
public void testEncodeWithMessageIdAndAckData() throws IOException {
    Packet packet = new Packet(PacketType.JSON);
    packet.setId(1L);//  www. j  a  va  2  s .c  om
    packet.setAck(Packet.ACK_DATA);
    packet.setData(Collections.singletonMap("a", "b"));
    ByteBuf result = Unpooled.buffer();
    encoder.encodePacket(packet, result);
    Assert.assertEquals("4:1+::{\"a\":\"b\"}", result.toString(CharsetUtil.UTF_8));
}

From source file:com.corundumstudio.socketio.parser.EncoderJsonPacketTest.java

License:Apache License

@Test
public void testPerf() throws IOException {
    List<Packet> packets = new ArrayList<Packet>();
    for (int i = 0; i < 100; i++) {
        Packet packet = new Packet(PacketType.JSON);
        packet.setId(1L);// w  ww.ja v a  2s .c  o  m
        packet.setData(Collections.singletonMap("", "123123jksdf213"));
        packets.add(packet);
    }

    List<Queue<Packet>> queues = new ArrayList<Queue<Packet>>();
    for (int i = 0; i < 5000; i++) {
        ConcurrentLinkedQueue queue = new ConcurrentLinkedQueue(packets);
        queues.add(queue);

    }
    long t = System.currentTimeMillis();

    for (int i = 0; i < 5000; i++) {
        encoder.encodePackets(queues.get(i), Unpooled.buffer(), UnpooledByteBufAllocator.DEFAULT);
        //            String message = encoder.encodePackets(queues.get(i));
        //            ChannelBuffers.copiedBuffer(message, CharsetUtil.UTF_8);
    }
    // 1000 ms

    System.out.println(System.currentTimeMillis() - t);
}

From source file:com.corundumstudio.socketio.parser.EncoderMessagePacketTest.java

License:Apache License

@Test
public void testEncode() throws IOException {
    Packet packet = new Packet(PacketType.MESSAGE);
    packet.setData("woot");
    ByteBuf result = Unpooled.buffer();
    //        encoder.encodePacket(packet, result);
    Assert.assertEquals("3:::woot", result.toString(CharsetUtil.UTF_8));
}