Example usage for io.netty.buffer ByteBuf release

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

Introduction

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

Prototype

boolean release();

Source Link

Document

Decreases the reference count by 1 and deallocates this object if the reference count reaches at 0 .

Usage

From source file:io.tetrapod.core.flashpolicy.FlashPolicyServerDecoder.java

License:Apache License

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
    ByteBuf data = in.readBytes(REQUEST.readableBytes());

    if (data.equals(REQUEST)) {
        out.add(Boolean.TRUE);/*from  ww w.  j  a  v  a2  s .  c  om*/
    } else {
        ctx.close();
    }
    data.release();
}

From source file:io.vertx.core.buffer.BufferTest.java

License:Open Source License

@Test
public void testDirect() {
    Buffer buff = Buffer.factory.directBuffer("hello world".getBytes());
    assertEquals("hello world", buff.toString());
    buff.appendString(" foobar");
    assertEquals("hello world foobar", buff.toString());
    ByteBuf bb = buff.getByteBuf().unwrap();
    assertTrue(bb.isDirect());//from   w  ww. j ava  2s.  c om
    assertTrue(bb.release());
    try {
        // Check it's deallocated
        buff.toString();
        fail();
    } catch (IllegalReferenceCountException e) {
    }
}

From source file:io.vertx.core.file.FileSystemTest.java

License:Open Source License

@Test
public void testWriteStreamWithCompositeBuffer() throws Exception {
    String fileName = "some-file.dat";
    int chunkSize = 1000;
    int chunks = 10;
    byte[] content1 = TestUtils.randomByteArray(chunkSize * (chunks / 2));
    byte[] content2 = TestUtils.randomByteArray(chunkSize * (chunks / 2));
    ByteBuf byteBuf = Unpooled.wrappedBuffer(content1, content2);
    Buffer buff = Buffer.buffer(byteBuf);
    vertx.fileSystem().open(testDir + pathSep + fileName, new OpenOptions(), ar -> {
        if (ar.succeeded()) {
            WriteStream<Buffer> ws = ar.result();
            ws.exceptionHandler(t -> fail(t.getMessage()));
            ws.write(buff);/*from   w w  w.j  a va  2s. co m*/
            ar.result().close(ar2 -> {
                if (ar2.failed()) {
                    fail(ar2.cause().getMessage());
                } else {
                    assertTrue(fileExists(fileName));
                    byte[] readBytes;
                    try {
                        readBytes = Files.readAllBytes(Paths.get(testDir + pathSep + fileName));
                    } catch (IOException e) {
                        fail(e.getMessage());
                        return;
                    }
                    assertEquals(buff, Buffer.buffer(readBytes));
                    byteBuf.release();
                    testComplete();
                }
            });
        } else {
            fail(ar.cause().getMessage());
        }
    });
    await();
}

From source file:io.vertx.core.http.impl.Http1xOrH2CHandler.java

License:Open Source License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    ByteBuf buf = (ByteBuf) msg;
    int len = Math.min(buf.readableBytes(), HTTP_2_PREFACE_ARRAY.length - current);
    int i = 0;//from w ww .  j a  v a2s  . co  m
    while (i < len) {
        if (buf.getByte(buf.readerIndex() + i) != HTTP_2_PREFACE_ARRAY[current + i]) {
            end(ctx, buf, false);
            return;
        }
        i++;
    }
    if (current + i == HTTP_2_PREFACE_ARRAY.length) {
        end(ctx, buf, true);
    } else {
        current += len;
        buf.release();
    }
}

From source file:io.vertx.core.http.impl.Http1xOrH2CHandler.java

License:Open Source License

private void end(ChannelHandlerContext ctx, ByteBuf buf, boolean h2c) {
    if (current > 0) {
        ByteBuf msg = Unpooled.buffer(current + buf.readableBytes());
        msg.writeBytes(HTTP_2_PREFACE_ARRAY, 0, current);
        msg.writeBytes(buf);//from  w w  w. j  a  va2s .c o  m
        buf.release();
        buf = msg;
    }
    configure(ctx, h2c);
    ctx.pipeline().remove(this);
    ctx.fireChannelRead(buf);
}

From source file:io.vertx.core.net.impl.VertxHandler.java

License:Open Source License

public static ByteBuf safeBuffer(ByteBuf buf, ByteBufAllocator allocator) {
    if (buf == Unpooled.EMPTY_BUFFER) {
        return buf;
    }//from w  w  w.j a  v  a 2 s. c  o m
    if (buf.isDirect() || buf instanceof CompositeByteBuf) {
        try {
            if (buf.isReadable()) {
                ByteBuf buffer = allocator.heapBuffer(buf.readableBytes());
                buffer.writeBytes(buf);
                return buffer;
            } else {
                return Unpooled.EMPTY_BUFFER;
            }
        } finally {
            buf.release();
        }
    }
    return buf;
}