Java 类com.facebook.common.internal.Files 实例源码

项目:GitHub    文件:DefaultDiskStorageTest.java   
@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);
}
项目:GitHub    文件:DefaultDiskStorageTest.java   
/**
 * 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));
}
项目:fresco    文件:DefaultDiskStorageTest.java   
@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);
}
项目:fresco    文件:DefaultDiskStorageTest.java   
/**
 * 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));
}
项目:actor-platform    文件:StickerView.java   
public byte[] getThumb() {
    try {
        return Files.toByteArray(imageFile);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return new byte[0];
}
项目:GitHub    文件:FileBinaryResource.java   
@Override
public byte[] read() throws IOException {
  return Files.toByteArray(mFile);
}
项目:GitHub    文件:DefaultDiskStorageTest.java   
/**
 * 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
}
项目:fresco    文件:FileBinaryResource.java   
@Override
public byte[] read() throws IOException {
  return Files.toByteArray(mFile);
}
项目:fresco    文件:DefaultDiskStorageTest.java   
/**
 * 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
}