Search in sources :

Example 16 with DefaultFileRegion

use of org.apache.flink.shaded.netty4.io.netty.channel.DefaultFileRegion in project crate by crate.

the class HttpBlobHandler method transferFile.

private ChannelFuture transferFile(final String digest, RandomAccessFile raf, long position, long count) throws IOException {
    Channel channel = ctx.channel();
    final ChannelFuture fileFuture;
    final ChannelFuture endMarkerFuture;
    if (sslEnabled) {
        HttpChunkedInput httpChunkedInput = new HttpChunkedInput(new ChunkedFile(raf, 0, count, HTTPS_CHUNK_SIZE));
        fileFuture = channel.writeAndFlush(httpChunkedInput, ctx.newProgressivePromise());
        // HttpChunkedInput also writes the end marker (LastHttpContent) for us.
        endMarkerFuture = fileFuture;
    } else {
        FileRegion region = new DefaultFileRegion(raf.getChannel(), position, count);
        fileFuture = channel.write(region, ctx.newProgressivePromise());
        // Flushes and sets the ending marker
        endMarkerFuture = channel.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);
    }
    fileFuture.addListener(new ChannelProgressiveFutureListener() {

        @Override
        public void operationProgressed(ChannelProgressiveFuture future, long progress, long total) throws Exception {
            LOGGER.debug("transferFile digest={} progress={} total={}", digest, progress, total);
        }

        @Override
        public void operationComplete(ChannelProgressiveFuture future) throws Exception {
            LOGGER.trace("transferFile operationComplete");
        }
    });
    return endMarkerFuture;
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) HttpChunkedInput(io.netty.handler.codec.http.HttpChunkedInput) Channel(io.netty.channel.Channel) ChunkedFile(io.netty.handler.stream.ChunkedFile) DefaultFileRegion(io.netty.channel.DefaultFileRegion) FileRegion(io.netty.channel.FileRegion) ChannelProgressiveFuture(io.netty.channel.ChannelProgressiveFuture) DefaultFileRegion(io.netty.channel.DefaultFileRegion) ChannelProgressiveFutureListener(io.netty.channel.ChannelProgressiveFutureListener) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) DigestMismatchException(io.crate.blob.exceptions.DigestMismatchException) NotSslRecordException(io.netty.handler.ssl.NotSslRecordException) BlobsDisabledException(io.crate.blob.v2.BlobsDisabledException) MissingHTTPEndpointException(io.crate.blob.exceptions.MissingHTTPEndpointException) ClosedChannelException(java.nio.channels.ClosedChannelException) DigestNotFoundException(io.crate.blob.exceptions.DigestNotFoundException) IOException(java.io.IOException) EsRejectedExecutionException(org.elasticsearch.common.util.concurrent.EsRejectedExecutionException)

Example 17 with DefaultFileRegion

use of org.apache.flink.shaded.netty4.io.netty.channel.DefaultFileRegion in project blade by biezhi.

the class OutputStreamWrapper method close.

@Override
public void close() throws IOException {
    try {
        this.flush();
        FileChannel file = new FileInputStream(this.file).getChannel();
        long fileLength = file.size();
        HttpResponse httpResponse = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
        httpResponse.headers().set(HttpConst.CONTENT_LENGTH, fileLength);
        httpResponse.headers().set(HttpConst.DATE, DateKit.gmtDate());
        httpResponse.headers().set(HttpConst.SERVER, "blade/" + Const.VERSION);
        boolean keepAlive = WebContext.request().keepAlive();
        if (keepAlive) {
            httpResponse.headers().set(HttpConst.CONNECTION, HttpConst.KEEP_ALIVE);
        }
        // Write the initial line and the header.
        ctx.write(httpResponse);
        ctx.write(new DefaultFileRegion(file, 0, fileLength), ctx.newProgressivePromise());
        // Write the end marker.
        ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);
    } finally {
        if (null != outputStream) {
            outputStream.close();
        }
    }
}
Also used : FileChannel(java.nio.channels.FileChannel) DefaultFileRegion(io.netty.channel.DefaultFileRegion)

Aggregations

DefaultFileRegion (io.netty.channel.DefaultFileRegion)14 RandomAccessFile (java.io.RandomAccessFile)11 File (java.io.File)9 IOException (java.io.IOException)8 FileNotFoundException (java.io.FileNotFoundException)7 ChunkedFile (io.netty.handler.stream.ChunkedFile)6 ChannelFuture (io.netty.channel.ChannelFuture)5 FileRegion (io.netty.channel.FileRegion)4 DefaultHttpResponse (io.netty.handler.codec.http.DefaultHttpResponse)4 HttpChunkedInput (io.netty.handler.codec.http.HttpChunkedInput)4 HttpResponse (io.netty.handler.codec.http.HttpResponse)4 URL (java.net.URL)4 FileChannel (java.nio.channels.FileChannel)4 SimpleDateFormat (java.text.SimpleDateFormat)4 Date (java.util.Date)4 Channel (io.netty.channel.Channel)3 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)3 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)3 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)3 SslHandler (io.netty.handler.ssl.SslHandler)3