org.jboss.netty.handler.stream.ChunkedFile Java Examples

The following examples show how to use org.jboss.netty.handler.stream.ChunkedFile. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HttpDataServerHandler.java    From incubator-tajo with Apache License 2.0 6 votes vote down vote up
private ChannelFuture sendFile(ChannelHandlerContext ctx, Channel ch, FileChunk file) throws IOException {
  RandomAccessFile raf;
  try {
    raf = new RandomAccessFile(file.getFile(), "r");
  } catch (FileNotFoundException fnfe) {
    return null;
  }

  ChannelFuture writeFuture;
  if (ch.getPipeline().get(SslHandler.class) != null) {
    // Cannot use zero-copy with HTTPS.
    writeFuture = ch.write(new ChunkedFile(raf, file.startOffset(), file.length(), 8192));
  } else {
    // No encryption - use zero-copy.
    final FileRegion region = new DefaultFileRegion(raf.getChannel(), file.startOffset(), file.length());
    writeFuture = ch.write(region);
    writeFuture.addListener(new ChannelFutureListener() {
      public void operationComplete(ChannelFuture future) {
        region.releaseExternalResources();
      }
    });
  }

  return writeFuture;
}
 
Example #2
Source File: HttpBlobHandler.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
private void fullContentResponse(HttpRequest request, String index, final String digest) throws  IOException {
    BlobShard blobShard = localBlobShard(index, digest);
    HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK);
    final RandomAccessFile raf = blobShard.blobContainer().getRandomAccessFile(digest);
    try {
        HttpHeaders.setContentLength(response, raf.length());
        setDefaultGetHeaders(response);
        LOGGER.trace("HttpResponse: {}", response);
        Channel channel = ctx.getChannel();
        channel.write(response);
        ChannelFuture writeFuture;
        if (sslEnabled) {
            // Cannot use zero-copy with HTTPS.
            writeFuture = channel.write(new ChunkedFile(raf, 0, raf.length(), 8192));
        } else {
            writeFuture = transferFile(digest, raf, 0, raf.length());
        }
        if (!HttpHeaders.isKeepAlive(request)) {
            writeFuture.addListener(ChannelFutureListener.CLOSE);
        }
    } catch (Throwable t) {
        /**
         * Make sure RandomAccessFile is closed when exception is raised.
         * In case of success, the ChannelFutureListener in "transferFile" will take care
         * that the resources are released.
         */
        raf.close();
        throw t;
    }
}
 
Example #3
Source File: HttpDataServerHandler.java    From incubator-tajo with Apache License 2.0 5 votes vote down vote up
private ChannelFuture sendFile(ChannelHandlerContext ctx,
                               Channel ch,
                               FileChunk file) throws IOException {
  RandomAccessFile raf;
  try {
    raf = new RandomAccessFile(file.getFile(), "r");
  } catch (FileNotFoundException fnfe) {
    return null;
  }

  ChannelFuture writeFuture;
  if (ch.getPipeline().get(SslHandler.class) != null) {
    // Cannot use zero-copy with HTTPS.
    writeFuture = ch.write(new ChunkedFile(raf, file.startOffset(),
        file.length(), 8192));
  } else {
    // No encryption - use zero-copy.
    final FileRegion region = new DefaultFileRegion(raf.getChannel(),
        file.startOffset(), file.length());
    writeFuture = ch.write(region);
    writeFuture.addListener(new ChannelFutureListener() {
      public void operationComplete(ChannelFuture future) {
        region.releaseExternalResources();
      }
    });
  }

  return writeFuture;
}