private File appStorageDirectory() { File appFilesDirectory = context.getFilesDir(); return new File(appFilesDirectory, "leakcanary"); }
private static File getBaseDir(Context context) { // getFilesDir() yields /data/data/<package>/files, we want the base package dir. return context.getFilesDir().getParentFile(); }
@Provides @Named("cacheDir") File cacheDir(Context context) { return new File(context.getFilesDir(), "cache"); } }
public static File getConvertedMarkedFile(final Context context) { return new File(context.getFilesDir().getAbsolutePath() + File.separator + INTERNAL_DOCUMENT_NAME, OLD_FILE_CONVERTED_FILE_NAME); }
@NonNull @Override public File getFile(@NonNull Context context, @NonNull String fileName) { return new File(context.getFilesDir(), fileName); } },
private static File getFile(final Context context, final User organization) { return new File(context.getFilesDir(), "recent-repos-" + organization.id() + ".ser"); }
public void cleanupFiles() { for (File file : mContext.getFilesDir().listFiles()) { if (file.getName().startsWith(FILENAME_PREFIX)) { if (!file.delete()) { LogRedirector.w(TAG, "Failed to delete " + file.getAbsolutePath()); } } } LogRedirector.i(TAG, "Cleaned up temporary network files."); }
@Override protected long getDataLength(Uri uri) { File f=new File(getContext().getFilesDir(), uri.getPath()); return(f.length()); }
@Override protected long getDataLength(Uri uri) { File f=new File(getContext().getFilesDir(), uri.getPath()); return(f.length()); }
public static String getRootDirPath(Context context) { if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) { File file = ContextCompat.getExternalFilesDirs(context.getApplicationContext(), null)[0]; return file.getAbsolutePath(); } else { return context.getApplicationContext().getFilesDir().getAbsolutePath(); } }
static String getPath() { String state = Environment.getExternalStorageState(); String logPath = BlockCanaryInternals.getContext() == null ? "" : BlockCanaryInternals.getContext().providePath(); if (Environment.MEDIA_MOUNTED.equals(state) && Environment.getExternalStorageDirectory().canWrite()) { return Environment.getExternalStorageDirectory().getPath() + logPath; } return getContext().provideContext().getFilesDir() + BlockCanaryInternals.getContext().providePath(); }
@Test public void openFileOutput_shouldReturnAFileOutputStream() throws Exception { File file = new File("__test__"); String fileContents = "blah"; try (FileOutputStream fileOutputStream = context.openFileOutput("__test__", Context.MODE_PRIVATE)) { fileOutputStream.write(fileContents.getBytes(UTF_8)); } try (FileInputStream fileInputStream = new FileInputStream(new File(context.getFilesDir(), file.getName()))) { byte[] readBuffer = new byte[fileContents.length()]; fileInputStream.read(readBuffer); assertThat(new String(readBuffer, UTF_8)).isEqualTo(fileContents); } }
@Override public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException { final File file = new File(ApplicationProvider.getApplicationContext().getFilesDir(), "test_file"); try { file.createNewFile(); } catch (IOException e) { throw new RuntimeException("error creating new file", e); } return ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY); } }
@Test public void openFileInput_shouldReturnAFileInputStream() throws Exception { String fileContents = "blah"; File file = new File(context.getFilesDir(), "__test__"); try (Writer fileWriter = Files.newBufferedWriter(file.toPath(), UTF_8)) { fileWriter.write(fileContents); } try (FileInputStream fileInputStream = context.openFileInput("__test__")) { byte[] bytes = new byte[fileContents.length()]; fileInputStream.read(bytes); assertThat(bytes).isEqualTo(fileContents.getBytes(UTF_8)); } }
@Before public void setUp() throws Exception { file = new File(ApplicationProvider.getApplicationContext().getFilesDir(), "test"); FileOutputStream os = new FileOutputStream(file); os.close(); readOnlyFile = new File(ApplicationProvider.getApplicationContext().getFilesDir(), "test_readonly"); os = new FileOutputStream(readOnlyFile); os.write(READ_ONLY_FILE_CONTENTS); os.close(); assertThat(readOnlyFile.setReadOnly()).isTrue(); }
@Test public void getFilesDir_shouldCreateDirectory() throws Exception { assertThat(context.getFilesDir().exists()).isTrue(); }
@Test public void deleteFile_shouldReturnFalse() throws IOException { File filesDir = context.getFilesDir(); File file = new File(filesDir, "test.txt"); boolean successfully = context.deleteFile(file.getName()); assertThat(successfully).isFalse(); }
@Test public void deleteFile_shouldReturnTrue() throws IOException { File filesDir = context.getFilesDir(); File file = new File(filesDir, "test.txt"); boolean successfully = file.createNewFile(); assertThat(successfully).isTrue(); successfully = context.deleteFile(file.getName()); assertThat(successfully).isTrue(); }
@Test public void fileList() throws Exception { assertThat(context.fileList()).isEqualTo(context.getFilesDir().list()); }
@Test public void testAutoCloseOutputStream() throws Exception { File f = new File(ApplicationProvider.getApplicationContext().getFilesDir(), "outfile"); ParcelFileDescriptor pfd = ParcelFileDescriptor.open(f, -1); ParcelFileDescriptor.AutoCloseOutputStream os = new ParcelFileDescriptor.AutoCloseOutputStream(pfd); os.close(); assertThat(pfd.getFileDescriptor().valid()).isFalse(); }