Search in sources :

Example 6 with FileCache

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"));
}
Also used : File(java.io.File) FileCache(com.koushikdutta.async.util.FileCache)

Example 7 with FileCache

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));
    }
}
Also used : File(java.io.File) FileCache(com.koushikdutta.async.util.FileCache)

Example 8 with FileCache

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) {
    }
}
Also used : IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream) FileCache(com.koushikdutta.async.util.FileCache)

Example 9 with FileCache

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();
    }
}
Also used : Bitmap(android.graphics.Bitmap) FileOutputStream(java.io.FileOutputStream) File(java.io.File) CancellationException(java.util.concurrent.CancellationException) FileCache(com.koushikdutta.async.util.FileCache)

Example 10 with FileCache

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);
}
Also used : ByteBufferListParser(com.koushikdutta.async.parser.ByteBufferListParser) File(java.io.File) MediaFile(com.koushikdutta.ion.loader.MediaFile) FileCache(com.koushikdutta.async.util.FileCache)

Aggregations

FileCache (com.koushikdutta.async.util.FileCache)11 File (java.io.File)9 IOException (java.io.IOException)3 FileInputStream (java.io.FileInputStream)2 Bitmap (android.graphics.Bitmap)1 AsyncHttpClientMiddleware (com.koushikdutta.async.http.AsyncHttpClientMiddleware)1 ByteBufferListParser (com.koushikdutta.async.parser.ByteBufferListParser)1 BitmapInfo (com.koushikdutta.ion.bitmap.BitmapInfo)1 MediaFile (com.koushikdutta.ion.loader.MediaFile)1 FileOutputStream (java.io.FileOutputStream)1 CancellationException (java.util.concurrent.CancellationException)1