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;
}
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);
}
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())));
}
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();
}
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();
}
}
Aggregations