Search in sources :

Example 1 with FilteredDataEmitter

use of com.koushikdutta.async.FilteredDataEmitter 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 2 with FilteredDataEmitter

use of com.koushikdutta.async.FilteredDataEmitter 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 3 with FilteredDataEmitter

use of com.koushikdutta.async.FilteredDataEmitter 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 4 with FilteredDataEmitter

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

the class GsonTests method testParserCastingCallbackError.

public void testParserCastingCallbackError() 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);
    final Semaphore s = new Semaphore(0);
    ret.setCallback(new FutureCallback<JsonObject>() {

        @Override
        public void onCompleted(Exception e, JsonObject result) {
            assertNull(result);
            assertNotNull(e);
            assertTrue(e instanceof ClassCastException);
            s.release();
        }
    });
    s.acquire();
}
Also used : FilteredDataEmitter(com.koushikdutta.async.FilteredDataEmitter) GsonObjectParser(com.koushikdutta.ion.gson.GsonObjectParser) ByteBufferList(com.koushikdutta.async.ByteBufferList) JsonObject(com.google.gson.JsonObject) Semaphore(java.util.concurrent.Semaphore) JsonParseException(com.google.gson.JsonParseException) ExecutionException(java.util.concurrent.ExecutionException)

Example 5 with FilteredDataEmitter

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

the class GsonTests method testParserCastingError.

public void testParserCastingError() 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);
    try {
        JsonObject j = ret.get();
        fail(j.toString());
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : FilteredDataEmitter(com.koushikdutta.async.FilteredDataEmitter) GsonObjectParser(com.koushikdutta.ion.gson.GsonObjectParser) ByteBufferList(com.koushikdutta.async.ByteBufferList) JsonObject(com.google.gson.JsonObject) JsonParseException(com.google.gson.JsonParseException) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

ByteBufferList (com.koushikdutta.async.ByteBufferList)10 FilteredDataEmitter (com.koushikdutta.async.FilteredDataEmitter)10 JsonObject (com.google.gson.JsonObject)3 PushParser (com.koushikdutta.async.PushParser)3 GsonObjectParser (com.koushikdutta.ion.gson.GsonObjectParser)3 JsonParseException (com.google.gson.JsonParseException)2 DataEmitter (com.koushikdutta.async.DataEmitter)2 TapCallback (com.koushikdutta.async.TapCallback)2 DataCallback (com.koushikdutta.async.callback.DataCallback)2 StringParser (com.koushikdutta.async.parser.StringParser)2 ExecutionException (java.util.concurrent.ExecutionException)2 Semaphore (java.util.concurrent.Semaphore)2 Base64OutputStream (android.util.Base64OutputStream)1 NetworkPeerManager (com.facebook.stetho.inspector.network.NetworkPeerManager)1 IOException (java.io.IOException)1 ByteBuffer (java.nio.ByteBuffer)1 WritableByteChannel (java.nio.channels.WritableByteChannel)1