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.couchbase.client.core.endpoint.kv.KeyValueHandlerTest.java

License:Apache License

@Test
public void shouldEncodeUpsertRequest() {
    ByteBuf content = Unpooled.copiedBuffer("content", CharsetUtil.UTF_8);

    UpsertRequest request = new UpsertRequest("key", content.copy(), "bucket");
    request.partition((short) 512);
    channel.writeOutbound(request);//from w ww.j av a2  s .c om
    FullBinaryMemcacheRequest outbound = (FullBinaryMemcacheRequest) channel.readOutbound();
    assertNotNull(outbound);
    assertEquals("key", new String(outbound.getKey(), CHARSET));
    assertEquals("key".length(), outbound.getKeyLength());
    assertEquals(512, outbound.getReserved());
    assertEquals(KeyValueHandler.OP_UPSERT, outbound.getOpcode());
    assertEquals(8, outbound.getExtrasLength());
    assertEquals(0, outbound.getExtras().readInt());
    assertEquals(0, outbound.getExtras().readInt());
    assertEquals(18, outbound.getTotalBodyLength());
    assertEquals(0, outbound.getCAS());
    assertEquals("content", outbound.content().toString(CharsetUtil.UTF_8));
    ReferenceCountUtil.releaseLater(outbound);

    request = new UpsertRequest("key", content.copy(), 10, 0, "bucket");
    request.partition((short) 512);
    channel.writeOutbound(request);
    outbound = (FullBinaryMemcacheRequest) channel.readOutbound();
    assertNotNull(outbound);
    assertEquals("key", new String(outbound.getKey(), CHARSET));
    assertEquals("key".length(), outbound.getKeyLength());
    assertEquals(512, outbound.getReserved());
    assertEquals(KeyValueHandler.OP_UPSERT, outbound.getOpcode());
    assertEquals(8, outbound.getExtrasLength());
    assertEquals(0, outbound.getExtras().readInt());
    assertEquals(10, outbound.getExtras().readInt());
    assertEquals(18, outbound.getTotalBodyLength());
    assertEquals("content", outbound.content().toString(CharsetUtil.UTF_8));
    ReferenceCountUtil.releaseLater(outbound);

    request = new UpsertRequest("key", content.copy(), 0, 5, "bucket");
    request.partition((short) 512);
    channel.writeOutbound(request);
    outbound = (FullBinaryMemcacheRequest) channel.readOutbound();
    assertNotNull(outbound);
    assertEquals("key", new String(outbound.getKey(), CHARSET));
    assertEquals("key".length(), outbound.getKeyLength());
    assertEquals(512, outbound.getReserved());
    assertEquals(KeyValueHandler.OP_UPSERT, outbound.getOpcode());
    assertEquals(8, outbound.getExtrasLength());
    assertEquals(5, outbound.getExtras().readInt());
    assertEquals(0, outbound.getExtras().readInt());
    assertEquals(18, outbound.getTotalBodyLength());
    assertEquals("content", outbound.content().toString(CharsetUtil.UTF_8));
    ReferenceCountUtil.releaseLater(outbound);

    request = new UpsertRequest("key", content.copy(), 30, 99, "bucket");
    request.partition((short) 512);
    channel.writeOutbound(request);
    outbound = (FullBinaryMemcacheRequest) channel.readOutbound();
    assertNotNull(outbound);
    assertEquals("key", new String(outbound.getKey(), CHARSET));
    assertEquals("key".length(), outbound.getKeyLength());
    assertEquals(512, outbound.getReserved());
    assertEquals(KeyValueHandler.OP_UPSERT, outbound.getOpcode());
    assertEquals(8, outbound.getExtrasLength());
    assertEquals(99, outbound.getExtras().readInt());
    assertEquals(30, outbound.getExtras().readInt());
    assertEquals(18, outbound.getTotalBodyLength());
    assertEquals("content", outbound.content().toString(CharsetUtil.UTF_8));
    ReferenceCountUtil.releaseLater(outbound);

    ReferenceCountUtil.release(content);
}

From source file:com.couchbase.client.core.endpoint.kv.KeyValueHandlerTest.java

License:Apache License

@Test
public void shouldEncodeReplaceRequest() {
    ByteBuf content = Unpooled.copiedBuffer("content", CharsetUtil.UTF_8);

    ReplaceRequest request = new ReplaceRequest("key", content.copy(), "bucket");
    request.partition((short) 512);
    channel.writeOutbound(request);//from   w  w w . ja  v  a 2 s.c  o  m
    FullBinaryMemcacheRequest outbound = (FullBinaryMemcacheRequest) channel.readOutbound();
    assertNotNull(outbound);
    assertEquals("key", new String(outbound.getKey(), CHARSET));
    assertEquals("key".length(), outbound.getKeyLength());
    assertEquals(512, outbound.getReserved());
    assertEquals(KeyValueHandler.OP_REPLACE, outbound.getOpcode());
    assertEquals(8, outbound.getExtrasLength());
    assertEquals(0, outbound.getExtras().readInt());
    assertEquals(0, outbound.getExtras().readInt());
    assertEquals(18, outbound.getTotalBodyLength());
    assertEquals(0, outbound.getCAS());
    assertEquals("content", outbound.content().toString(CharsetUtil.UTF_8));
    ReferenceCountUtil.releaseLater(outbound);

    request = new ReplaceRequest("key", content.copy(), 0, 10, 0, "bucket");
    request.partition((short) 512);
    channel.writeOutbound(request);
    outbound = (FullBinaryMemcacheRequest) channel.readOutbound();
    assertNotNull(outbound);
    assertEquals("key", new String(outbound.getKey(), CHARSET));
    assertEquals("key".length(), outbound.getKeyLength());
    assertEquals(512, outbound.getReserved());
    assertEquals(KeyValueHandler.OP_REPLACE, outbound.getOpcode());
    assertEquals(8, outbound.getExtrasLength());
    assertEquals(0, outbound.getExtras().readInt());
    assertEquals(10, outbound.getExtras().readInt());
    assertEquals(18, outbound.getTotalBodyLength());
    assertEquals("content", outbound.content().toString(CharsetUtil.UTF_8));
    ReferenceCountUtil.releaseLater(outbound);

    request = new ReplaceRequest("key", content.copy(), 0, 0, 5, "bucket");
    request.partition((short) 512);
    channel.writeOutbound(request);
    outbound = (FullBinaryMemcacheRequest) channel.readOutbound();
    assertNotNull(outbound);
    assertEquals("key", new String(outbound.getKey(), CHARSET));
    assertEquals("key".length(), outbound.getKeyLength());
    assertEquals(512, outbound.getReserved());
    assertEquals(KeyValueHandler.OP_REPLACE, outbound.getOpcode());
    assertEquals(8, outbound.getExtrasLength());
    assertEquals(5, outbound.getExtras().readInt());
    assertEquals(0, outbound.getExtras().readInt());
    assertEquals(18, outbound.getTotalBodyLength());
    assertEquals("content", outbound.content().toString(CharsetUtil.UTF_8));
    ReferenceCountUtil.releaseLater(outbound);

    request = new ReplaceRequest("key", content.copy(), 0, 30, 99, "bucket");
    request.partition((short) 512);
    channel.writeOutbound(request);
    outbound = (FullBinaryMemcacheRequest) channel.readOutbound();
    assertNotNull(outbound);
    assertEquals("key", new String(outbound.getKey(), CHARSET));
    assertEquals("key".length(), outbound.getKeyLength());
    assertEquals(512, outbound.getReserved());
    assertEquals(KeyValueHandler.OP_REPLACE, outbound.getOpcode());
    assertEquals(8, outbound.getExtrasLength());
    assertEquals(99, outbound.getExtras().readInt());
    assertEquals(30, outbound.getExtras().readInt());
    assertEquals(18, outbound.getTotalBodyLength());
    assertEquals("content", outbound.content().toString(CharsetUtil.UTF_8));
    ReferenceCountUtil.releaseLater(outbound);

    ReferenceCountUtil.release(content);
}

From source file:com.couchbase.client.core.endpoint.kv.KeyValueHandlerTest.java

License:Apache License

@Test
public void shouldEncodeReplaceWithCASRequest() {
    ByteBuf content = Unpooled.copiedBuffer("content", CharsetUtil.UTF_8);
    ReplaceRequest request = new ReplaceRequest("key", content.copy(), 4234234234L, 30, 99, "bucket");
    request.partition((short) 512);
    channel.writeOutbound(request);/*from   w  w w . j av a 2 s.c om*/
    FullBinaryMemcacheRequest outbound = (FullBinaryMemcacheRequest) channel.readOutbound();
    assertEquals(4234234234L, outbound.getCAS());
    ReferenceCountUtil.releaseLater(outbound);
    ReferenceCountUtil.release(content);
}

From source file:com.couchbase.client.core.endpoint.kv.KeyValueHandlerTest.java

License:Apache License

@Test
public void shouldEncodeObserveRequest() {
    ObserveRequest request = new ObserveRequest("key", 234234234L, true, (short) 0, "bucket");
    request.partition((short) 1);

    channel.writeOutbound(request);/*w ww . j  ava2s.  com*/
    FullBinaryMemcacheRequest outbound = (FullBinaryMemcacheRequest) channel.readOutbound();
    assertNotNull(outbound);
    assertEquals(0, outbound.getKeyLength());
    assertEquals(0, outbound.getExtrasLength());
    assertEquals(7, outbound.getTotalBodyLength());
    assertEquals(KeyValueHandler.OP_OBSERVE, outbound.getOpcode());
    assertEquals(1, outbound.content().readShort());
    assertEquals("key".length(), outbound.content().readShort());
    assertEquals("key",
            outbound.content().readBytes(outbound.content().readableBytes()).toString(CharsetUtil.UTF_8));
    ReferenceCountUtil.releaseLater(outbound);
}

From source file:com.couchbase.client.core.endpoint.kv.KeyValueHandlerTest.java

License:Apache License

@Test
public void shouldDecodeGetBucketConfigResponse() throws Exception {
    ByteBuf content = Unpooled.copiedBuffer("content", CharsetUtil.UTF_8);
    FullBinaryMemcacheResponse response = new DefaultFullBinaryMemcacheResponse(KEY, Unpooled.EMPTY_BUFFER,
            content.copy());// w w w  .  ja v a 2 s  .com

    GetBucketConfigRequest requestMock = mock(GetBucketConfigRequest.class);
    when(requestMock.bucket()).thenReturn("bucket");
    when(requestMock.hostname()).thenReturn(InetAddress.getLocalHost());
    requestQueue.add(requestMock);
    channel.writeInbound(response);

    assertEquals(1, eventSink.responseEvents().size());
    GetBucketConfigResponse event = (GetBucketConfigResponse) eventSink.responseEvents().get(0).getMessage();
    assertEquals(BUCKET, event.bucket());
    assertEquals(ResponseStatus.SUCCESS, event.status());
    assertEquals(InetAddress.getLocalHost(), event.hostname());
    assertEquals("content", event.content().toString(CHARSET));

    ReferenceCountUtil.release(content);
}

From source file:com.couchbase.client.core.endpoint.kv.KeyValueHandlerTest.java

License:Apache License

@Test
public void shouldDecodeObserveResponseDuringRebalance() throws Exception {
    ByteBuf content = Unpooled.copiedBuffer("{someconfig...}", CharsetUtil.UTF_8);
    FullBinaryMemcacheResponse response = new DefaultFullBinaryMemcacheResponse(new byte[] {},
            Unpooled.EMPTY_BUFFER, content.copy());
    response.setStatus(KeyValueStatus.ERR_NOT_MY_VBUCKET.code());

    ObserveRequest requestMock = mock(ObserveRequest.class);
    requestQueue.add(requestMock);/*from  w  ww . j  a  v  a 2  s  . co  m*/
    channel.writeInbound(response);

    assertEquals(1, eventSink.responseEvents().size());
    ObserveResponse event = (ObserveResponse) eventSink.responseEvents().get(0).getMessage();
    assertEquals(ResponseStatus.RETRY, event.status());
    assertEquals(ObserveResponse.ObserveStatus.UNKNOWN, event.observeStatus());
}

From source file:com.couchbase.client.core.endpoint.kv.KeyValueHandlerTest.java

License:Apache License

@Test
public void shouldRetainContentOnStore() throws Exception {
    ByteBuf content = Unpooled.copiedBuffer("foo", CharsetUtil.UTF_8);
    UpsertRequest request = new UpsertRequest("key", content, "bucket");
    request.partition((short) 1);

    assertEquals(1, content.refCnt());//from w ww .ja v a 2s  .c o  m
    channel.writeOutbound(request);
    assertEquals(2, content.refCnt());
    ReferenceCountUtil.releaseLater(content); //release content first time
    ReferenceCountUtil.releaseLater(channel.readOutbound()); //releases extra once + content second time
}

From source file:com.couchbase.client.core.endpoint.kv.KeyValueHandlerTest.java

License:Apache License

@Test
public void shouldRetainContentOnAppend() throws Exception {
    ByteBuf content = Unpooled.copiedBuffer("foo", CharsetUtil.UTF_8);
    AppendRequest request = new AppendRequest("key", 0, content, "bucket");
    request.partition((short) 1);

    assertEquals(1, content.refCnt());/*from   w ww  . jav  a 2s . c  om*/
    channel.writeOutbound(request);
    assertEquals(2, content.refCnt());
}

From source file:com.couchbase.client.core.endpoint.kv.KeyValueHandlerTest.java

License:Apache License

@Test
public void shouldRetainContentOnPrepend() throws Exception {
    ByteBuf content = Unpooled.copiedBuffer("foo", CharsetUtil.UTF_8);
    PrependRequest request = new PrependRequest("key", 0, content, "bucket");
    request.partition((short) 1);

    assertEquals(1, content.refCnt());/*from ww w .  ja v  a2  s .  c om*/
    channel.writeOutbound(request);
    assertEquals(2, content.refCnt());
}

From source file:com.couchbase.client.core.endpoint.kv.KeyValueHandlerTest.java

License:Apache License

@Test
public void shouldReleaseStoreRequestContentOnSuccess() throws Exception {
    ByteBuf content = Unpooled.copiedBuffer("content", CharsetUtil.UTF_8);
    FullBinaryMemcacheResponse response = new DefaultFullBinaryMemcacheResponse(KEY, Unpooled.EMPTY_BUFFER,
            content);/*from   w w w . ja  va2  s  .co m*/
    response.setStatus(BinaryMemcacheResponseStatus.SUCCESS);

    UpsertRequest requestMock = mock(UpsertRequest.class);
    ByteBuf requestContent = Unpooled.copiedBuffer("content", CharsetUtil.UTF_8);
    when(requestMock.bucket()).thenReturn("bucket");
    when(requestMock.observable()).thenReturn(AsyncSubject.<CouchbaseResponse>create());
    when(requestMock.content()).thenReturn(requestContent);
    requestQueue.add(requestMock);

    assertEquals(1, content.refCnt());
    assertEquals(1, requestContent.refCnt());
    channel.writeInbound(response);
    assertEquals(1, content.refCnt());
    assertEquals(0, requestContent.refCnt());
}