Example usage for io.netty.buffer Unpooled directBuffer

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

Introduction

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

Prototype

public static ByteBuf directBuffer() 

Source Link

Document

Creates a new big-endian direct buffer with reasonably small initial capacity, which expands its capacity boundlessly on demand.

Usage

From source file:io.gatling.netty.util.ahc.Utf8ByteBufCharsetDecoderTest.java

License:Apache License

@Test
void testByteBuf2BytesNoBackingArray() {
    byte[] inputBytes = "testdata".getBytes(US_ASCII);
    ByteBuf buf = Unpooled.directBuffer();
    try {//  ww  w .j a  v a 2  s.c  o  m
        buf.writeBytes(inputBytes);
        byte[] output = ByteBufUtils.byteBuf2Bytes(buf);
        assertArrayEquals(inputBytes, output);
    } finally {
        buf.release();
    }
}

From source file:io.github.lordakkarin.nbt.event.TagReader.java

License:Apache License

public TagReader(@NonNull ReadableByteChannel channel) throws IOException {
    this.buffer = Unpooled.directBuffer();

    {/*www .  j  a  va 2 s  .c om*/
        ByteBuffer tmp = ByteBuffer.allocateDirect(128);
        ByteBuf wrapped = Unpooled.wrappedBuffer(tmp);
        int length;

        while ((length = channel.read(tmp)) > 0) {
            tmp.flip();
            this.buffer.writeBytes(wrapped, length);

            wrapped.resetReaderIndex();
            tmp.rewind();
        }
    }
}

From source file:io.github.lordakkarin.nbt.event.TagWriter.java

License:Apache License

public TagWriter(@Nullable TagVisitor next) {
    super(next);

    this.buffer = Unpooled.directBuffer();
}

From source file:io.github.lordakkarin.nbt.event.TagWriterTest.java

License:Apache License

@NonNull
private ByteBuf readResource(@NonNull ReadableByteChannel channel) throws IOException {
    ByteBuf target = Unpooled.directBuffer();

    ByteBuffer tmp = ByteBuffer.allocateDirect(128);
    ByteBuf wrapped = Unpooled.wrappedBuffer(tmp);
    int length;/*from w  w  w. ja v a  2 s  . c  om*/

    while ((length = channel.read(tmp)) > 0) {
        tmp.flip();
        target.writeBytes(wrapped, length);

        wrapped.resetReaderIndex();
        tmp.rewind();
    }

    return target;
}

From source file:io.servicecomb.foundation.vertx.TestVertxUtils.java

License:Apache License

@Test
public void testgetBytesFastByteBufCopy() {
    ByteBuf byteBuf = Unpooled.directBuffer();
    byteBuf.writeByte(1);/*  w w w  .  jav a  2 s  .  c o m*/
    Assert.assertFalse(byteBuf.hasArray());

    byte[] result = VertxUtils.getBytesFast(byteBuf);
    Assert.assertEquals(1, result[0]);

    byteBuf.release();
}

From source file:io.vertx.benchmarks.HttpServerHandlerBenchmark.java

License:Open Source License

@Setup
public void setup() {
    vertx = (VertxInternal) Vertx.vertx();
    HttpServerOptions options = new HttpServerOptions();
    vertxChannel = new EmbeddedChannel(
            new HttpRequestDecoder(options.getMaxInitialLineLength(), options.getMaxHeaderSize(),
                    options.getMaxChunkSize(), false, options.getDecoderInitialBufferSize()),
            new HttpResponseEncoder());
    vertxChannel.config().setAllocator(new Alloc());

    ContextInternal context = new EventLoopContext(vertx, vertxChannel.eventLoop(), null, null, null,
            new JsonObject(), Thread.currentThread().getContextClassLoader());
    Handler<HttpServerRequest> app = request -> {
        HttpServerResponse response = request.response();
        MultiMap headers = response.headers();
        headers.add(HEADER_CONTENT_TYPE, RESPONSE_TYPE_PLAIN).add(HEADER_SERVER, SERVER)
                .add(HEADER_DATE, DATE_STRING).add(HEADER_CONTENT_LENGTH, HELLO_WORLD_LENGTH);
        response.end(HELLO_WORLD_BUFFER);
    };//from w ww .j av a  2  s . com
    HandlerHolder<HttpHandlers> holder = new HandlerHolder<>(context, new HttpHandlers(app, null, null, null));
    Http1xServerHandler handler = new Http1xServerHandler(null, new HttpServerOptions(), "localhost", holder,
            null);
    vertxChannel.pipeline().addLast("handler", handler);

    nettyChannel = new EmbeddedChannel(
            new HttpRequestDecoder(options.getMaxInitialLineLength(), options.getMaxHeaderSize(),
                    options.getMaxChunkSize(), false, options.getDecoderInitialBufferSize()),
            new HttpResponseEncoder(), new SimpleChannelInboundHandler<HttpRequest>() {

                private final byte[] STATIC_PLAINTEXT = "Hello, World!".getBytes(CharsetUtil.UTF_8);
                private final int STATIC_PLAINTEXT_LEN = STATIC_PLAINTEXT.length;
                private final ByteBuf PLAINTEXT_CONTENT_BUFFER = Unpooled
                        .unreleasableBuffer(Unpooled.directBuffer().writeBytes(STATIC_PLAINTEXT));
                private final CharSequence PLAINTEXT_CLHEADER_VALUE = new AsciiString(
                        String.valueOf(STATIC_PLAINTEXT_LEN));

                private final CharSequence TYPE_PLAIN = new AsciiString("text/plain");
                private final CharSequence SERVER_NAME = new AsciiString("Netty");
                private final CharSequence CONTENT_TYPE_ENTITY = HttpHeaderNames.CONTENT_TYPE;
                private final CharSequence DATE_ENTITY = HttpHeaderNames.DATE;
                private final CharSequence CONTENT_LENGTH_ENTITY = HttpHeaderNames.CONTENT_LENGTH;
                private final CharSequence SERVER_ENTITY = HttpHeaderNames.SERVER;

                private final DateFormat FORMAT = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss z");
                private final CharSequence date = new AsciiString(FORMAT.format(new Date()));

                @Override
                protected void channelRead0(ChannelHandlerContext ctx, HttpRequest msg) throws Exception {
                    writeResponse(ctx, msg, PLAINTEXT_CONTENT_BUFFER.duplicate(), TYPE_PLAIN,
                            PLAINTEXT_CLHEADER_VALUE);
                }

                @Override
                public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
                    ctx.flush();
                }

                private void writeResponse(ChannelHandlerContext ctx, HttpRequest request, ByteBuf buf,
                        CharSequence contentType, CharSequence contentLength) {

                    // Build the response object.
                    FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,
                            HttpResponseStatus.OK, buf, false);
                    HttpHeaders headers = response.headers();
                    headers.set(CONTENT_TYPE_ENTITY, contentType);
                    headers.set(SERVER_ENTITY, SERVER_NAME);
                    headers.set(DATE_ENTITY, date);
                    headers.set(CONTENT_LENGTH_ENTITY, contentLength);

                    // Close the non-keep-alive connection after the write operation is done.
                    ctx.write(response, ctx.voidPromise());
                }
            });
    nettyChannel.config().setAllocator(new Alloc());

    GET = Unpooled.unreleasableBuffer(Unpooled.copiedBuffer(("GET / HTTP/1.1\r\n" + "\r\n").getBytes()));
    readerIndex = GET.readerIndex();
    writeIndex = GET.writerIndex();
}

From source file:io.vertx.proton.impl.ProtonReadableBufferImplTest.java

License:Apache License

@Test
public void testArrayAccessWhenNoArray() {
    ByteBuf byteBuffer = Unpooled.directBuffer();
    ProtonReadableBufferImpl buffer = new ProtonReadableBufferImpl(byteBuffer);

    assertFalse(buffer.hasArray());/*from www. j  av a 2  s . c om*/
}

From source file:org.apache.activemq.artemis.protocol.amqp.util.NettyReadableTest.java

License:Apache License

@Test
public void testArrayAccessWhenNoArray() {
    ByteBuf byteBuffer = Unpooled.directBuffer();
    NettyReadable buffer = new NettyReadable(byteBuffer);

    assertFalse(buffer.hasArray());//  www .j  a  v a2  s . com
}

From source file:org.apache.qpid.jms.provider.amqp.message.AmqpReadableBufferTest.java

License:Apache License

@Test
public void testArrayAccessWhenNoArray() {
    ByteBuf byteBuffer = Unpooled.directBuffer();
    AmqpReadableBuffer buffer = new AmqpReadableBuffer(byteBuffer);

    assertFalse(buffer.hasArray());// w  ww. j  av  a  2  s. c om
}

From source file:org.asynchttpclient.netty.util.Utf8ByteBufCharsetDecoderTest.java

License:Open Source License

@Test
public void testByteBuf2BytesNoBackingArray() {
    byte[] inputBytes = "testdata".getBytes(US_ASCII);
    ByteBuf buf = Unpooled.directBuffer();
    try {//from   w w w . j  a  va 2  s.c  om
        buf.writeBytes(inputBytes);
        byte[] output = ByteBufUtils.byteBuf2Bytes(buf);
        assertEquals(output, inputBytes);
    } finally {
        buf.release();
    }
}