Search in sources :

Example 1 with OutputStreamDataCallback

use of com.koushikdutta.async.stream.OutputStreamDataCallback in project AndroidAsync by koush.

the class AsyncHttpClient method executeFile.

public Future<File> executeFile(AsyncHttpRequest req, final String filename, final FileCallback callback) {
    final File file = new File(filename);
    file.getParentFile().mkdirs();
    final OutputStream fout;
    try {
        fout = new BufferedOutputStream(new FileOutputStream(file), 8192);
    } catch (FileNotFoundException e) {
        SimpleFuture<File> ret = new SimpleFuture<File>();
        ret.setComplete(e);
        return ret;
    }
    final FutureAsyncHttpResponse cancel = new FutureAsyncHttpResponse();
    final SimpleFuture<File> ret = new SimpleFuture<File>() {

        @Override
        public void cancelCleanup() {
            try {
                cancel.get().setDataCallback(new DataCallback.NullDataCallback());
                cancel.get().close();
            } catch (Exception e) {
            }
            try {
                fout.close();
            } catch (Exception e) {
            }
            file.delete();
        }
    };
    ret.setParent(cancel);
    execute(req, 0, cancel, new HttpConnectCallback() {

        long mDownloaded = 0;

        @Override
        public void onConnectCompleted(Exception ex, final AsyncHttpResponse response) {
            if (ex != null) {
                try {
                    fout.close();
                } catch (IOException e) {
                }
                file.delete();
                invoke(callback, ret, response, ex, null);
                return;
            }
            invokeConnect(callback, response);
            final long contentLength = HttpUtil.contentLength(response.headers());
            response.setDataCallback(new OutputStreamDataCallback(fout) {

                @Override
                public void onDataAvailable(DataEmitter emitter, ByteBufferList bb) {
                    mDownloaded += bb.remaining();
                    super.onDataAvailable(emitter, bb);
                    invokeProgress(callback, response, mDownloaded, contentLength);
                }
            });
            response.setEndCallback(new CompletedCallback() {

                @Override
                public void onCompleted(Exception ex) {
                    try {
                        fout.close();
                    } catch (IOException e) {
                        ex = e;
                    }
                    if (ex != null) {
                        file.delete();
                        invoke(callback, ret, response, ex, null);
                    } else {
                        invoke(callback, ret, response, null, file);
                    }
                }
            });
        }
    });
    return ret;
}
Also used : CompletedCallback(com.koushikdutta.async.callback.CompletedCallback) ByteBufferList(com.koushikdutta.async.ByteBufferList) HttpConnectCallback(com.koushikdutta.async.http.callback.HttpConnectCallback) OutputStreamDataCallback(com.koushikdutta.async.stream.OutputStreamDataCallback) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) OutputStreamDataCallback(com.koushikdutta.async.stream.OutputStreamDataCallback) DataCallback(com.koushikdutta.async.callback.DataCallback) TimeoutException(java.util.concurrent.TimeoutException) AsyncSSLException(com.koushikdutta.async.AsyncSSLException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) FileOutputStream(java.io.FileOutputStream) DataEmitter(com.koushikdutta.async.DataEmitter) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) SimpleFuture(com.koushikdutta.async.future.SimpleFuture)

Aggregations

AsyncSSLException (com.koushikdutta.async.AsyncSSLException)1 ByteBufferList (com.koushikdutta.async.ByteBufferList)1 DataEmitter (com.koushikdutta.async.DataEmitter)1 CompletedCallback (com.koushikdutta.async.callback.CompletedCallback)1 DataCallback (com.koushikdutta.async.callback.DataCallback)1 SimpleFuture (com.koushikdutta.async.future.SimpleFuture)1 HttpConnectCallback (com.koushikdutta.async.http.callback.HttpConnectCallback)1 OutputStreamDataCallback (com.koushikdutta.async.stream.OutputStreamDataCallback)1 BufferedOutputStream (java.io.BufferedOutputStream)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 TimeoutException (java.util.concurrent.TimeoutException)1