Example usage for io.netty.channel FileRegion refCnt

List of usage examples for io.netty.channel FileRegion refCnt

Introduction

In this page you can find the example usage for io.netty.channel FileRegion refCnt.

Prototype

int refCnt();

Source Link

Document

Returns the reference count of this object.

Usage

From source file:org.apache.tajo.pullserver.HttpDataServerHandler.java

License:Apache License

private ChannelFuture sendFile(ChannelHandlerContext ctx, FileChunk file) throws IOException {
    RandomAccessFile raf;/*from w w w.j  a  va2  s .  c  om*/
    try {
        raf = new RandomAccessFile(file.getFile(), "r");
    } catch (FileNotFoundException fnfe) {
        return null;
    }

    ChannelFuture writeFuture;
    ChannelFuture lastContentFuture;
    if (ctx.pipeline().get(SslHandler.class) != null) {
        // Cannot use zero-copy with HTTPS.
        lastContentFuture = ctx
                .write(new HttpChunkedInput(new ChunkedFile(raf, file.startOffset(), file.length(), 8192)));
    } else {
        // No encryption - use zero-copy.
        final FileRegion region = new DefaultFileRegion(raf.getChannel(), file.startOffset(), file.length());
        writeFuture = ctx.write(region);
        lastContentFuture = ctx.write(LastHttpContent.EMPTY_LAST_CONTENT);
        writeFuture.addListener(new ChannelFutureListener() {
            public void operationComplete(ChannelFuture future) {
                if (region.refCnt() > 0) {
                    region.release();
                }
            }
        });
    }

    return lastContentFuture;
}