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) throws IOException 

Source Link

Document

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

Usage

From source file:divconq.web.asset.AssetInfo.java

License:Open Source License

public ChunkedInput<HttpContent> getChunks() {
    try {/*  w  w w. j a  v  a  2s  .c om*/
        return new HttpChunkedInput(new ChunkedNioFile(this.fpath.toFile()));
    } catch (IOException x) {
        // TODO improve support
    }

    return null;
}

From source file:io.netty.handler.stream.ChunkedWriteHandlerTest.java

License:Apache License

@Test
public void testChunkedNioFile() throws IOException {
    check(new ChunkedNioFile(TMP));

    check(new ChunkedNioFile(TMP), new ChunkedNioFile(TMP), new ChunkedNioFile(TMP));
}

From source file:jj.http.server.HttpServerResponseImpl.java

License:Apache License

/**
 * actually writes the stuff to the channel
 * //from  ww  w  .j  a v a 2 s .c o  m
 * how to test this? 
 */
protected HttpServerResponse doSendTransferableResource(TransferableResource resource) throws IOException {

    ctx.write(response);
    ctx.write(new ChunkedNioFile(resource.fileChannel()));
    maybeClose(ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT));

    // configuration can decide if we're doing zero-copy or chunking?
    // can i even make zero-copy work again? haha
    //maybeClose(ctx.writeAndFlush(new DefaultFileRegion(resource.fileChannel(), 0, resource.size())));

    markCommitted();
    return this;
}

From source file:org.waarp.gateway.kernel.http.HttpWriteCacheEnable.java

License:Open Source License

/**
 * Write a file, taking into account cache enabled and removing session cookie
 * /*from   w  ww. ja va  2 s  .co m*/
 * @param request
 * @param ctx
 * @param filename
 * @param cookieNameToRemove
 */
public static void writeFile(HttpRequest request, ChannelHandlerContext ctx, String filename,
        String cookieNameToRemove) {
    // Convert the response content to a ByteBuf.
    HttpResponse response;
    File file = new File(filename);
    if (!file.isFile() || !file.canRead()) {
        response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.NOT_FOUND);
        handleCookies(request, response, cookieNameToRemove);
        ctx.writeAndFlush(response);
        return;
    }
    DateFormat rfc1123Format = new SimpleDateFormat(RFC1123_PATTERN, LOCALE_US);
    rfc1123Format.setTimeZone(GMT_ZONE);
    Date lastModifDate = new Date(file.lastModified());
    if (request.headers().contains(HttpHeaderNames.IF_MODIFIED_SINCE)) {
        String sdate = request.headers().get(HttpHeaderNames.IF_MODIFIED_SINCE);
        try {
            Date ifmodif = rfc1123Format.parse(sdate);
            if (ifmodif.after(lastModifDate)) {
                response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.NOT_MODIFIED);
                handleCookies(request, response, cookieNameToRemove);
                ctx.writeAndFlush(response);
                return;
            }
        } catch (ParseException e) {
        }
    }
    long size = file.length();
    ChunkedNioFile nioFile;
    try {
        nioFile = new ChunkedNioFile(file);
    } catch (IOException e) {
        response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.NOT_FOUND);
        handleCookies(request, response, cookieNameToRemove);
        ctx.writeAndFlush(response);
        return;
    }
    response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);

    String type = mimetypesFileTypeMap.getContentType(filename);
    response.headers().set(HttpHeaderNames.CONTENT_TYPE, type);
    response.headers().set(HttpHeaderNames.CACHE_CONTROL, cache_control);
    response.headers().set(HttpHeaderNames.LAST_MODIFIED, rfc1123Format.format(lastModifDate));
    handleCookies(request, response, cookieNameToRemove);
    // Write the response.
    ctx.write(response);
    ctx.write(new HttpChunkedInput(nioFile));
    ChannelFuture future = ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);
    if (!HttpUtil.isKeepAlive(request)) {
        // Close the connection when the whole content is written out.
        future.addListener(ChannelFutureListener.CLOSE);
    }
}