use of com.koushikdutta.async.util.FileCache in project AndroidAsync by koush.
the class FileCacheTests method testCacheOrder.
public void testCacheOrder() throws Exception {
FileCache cache = new FileCache(new File(getContext().getCacheDir(), "filecache"), 10, false);
cache.setBlockSize(1);
File temp = cache.getTempFile();
StreamUtility.writeFile(temp, "hello");
cache.commitTempFiles("test", temp);
temp = cache.getTempFile();
StreamUtility.writeFile(temp, "hello");
cache.commitTempFiles("test2", temp);
assertEquals(cache.size(), 10);
String value = StreamUtility.readToEnd(cache.get("test"));
assertEquals(value, "hello");
// should push test2 off
temp = cache.getTempFile();
StreamUtility.writeFile(temp, "hello");
cache.commitTempFiles("test3", temp);
assertTrue(cache.exists("test"));
assertFalse(cache.exists("test2"));
assertTrue(cache.exists("test3"));
}
use of com.koushikdutta.async.util.FileCache in project AndroidAsync by koush.
the class FileCacheTests method testMultipleParts.
public void testMultipleParts() throws Exception {
FileCache cache = new FileCache(new File(getContext().getCacheDir(), "filecache"), 100000, false);
cache.setBlockSize(1);
File[] temps = new File[10];
for (int i = 0; i < temps.length; i++) {
File temp = temps[i] = cache.getTempFile();
StreamUtility.writeFile(temp, "hello" + i);
}
cache.commitTempFiles("test", temps);
assertEquals(cache.size(), temps.length * 6);
File dir = new File(getContext().getCacheDir(), "filecache");
File[] files = dir.listFiles();
assertEquals(files.length, temps.length);
for (int i = 5; i < 10; i++) {
assertTrue(cache.exists("test", i));
}
}
use of com.koushikdutta.async.util.FileCache in project AndroidAsync by koush.
the class FileCacheTests method testMultipartEviction.
public void testMultipartEviction() throws Exception {
FileCache cache = new FileCache(new File(getContext().getCacheDir(), "filecache"), 12, false);
cache.setBlockSize(1);
File[] temps = new File[10];
for (int i = 0; i < temps.length; i++) {
File temp = temps[i] = cache.getTempFile();
StreamUtility.writeFile(temp, "hello" + i);
}
cache.commitTempFiles("test", temps);
assertEquals(cache.size(), 12);
File dir = new File(getContext().getCacheDir(), "filecache");
File[] files = dir.listFiles();
assertEquals(files.length, 2);
for (int i = 8; i < 10; i++) {
assertTrue(cache.exists("test", i));
}
try {
FileInputStream[] fins = cache.get("test", temps.length);
fail();
} catch (IOException e) {
}
}
use of com.koushikdutta.async.util.FileCache in project ion by koush.
the class BitmapCallback method saveBitmapSnapshot.
public static void saveBitmapSnapshot(Ion ion, BitmapInfo info) {
if (info.bitmap == null)
return;
FileCache cache = ion.responseCache.getFileCache();
if (cache == null)
return;
File tempFile = cache.getTempFile();
try {
FileOutputStream out = new FileOutputStream(tempFile);
Bitmap.CompressFormat format = info.bitmap.hasAlpha() ? Bitmap.CompressFormat.PNG : Bitmap.CompressFormat.JPEG;
info.bitmap.compress(format, 100, out);
out.close();
cache.commitTempFiles(info.key, tempFile);
} catch (Exception ex) {
} finally {
tempFile.delete();
}
}
use of com.koushikdutta.async.util.FileCache in project ion by koush.
the class BitmapFetcher method execute.
public void execute() {
final Ion ion = builder.ion;
// bitmaps that were transformed are put into the FileCache to prevent
// subsequent retransformation. See if we can retrieve the bitmap from the disk cache.
// See TransformBitmap for where the cache is populated.
FileCache fileCache = ion.responseCache.getFileCache();
if (!builder.noCache && fileCache.exists(bitmapKey) && !deepZoom) {
BitmapCallback.getBitmapSnapshot(ion, bitmapKey, postProcess);
return;
}
// Perform a download as necessary.
if (ion.bitmapsPending.tag(decodeKey) == null && !fastLoad(builder.uri)) {
builder.setHandler(null);
builder.loadRequestCallback = this;
if (!deepZoom) {
Future<Response<ByteBufferList>> emitterTransform = builder.execute(new ByteBufferListParser(), new Runnable() {
@Override
public void run() {
AsyncServer.post(Ion.mainHandler, new Runnable() {
@Override
public void run() {
ion.bitmapsPending.remove(decodeKey);
}
});
}
}).withResponse();
emitterTransform.setCallback(new LoadBitmap(ion, decodeKey, !hasTransforms, sampleWidth, sampleHeight, animateGif));
} else {
// System.out.println("downloading file for deepZoom");
File file = fileCache.getTempFile();
Future<Response<File>> emitterTransform = builder.write(file).withResponse();
LoadDeepZoom loadDeepZoom = new LoadDeepZoom(ion, decodeKey, animateGif, fileCache);
emitterTransform.setCallback(loadDeepZoom);
}
}
executeTransforms(ion);
}
Aggregations