Search in sources :

Example 11 with BinaryResource

use of com.facebook.binaryresource.BinaryResource in project fresco by facebook.

the class DefaultDiskStorageTest method testEntryIds.

@Test
public void testEntryIds() throws Exception {
    DefaultDiskStorage storage = getStorageSupplier(1).get();
    final byte[] value1 = new byte[101];
    final byte[] value2 = new byte[102];
    final byte[] value3 = new byte[103];
    value1[80] = 123;
    value2[80] = 45;
    value3[80] = 67;
    writeFileToStorage(storage, "resourceId1", value1);
    writeFileToStorage(storage, "resourceId2", value2);
    writeFileToStorage(storage, "resourceId3", value3);
    // check that resources are retrieved by the right name, before testing getEntries
    BinaryResource res1 = storage.getResource("resourceId1", null);
    BinaryResource res2 = storage.getResource("resourceId2", null);
    BinaryResource res3 = storage.getResource("resourceId3", null);
    assertArrayEquals(value1, res1.read());
    assertArrayEquals(value2, res2.read());
    assertArrayEquals(value3, res3.read());
    // obtain entries and sort by name
    List<DiskStorage.Entry> entries = new ArrayList<>(storage.getEntries());
    Collections.sort(entries, new Comparator<DiskStorage.Entry>() {

        @Override
        public int compare(DiskStorage.Entry lhs, DiskStorage.Entry rhs) {
            return lhs.getId().compareTo(rhs.getId());
        }
    });
    assertEquals(3, entries.size());
    assertEquals("resourceId1", entries.get(0).getId());
    assertEquals("resourceId2", entries.get(1).getId());
    assertEquals("resourceId3", entries.get(2).getId());
    assertArrayEquals(value1, entries.get(0).getResource().read());
    assertArrayEquals(value2, entries.get(1).getResource().read());
    assertArrayEquals(value3, entries.get(2).getResource().read());
}
Also used : BinaryResource(com.facebook.binaryresource.BinaryResource) FileBinaryResource(com.facebook.binaryresource.FileBinaryResource) ArrayList(java.util.ArrayList) Test(org.junit.Test) PrepareOnlyThisForTest(org.powermock.core.classloader.annotations.PrepareOnlyThisForTest)

Example 12 with BinaryResource

use of com.facebook.binaryresource.BinaryResource in project fresco by facebook.

the class DefaultDiskStorageTest method testBasicOperations.

@Test
public void testBasicOperations() throws Exception {
    DefaultDiskStorage storage = getStorageSupplier(1).get();
    final String resourceId1 = "R1";
    final String resourceId2 = "R2";
    // no file - get should fail
    BinaryResource resource1 = storage.getResource(resourceId1, null);
    Assert.assertNull(resource1);
    // write out the file now
    byte[] key1Contents = new byte[] { 0, 1, 2 };
    writeToStorage(storage, resourceId1, key1Contents);
    // get should succeed now
    resource1 = storage.getResource(resourceId1, null);
    Assert.assertNotNull(resource1);
    File underlyingFile = ((FileBinaryResource) resource1).getFile();
    Assert.assertArrayEquals(key1Contents, Files.toByteArray(underlyingFile));
    // remove the file now - get should fail again
    Assert.assertTrue(underlyingFile.delete());
    resource1 = storage.getResource(resourceId1, null);
    Assert.assertNull(resource1);
    // no file
    BinaryResource resource2 = storage.getResource(resourceId2, null);
    Assert.assertNull(resource2);
}
Also used : BinaryResource(com.facebook.binaryresource.BinaryResource) FileBinaryResource(com.facebook.binaryresource.FileBinaryResource) File(java.io.File) FileBinaryResource(com.facebook.binaryresource.FileBinaryResource) Test(org.junit.Test) PrepareOnlyThisForTest(org.powermock.core.classloader.annotations.PrepareOnlyThisForTest)

Example 13 with BinaryResource

use of com.facebook.binaryresource.BinaryResource in project fresco by facebook.

the class DiskStorageCache method insert.

@Override
public BinaryResource insert(CacheKey key, WriterCallback callback) throws IOException {
    // Write to a temp file, then move it into place. This allows more parallelism
    // when writing files.
    SettableCacheEvent cacheEvent = SettableCacheEvent.obtain().setCacheKey(key);
    mCacheEventListener.onWriteAttempt(cacheEvent);
    String resourceId;
    synchronized (mLock) {
        // for multiple resource ids associated with the same image, we only write one file
        resourceId = CacheKeyUtil.getFirstResourceId(key);
    }
    cacheEvent.setResourceId(resourceId);
    try {
        // getting the file is synchronized
        DiskStorage.Inserter inserter = startInsert(resourceId, key);
        try {
            inserter.writeData(callback, key);
            // Committing the file is synchronized
            BinaryResource resource = endInsert(inserter, key, resourceId);
            cacheEvent.setItemSize(resource.size()).setCacheSize(mCacheStats.getSize());
            mCacheEventListener.onWriteSuccess(cacheEvent);
            return resource;
        } finally {
            if (!inserter.cleanUp()) {
                FLog.e(TAG, "Failed to delete temp file");
            }
        }
    } catch (IOException ioe) {
        cacheEvent.setException(ioe);
        mCacheEventListener.onWriteException(cacheEvent);
        FLog.e(TAG, "Failed inserting a file into the cache", ioe);
        throw ioe;
    } finally {
        cacheEvent.recycle();
    }
}
Also used : BinaryResource(com.facebook.binaryresource.BinaryResource) IOException(java.io.IOException)

Aggregations

BinaryResource (com.facebook.binaryresource.BinaryResource)13 FileBinaryResource (com.facebook.binaryresource.FileBinaryResource)6 CacheKey (com.facebook.cache.common.CacheKey)5 SimpleCacheKey (com.facebook.cache.common.SimpleCacheKey)5 IOException (java.io.IOException)5 Test (org.junit.Test)5 PrepareOnlyThisForTest (org.powermock.core.classloader.annotations.PrepareOnlyThisForTest)5 File (java.io.File)4 MultiCacheKey (com.facebook.cache.common.MultiCacheKey)3 ImageRequest (com.facebook.imagepipeline.request.ImageRequest)3 Bitmap (android.graphics.Bitmap)2 CloseableReference (com.facebook.common.references.CloseableReference)2 ImagePipeline (com.facebook.imagepipeline.core.ImagePipeline)2 FileInputStream (java.io.FileInputStream)2 InputStream (java.io.InputStream)2 Nullable (javax.annotation.Nullable)2 WriterCallback (com.facebook.cache.common.WriterCallback)1 PooledByteBuffer (com.facebook.common.memory.PooledByteBuffer)1 DataSource (com.facebook.datasource.DataSource)1 ImagePipelineFactory (com.facebook.imagepipeline.core.ImagePipelineFactory)1