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