Skip to content

Instantly share code, notes, and snippets.

@nickman
Created April 3, 2016 19:58
Show Gist options
  • Save nickman/8ad8f3294deba5f7a371b9bfb4cb7993 to your computer and use it in GitHub Desktop.
Save nickman/8ad8f3294deba5f7a371b9bfb4cb7993 to your computer and use it in GitHub Desktop.
final DefaultFileRegion region = new DefaultFileRegion(file.getChannel(), 0, length);
final ByteBuf fb = ctx().alloc().directBuffer((int)length);
region.transferTo(new WritableByteChannel(){
@Override
public boolean isOpen() {
return true;
}
@Override
public void close() throws IOException { /* No Op */}
@Override
public int write(final ByteBuffer src) throws IOException {
final int start = fb.writerIndex();
fb.writeBytes(src);
return fb.writerIndex() - start;
}
}, 0);
FullHttpResponse fhr = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, fb);
fhr.headers().add(request().headers());
final ChannelFuture future = ctx().writeAndFlush(fhr);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment