Java Code Examples for com.facebook.common.internal.Files

The following examples show how to use com.facebook.common.internal.Files. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: fresco   Source File: DefaultDiskStorageTest.java    License: MIT License 6 votes vote down vote up
@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);
}
 
Example 2
Source Project: fresco   Source File: DefaultDiskStorageTest.java    License: MIT License 6 votes vote down vote up
/**
 * Test that a file is stored in a new file, and the bytes are stored plainly in the file.
 *
 * @throws Exception
 */
@Test
public void testStoreFile() throws Exception {
  DefaultDiskStorage storage = getStorageSupplier(1).get();
  final String resourceId1 = "resource1";
  final byte[] value1 = new byte[100];
  value1[80] = 101;
  File file1 = writeFileToStorage(storage, resourceId1, value1);

  Set<File> files = new HashSet<>();
  Assert.assertTrue(mDirectory.exists());
  List<File> founds1 = findNewFiles(mDirectory, files, /*recurse*/ true);
  Assert.assertNotNull(file1);
  Assert.assertTrue(founds1.contains(file1));
  Assert.assertTrue(file1.exists());
  assertEquals(100, file1.length());
  Assert.assertArrayEquals(value1, Files.toByteArray(file1));
}
 
Example 3
public byte[] getThumb() {
    try {
        return Files.toByteArray(imageFile);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return new byte[0];
}
 
Example 4
@Override
public byte[] read() throws IOException {
  return Files.toByteArray(mFile);
}
 
Example 5
Source Project: fresco   Source File: FileBinaryResource.java    License: MIT License 4 votes vote down vote up
@Override
public byte[] read() throws IOException {
  return Files.toByteArray(mFile);
}
 
Example 6
Source Project: fresco   Source File: DefaultDiskStorageTest.java    License: MIT License 4 votes vote down vote up
/**
 * Test that purgeUnexpectedResources deletes all files/directories outside the version directory
 * but leaves untouched the version directory and the content files.
 *
 * @throws Exception
 */
@Test
public void testPurgeUnexpectedFiles() throws Exception {
  final DefaultDiskStorage storage = getStorageSupplier(1).get();

  final String resourceId = "file1";
  final byte[] CONTENT = "content".getBytes("UTF-8");

  File file = writeFileToStorage(storage, resourceId, CONTENT);

  // check file exists
  Assert.assertTrue(file.exists());
  Assert.assertArrayEquals(CONTENT, Files.toByteArray(file));

  final File unexpectedFile1 = new File(mDirectory, "unexpected-file-1");
  final File unexpectedFile2 = new File(mDirectory, "unexpected-file-2");

  Assert.assertTrue(unexpectedFile1.createNewFile());
  Assert.assertTrue(unexpectedFile2.createNewFile());

  final File unexpectedDir1 = new File(mDirectory, "unexpected-dir-1");
  Assert.assertTrue(unexpectedDir1.mkdirs());

  final File unexpectedDir2 = new File(mDirectory, "unexpected-dir-2");
  Assert.assertTrue(unexpectedDir2.mkdirs());
  final File unexpectedSubfile1 = new File(unexpectedDir2, "unexpected-sub-file-1");
  Assert.assertTrue(unexpectedSubfile1.createNewFile());

  Assert.assertEquals(5, mDirectory.listFiles().length); // 4 unexpected (files+dirs) + ver. dir
  Assert.assertEquals(1, unexpectedDir2.listFiles().length);
  Assert.assertEquals(0, unexpectedDir1.listFiles().length);

  File unexpectedFileInShard = new File(file.getParentFile(), "unexpected-in-shard");
  Assert.assertTrue(unexpectedFileInShard.createNewFile());

  storage.purgeUnexpectedResources();
  Assert.assertFalse(unexpectedFile1.exists());
  Assert.assertFalse(unexpectedFile2.exists());
  Assert.assertFalse(unexpectedSubfile1.exists());
  Assert.assertFalse(unexpectedDir1.exists());
  Assert.assertFalse(unexpectedDir2.exists());

  // check file still exists
  Assert.assertTrue(file.exists());
  // check unexpected sibling is gone
  Assert.assertFalse(unexpectedFileInShard.exists());
  // check the only thing in root is the version directory
  Assert.assertEquals(1, mDirectory.listFiles().length); // just the version directory
}