Search in sources :

Example 1 with FileDataSink

use of com.koushikdutta.async.stream.FileDataSink in project ion by koush.

the class FileCacheStore method put.

private <T> Future<T> put(final T value, final AsyncParser<T> parser) {
    final SimpleFuture<T> ret = new SimpleFuture<T>();
    Ion.getIoExecutorService().execute(new Runnable() {

        @Override
        public void run() {
            final String key = computeKey();
            final File file = cache.getTempFile();
            final FileDataSink sink = new FileDataSink(ion.getServer(), file);
            parser.write(sink, value, new CompletedCallback() {

                @Override
                public void onCompleted(Exception ex) {
                    sink.end();
                    if (ex != null) {
                        file.delete();
                        ret.setComplete(ex);
                        return;
                    }
                    cache.commitTempFiles(key, file);
                    ret.setComplete(value);
                }
            });
        }
    });
    return ret;
}
Also used : FileDataSink(com.koushikdutta.async.stream.FileDataSink) CompletedCallback(com.koushikdutta.async.callback.CompletedCallback) File(java.io.File) SimpleFuture(com.koushikdutta.async.future.SimpleFuture)

Aggregations

CompletedCallback (com.koushikdutta.async.callback.CompletedCallback)1 SimpleFuture (com.koushikdutta.async.future.SimpleFuture)1 FileDataSink (com.koushikdutta.async.stream.FileDataSink)1 File (java.io.File)1