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;
}
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();
}
}
}
Aggregations