Example usage for io.netty.util CharsetUtil UTF_8

List of usage examples for io.netty.util CharsetUtil UTF_8

Introduction

In this page you can find the example usage for io.netty.util CharsetUtil UTF_8.

Prototype

Charset UTF_8

To view the source code for io.netty.util CharsetUtil UTF_8.

Click Source Link

Document

8-bit UTF (UCS Transformation Format)

Usage

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();/*from w  ww.j  a  v  a  2s.c  om*/
    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);/*ww w .  ja  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.EncoderMessagePacketTest.java

License:Apache License

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

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

License:Apache License

@Test
public void testEncodeWithIdAndEndpoint() throws IOException {
    Packet packet = new Packet(PacketType.MESSAGE);
    //        packet.setId(5L);
    //        packet.setAck(true);
    packet.setNsp("/tobi");
    ByteBuf result = Unpooled.buffer();//from  ww  w  .ja v a  2 s  .  co m
    //        encoder.encodePacket(packet, result);
    Assert.assertEquals("3:5:/tobi", result.toString(CharsetUtil.UTF_8));
}

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

License:Apache License

@Test
public void testPayloadEncode() throws IOException {
    Queue<Packet> packets = new ConcurrentLinkedQueue<Packet>();
    Packet packet1 = new Packet(PacketType.MESSAGE);
    packet1.setData("5");
    packets.add(packet1);/*from w ww  . ja  v a 2  s . co  m*/

    Packet packet2 = new Packet(PacketType.MESSAGE);
    packet2.setData("53d");
    packets.add(packet2);

    ByteBuf result = Unpooled.buffer();
    //        encoder.encodePackets(packets, result, UnpooledByteBufAllocator.DEFAULT);
    Assert.assertEquals("\ufffd5\ufffd3:::5\ufffd7\ufffd3:::53d", result.toString(CharsetUtil.UTF_8));
}

From source file:com.corundumstudio.socketio.protocol.PacketDecoder.java

License:Apache License

public ByteBuf preprocessJson(Integer jsonIndex, ByteBuf content) throws IOException {
    String packet = URLDecoder.decode(content.toString(CharsetUtil.UTF_8), CharsetUtil.UTF_8.name());

    int startPos = 0;
    if (jsonIndex != null) {
        // skip "d="
        startPos = 2;//www .jav  a2  s .  c  o m

        /**
        * double escaping is required for escaped new lines because unescaping of new lines can be done safely on server-side
        * (c) socket.io.js
        *
        * @see https://github.com/Automattic/socket.io-client/blob/1.3.3/socket.io.js#L2682
        */
        packet = packet.replace("\\\\n", "\\n");
    }

    int splitIndex = packet.indexOf(":");
    String len = packet.substring(startPos, splitIndex);
    Integer length = Integer.valueOf(len);

    packet = packet.substring(splitIndex + 1, splitIndex + length + 1);
    packet = new String(packet.getBytes(CharsetUtil.ISO_8859_1), CharsetUtil.UTF_8);

    return Unpooled.wrappedBuffer(packet.getBytes(CharsetUtil.UTF_8));
}

From source file:com.corundumstudio.socketio.protocol.PacketDecoder.java

License:Apache License

@Deprecated
public Packet decodePacket(String string, UUID uuid) throws IOException {
    ByteBuf buf = Unpooled.copiedBuffer(string, CharsetUtil.UTF_8);
    try {/*from   w w  w  . jav  a  2 s.  c  o m*/
        return null;
    } finally {
        buf.release();
    }
}

From source file:com.corundumstudio.socketio.protocol.PacketDecoder.java

License:Apache License

public Packet decodePackets(ByteBuf buffer, ClientHead client) throws IOException {
    if (isStringPacket(buffer)) {
        // TODO refactor
        int headEndIndex = buffer.bytesBefore((byte) -1);
        int len = (int) readLong(buffer, headEndIndex);

        ByteBuf frame = buffer.slice(buffer.readerIndex() + 1, len);
        // skip this frame
        buffer.readerIndex(buffer.readerIndex() + 1 + len);
        return decode(client, frame);
    } else if (hasLengthHeader(buffer)) {
        // TODO refactor
        int lengthEndIndex = buffer.bytesBefore((byte) ':');
        int lenHeader = (int) readLong(buffer, lengthEndIndex);
        int len = utf8scanner.getActualLength(buffer, lenHeader);

        ByteBuf frame = buffer.slice(buffer.readerIndex() + 1, len);
        if (lenHeader != len) {
            frame = Unpooled.wrappedBuffer(frame.toString(CharsetUtil.UTF_8).getBytes(CharsetUtil.ISO_8859_1));
        }//from   w w  w.ja  va  2 s  .  c om
        // skip this frame
        buffer.readerIndex(buffer.readerIndex() + 1 + len);
        return decode(client, frame);
    }
    return decode(client, buffer);
}

From source file:com.corundumstudio.socketio.protocol.PacketDecoder.java

License:Apache License

private String readString(ByteBuf frame, int size) {
    byte[] bytes = new byte[size];
    frame.readBytes(bytes);//from www  .  ja v  a  2s .  c  om
    return new String(bytes, CharsetUtil.UTF_8);
}

From source file:com.corundumstudio.socketio.protocol.PacketDecoder.java

License:Apache License

private Packet parseBinary(ClientHead head, ByteBuf frame) throws IOException {
    if (frame.getByte(0) == 1) {
        frame.readByte();/*from  w ww  . ja  v  a 2  s  .  c om*/
        int headEndIndex = frame.bytesBefore((byte) -1);
        int len = (int) readLong(frame, headEndIndex);
        ByteBuf oldFrame = frame;
        frame = frame.slice(oldFrame.readerIndex() + 1, len);
        oldFrame.readerIndex(oldFrame.readerIndex() + 1 + len);
    }

    if (frame.getByte(0) == 'b' && frame.getByte(1) == '4') {
        frame.readShort();
    } else if (frame.getByte(0) == 4) {
        frame.readByte();
    }

    Packet binaryPacket = head.getLastBinaryPacket();
    if (binaryPacket != null) {
        ByteBuf attachBuf;
        if (frame.getByte(0) == 'b' && frame.getByte(1) == '4') {
            attachBuf = frame;
        } else {
            attachBuf = Base64.encode(frame);
        }
        binaryPacket.addAttachment(Unpooled.copiedBuffer(attachBuf));
        frame.readerIndex(frame.readerIndex() + frame.readableBytes());

        if (binaryPacket.isAttachmentsLoaded()) {
            LinkedList<ByteBuf> slices = new LinkedList<ByteBuf>();
            ByteBuf source = binaryPacket.getDataSource();
            for (int i = 0; i < binaryPacket.getAttachments().size(); i++) {
                ByteBuf attachment = binaryPacket.getAttachments().get(i);
                ByteBuf scanValue = Unpooled.copiedBuffer("{\"_placeholder\":true,\"num\":" + i + "}",
                        CharsetUtil.UTF_8);
                int pos = PacketEncoder.find(source, scanValue);
                if (pos == -1) {
                    throw new IllegalStateException(
                            "Can't find attachment by index: " + i + " in packet source");
                }

                ByteBuf prefixBuf = source.slice(source.readerIndex(), pos - source.readerIndex());
                slices.add(prefixBuf);
                slices.add(QUOTES);
                slices.add(attachment);
                slices.add(QUOTES);

                source.readerIndex(pos + scanValue.readableBytes());
            }
            slices.add(source.slice());

            ByteBuf compositeBuf = Unpooled.wrappedBuffer(slices.toArray(new ByteBuf[slices.size()]));
            parseBody(head, compositeBuf, binaryPacket);
            head.setLastBinaryPacket(null);
            return binaryPacket;
        }
    }
    return new Packet(PacketType.MESSAGE);
}