Search in sources :

Example 1 with FileChunkedStrategy

use of reactor.ipc.netty.channel.data.FileChunkedStrategy in project reactor-netty by reactor.

the class NettyOutbound method sendFileChunked.

default NettyOutbound sendFileChunked(Path file, long position, long count) {
    Objects.requireNonNull(file);
    final FileChunkedStrategy strategy = getFileChunkedStrategy();
    final boolean needChunkedWriteHandler = context().channel().pipeline().get(NettyPipeline.ChunkedWriter) == null;
    if (needChunkedWriteHandler) {
        strategy.preparePipeline(context());
    }
    return then(Mono.using(() -> FileChannel.open(file, StandardOpenOption.READ), fc -> {
        try {
            ChunkedInput<?> message = strategy.chunkFile(fc);
            return FutureMono.from(context().channel().writeAndFlush(message));
        } catch (Exception e) {
            return Mono.error(e);
        }
    }, fc -> {
        try {
            fc.close();
        } catch (IOException ioe) {
        /*IGNORE*/
        } finally {
            strategy.cleanupPipeline(context());
        }
    }));
}
Also used : ByteBufAllocator(io.netty.buffer.ByteBufAllocator) Unpooled(io.netty.buffer.Unpooled) Charset(java.nio.charset.Charset) ByteBuf(io.netty.buffer.ByteBuf) Path(java.nio.file.Path) Subscriber(org.reactivestreams.Subscriber) FileChunkedStrategy(reactor.ipc.netty.channel.data.FileChunkedStrategy) Files(java.nio.file.Files) Publisher(org.reactivestreams.Publisher) StandardOpenOption(java.nio.file.StandardOpenOption) IOException(java.io.IOException) Mono(reactor.core.publisher.Mono) File(java.io.File) Objects(java.util.Objects) Consumer(java.util.function.Consumer) Channel(io.netty.channel.Channel) ChunkedNioFile(io.netty.handler.stream.ChunkedNioFile) Flux(reactor.core.publisher.Flux) SslHandler(io.netty.handler.ssl.SslHandler) DefaultFileRegion(io.netty.channel.DefaultFileRegion) WritableByteChannel(java.nio.channels.WritableByteChannel) ChunkedInput(io.netty.handler.stream.ChunkedInput) Exceptions(reactor.core.Exceptions) FileChannel(java.nio.channels.FileChannel) AbstractFileChunkedStrategy(reactor.ipc.netty.channel.data.AbstractFileChunkedStrategy) ChunkedInput(io.netty.handler.stream.ChunkedInput) FileChunkedStrategy(reactor.ipc.netty.channel.data.FileChunkedStrategy) AbstractFileChunkedStrategy(reactor.ipc.netty.channel.data.AbstractFileChunkedStrategy) IOException(java.io.IOException) IOException(java.io.IOException)

Aggregations

ByteBuf (io.netty.buffer.ByteBuf)1 ByteBufAllocator (io.netty.buffer.ByteBufAllocator)1 Unpooled (io.netty.buffer.Unpooled)1 Channel (io.netty.channel.Channel)1 DefaultFileRegion (io.netty.channel.DefaultFileRegion)1 SslHandler (io.netty.handler.ssl.SslHandler)1 ChunkedInput (io.netty.handler.stream.ChunkedInput)1 ChunkedNioFile (io.netty.handler.stream.ChunkedNioFile)1 File (java.io.File)1 IOException (java.io.IOException)1 FileChannel (java.nio.channels.FileChannel)1 WritableByteChannel (java.nio.channels.WritableByteChannel)1 Charset (java.nio.charset.Charset)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1 StandardOpenOption (java.nio.file.StandardOpenOption)1 Objects (java.util.Objects)1 Consumer (java.util.function.Consumer)1 Publisher (org.reactivestreams.Publisher)1 Subscriber (org.reactivestreams.Subscriber)1