本文整理汇总了Java中io.netty.handler.stream.ChunkedNioFile类的典型用法代码示例。如果您正苦于以下问题:Java ChunkedNioFile类的具体用法?Java ChunkedNioFile怎么用?Java ChunkedNioFile使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ChunkedNioFile类属于io.netty.handler.stream包,在下文中一共展示了ChunkedNioFile类的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: write
import io.netty.handler.stream.ChunkedNioFile; //导入依赖的package包/类
@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 ProgressListener(future.getAsyncHandler(), future, false, getContentLength()));
channel.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);
}
示例2: chunkFile
import io.netty.handler.stream.ChunkedNioFile; //导入依赖的package包/类
@Override
public ChunkedInput<ByteBuf> chunkFile(FileChannel fileChannel) {
try {
//TODO tune the chunk size
return new ChunkedNioFile(fileChannel, 1024);
}
catch (IOException e) {
throw Exceptions.propagate(e);
}
}
示例3: chunkFile
import io.netty.handler.stream.ChunkedNioFile; //导入依赖的package包/类
@Override
public ChunkedInput<HttpContent> chunkFile(FileChannel fileChannel) {
try {
//TODO tune the chunk size
return new HttpChunkedInput(new ChunkedNioFile(fileChannel, 1024));
}
catch (IOException e) {
throw Exceptions.propagate(e);
}
}
示例4: chunkFile
import io.netty.handler.stream.ChunkedNioFile; //导入依赖的package包/类
@Override
public ChunkedInput<ByteBuf> chunkFile(FileChannel fileChannel) {
try {
return new ChunkedNioFile(fileChannel, 1024);
}
catch (IOException e) {
throw Exceptions.propagate(e);
}
}
示例5: channelRead0
import io.netty.handler.stream.ChunkedNioFile; //导入依赖的package包/类
@Override
public void channelRead0(ChannelHandlerContext ctx, FullHttpRequest request) throws Exception {
if (wsUri.equalsIgnoreCase(request.getUri())) {
ctx.fireChannelRead(request.retain()); //2
} else {
if (HttpHeaders.is100ContinueExpected(request)) {
send100Continue(ctx); //3
}
RandomAccessFile file = new RandomAccessFile(INDEX, "r");//4
HttpResponse response = new DefaultHttpResponse(request.getProtocolVersion(), HttpResponseStatus.OK);
response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "text/html; charset=UTF-8");
boolean keepAlive = HttpHeaders.isKeepAlive(request);
if (keepAlive) { //5
response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, file.length());
response.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
}
ctx.write(response); //6
if (ctx.pipeline().get(SslHandler.class) == null) { //7
ctx.write(new DefaultFileRegion(file.getChannel(), 0, file.length()));
} else {
ctx.write(new ChunkedNioFile(file.getChannel()));
}
ChannelFuture future = ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT); //8
if (!keepAlive) {
future.addListener(ChannelFutureListener.CLOSE); //9
}
file.close();
}
}
示例6: channelRead0
import io.netty.handler.stream.ChunkedNioFile; //导入依赖的package包/类
@Override
protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest request) throws Exception {
if( wsURI.equalsIgnoreCase(request.getUri()) ) {
ctx.fireChannelRead(request.retain());
} else {
if( HttpHeaders.is100ContinueExpected(request) ) {
send100Continue(ctx);
}
try (
RandomAccessFile rFile = new RandomAccessFile(indexHTML, "r")
) {
HttpResponse response = new DefaultHttpResponse( request.getProtocolVersion(), HttpResponseStatus.OK );
response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "text/html; charset=UTF-8");
boolean keepAlive = HttpHeaders.isKeepAlive(request);
if( keepAlive ) {
response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, rFile.length());
response.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
}
ctx.write(response);
if( ctx.pipeline().get(SslHandler.class) == null ) {
ctx.write(new DefaultFileRegion(rFile.getChannel(), 0, rFile.length()));
} else {
ctx.write(new ChunkedNioFile(rFile.getChannel()));
}
ChannelFuture future = ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);
if( !keepAlive ) {
future.addListener(ChannelFutureListener.CLOSE);
}
}
}
}
示例7: messageReceived
import io.netty.handler.stream.ChunkedNioFile; //导入依赖的package包/类
@Override
protected void messageReceived(ChannelHandlerContext ctx, FullHttpRequest request) throws Exception {
//Check if the request is an WebSocket Upgrade request and if so retain it and pass it to the next ChannelInboundHandler in the ChannelPipeline.
if (wsUri.equalsIgnoreCase(request.getUri())) {
ctx.fireChannelRead(request.retain());
} else {
if (HttpHeaders.is100ContinueExpected(request)) {
//Handle 100 Continue requests to conform HTTP 1.1
send100Continue(ctx);
}
RandomAccessFile file = new RandomAccessFile("D:\\dev\\workspace\\javase-study\\netty-study\\src\\main\\resources\\index.html", "r");
HttpResponse response = new DefaultHttpResponse(request.getProtocolVersion(), HttpResponseStatus.OK);
response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "text/plan; charset=UTF-8");
boolean keeplive = HttpHeaders.isKeepAlive(request);
//Add needed headers depending of if keepalive is used or not
if (keeplive) {
response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, file.length());
response.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
}
//Write the HttpRequest to the client. Be aware that we use a HttpRequest and not a FullHttpRequest as it is only the first part of the request. Also we not use writeAndFlush(..) as this should be done later
ctx.write(response);
//Write the index.html to the client. Depending on if SslHandler is in the ChannelPipeline use DefaultFileRegion or ChunkedNioFile
if (ctx.pipeline().get(SslHandler.class) == null) {
ctx.write(new DefaultFileRegion(file.getChannel(), 0, file.length()));
} else {
ctx.write(new ChunkedNioFile(file.getChannel()));
}
//Write and flush the LastHttpContent to the client which marks the requests as complete
ChannelFuture cf = ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);
//Depending on if keepalive is used close the Channel after the write completes
if (!keeplive) {
cf.addListener(ChannelFutureListener.CLOSE);
}
}
}
示例8: execute
import io.netty.handler.stream.ChunkedNioFile; //导入依赖的package包/类
@Override
public void execute(WebContext ctx) throws Exception {
Response resp = ctx.getResponse();
resp.setHeader("Content-Type", this.mime);
resp.setDateHeader("Date", System.currentTimeMillis());
resp.setDateHeader("Last-Modified", this.file.getWhen());
resp.setHeader("X-UA-Compatible", "IE=Edge,chrome=1");
if (ctx.getRequest().hasHeader("If-Modified-Since")) {
long dd = this.file.getWhen() - ctx.getRequest().getDateHeader("If-Modified-Since");
// getDate does not return consistent results because milliseconds
// are not cleared correctly see:
// https://sourceforge.net/tracker/index.php?func=detail&aid=3162870&group_id=62369&atid=500353
// so ignore differences of less than 1000, they are false positives
if (dd < 1000) {
resp.setStatus(HttpResponseStatus.NOT_MODIFIED);
ctx.send();
return;
}
}
if (!ctx.getSite().getMimeCompress(this.mime))
resp.setHeader(HttpHeaders.Names.CONTENT_ENCODING, HttpHeaders.Values.IDENTITY);
ctx.sendStart(0);
// TODO send from memory cache if small enough
try {
ctx.send(new HttpChunkedInput(new ChunkedNioFile(this.file.getFilePath().toFile()))); // TODO not ideal, cleanup so direct reference to path is not needed
}
catch (IOException x) {
// TODO improve support
}
ctx.sendEnd();
}
示例9: testChunkedNioFile
import io.netty.handler.stream.ChunkedNioFile; //导入依赖的package包/类
@Test
public void testChunkedNioFile() throws IOException {
check(new HttpChunkedInput(new ChunkedNioFile(TMP)));
}
示例10: send
import io.netty.handler.stream.ChunkedNioFile; //导入依赖的package包/类
@Override
public void send(final FileChannel channel, final long offset, final long count)
throws Exception {
DefaultHttpResponse rsp = new DefaultHttpResponse(HttpVersion.HTTP_1_1, status);
headers.remove(HttpHeaderNames.TRANSFER_ENCODING);
headers.set(HttpHeaderNames.CONTENT_LENGTH, count);
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, ctx.voidPromise());
// chunked file
if (keepAlive) {
ctx.writeAndFlush(chunkedInput, ctx.voidPromise());
} else {
ctx.writeAndFlush(chunkedInput).addListener(CLOSE);
}
});
} else {
ctx.channel().eventLoop().execute(() -> {
// send headers
ctx.write(rsp, ctx.voidPromise());
// file region
ctx.write(new DefaultFileRegion(channel, offset, count), ctx.voidPromise());
if (keepAlive) {
ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT, ctx.voidPromise());
} else {
ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT).addListener(CLOSE);
}
});
}
committed = true;
}