Example usage for io.netty.buffer ByteBuf refCnt

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

Introduction

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

Prototype

int refCnt();

Source Link

Document

Returns the reference count of this object.

Usage

From source file:org.apache.spark.network.util.TransportFrameDecoderSuite.java

License:Apache License

@Test
public void testInterception() throws Exception {
    int interceptedReads = 3;
    TransportFrameDecoder decoder = new TransportFrameDecoder();
    TransportFrameDecoder.Interceptor interceptor = spy(new MockInterceptor(interceptedReads));
    ChannelHandlerContext ctx = mockChannelHandlerContext();

    byte[] data = new byte[8];
    ByteBuf len = Unpooled.copyLong(8 + data.length);
    ByteBuf dataBuf = Unpooled.wrappedBuffer(data);

    try {//from w w w.  j  av  a 2 s .  c o  m
        decoder.setInterceptor(interceptor);
        for (int i = 0; i < interceptedReads; i++) {
            decoder.channelRead(ctx, dataBuf);
            assertEquals(0, dataBuf.refCnt());
            dataBuf = Unpooled.wrappedBuffer(data);
        }
        decoder.channelRead(ctx, len);
        decoder.channelRead(ctx, dataBuf);
        verify(interceptor, times(interceptedReads)).handle(any(ByteBuf.class));
        verify(ctx).fireChannelRead(any(ByteBuffer.class));
        assertEquals(0, len.refCnt());
        assertEquals(0, dataBuf.refCnt());
    } finally {
        release(len);
        release(dataBuf);
    }
}

From source file:org.apache.spark.network.util.TransportFrameDecoderSuite.java

License:Apache License

@Test
public void testSplitLengthField() throws Exception {
    byte[] frame = new byte[1024 * (RND.nextInt(31) + 1)];
    ByteBuf buf = Unpooled.buffer(frame.length + 8);
    buf.writeLong(frame.length + 8);/* w w w . j  av a  2s .c  o  m*/
    buf.writeBytes(frame);

    TransportFrameDecoder decoder = new TransportFrameDecoder();
    ChannelHandlerContext ctx = mockChannelHandlerContext();
    try {
        decoder.channelRead(ctx, buf.readSlice(RND.nextInt(7)).retain());
        verify(ctx, never()).fireChannelRead(any(ByteBuf.class));
        decoder.channelRead(ctx, buf);
        verify(ctx).fireChannelRead(any(ByteBuf.class));
        assertEquals(0, buf.refCnt());
    } finally {
        decoder.channelInactive(ctx);
        release(buf);
    }
}

From source file:org.apache.spark.network.util.TransportFrameDecoderSuite.java

License:Apache License

private void release(ByteBuf buf) {
    if (buf.refCnt() > 0) {
        buf.release(buf.refCnt());
    }
}

From source file:org.apache.tajo.plan.function.stream.BufferPool.java

License:Apache License

@InterfaceStability.Unstable
public static void forceRelease(ByteBuf buf) {
    buf.release(buf.refCnt());
}

From source file:org.ballerinalang.net.grpc.CompositeContent.java

License:Open Source License

public void close() {
    while (!buffers.isEmpty()) {
        ByteBuf byteBuf = buffers.remove();
        if (byteBuf.refCnt() != 0) {
            byteBuf.release();//from   w  ww  .j  ava 2  s .com
        }
    }
}

From source file:org.ballerinalang.net.grpc.CompositeContent.java

License:Open Source License

/**
 * If the current buffer is exhausted, removes and closes it.
 *///  w w w . j  av a  2  s. c  o m
private void advanceBufferIfNecessary() {
    ByteBuf buffer = buffers.peek();
    if (buffer != null && buffer.readableBytes() == 0 && buffer.refCnt() != 0) {
        buffers.remove().release();
    }
}

From source file:org.rzo.netty.ahessian.io.InputStreamBuffer.java

License:Apache License

private void checkBufs() {
    if (!_bufs.isEmpty() && _bufs.getFirst().readableBytes() == 0) {
        ByteBuf buf = _bufs.removeFirst();
        buf.release(buf.refCnt());
    }/*from ww  w  . j  a v a2 s  . c  o  m*/
}

From source file:org.rzo.netty.ahessian.io.OutputStreamBuffer.java

License:Apache License

private void sendDownstream(final ChannelPromise future, boolean wait) throws IOException {
    if (future != null && future.isDone())
        return;//from  ww w. ja v a  2 s .c o  m
    if (!_ctx.channel().isActive())
        throw new IOException("channel disconnected");
    _lock.lock();
    ChannelFuture result = null;
    try {
        final ByteBuf toSend = _buf;
        _buf = null;
        if (toSend.refCnt() > 1)
            toSend.release(toSend.refCnt() - 1);
        // System.out.println(System.currentTimeMillis()+" outputstream send downstream +");
        if (future == null || future.isDone())
            result = _ctx.write(toSend);
        else
            _ctx.write(toSend, future);
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        _lock.unlock();
    }
    // System.out.println(System.currentTimeMillis()+" outputstream send downstream -");

}

From source file:org.rzo.netty.ahessian.session.ServerSessionFilter.java

License:Apache License

public void channelRead(ChannelHandlerContext ctx, Object e) throws Exception {
    // if session established forward all messages
    if (_hasSession) {
        Session session = ctx.channel().attr(SESSION).get();
        session.onMessage();//  www. j  a  v a2 s.  c o m
        ctx.fireChannelRead(e);
    } else {
        ByteBuf b = (ByteBuf) e;
        _sessionId += b.toString(Charset.forName("UTF-8"));
        b.release(b.refCnt());
        if (_sessionId.equals("?"))
            newSession(ctx);
        else
            checkSession(ctx);
    }
}

From source file:org.springframework.boot.context.embedded.netty.HttpResponseOutputStream.java

License:Apache License

private void writeContent(ByteBuf content, boolean lastContent) {
    // TODO block if channel is not writable to avoid heap utilisation
    if (!servletResponse.isCommitted()) {
        writeResponse(lastContent);//from   w w  w  .java2 s  .  c o  m
    }
    if (content.readableBytes() > 0) {
        assert content.refCnt() == 1;
        ctx.write(content, ctx.voidPromise());
    }
    if (lastContent) {
        HttpResponse nettyResponse = servletResponse.getNettyResponse();
        ChannelFuture future = ctx.write(DefaultLastHttpContent.EMPTY_LAST_CONTENT);
        if (!HttpHeaders.isKeepAlive(nettyResponse)) {
            future.addListener(ChannelFutureListener.CLOSE);
        }
    }
}