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.protocol.PacketEncoder.java

License:Apache License

public void encodePacket(Packet packet, ByteBuf buffer, ByteBufAllocator allocator, boolean binary)
        throws IOException {
    ByteBuf buf = buffer;//ww w .java2 s .  c o m
    if (!binary) {
        buf = allocateBuffer(allocator);
    }
    byte type = toChar(packet.getType().getValue());
    buf.writeByte(type);

    try {
        switch (packet.getType()) {

        case PONG: {
            buf.writeBytes(packet.getData().toString().getBytes(CharsetUtil.UTF_8));
            break;
        }

        case OPEN: {
            ByteBufOutputStream out = new ByteBufOutputStream(buf);
            jsonSupport.writeValue(out, packet.getData());
            break;
        }

        case MESSAGE: {

            ByteBuf encBuf = null;

            if (packet.getSubType() == PacketType.ERROR) {
                encBuf = allocateBuffer(allocator);

                ByteBufOutputStream out = new ByteBufOutputStream(encBuf);
                jsonSupport.writeValue(out, packet.getData());
            }

            if (packet.getSubType() == PacketType.EVENT || packet.getSubType() == PacketType.ACK) {

                List<Object> values = new ArrayList<Object>();
                if (packet.getSubType() == PacketType.EVENT) {
                    values.add(packet.getName());
                }

                encBuf = allocateBuffer(allocator);

                List<Object> args = packet.getData();
                values.addAll(args);
                ByteBufOutputStream out = new ByteBufOutputStream(encBuf);
                jsonSupport.writeValue(out, values);

                if (!jsonSupport.getArrays().isEmpty()) {
                    packet.initAttachments(jsonSupport.getArrays().size());
                    for (byte[] array : jsonSupport.getArrays()) {
                        packet.addAttachment(Unpooled.wrappedBuffer(array));
                    }
                    packet.setSubType(PacketType.BINARY_EVENT);
                }
            }

            byte subType = toChar(packet.getSubType().getValue());
            buf.writeByte(subType);

            if (packet.hasAttachments()) {
                byte[] ackId = toChars(packet.getAttachments().size());
                buf.writeBytes(ackId);
                buf.writeByte('-');
            }

            if (packet.getSubType() == PacketType.CONNECT) {
                if (!packet.getNsp().isEmpty()) {
                    buf.writeBytes(packet.getNsp().getBytes(CharsetUtil.UTF_8));
                }
            } else {
                if (!packet.getNsp().isEmpty()) {
                    buf.writeBytes(packet.getNsp().getBytes(CharsetUtil.UTF_8));
                    buf.writeByte(',');
                }
            }

            if (packet.getAckId() != null) {
                byte[] ackId = toChars(packet.getAckId());
                buf.writeBytes(ackId);
            }

            if (encBuf != null) {
                buf.writeBytes(encBuf);
                encBuf.release();
            }

            break;
        }
        }
    } finally {
        // we need to write a buffer in any case
        if (!binary) {
            buffer.writeByte(0);
            int length = buf.writerIndex();
            buffer.writeBytes(longToBytes(length));
            buffer.writeByte(0xff);
            buffer.writeBytes(buf);

            buf.release();
        }
    }
}

From source file:com.corundumstudio.socketio.SocketIOEncoder.java

License:Apache License

private void sendMessage(HttpMessage msg, Channel channel, ByteBuf out) {
    HttpResponse res = createHttpResponse(msg.getOrigin(), out);
    channel.write(res);/*from   www.j a v a2  s . c  om*/

    if (log.isTraceEnabled()) {
        log.trace("Out message: {} - sessionId: {}", out.toString(CharsetUtil.UTF_8), msg.getSessionId());
    }
    if (out.isReadable()) {
        channel.write(out);
    } else {
        out.release();
    }

    ChannelFuture f = channel.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);
    f.addListener(ChannelFutureListener.CLOSE);
}

From source file:com.corundumstudio.socketio.SocketIOEncoder.java

License:Apache License

private void handle(WebSocketPacketMessage webSocketPacketMessage, Channel channel, ByteBuf out)
        throws IOException {
    encoder.encodePacket(webSocketPacketMessage.getPacket(), out);
    WebSocketFrame res = new TextWebSocketFrame(out);
    log.trace("Out message: {} sessionId: {}", out.toString(CharsetUtil.UTF_8),
            webSocketPacketMessage.getSessionId());
    channel.writeAndFlush(res);/*from w  w  w.  j  ava 2 s  .c  o m*/
    if (!out.isReadable()) {
        out.release();
    }
}

From source file:com.couchbase.client.core.cluster.BinaryMessageTest.java

License:Open Source License

@Test
public void shouldUpsertAndGetDocument() {
    String key = "upsert-key";
    String content = "Hello World!";
    UpsertRequest upsert = new UpsertRequest(key, Unpooled.copiedBuffer(content, CharsetUtil.UTF_8), bucket);
    cluster.<UpsertResponse>send(upsert).toBlockingObservable().single();

    GetRequest request = new GetRequest(key, bucket);
    assertEquals(content, cluster.<GetResponse>send(request).toBlockingObservable().single().content()
            .toString(CharsetUtil.UTF_8));
}

From source file:com.couchbase.client.core.cluster.BinaryMessageTest.java

License:Open Source License

@Test
public void shouldUpsertWithExpiration() throws Exception {
    String key = "upsert-key-vanish";
    String content = "Hello World!";
    UpsertRequest upsert = new UpsertRequest(key, Unpooled.copiedBuffer(content, CharsetUtil.UTF_8), 1, 0,
            bucket);/*  w ww . j ava  2 s.co  m*/
    cluster.<UpsertResponse>send(upsert).toBlockingObservable().single();

    Thread.sleep(2000);

    GetRequest request = new GetRequest(key, bucket);
    assertEquals(ResponseStatus.NOT_EXISTS,
            cluster.<GetResponse>send(request).toBlockingObservable().single().status());
}

From source file:com.couchbase.client.core.cluster.BinaryMessageTest.java

License:Open Source License

@Test
public void shouldHandleDoubleInsert() {
    String key = "insert-key";
    String content = "Hello World!";
    InsertRequest insert = new InsertRequest(key, Unpooled.copiedBuffer(content, CharsetUtil.UTF_8), bucket);
    assertEquals(ResponseStatus.SUCCESS,
            cluster.<InsertResponse>send(insert).toBlockingObservable().single().status());

    insert = new InsertRequest(key, Unpooled.copiedBuffer(content, CharsetUtil.UTF_8), bucket);
    assertEquals(ResponseStatus.EXISTS,/*from w  ww .  ja va2s  .co m*/
            cluster.<InsertResponse>send(insert).toBlockingObservable().single().status());
}

From source file:com.couchbase.client.core.cluster.BinaryMessageTest.java

License:Open Source License

@Test
public void shouldReplaceWithoutCAS() {
    final String key = "replace-key";
    final String content = "replace content";

    ReplaceRequest insert = new ReplaceRequest(key, Unpooled.copiedBuffer(content, CharsetUtil.UTF_8), bucket);
    assertEquals(ResponseStatus.NOT_EXISTS,
            cluster.<ReplaceResponse>send(insert).toBlockingObservable().single().status());

    UpsertRequest upsert = new UpsertRequest(key, Unpooled.copiedBuffer("insert content", CharsetUtil.UTF_8),
            bucket);/*from w ww .ja v  a  2  s  .co  m*/
    ReplaceResponse response = cluster.<UpsertResponse>send(upsert)
            .flatMap(new Func1<UpsertResponse, Observable<ReplaceResponse>>() {
                @Override
                public Observable<ReplaceResponse> call(UpsertResponse response) {
                    return cluster.send(
                            new ReplaceRequest(key, Unpooled.copiedBuffer(content, CharsetUtil.UTF_8), bucket));
                }
            }).toBlockingObservable().single();

    assertEquals(ResponseStatus.SUCCESS, response.status());
}

From source file:com.couchbase.client.core.cluster.BinaryMessageTest.java

License:Open Source License

@Test
public void shouldReplaceWithFailingCAS() {
    final String key = "replace-key-cas-fail";
    final String content = "replace content";

    ReplaceRequest insert = new ReplaceRequest(key, Unpooled.copiedBuffer(content, CharsetUtil.UTF_8), bucket);
    assertEquals(ResponseStatus.NOT_EXISTS,
            cluster.<ReplaceResponse>send(insert).toBlockingObservable().single().status());

    UpsertRequest upsert = new UpsertRequest(key, Unpooled.copiedBuffer("insert content", CharsetUtil.UTF_8),
            bucket);/*w  w  w  . j ava 2  s . c om*/
    ReplaceResponse response = cluster.<UpsertResponse>send(upsert)
            .flatMap(new Func1<UpsertResponse, Observable<ReplaceResponse>>() {
                @Override
                public Observable<ReplaceResponse> call(UpsertResponse response) {
                    return cluster.send(new ReplaceRequest(key,
                            Unpooled.copiedBuffer(content, CharsetUtil.UTF_8), 24234234L, bucket));
                }
            }).toBlockingObservable().single();

    assertEquals(ResponseStatus.EXISTS, response.status());
}

From source file:com.couchbase.client.core.cluster.BinaryMessageTest.java

License:Open Source License

@Test
public void shouldReplaceWithMatchingCAS() {
    final String key = "replace-key-cas-match";
    final String content = "replace content";

    ReplaceRequest insert = new ReplaceRequest(key, Unpooled.copiedBuffer(content, CharsetUtil.UTF_8), bucket);
    assertEquals(ResponseStatus.NOT_EXISTS,
            cluster.<ReplaceResponse>send(insert).toBlockingObservable().single().status());

    UpsertRequest upsert = new UpsertRequest(key, Unpooled.copiedBuffer("insert content", CharsetUtil.UTF_8),
            bucket);/* www.  j ava  2s .  co m*/
    ReplaceResponse response = cluster.<UpsertResponse>send(upsert)
            .flatMap(new Func1<UpsertResponse, Observable<ReplaceResponse>>() {
                @Override
                public Observable<ReplaceResponse> call(UpsertResponse response) {
                    return cluster.send(new ReplaceRequest(key,
                            Unpooled.copiedBuffer(content, CharsetUtil.UTF_8), response.cas(), bucket));
                }
            }).toBlockingObservable().single();

    assertEquals(ResponseStatus.SUCCESS, response.status());
}

From source file:com.couchbase.client.core.cluster.BinaryMessageTest.java

License:Open Source License

@Test
public void shouldRemoveDocumentWithoutCAS() {
    String key = "remove-key";
    String content = "Hello World!";
    UpsertRequest upsert = new UpsertRequest(key, Unpooled.copiedBuffer(content, CharsetUtil.UTF_8), bucket);
    assertEquals(ResponseStatus.SUCCESS,
            cluster.<UpsertResponse>send(upsert).toBlockingObservable().single().status());

    RemoveRequest remove = new RemoveRequest(key, bucket);
    assertEquals(ResponseStatus.SUCCESS,
            cluster.<RemoveResponse>send(remove).toBlockingObservable().single().status());
    GetRequest get = new GetRequest(key, bucket);
    assertEquals(ResponseStatus.NOT_EXISTS,
            cluster.<GetResponse>send(get).toBlockingObservable().single().status());
}