Search in sources :

Example 1 with FileDataEmitter

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

the class FileTests method testFileDataEmitter.

public void testFileDataEmitter() throws Exception {
    final Semaphore semaphore = new Semaphore(0);
    File f = getContext().getFileStreamPath("test.txt");
    StreamUtility.writeFile(f, "hello world");
    FileDataEmitter fdm = new FileDataEmitter(AsyncServer.getDefault(), f);
    final Md5 md5 = Md5.createInstance();
    Future<String> stringBody = new StringParser().parse(fdm).setCallback(new FutureCallback<String>() {

        @Override
        public void onCompleted(Exception e, String result) {
            semaphore.release();
        }
    });
    fdm.resume();
    assertTrue("timeout", semaphore.tryAcquire(TIMEOUT, TimeUnit.MILLISECONDS));
    assertEquals("hello world", stringBody.get());
}
Also used : StringParser(com.koushikdutta.async.parser.StringParser) Semaphore(java.util.concurrent.Semaphore) File(java.io.File) FileDataEmitter(com.koushikdutta.async.FileDataEmitter)

Example 2 with FileDataEmitter

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

the class FileLoader method load.

@Override
public Future<DataEmitter> load(final Ion ion, final AsyncHttpRequest request, final FutureCallback<LoaderEmitter> callback) {
    if (!request.getUri().getScheme().startsWith("file"))
        return null;
    final FileFuture ret = new FileFuture();
    ion.getHttpClient().getServer().post(new Runnable() {

        @Override
        public void run() {
            File file = new File(URI.create(request.getUri().toString()));
            FileDataEmitter emitter = new FileDataEmitter(ion.getHttpClient().getServer(), file);
            ret.setComplete(emitter);
            callback.onCompleted(null, new LoaderEmitter(emitter, (int) file.length(), ResponseServedFrom.LOADED_FROM_CACHE, null, request));
        }
    });
    return ret;
}
Also used : File(java.io.File) FileDataEmitter(com.koushikdutta.async.FileDataEmitter)

Aggregations

FileDataEmitter (com.koushikdutta.async.FileDataEmitter)2 File (java.io.File)2 StringParser (com.koushikdutta.async.parser.StringParser)1 Semaphore (java.util.concurrent.Semaphore)1