Example usage for io.netty.buffer ByteBuf copy

List of usage examples for io.netty.buffer ByteBuf copy

Introduction

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

Prototype

public abstract ByteBuf copy();

Source Link

Document

Returns a copy of this buffer's readable bytes.

Usage

From source file:at.yawk.accordion.distributed.ConnectionManager.java

License:Mozilla Public License

/**
 * Handle a decoded message from the given connection.
 *
 * @return a stream of connections the message should be forwarded to.
 *///from   w  ww.j a v a  2  s .c  o m
private Stream<Connection> handleDecodedMessage(Connection sender, ByteBuf decoded, long id) {
    // read channel name
    String channelName = InternalProtocol.readByteString(decoded);

    Log.debug(logger, () -> "Received packet " + Long.toHexString(id) + " in channel '" + channelName + "' ("
            + decoded.readableBytes() + " bytes)");

    // handle internally
    BiConsumer<ByteBuf, Connection> internalHandler = internalHandlers.get(channelName);
    if (internalHandler != null) {
        internalHandler.accept(decoded, sender);
        // internally handled, do not handle in user code or forward
        return Stream.empty();
    }

    // handle payload in listeners
    Collection<Consumer<ByteBuf>> subs = listeners.getOrDefault(channelName, Collections.emptySet());
    if (!subs.isEmpty()) {
        subs.forEach(listener -> listener.accept(decoded.copy()));
    }
    return getConnectionsSubscribedTo(channelName);
}

From source file:at.yawk.accordion.distributed.ConnectionManager.java

License:Mozilla Public License

/**
 * Send a raw packet (with header fields already included) to the given connection. The given ByteBuf will not be
 * modified.//from ww w .j  a  v  a2  s . c o  m
 */
private void copyAndSend(Connection connection, ByteBuf full) {
    ByteBuf copy = full.copy();
    // 8 for packet ID, at least 1 for channel name or we're doing something wrong
    assert copy.readableBytes() > 9 : Arrays.toString(copy.array());
    connection.send(copy);
}

From source file:buildcraft.core.lib.network.ChannelHandler.java

License:Minecraft Mod Public

@Override
protected void encode(ChannelHandlerContext ctx, Packet msg, List<Object> out) throws Exception {
    ByteBuf buffer = Unpooled.buffer();
    Class<? extends Packet> clazz = msg.getClass();
    byte discriminator = types.get(clazz);
    buffer.writeByte(discriminator);//  w w w  .  j  ava 2s. c  o  m
    msg.writeData(buffer);
    FMLProxyPacket proxy = new FMLProxyPacket(buffer.copy(),
            ctx.channel().attr(NetworkRegistry.FML_CHANNEL).get());
    WeakReference<FMLProxyPacket> ref = ctx.attr(INBOUNDPACKETTRACKER).get().get();
    FMLProxyPacket old = ref == null ? null : ref.get();
    if (old != null) {
        proxy.setDispatcher(old.getDispatcher());
    }
    out.add(proxy);
}

From source file:com.basho.riak.client.core.netty.RiakHttpMessageHandlerTest.java

License:Apache License

@Test
public void producesCorrectResponse() throws Exception {
    HttpResponse response = mock(HttpResponse.class);
    HttpResponseStatus status = mock(HttpResponseStatus.class);
    doReturn(status).when(response).getStatus();
    doReturn(200).when(status).code();/*from w  w  w  . j a va2 s  .c o  m*/
    HttpContent content = mock(HttpContent.class);
    LastHttpContent lastContent = mock(LastHttpContent.class);

    ByteBuf bb = Unpooled.buffer();
    bb.writeByte((byte) 1);
    doReturn(bb).when(content).data();
    doReturn(bb.copy()).when(lastContent).data();

    handler.messageReceived(mockContext, response);
    handler.messageReceived(mockContext, content);
    handler.messageReceived(mockContext, lastContent);

    RiakHttpMessage message = Whitebox.getInternalState(handler, "message");

    verify(mockListener).onSuccess(mockChannel, message);
    assertEquals(message.getResponse(), response);
    byte[] bytes = new byte[] { 1, 1 };
    assertArrayEquals(message.getContent(), bytes);
}

From source file:com.couchbase.client.core.endpoint.binary.BinaryHandlerTest.java

License:Open Source License

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

    InsertRequest request = new InsertRequest("key", content.copy(), "bucket");
    request.partition((short) 512);
    channel.writeOutbound(request);//from w  w w . j  av a2 s  . co  m
    FullBinaryMemcacheRequest outbound = (FullBinaryMemcacheRequest) channel.readOutbound();
    assertNotNull(outbound);
    assertEquals("key", outbound.getKey());
    assertEquals("key".length(), outbound.getKeyLength());
    assertEquals(512, outbound.getReserved());
    assertEquals(BinaryHandler.OP_INSERT, 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));

    request = new InsertRequest("key", content.copy(), 10, 0, "bucket");
    request.partition((short) 512);
    channel.writeOutbound(request);
    outbound = (FullBinaryMemcacheRequest) channel.readOutbound();
    assertNotNull(outbound);
    assertEquals("key", outbound.getKey());
    assertEquals("key".length(), outbound.getKeyLength());
    assertEquals(512, outbound.getReserved());
    assertEquals(BinaryHandler.OP_INSERT, 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));

    request = new InsertRequest("key", content.copy(), 0, 5, "bucket");
    request.partition((short) 512);
    channel.writeOutbound(request);
    outbound = (FullBinaryMemcacheRequest) channel.readOutbound();
    assertNotNull(outbound);
    assertEquals("key", outbound.getKey());
    assertEquals("key".length(), outbound.getKeyLength());
    assertEquals(512, outbound.getReserved());
    assertEquals(BinaryHandler.OP_INSERT, 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));

    request = new InsertRequest("key", content.copy(), 30, 99, "bucket");
    request.partition((short) 512);
    channel.writeOutbound(request);
    outbound = (FullBinaryMemcacheRequest) channel.readOutbound();
    assertNotNull(outbound);
    assertEquals("key", outbound.getKey());
    assertEquals("key".length(), outbound.getKeyLength());
    assertEquals(512, outbound.getReserved());
    assertEquals(BinaryHandler.OP_INSERT, 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.release(content);
}

From source file:com.couchbase.client.core.endpoint.binary.BinaryHandlerTest.java

License:Open Source 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);//w  w  w  .  j av  a 2s  .co  m
    FullBinaryMemcacheRequest outbound = (FullBinaryMemcacheRequest) channel.readOutbound();
    assertNotNull(outbound);
    assertEquals("key", outbound.getKey());
    assertEquals("key".length(), outbound.getKeyLength());
    assertEquals(512, outbound.getReserved());
    assertEquals(BinaryHandler.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));

    request = new UpsertRequest("key", content.copy(), 10, 0, "bucket");
    request.partition((short) 512);
    channel.writeOutbound(request);
    outbound = (FullBinaryMemcacheRequest) channel.readOutbound();
    assertNotNull(outbound);
    assertEquals("key", outbound.getKey());
    assertEquals("key".length(), outbound.getKeyLength());
    assertEquals(512, outbound.getReserved());
    assertEquals(BinaryHandler.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));

    request = new UpsertRequest("key", content.copy(), 0, 5, "bucket");
    request.partition((short) 512);
    channel.writeOutbound(request);
    outbound = (FullBinaryMemcacheRequest) channel.readOutbound();
    assertNotNull(outbound);
    assertEquals("key", outbound.getKey());
    assertEquals("key".length(), outbound.getKeyLength());
    assertEquals(512, outbound.getReserved());
    assertEquals(BinaryHandler.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));

    request = new UpsertRequest("key", content.copy(), 30, 99, "bucket");
    request.partition((short) 512);
    channel.writeOutbound(request);
    outbound = (FullBinaryMemcacheRequest) channel.readOutbound();
    assertNotNull(outbound);
    assertEquals("key", outbound.getKey());
    assertEquals("key".length(), outbound.getKeyLength());
    assertEquals(512, outbound.getReserved());
    assertEquals(BinaryHandler.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.release(content);
}

From source file:com.couchbase.client.core.endpoint.binary.BinaryHandlerTest.java

License:Open Source 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);/*  ww  w.j av a  2s  . com*/
    FullBinaryMemcacheRequest outbound = (FullBinaryMemcacheRequest) channel.readOutbound();
    assertNotNull(outbound);
    assertEquals("key", outbound.getKey());
    assertEquals("key".length(), outbound.getKeyLength());
    assertEquals(512, outbound.getReserved());
    assertEquals(BinaryHandler.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));

    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", outbound.getKey());
    assertEquals("key".length(), outbound.getKeyLength());
    assertEquals(512, outbound.getReserved());
    assertEquals(BinaryHandler.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));

    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", outbound.getKey());
    assertEquals("key".length(), outbound.getKeyLength());
    assertEquals(512, outbound.getReserved());
    assertEquals(BinaryHandler.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));

    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", outbound.getKey());
    assertEquals("key".length(), outbound.getKeyLength());
    assertEquals(512, outbound.getReserved());
    assertEquals(BinaryHandler.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.release(content);
}

From source file:com.couchbase.client.core.endpoint.binary.BinaryHandlerTest.java

License:Open Source 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 . jav  a2 s  .c  o  m
    FullBinaryMemcacheRequest outbound = (FullBinaryMemcacheRequest) channel.readOutbound();
    assertEquals(4234234234L, outbound.getCAS());
    ReferenceCountUtil.release(content);
}

From source file:com.couchbase.client.core.endpoint.binary.BinaryHandlerTest.java

License:Open Source License

@Test
public void shouldDecodeGetSuccessResponse() throws Exception {
    ByteBuf content = Unpooled.copiedBuffer("content", CharsetUtil.UTF_8);
    FullBinaryMemcacheResponse response = new DefaultFullBinaryMemcacheResponse("key", Unpooled.EMPTY_BUFFER,
            content.copy());
    response.setCAS(123456789L);// ww w  .  java 2s.c o  m
    response.setExtras(Unpooled.buffer().writeInt(123));
    response.setExtrasLength((byte) 4);

    GetRequest requestMock = mock(GetRequest.class);
    when(requestMock.bucket()).thenReturn("bucket");
    queue.add(requestMock);
    channel.writeInbound(response);
    channel.readInbound();

    latch.await(1, TimeUnit.SECONDS);
    assertEquals(1, firedEvents.size());
    GetResponse inbound = (GetResponse) firedEvents.get(0);
    assertEquals(ResponseStatus.SUCCESS, inbound.status());
    assertEquals("bucket", inbound.bucket());
    assertEquals(123456789L, inbound.cas());
    assertEquals(123, inbound.flags());
    assertEquals("content", inbound.content().toString(CharsetUtil.UTF_8));
    assertTrue(queue.isEmpty());

    ReferenceCountUtil.release(content);
}

From source file:com.couchbase.client.core.endpoint.binary.BinaryHandlerTest.java

License:Open Source License

@Test
public void shouldDecodeGetNotFoundResponse() throws Exception {
    ByteBuf content = Unpooled.copiedBuffer("Not Found", CharsetUtil.UTF_8);
    FullBinaryMemcacheResponse response = new DefaultFullBinaryMemcacheResponse("key", Unpooled.EMPTY_BUFFER,
            content.copy());
    response.setStatus(BinaryMemcacheResponseStatus.KEY_ENOENT);

    GetRequest requestMock = mock(GetRequest.class);
    when(requestMock.bucket()).thenReturn("bucket");
    queue.add(requestMock);/*from w w  w  .  ja va2 s  .co  m*/
    channel.writeInbound(response);
    channel.readInbound();

    latch.await(1, TimeUnit.SECONDS);
    assertEquals(1, firedEvents.size());
    GetResponse inbound = (GetResponse) firedEvents.get(0);
    assertEquals(ResponseStatus.NOT_EXISTS, inbound.status());
    assertEquals("bucket", inbound.bucket());
    assertEquals(0, inbound.cas());
    assertEquals(0, inbound.flags());
    assertEquals("Not Found", inbound.content().toString(CharsetUtil.UTF_8));
    assertTrue(queue.isEmpty());

    ReferenceCountUtil.release(content);
}