Search in sources :

Example 31 with ByteBufferList

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

the class HttpClientTests method testGithubRandomData.

public void testGithubRandomData() throws Exception {
    final Semaphore semaphore = new Semaphore(0);
    final Md5 md5 = Md5.createInstance();
    AsyncHttpGet get = new AsyncHttpGet(github);
    get.setLogging("AsyncTest", Log.VERBOSE);
    client.execute(get, new HttpConnectCallback() {

        @Override
        public void onConnectCompleted(Exception ex, AsyncHttpResponse response) {
            assertNull(ex);
            // make sure gzip decoding works, as that is generally what github sends.
            // this broke sometime in 03/2014
            //                Assert.assertEquals("gzip", response.getHeaders().getContentEncoding());
            response.setDataCallback(new DataCallback() {

                @Override
                public void onDataAvailable(DataEmitter emitter, ByteBufferList bb) {
                    md5.update(bb);
                }
            });
            response.setEndCallback(new CompletedCallback() {

                @Override
                public void onCompleted(Exception ex) {
                    semaphore.release();
                }
            });
        }
    });
    assertTrue("timeout", semaphore.tryAcquire(TIMEOUT, TimeUnit.MILLISECONDS));
    assertEquals(md5.digest(), dataNameAndHash);
}
Also used : AsyncHttpGet(com.koushikdutta.async.http.AsyncHttpGet) CompletedCallback(com.koushikdutta.async.callback.CompletedCallback) AsyncHttpResponse(com.koushikdutta.async.http.AsyncHttpResponse) ByteBufferList(com.koushikdutta.async.ByteBufferList) HttpConnectCallback(com.koushikdutta.async.http.callback.HttpConnectCallback) DataEmitter(com.koushikdutta.async.DataEmitter) Semaphore(java.util.concurrent.Semaphore) DataCallback(com.koushikdutta.async.callback.DataCallback) TimeoutException(java.util.concurrent.TimeoutException) CancellationException(java.util.concurrent.CancellationException) ExecutionException(java.util.concurrent.ExecutionException)

Example 32 with ByteBufferList

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

the class HttpClientTests method testClockworkMod.

public void testClockworkMod() throws Exception {
    final Semaphore semaphore = new Semaphore(0);
    final Md5 md5 = Md5.createInstance();
    client.execute("http://www.clockworkmod.com", new HttpConnectCallback() {

        @Override
        public void onConnectCompleted(Exception ex, AsyncHttpResponse response) {
            // make sure gzip decoding works, as that is generally what github sends.
            Assert.assertEquals("gzip", response.headers().get("Content-Encoding"));
            response.setDataCallback(new DataCallback() {

                @Override
                public void onDataAvailable(DataEmitter emitter, ByteBufferList bb) {
                    md5.update(bb);
                }
            });
            response.setEndCallback(new CompletedCallback() {

                @Override
                public void onCompleted(Exception ex) {
                    semaphore.release();
                }
            });
        }
    });
    assertTrue("timeout", semaphore.tryAcquire(TIMEOUT, TimeUnit.MILLISECONDS));
}
Also used : CompletedCallback(com.koushikdutta.async.callback.CompletedCallback) AsyncHttpResponse(com.koushikdutta.async.http.AsyncHttpResponse) ByteBufferList(com.koushikdutta.async.ByteBufferList) HttpConnectCallback(com.koushikdutta.async.http.callback.HttpConnectCallback) DataEmitter(com.koushikdutta.async.DataEmitter) Semaphore(java.util.concurrent.Semaphore) DataCallback(com.koushikdutta.async.callback.DataCallback) TimeoutException(java.util.concurrent.TimeoutException) CancellationException(java.util.concurrent.CancellationException) ExecutionException(java.util.concurrent.ExecutionException)

Example 33 with ByteBufferList

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

the class GsonTests method testParserCastingSuccess.

public void testParserCastingSuccess() throws Exception {
    ByteBufferList b = new ByteBufferList(ByteBuffer.wrap("{}".getBytes()));
    FilteredDataEmitter emitter = new FilteredDataEmitter() {

        @Override
        public boolean isPaused() {
            return false;
        }
    };
    GsonObjectParser g = new GsonObjectParser();
    Future<JsonObject> ret = g.parse(emitter);
    emitter.onDataAvailable(emitter, b);
    emitter.getEndCallback().onCompleted(null);
    JsonObject j = ret.get();
    assertNotNull(j);
}
Also used : FilteredDataEmitter(com.koushikdutta.async.FilteredDataEmitter) GsonObjectParser(com.koushikdutta.ion.gson.GsonObjectParser) ByteBufferList(com.koushikdutta.async.ByteBufferList) JsonObject(com.google.gson.JsonObject)

Example 34 with ByteBufferList

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

the class StreamTests method setUp.

@Override
protected void setUp() throws Exception {
    super.setUp();
    AsyncHttpServer server = new AsyncHttpServer();
    port = server.listen(0).getLocalPort();
    server.get("/", new HttpServerRequestCallback() {

        @Override
        public void onRequest(AsyncHttpServerRequest request, final AsyncHttpServerResponse response) {
            response.code(200);
            ByteBuffer b = ByteBufferList.obtain(random.length);
            b.put(random);
            b.flip();
            ByteBufferList list = new ByteBufferList(b);
            Util.writeAll(response, list, new CompletedCallback() {

                @Override
                public void onCompleted(Exception ex) {
                    response.end();
                }
            });
        }
    });
}
Also used : CompletedCallback(com.koushikdutta.async.callback.CompletedCallback) HttpServerRequestCallback(com.koushikdutta.async.http.server.HttpServerRequestCallback) ByteBufferList(com.koushikdutta.async.ByteBufferList) AsyncHttpServerRequest(com.koushikdutta.async.http.server.AsyncHttpServerRequest) AsyncHttpServer(com.koushikdutta.async.http.server.AsyncHttpServer) AsyncHttpServerResponse(com.koushikdutta.async.http.server.AsyncHttpServerResponse) ByteBuffer(java.nio.ByteBuffer)

Example 35 with ByteBufferList

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

the class LoadBitmap method onCompleted.

@Override
public void onCompleted(Exception e, final Response<ByteBufferList> response) {
    if (e == null)
        e = response.getException();
    if (e != null) {
        report(e, null);
        return;
    }
    final ByteBufferList result = response.getResult();
    if (ion.bitmapsPending.tag(key) != this) {
        result.recycle();
        return;
    }
    Ion.getBitmapLoadExecutorService().execute(new Runnable() {

        @Override
        public void run() {
            if (ion.bitmapsPending.tag(key) != LoadBitmap.this) {
                result.recycle();
                return;
            }
            ByteBuffer bb = null;
            try {
                bb = result.getAll();
                Bitmap bitmap;
                GifDecoder gifDecoder;
                BitmapFactory.Options options = ion.bitmapCache.prepareBitmapOptions(bb.array(), bb.arrayOffset() + bb.position(), bb.remaining(), resizeWidth, resizeHeight);
                final Point size = new Point(options.outWidth, options.outHeight);
                if (animateGif && TextUtils.equals("image/gif", options.outMimeType)) {
                    //                        new GifDecoder(bb.array(), bb.arrayOffset() + bb.position(), bb.remaining());
                    gifDecoder = new GifDecoder(bb.array(), bb.arrayOffset() + bb.position(), bb.remaining());
                    GifFrame frame = gifDecoder.nextFrame();
                    bitmap = frame.image;
                    // the byte buffer is needed by the decoder
                    bb = null;
                } else {
                    bitmap = IonBitmapCache.loadBitmap(bb.array(), bb.arrayOffset() + bb.position(), bb.remaining(), options);
                    gifDecoder = null;
                    if (bitmap == null)
                        throw new Exception("failed to load bitmap");
                }
                BitmapInfo info = new BitmapInfo(key, options.outMimeType, bitmap, size);
                info.gifDecoder = gifDecoder;
                info.servedFrom = response.getServedFrom();
                report(null, info);
            } catch (OutOfMemoryError e) {
                report(new Exception(e), null);
            } catch (Exception e) {
                report(e, null);
            } finally {
                ByteBufferList.reclaim(bb);
            }
        }
    });
}
Also used : GifDecoder(com.koushikdutta.ion.gif.GifDecoder) Bitmap(android.graphics.Bitmap) ByteBufferList(com.koushikdutta.async.ByteBufferList) GifFrame(com.koushikdutta.ion.gif.GifFrame) Point(android.graphics.Point) ByteBuffer(java.nio.ByteBuffer) BitmapInfo(com.koushikdutta.ion.bitmap.BitmapInfo)

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