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:com.turo.pushy.apns.server.MockApnsServerHandler.java

License:Open Source License

@Override
public void write(final ChannelHandlerContext context, final Object message,
        final ChannelPromise writePromise) {
    if (message instanceof AcceptNotificationResponse) {
        final AcceptNotificationResponse acceptNotificationResponse = (AcceptNotificationResponse) message;

        final Http2Headers headers = new DefaultHttp2Headers().status(HttpResponseStatus.OK.codeAsText())
                .add(APNS_ID_HEADER, FastUUID.toString(acceptNotificationResponse.getApnsId()));

        this.encoder().writeHeaders(context, acceptNotificationResponse.getStreamId(), headers, 0, true,
                writePromise);//from  www .ja  v  a2 s .  c  o  m

        log.trace("Accepted push notification on stream {}", acceptNotificationResponse.getStreamId());
    } else if (message instanceof RejectNotificationResponse) {
        final RejectNotificationResponse rejectNotificationResponse = (RejectNotificationResponse) message;

        final Http2Headers headers = new DefaultHttp2Headers()
                .status(rejectNotificationResponse.getErrorReason().getHttpResponseStatus().codeAsText())
                .add(HttpHeaderNames.CONTENT_TYPE, "application/json")
                .add(APNS_ID_HEADER, FastUUID.toString(rejectNotificationResponse.getApnsId()));

        final byte[] payloadBytes;
        {
            final ErrorPayload errorPayload = new ErrorPayload(
                    rejectNotificationResponse.getErrorReason().getReasonText(),
                    rejectNotificationResponse.getTimestamp());

            payloadBytes = GSON.toJson(errorPayload).getBytes();
        }

        final ChannelPromise headersPromise = context.newPromise();
        this.encoder().writeHeaders(context, rejectNotificationResponse.getStreamId(), headers, 0, false,
                headersPromise);

        final ChannelPromise dataPromise = context.newPromise();
        this.encoder().writeData(context, rejectNotificationResponse.getStreamId(),
                Unpooled.wrappedBuffer(payloadBytes), 0, true, dataPromise);

        final PromiseCombiner promiseCombiner = new PromiseCombiner();
        promiseCombiner.addAll((ChannelFuture) headersPromise, dataPromise);
        promiseCombiner.finish(writePromise);

        log.trace("Rejected push notification on stream {}: {}", rejectNotificationResponse.getStreamId(),
                rejectNotificationResponse.getErrorReason());
    } else {
        context.write(message, writePromise);
    }
}

From source file:com.twitter.http2.HttpFrameDecoderTest.java

License:Apache License

@Test
public void testClientConnectionPreface() throws Exception {
    decoder = new HttpFrameDecoder(true, delegate);
    ByteBuf connectionPreface = releaseLater(
            Unpooled.wrappedBuffer(new byte[] { 0x50, 0x52, 0x49, 0x20, 0x2a, 0x20, 0x48, 0x54, 0x54, 0x50,
                    0x2f, 0x32, 0x2e, 0x30, 0x0d, 0x0a, 0x0d, 0x0a, 0x53, 0x4d, 0x0d, 0x0a, 0x0d, 0x0a }));
    int length = 0;
    byte flags = 0;
    int streamId = 0; // connection identifier

    ByteBuf frame = settingsFrame(length, flags, streamId);
    decoder.decode(releaseLater(Unpooled.wrappedBuffer(connectionPreface, frame)));

    verify(delegate).readSettingsFrame(false);
    verify(delegate).readSettingsEnd();//from   w w  w.  j a va  2s . c  o m
    verifyNoMoreInteractions(delegate);
}

From source file:com.twocater.diamond.core.test.HttpHelloWorldServerHandler.java

License:Apache License

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

        if (HttpHeaders.is100ContinueExpected(req)) {
            ctx.write(new DefaultFullHttpResponse(HTTP_1_1, CONTINUE));
        }/*ww  w.ja v  a 2s.  c  o  m*/
        boolean keepAlive = HttpHeaders.isKeepAlive(req);
        FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, Unpooled.wrappedBuffer(CONTENT));
        response.headers().set(CONTENT_TYPE, "text/plain");
        response.headers().set(CONTENT_LENGTH, response.content().readableBytes());

        keepAlive = false;
        if (!keepAlive) {
            ctx.write(response).addListener(new ChannelFutureListener() {
                @Override
                public void operationComplete(ChannelFuture channelFuture) throws Exception {
                    channelFuture.channel().close();
                    System.out.println("asdfsadasdfsdfsadfsadfsdfsdfdfasdfasdfasdfasdfasf");
                }
            });
        } else {
            response.headers().set(CONNECTION, Values.KEEP_ALIVE);
            ctx.write(response);
        }
    }
}

From source file:com.uber.tchannel.benchmarks.LargePayloadBenchmark.java

License:Open Source License

@Setup(Level.Trial)
public void setup() throws Exception {
    InternalLoggerFactory.setDefaultFactory(new Slf4JLoggerFactory());
    BasicConfigurator.configure();//ww w . ja  v a 2  s. com
    LogManager.getRootLogger().setLevel(org.apache.log4j.Level.INFO);

    this.host = InetAddress.getByName("127.0.0.1");
    this.channel = new TChannel.Builder("ping-server").setServerHost(host).setBossGroup(bossGroup)
            .setChildGroup(childGroup).build();
    channel.makeSubChannel("ping-server").register("ping", new PingDefaultRequestHandler());
    channel.listen();
    this.port = this.channel.getListeningPort();

    this.client = new TChannel.Builder("ping-client")
            // .setResetOnTimeoutLimit(100)
            .setClientMaxPendingRequests(200000).setBossGroup(bossGroup).setChildGroup(childGroup).build();
    this.subClient = this.client.makeSubChannel("ping-server");
    this.client.listen();

    byte[] buf = new byte[60 * 1024];
    new Random().nextBytes(buf);
    payload = Unpooled.wrappedBuffer(buf);
}

From source file:com.uber.tchannel.codecs.CallRequestFrameCodecTest.java

License:Open Source License

@Test
public void testEncodeDecode() throws Exception {

    CallRequestFrame callRequestFrame = Fixtures.callRequest(42, false,
            Unpooled.wrappedBuffer("Hello, World!".getBytes()));
    CallRequestFrame inboundCallRequestFrame = (CallRequestFrame) MessageCodec.decode(
            CodecTestUtil.encodeDecode(MessageCodec.encode(ByteBufAllocator.DEFAULT, callRequestFrame)));

    assertEquals("Hello, World!", inboundCallRequestFrame.getPayload().toString(CharsetUtil.UTF_8));
    inboundCallRequestFrame.getPayload().release();
}

From source file:com.uber.tchannel.codecs.CallRequestFrameContinueCodecTest.java

License:Open Source License

@Test
public void testEncodeDecode() throws Exception {

    CallRequestContinueFrame callRequestContinueFrame = Fixtures.callRequestContinue(42, false,
            Unpooled.wrappedBuffer("Hello, World!".getBytes()));

    CallRequestContinueFrame inboundCallRequestContinueFrame = (CallRequestContinueFrame) MessageCodec
            .decode(CodecTestUtil//from w  w  w .  j  a va  2 s. c  o m
                    .encodeDecode(MessageCodec.encode(ByteBufAllocator.DEFAULT, callRequestContinueFrame)));

    assertEquals("Hello, World!", inboundCallRequestContinueFrame.getPayload().toString(CharsetUtil.UTF_8));
    inboundCallRequestContinueFrame.getPayload().release();
}

From source file:com.uber.tchannel.codecs.CallResponseFrameCodecTest.java

License:Open Source License

@Test
public void testEncodeDecode() throws Exception {
    CallResponseFrame callResponseFrame = Fixtures.callResponse(42, false,
            Unpooled.wrappedBuffer("Hello, World!".getBytes()));

    CallResponseFrame inboundCallResponseFrame = (CallResponseFrame) MessageCodec.decode(
            CodecTestUtil.encodeDecode(MessageCodec.encode(ByteBufAllocator.DEFAULT, callResponseFrame)));

    assertEquals("Hello, World!", inboundCallResponseFrame.getPayload().toString(CharsetUtil.UTF_8));
    inboundCallResponseFrame.release();//from  w  w w.  j a v  a  2 s.co  m
}

From source file:com.uber.tchannel.codecs.CallResponseFrameContinueCodecTest.java

License:Open Source License

@Test
public void testEncode() throws Exception {
    CallResponseContinueFrame callRequestContinue = Fixtures.callResponseContinue(42, false,
            Unpooled.wrappedBuffer("Hello, World!".getBytes()));

    CallResponseContinueFrame inboundCallResponseContinueFrame = (CallResponseContinueFrame) MessageCodec
            .decode(CodecTestUtil//from www .  ja  v  a  2 s.c o m
                    .encodeDecode(MessageCodec.encode(ByteBufAllocator.DEFAULT, callRequestContinue)));

    assertEquals("Hello, World!", inboundCallResponseContinueFrame.getPayload().toString(CharsetUtil.UTF_8));
    inboundCallResponseContinueFrame.release();
}

From source file:com.uber.tchannel.codecs.TFrameCodecTest.java

License:Open Source License

@Test
public void shouldEncodeAndDecodeFrame() {

    EmbeddedChannel channel = new EmbeddedChannel(new TChannelLengthFieldBasedFrameDecoder(),
            new TFrameCodec());

    String payload = "Hello, World!";
    ByteBuf buffer = Unpooled.wrappedBuffer(payload.getBytes());

    TFrame frame = new TFrame(payload.getBytes().length, FrameType.InitRequest, Integer.MAX_VALUE, buffer);

    channel.writeOutbound(frame);/*w w  w  .  j  av a2  s.co m*/
    channel.writeInbound(channel.readOutbound());

    TFrame newFrame = channel.readInbound();
    assertNotNull(newFrame);
    assertEquals(frame.size, newFrame.size);
    assertEquals(frame.type, newFrame.type);
    assertEquals(frame.id, newFrame.id);
    newFrame.release();

}

From source file:com.uber.tchannel.handlers.FrameFragmenterTest.java

License:Open Source License

@Test
public void testEncode() throws Exception {
    EmbeddedChannel channel = new EmbeddedChannel(new MessageFragmenter());

    // arg1//from  w w  w . jav a 2  s  .  co m
    byte[] arg1Bytes = new byte[CallFrame.MAX_ARG1_LENGTH];
    new Random().nextBytes(arg1Bytes);
    ByteBuf arg1 = Unpooled.wrappedBuffer(arg1Bytes);

    // arg2
    byte[] arg2Bytes = new byte[BUFFER_SIZE];
    new Random().nextBytes(arg2Bytes);
    ByteBuf arg2 = Unpooled.wrappedBuffer(arg2Bytes);

    // arg 3
    byte[] arg3Bytes = new byte[BUFFER_SIZE];
    new Random().nextBytes(arg3Bytes);
    ByteBuf arg3 = Unpooled.wrappedBuffer(arg3Bytes);

    RawRequest rawRequest = new RawRequest.Builder("some-service", arg1).setArg2(arg2).setArg3(arg3).setId(0)
            .setTimeout(100).build();

    channel.writeOutbound(rawRequest);

    for (int i = 0; i < 4; i++) {
        CallFrame req = (CallFrame) MessageCodec.decode(MessageCodec.decode((ByteBuf) channel.readOutbound()));
        req.release();
        assertNotNull(req);
    }

    ByteBuf buf = channel.readOutbound();
    assertNull(buf);

    rawRequest.release();
}