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