Search in sources :

Example 6 with ByteBufferList

use of com.koushikdutta.async.ByteBufferList in project AndroidAsync by koush.

the class NetworkEventReporterWrapper method interpretResponseEmitter.

public DataEmitter interpretResponseEmitter(final String requestId, @Nullable DataEmitter body, final boolean b64Encode) {
    final NetworkPeerManager peerManager = getPeerManagerIfEnabled();
    if (peerManager == null)
        return null;
    final WritableByteChannel channel;
    try {
        if (b64Encode) {
            final Base64OutputStream b64out = new Base64OutputStream(peerManager.getResponseBodyFileManager().openResponseBodyFile(requestId, false), Base64.DEFAULT);
            channel = Channels.newChannel(b64out);
        } else {
            channel = ((FileOutputStream) peerManager.getResponseBodyFileManager().openResponseBodyFile(requestId, false)).getChannel();
        }
    } catch (IOException e) {
        return null;
    }
    FilteredDataEmitter ret = new FilteredDataEmitter() {

        ByteBufferList pending = new ByteBufferList();

        @Override
        protected void report(Exception e) {
            super.report(e);
            StreamUtility.closeQuietly(channel);
            if (e == null)
                responseReadFinished(requestId);
            else
                responseReadFailed(requestId, e.toString());
        }

        @Override
        public void onDataAvailable(DataEmitter emitter, ByteBufferList bb) {
            int amount = bb.remaining();
            ByteBuffer[] original = bb.getAllArray();
            ByteBuffer[] copy = new ByteBuffer[original.length];
            for (int i = 0; i < original.length; i++) {
                copy[i] = original[i].duplicate();
            }
            try {
                for (ByteBuffer c : copy) {
                    channel.write(c);
                }
            } catch (IOException ignored) {
                StreamUtility.closeQuietly(channel);
            }
            pending.addAll(original);
            dataReceived(requestId, amount, amount);
            super.onDataAvailable(emitter, pending);
        }
    };
    ret.setDataEmitter(body);
    return ret;
}
Also used : FilteredDataEmitter(com.koushikdutta.async.FilteredDataEmitter) ByteBufferList(com.koushikdutta.async.ByteBufferList) NetworkPeerManager(com.facebook.stetho.inspector.network.NetworkPeerManager) DataEmitter(com.koushikdutta.async.DataEmitter) FilteredDataEmitter(com.koushikdutta.async.FilteredDataEmitter) WritableByteChannel(java.nio.channels.WritableByteChannel) IOException(java.io.IOException) Base64OutputStream(android.util.Base64OutputStream) ByteBuffer(java.nio.ByteBuffer) IOException(java.io.IOException)

Example 7 with ByteBufferList

use of com.koushikdutta.async.ByteBufferList in project AndroidAsync by koush.

the class HttpClientTests method testGithubRandomDataWithFuture.

public void testGithubRandomDataWithFuture() throws Exception {
    final Md5 md5 = Md5.createInstance();
    Future<ByteBufferList> bb = client.executeByteBufferList(new AsyncHttpGet(github), null);
    md5.update(bb.get(TIMEOUT, TimeUnit.MILLISECONDS));
    assertEquals(md5.digest(), dataNameAndHash);
}
Also used : AsyncHttpGet(com.koushikdutta.async.http.AsyncHttpGet) ByteBufferList(com.koushikdutta.async.ByteBufferList)

Example 8 with ByteBufferList

use of com.koushikdutta.async.ByteBufferList in project AndroidAsync by koush.

the class ByteUtilTests method testPushParserTapUntil.

public void testPushParserTapUntil() {
    valRead = 0;
    FilteredDataEmitter mock = new FilteredDataEmitter() {

        @Override
        public boolean isPaused() {
            return false;
        }
    };
    new PushParser(mock).until((byte) 0, new DataCallback.NullDataCallback()).readInt().tap(new TapCallback() {

        public void parsed(int arg) {
            valRead = arg;
        }
    });
    byte[] bytes = new byte[] { 5, 5, 5, 5, 0, 10, 5, 5, 5 };
    Util.emitAllData(mock, new ByteBufferList(bytes));
    assertEquals(valRead, 0x0A050505);
}
Also used : FilteredDataEmitter(com.koushikdutta.async.FilteredDataEmitter) PushParser(com.koushikdutta.async.PushParser) ByteBufferList(com.koushikdutta.async.ByteBufferList) DataCallback(com.koushikdutta.async.callback.DataCallback) TapCallback(com.koushikdutta.async.TapCallback)

Example 9 with ByteBufferList

use of com.koushikdutta.async.ByteBufferList in project AndroidAsync by koush.

the class CacheTests method testFilteredDataEmitter.

public void testFilteredDataEmitter() throws Exception {
    final Semaphore semaphore = new Semaphore(0);
    FilteredDataEmitter f = new FilteredDataEmitter() {

        @Override
        public boolean isPaused() {
            return false;
        }
    };
    f.setDataCallback(new DataCallback() {

        @Override
        public void onDataAvailable(DataEmitter emitter, ByteBufferList bb) {
            assertEquals(bb.readString(), "hello");
            bb.recycle();
            semaphore.release();
        }
    });
    f.onDataAvailable(f, new ByteBufferList().add(ByteBuffer.wrap("hello".getBytes())));
    assertTrue("timeout", semaphore.tryAcquire(TIMEOUT, TimeUnit.MILLISECONDS));
    f.setDataCallback(new DataCallback() {

        @Override
        public void onDataAvailable(DataEmitter emitter, ByteBufferList bb) {
            fail();
        }
    });
    f.close();
    f.onDataAvailable(f, new ByteBufferList().add(ByteBuffer.wrap("hello".getBytes())));
}
Also used : FilteredDataEmitter(com.koushikdutta.async.FilteredDataEmitter) ByteBufferList(com.koushikdutta.async.ByteBufferList) DataEmitter(com.koushikdutta.async.DataEmitter) FilteredDataEmitter(com.koushikdutta.async.FilteredDataEmitter) Semaphore(java.util.concurrent.Semaphore) DataCallback(com.koushikdutta.async.callback.DataCallback)

Example 10 with ByteBufferList

use of com.koushikdutta.async.ByteBufferList 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

ByteBufferList (com.koushikdutta.async.ByteBufferList)39 DataEmitter (com.koushikdutta.async.DataEmitter)13 DataCallback (com.koushikdutta.async.callback.DataCallback)13 FilteredDataEmitter (com.koushikdutta.async.FilteredDataEmitter)10 CompletedCallback (com.koushikdutta.async.callback.CompletedCallback)10 ByteBuffer (java.nio.ByteBuffer)10 IOException (java.io.IOException)6 Semaphore (java.util.concurrent.Semaphore)5 PushParser (com.koushikdutta.async.PushParser)4 ExecutionException (java.util.concurrent.ExecutionException)4 JsonObject (com.google.gson.JsonObject)3 SimpleFuture (com.koushikdutta.async.future.SimpleFuture)3 HttpConnectCallback (com.koushikdutta.async.http.callback.HttpConnectCallback)3 GsonObjectParser (com.koushikdutta.ion.gson.GsonObjectParser)3 TimeoutException (java.util.concurrent.TimeoutException)3 BEncodedDictionary (org.cyanogenmod.pushsms.bencode.BEncodedDictionary)3 RemoteException (android.os.RemoteException)2 JsonParseException (com.google.gson.JsonParseException)2 LineEmitter (com.koushikdutta.async.LineEmitter)2 TapCallback (com.koushikdutta.async.TapCallback)2