Example usage for io.netty.handler.stream ChunkedNioFile ChunkedNioFile

List of usage examples for io.netty.handler.stream ChunkedNioFile ChunkedNioFile

Introduction

In this page you can find the example usage for io.netty.handler.stream ChunkedNioFile ChunkedNioFile.

Prototype

public ChunkedNioFile(FileChannel in, long offset, long length, int chunkSize) throws IOException 

Source Link

Document

Creates a new instance that fetches data from the specified file.

Usage

From source file:com.king.platform.net.http.netty.request.FileHttpBody.java

License:Apache License

private ChannelFuture writeChunkedContent(ChannelHandlerContext ctx) throws IOException {
    Channel channel = ctx.channel();
    FileChannel fileChannel = new FileInputStream(file).getChannel();
    long length = file.length();
    return channel.write(new ChunkedNioFile(fileChannel, 0, length, 1024 * 8), channel.newProgressivePromise());
}

From source file:org.asynchttpclient.netty.request.body.NettyFileBody.java

License:Open Source License

@Override
public void write(Channel channel, NettyResponseFuture<?> future) throws IOException {
    @SuppressWarnings("resource")
    // Netty will close the ChunkedNioFile or the DefaultFileRegion
    final FileChannel fileChannel = new RandomAccessFile(file, "r").getChannel();

    Object message = (ChannelManager.isSslHandlerConfigured(channel.pipeline()) || config.isDisableZeroCopy()) ? //
            new ChunkedNioFile(fileChannel, offset, length, config.getChunkedFileChunkSize())
            : new DefaultFileRegion(fileChannel, offset, length);

    channel.write(message, channel.newProgressivePromise())//
            .addListener(new WriteProgressListener(future, false, getContentLength()));
    channel.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT, channel.voidPromise());
}

From source file:org.jooby.internal.netty.NettyResponse.java

License:Apache License

@Override
public void send(final FileChannel channel, final long offset, final long count) throws Exception {
    DefaultHttpResponse rsp = new DefaultHttpResponse(HttpVersion.HTTP_1_1, status);
    if (!headers.contains(HttpHeaderNames.CONTENT_LENGTH)) {
        headers.remove(HttpHeaderNames.TRANSFER_ENCODING);
        headers.set(HttpHeaderNames.CONTENT_LENGTH, count);
    }/*from  w  ww . ja v  a 2  s  .com*/

    if (keepAlive) {
        headers.set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
    }

    // dump headers
    rsp.headers().set(headers);
    ChannelHandlerContext ctx = this.ctx;
    ctx.channel().attr(NettyRequest.NEED_FLUSH).set(false);

    ChannelPipeline pipeline = ctx.pipeline();
    boolean ssl = pipeline.get(SslHandler.class) != null;

    if (ssl) {
        // add chunker
        chunkHandler(pipeline);

        // Create the chunked input here already, to properly handle the IOException
        HttpChunkedInput chunkedInput = new HttpChunkedInput(
                new ChunkedNioFile(channel, offset, count, bufferSize));

        ctx.channel().eventLoop().execute(() -> {
            // send headers
            ctx.write(rsp);
            // chunked file
            keepAlive(ctx.writeAndFlush(chunkedInput));
        });
    } else {
        ctx.channel().eventLoop().execute(() -> {
            // send headers
            ctx.write(rsp);
            // file region
            ctx.write(new DefaultFileRegion(channel, offset, count));
            keepAlive(ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT));
        });
    }

    committed = true;

}