Example usage for io.netty.buffer Unpooled wrappedBuffer

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

Introduction

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

Prototype

public static ByteBuf wrappedBuffer(ByteBuffer... buffers) 

Source Link

Document

Creates a new big-endian composite buffer which wraps the slices of the specified NIO buffers without copying them.

Usage

From source file:io.lettuce.core.pubsub.PubSubCommandHandlerTest.java

License:Apache License

@Test
public void shouldPropagatePubSubResponseToOutput() throws Exception {

    Command<String, String, String> command1 = new Command<>(CommandType.APPEND,
            new StatusOutput<>(new Utf8StringCodec()), null);

    sut.channelRegistered(context);/*w  w w.  j a  v a  2  s. co m*/
    sut.channelActive(context);
    stack.add(command1);

    sut.channelRead(context,
            Unpooled.wrappedBuffer("*3\r\n$7\r\nmessage\r\n$3\r\nfoo\r\n$3\r\nbar\r\n".getBytes()));

    assertThat(command1.isDone()).isFalse();

    verify(endpoint).notifyMessage(any());
}

From source file:io.lettuce.core.pubsub.PubSubCommandHandlerTest.java

License:Apache License

@Test
public void shouldPropagateInterleavedPubSubResponseToOutput() throws Exception {

    Command<String, String, String> command1 = new Command<>(CommandType.APPEND,
            new StatusOutput<>(new Utf8StringCodec()), null);
    Command<String, String, String> command2 = new Command<>(CommandType.APPEND,
            new StatusOutput<>(new Utf8StringCodec()), null);

    sut.channelRegistered(context);/* ww  w  .ja  va 2s .  c o m*/
    sut.channelActive(context);
    stack.add(command1);
    stack.add(command2);

    sut.channelRead(context, Unpooled.wrappedBuffer(
            "+OK\r\n*4\r\n$8\r\npmessage\r\n$1\r\n*\r\n$3\r\nfoo\r\n$3\r\nbar\r\n+YEAH\r\n".getBytes()));

    assertThat(command1.get()).isEqualTo("OK");
    assertThat(command2.get()).isEqualTo("YEAH");

    ArgumentCaptor<PubSubOutput> captor = ArgumentCaptor.forClass(PubSubOutput.class);
    verify(endpoint).notifyMessage(captor.capture());

    assertThat(captor.getValue().pattern()).isEqualTo("*");
    assertThat(captor.getValue().channel()).isEqualTo("foo");
    assertThat(captor.getValue().get()).isEqualTo("bar");
}

From source file:io.lettuce.core.ReactiveBackpressurePropagationTest.java

License:Apache License

@Test
public void writeCommand() throws Exception {

    Command<String, String, List<String>> lrange = new Command<>(CommandType.LRANGE,
            new ValueListOutput<>(StringCodec.UTF8));
    RedisPublisher<String, String, String> publisher = new RedisPublisher<>((Command) lrange,
            statefulConnection, true);/*from  w  ww.j  av  a 2 s  .  co  m*/

    CountDownLatch pressureArrived = new CountDownLatch(1);
    CountDownLatch buildPressure = new CountDownLatch(1);
    CountDownLatch waitForPressureReduced = new CountDownLatch(2);
    CountDownLatch waitForWorkCompleted = new CountDownLatch(4);

    Flux.from(publisher).limitRate(2).publishOn(Schedulers.single()).doOnNext(s -> {

        try {
            pressureArrived.countDown();
            buildPressure.await();
        } catch (InterruptedException e) {
        }

        waitForPressureReduced.countDown();
        waitForWorkCompleted.countDown();

    }).subscribe();

    assertThat(embeddedChannel.config().isAutoRead()).isTrue();

    // produce some back pressure
    embeddedChannel.writeInbound(Unpooled.wrappedBuffer(RESP.arrayHeader(4)));
    embeddedChannel.writeInbound(Unpooled.wrappedBuffer(RESP.bulkString("one")));
    pressureArrived.await();
    assertThat(embeddedChannel.config().isAutoRead()).isTrue();

    embeddedChannel.writeInbound(Unpooled.wrappedBuffer(RESP.bulkString("two")));
    embeddedChannel.writeInbound(Unpooled.wrappedBuffer(RESP.bulkString("three")));
    assertThat(embeddedChannel.config().isAutoRead()).isFalse();

    // allow processing
    buildPressure.countDown();

    // wait until processing caught up
    waitForPressureReduced.await();
    assertThat(embeddedChannel.config().isAutoRead()).isTrue();

    // emit the last item
    embeddedChannel.writeInbound(Unpooled.wrappedBuffer(RESP.bulkString("four")));

    // done
    waitForWorkCompleted.await();
    assertThat(embeddedChannel.config().isAutoRead()).isTrue();
}

From source file:io.lettuce.core.ReactiveBackpressurePropagationTest.java

License:Apache License

@Test
public void writeCommandAndCancelInTheMiddle() throws Exception {

    Command<String, String, List<String>> lrange = new Command<>(CommandType.LRANGE,
            new ValueListOutput<>(StringCodec.UTF8));
    RedisPublisher<String, String, String> publisher = new RedisPublisher<>((Command) lrange,
            statefulConnection, true);//from   ww  w. ja  v  a 2 s. c om

    CountDownLatch pressureArrived = new CountDownLatch(1);
    CountDownLatch buildPressure = new CountDownLatch(1);
    CountDownLatch waitForPressureReduced = new CountDownLatch(2);

    Disposable cancellation = Flux.from(publisher).limitRate(2).publishOn(Schedulers.single()).doOnNext(s -> {

        try {
            pressureArrived.countDown();
            buildPressure.await();
        } catch (InterruptedException e) {
        }

        waitForPressureReduced.countDown();

    }).subscribe();

    assertThat(embeddedChannel.config().isAutoRead()).isTrue();

    // produce some back pressure
    embeddedChannel.writeInbound(Unpooled.wrappedBuffer(RESP.arrayHeader(4)));
    embeddedChannel.writeInbound(Unpooled.wrappedBuffer(RESP.bulkString("one")));
    pressureArrived.await();
    assertThat(embeddedChannel.config().isAutoRead()).isTrue();

    embeddedChannel.writeInbound(Unpooled.wrappedBuffer(RESP.bulkString("two")));
    embeddedChannel.writeInbound(Unpooled.wrappedBuffer(RESP.bulkString("three")));
    assertThat(embeddedChannel.config().isAutoRead()).isFalse();

    cancellation.dispose();

    assertThat(embeddedChannel.config().isAutoRead()).isTrue();

    // allow processing
    buildPressure.countDown();

    // emit the last item
    embeddedChannel.writeInbound(Unpooled.wrappedBuffer(RESP.bulkString("four")));

    // done
    assertThat(embeddedChannel.config().isAutoRead()).isTrue();
}

From source file:io.liveoak.container.codec.form.FormURLDecoderTest.java

License:Open Source License

protected ResourceState decode(String data) throws Exception {

    FormURLDecoder decoder = new FormURLDecoder();
    ByteBuf buffer = Unpooled.wrappedBuffer(data.getBytes(Charset.defaultCharset()));
    return decoder.decode(buffer);
}

From source file:io.maelstorm.server.ResponseUtils.java

License:Open Source License

public static ByteBuf getChannelBuffer(StringBuilder buffer) {
    return Unpooled.wrappedBuffer(buffer.toString().getBytes());
}

From source file:io.moquette.server.HazelcastListener.java

License:Open Source License

@Override
public void onMessage(Message<HazelcastMsg> msg) {
    try {/*from   www . ja v a2s . c  o m*/
        if (!msg.getPublishingMember().equals(server.getHazelcastInstance().getCluster().getLocalMember())) {
            HazelcastMsg hzMsg = msg.getMessageObject();
            LOG.info("{} received from hazelcast for topic {} message: {}", hzMsg.getClientId(),
                    hzMsg.getTopic(), hzMsg.getPayload());
            // TODO pass forward this information in somehow publishMessage.setLocal(false);

            MqttQoS qos = MqttQoS.valueOf(hzMsg.getQos());
            MqttFixedHeader fixedHeader = new MqttFixedHeader(MqttMessageType.PUBLISH, false, qos, false, 0);
            MqttPublishVariableHeader varHeader = new MqttPublishVariableHeader(hzMsg.getTopic(), 0);
            ByteBuf payload = Unpooled.wrappedBuffer(hzMsg.getPayload());
            MqttPublishMessage publishMessage = new MqttPublishMessage(fixedHeader, varHeader, payload);
            server.internalPublish(publishMessage, hzMsg.getClientId());
        }
    } catch (Exception ex) {
        LOG.error("error polling hazelcast msg queue", ex);
    }
}

From source file:io.netty.example.http.helloworld.HttpHelloWorldServerHandler.java

License:Apache License

@Override
public void channelRead0(ChannelHandlerContext ctx, HttpObject msg) {
    if (msg instanceof HttpRequest) {
        HttpRequest req = (HttpRequest) msg;

        boolean keepAlive = HttpUtil.isKeepAlive(req);
        FullHttpResponse response = new DefaultFullHttpResponse(req.protocolVersion(), OK,
                Unpooled.wrappedBuffer(CONTENT));
        response.headers().set(CONTENT_TYPE, TEXT_PLAIN).setInt(CONTENT_LENGTH,
                response.content().readableBytes());

        if (keepAlive) {
            if (!req.protocolVersion().isKeepAliveDefault()) {
                response.headers().set(CONNECTION, KEEP_ALIVE);
            }//from   ww w .  j  a  va2s. c o  m
        } else {
            // Tell the client we're going to close the connection.
            response.headers().set(CONNECTION, CLOSE);
        }

        ChannelFuture f = ctx.write(response);

        if (!keepAlive) {
            f.addListener(ChannelFutureListener.CLOSE);
        }
    }
}

From source file:io.nodyn.crypto.Hash.java

License:Apache License

public ByteBuf digest() throws NoSuchAlgorithmException {
    byte[] digestBytes = new byte[this.digest.getDigestSize()];
    this.digest.doFinal(digestBytes, 0);
    return Unpooled.wrappedBuffer(digestBytes);
}