Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Created this as a template for "async" decompressing - check for more details
import com.ning.compress.DataHandler;
import com.ning.compress.UncompressorOutputStream;
import com.ning.compress.gzip.GZIPUncompressor;
import com.ning.compress.lzf.LZFUncompressor;
import io.netty.handler.codec.http.HttpHeaders;
import org.asynchttpclient.AsyncHandler;
import org.asynchttpclient.HttpResponseBodyPart;
import org.asynchttpclient.HttpResponseStatus;
import java.util.Optional;
import static org.asynchttpclient.AsyncHandler.State.ABORT;
import static org.asynchttpclient.AsyncHandler.State.CONTINUE;
* reference:
* 1. Created this as a template for "async" decompressing
* 2. modify it to support async file download [again sample in above link]
* 3. use {mmap raf, buffer, /dev/shm file} pool as possible enhancement
* if bounded upstreams [disruptor?] or bounded number of connections?
// dependencies:
// "org.asynchttpclient:async-http-client:2.4.4",
// "com.ning:compress-lzf:1.0.4
public class UncompressingFileHandler implements AsyncHandler<Optional<File>>, DataHandler {
private File file;
private final OutputStream out;
private boolean failed = false;
private UncompressorOutputStream uncompressingStream;
public UncompressingFileHandler(File f) throws IOException {
file = f;
out = new FileOutputStream(f);
public State onBodyPartReceived(HttpResponseBodyPart part) throws IOException {
if (!failed) {
// if compressed, pass through uncompressing stream
if (uncompressingStream != null) {
} else { // otherwise write directly
return CONTINUE;
public Optional<File> onCompleted() throws IOException {
if (uncompressingStream != null) { uncompressingStream.close();}
if (failed) {
return Optional.empty();
return Optional.ofNullable(file);
public State onHeadersReceived(HttpHeaders h) throws Exception {
// must verify that we are getting compressed stuff here:
String compression = h.get("Content-Encoding");
if (compression != null) {
switch (compression) {
case "lzf":
uncompressingStream = new UncompressorOutputStream(new LZFUncompressor(this));
case "gzip":
uncompressingStream = new UncompressorOutputStream(new GZIPUncompressor(this));
// unsupported compression scheme or, no compression ==> do nothing
// nothing to check here as of yet
return CONTINUE;
public State onStatusReceived(HttpResponseStatus status) {
failed = (status.getStatusCode() != 200);
return failed ? ABORT : CONTINUE;
public void onThrowable(Throwable t) {
failed = true;
public boolean handleData(byte[] buffer, int offset, int len) throws IOException {
// DataHandler implementation for Uncompressor; called with uncompressed content:
out.write(buffer, offset, len);
return true;
public void allDataHandled() throws IOException {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.