Search in sources :

Example 6 with WritableCallback

use of com.koushikdutta.async.callback.WritableCallback in project AndroidAsync by koush.

the class Util method pump.

public static void pump(final InputStream is, final long max, final DataSink ds, final CompletedCallback callback) {
    final CompletedCallback wrapper = new CompletedCallback() {

        boolean reported;

        @Override
        public void onCompleted(Exception ex) {
            if (reported)
                return;
            reported = true;
            callback.onCompleted(ex);
        }
    };
    final WritableCallback cb = new WritableCallback() {

        int totalRead = 0;

        private void cleanup() {
            ds.setClosedCallback(null);
            ds.setWriteableCallback(null);
            pending.recycle();
            StreamUtility.closeQuietly(is);
        }

        ByteBufferList pending = new ByteBufferList();

        Allocator allocator = new Allocator();

        @Override
        public void onWriteable() {
            try {
                do {
                    if (!pending.hasRemaining()) {
                        ByteBuffer b = allocator.allocate();
                        long toRead = Math.min(max - totalRead, b.capacity());
                        int read = is.read(b.array(), 0, (int) toRead);
                        if (read == -1 || totalRead == max) {
                            cleanup();
                            wrapper.onCompleted(null);
                            return;
                        }
                        allocator.track(read);
                        totalRead += read;
                        b.position(0);
                        b.limit(read);
                        pending.add(b);
                    }
                    ds.write(pending);
                } while (!pending.hasRemaining());
            } catch (Exception e) {
                cleanup();
                wrapper.onCompleted(e);
            }
        }
    };
    ds.setWriteableCallback(cb);
    ds.setClosedCallback(wrapper);
    cb.onWriteable();
}
Also used : Allocator(com.koushikdutta.async.util.Allocator) CompletedCallback(com.koushikdutta.async.callback.CompletedCallback) WritableCallback(com.koushikdutta.async.callback.WritableCallback) ByteBuffer(java.nio.ByteBuffer) IOException(java.io.IOException)

Aggregations

WritableCallback (com.koushikdutta.async.callback.WritableCallback)6 CompletedCallback (com.koushikdutta.async.callback.CompletedCallback)4 IOException (java.io.IOException)2 AsyncServer (com.koushikdutta.async.AsyncServer)1 ByteBufferList (com.koushikdutta.async.ByteBufferList)1 DataSink (com.koushikdutta.async.DataSink)1 DataCallback (com.koushikdutta.async.callback.DataCallback)1 ChunkedOutputFilter (com.koushikdutta.async.http.filter.ChunkedOutputFilter)1 Allocator (com.koushikdutta.async.util.Allocator)1 FileNotFoundException (java.io.FileNotFoundException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 ByteBuffer (java.nio.ByteBuffer)1