Search in sources :

Example 1 with FileCache

use of com.koushikdutta.async.util.FileCache in project AndroidAsync by koush.

the class ResponseCacheMiddleware method addCache.

public static ResponseCacheMiddleware addCache(AsyncHttpClient client, File cacheDir, long size) throws IOException {
    for (AsyncHttpClientMiddleware middleware : client.getMiddleware()) {
        if (middleware instanceof ResponseCacheMiddleware)
            throw new IOException("Response cache already added to http client");
    }
    ResponseCacheMiddleware ret = new ResponseCacheMiddleware();
    ret.server = client.getServer();
    ret.cache = new FileCache(cacheDir, size, false);
    client.insertMiddleware(ret);
    return ret;
}
Also used : AsyncHttpClientMiddleware(com.koushikdutta.async.http.AsyncHttpClientMiddleware) IOException(java.io.IOException) FileCache(com.koushikdutta.async.util.FileCache)

Example 2 with FileCache

use of com.koushikdutta.async.util.FileCache in project AndroidAsync by koush.

the class FileCacheTests method testEviction.

public void testEviction() throws Exception {
    FileCache cache = new FileCache(new File(getContext().getCacheDir(), "filecache"), 25, false);
    cache.setBlockSize(1);
    for (int i = 0; i < 10; i++) {
        File temp = cache.getTempFile();
        StreamUtility.writeFile(temp, "hello");
        cache.commitTempFiles("test" + i, temp);
        String value = StreamUtility.readToEnd(cache.get("test" + i));
        assertEquals(value, "hello");
    }
    assertEquals(cache.size(), 25);
    File dir = new File(getContext().getCacheDir(), "filecache");
    File[] files = dir.listFiles();
    assertEquals(files.length, 5);
    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 3 with FileCache

use of com.koushikdutta.async.util.FileCache in project AndroidAsync by koush.

the class FileCacheTests method testSimple.

public void testSimple() throws Exception {
    FileCache cache = new FileCache(new File(getContext().getCacheDir(), "filecache"), 100000, false);
    cache.setBlockSize(1);
    File temp = cache.getTempFile();
    StreamUtility.writeFile(temp, "hello");
    cache.commitTempFiles("test", temp);
    String value = StreamUtility.readToEnd(cache.get("test"));
    assertEquals(value, "hello");
}
Also used : File(java.io.File) FileCache(com.koushikdutta.async.util.FileCache)

Example 4 with FileCache

use of com.koushikdutta.async.util.FileCache in project AndroidAsync by koush.

the class FileCacheTests method testReinit.

public void testReinit() 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);
    cache = new FileCache(new File(getContext().getCacheDir(), "filecache"), 10, false);
    cache.setBlockSize(1);
    String value = StreamUtility.readToEnd(cache.get("test"));
    assertEquals(value, "hello");
    value = StreamUtility.readToEnd(cache.get("test2"));
    assertEquals(value, "hello");
}
Also used : File(java.io.File) FileCache(com.koushikdutta.async.util.FileCache)

Example 5 with FileCache

use of com.koushikdutta.async.util.FileCache in project AndroidAsync by koush.

the class FileCacheTests method testMultipartEvictionAgain.

public void testMultipartEvictionAgain() throws Exception {
    FileCache cache = new FileCache(new File(getContext().getCacheDir(), "filecache"), 72, 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(), 60);
    File dir = new File(getContext().getCacheDir(), "filecache");
    File[] files = dir.listFiles();
    assertEquals(files.length, 10);
    for (int i = 0; i < temps.length; i++) {
        assertTrue(cache.exists("test", i));
    }
    FileInputStream[] fins = cache.get("test", temps.length);
    StreamUtility.closeQuietly(fins);
    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("test2", temps);
    assertEquals(cache.size(), 72);
    fins = cache.get("test2", temps.length);
    StreamUtility.closeQuietly(fins);
    try {
        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)

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