• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.sdklib.io;
18 
19 import java.io.File;
20 import java.io.FileNotFoundException;
21 import java.io.FileOutputStream;
22 import java.io.IOException;
23 import java.io.OutputStream;
24 
25 
26 /**
27  * Wraps some common {@link File} operations on files and folders.
28  * <p/>
29  * This makes it possible to override/mock/stub some file operations in unit tests.
30  */
31 public interface IFileOp {
32 
33     /**
34      * Helper to delete a file or a directory.
35      * For a directory, recursively deletes all of its content.
36      * Files that cannot be deleted right away are marked for deletion on exit.
37      * The argument can be null.
38      */
deleteFileOrFolder(File fileOrFolder)39     public abstract void deleteFileOrFolder(File fileOrFolder);
40 
41     /**
42      * Sets the executable Unix permission (+x) on a file or folder.
43      * <p/>
44      * This attempts to use File#setExecutable through reflection if
45      * it's available.
46      * If this is not available, this invokes a chmod exec instead,
47      * so there is no guarantee of it being fast.
48      * <p/>
49      * Caller must make sure to not invoke this under Windows.
50      *
51      * @param file The file to set permissions on.
52      * @throws IOException If an I/O error occurs
53      */
setExecutablePermission(File file)54     public abstract void setExecutablePermission(File file) throws IOException;
55 
56     /**
57      * Copies a binary file.
58      *
59      * @param source the source file to copy.
60      * @param dest the destination file to write.
61      * @throws FileNotFoundException if the source file doesn't exist.
62      * @throws IOException if there's a problem reading or writing the file.
63      */
copyFile(File source, File dest)64     public abstract void copyFile(File source, File dest) throws IOException;
65 
66     /**
67      * Checks whether 2 binary files are the same.
68      *
69      * @param source the source file to copy
70      * @param destination the destination file to write
71      * @throws FileNotFoundException if the source files don't exist.
72      * @throws IOException if there's a problem reading the files.
73      */
isSameFile(File source, File destination)74     public abstract boolean isSameFile(File source, File destination)
75             throws IOException;
76 
77     /** Invokes {@link File#exists()} on the given {@code file}. */
exists(File file)78     public abstract boolean exists(File file);
79 
80     /** Invokes {@link File#isFile()} on the given {@code file}. */
isFile(File file)81     public abstract boolean isFile(File file);
82 
83     /** Invokes {@link File#isDirectory()} on the given {@code file}. */
isDirectory(File file)84     public abstract boolean isDirectory(File file);
85 
86     /** Invokes {@link File#length()} on the given {@code file}. */
length(File file)87     public abstract long length(File file);
88 
89     /**
90      * Invokes {@link File#delete()} on the given {@code file}.
91      * Note: for a recursive folder version, consider {@link #deleteFileOrFolder(File)}.
92      */
delete(File file)93     public abstract boolean delete(File file);
94 
95     /** Invokes {@link File#mkdirs()} on the given {@code file}. */
mkdirs(File file)96     public abstract boolean mkdirs(File file);
97 
98     /** Invokes {@link File#listFiles()} on the given {@code file}. */
listFiles(File file)99     public abstract File[] listFiles(File file);
100 
101     /** Invokes {@link File#renameTo(File)} on the given files. */
renameTo(File oldDir, File newDir)102     public abstract boolean renameTo(File oldDir, File newDir);
103 
104     /** Creates a new {@link FileOutputStream} for the given {@code file}. */
newFileOutputStream(File file)105     public abstract OutputStream newFileOutputStream(File file) throws FileNotFoundException;
106 
107 }
108