• 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      * Sets the file or directory as read-only.
58      *
59      * @param file The file or directory to set permissions on.
60      */
setReadOnly(File file)61     public abstract void setReadOnly(File file);
62 
63     /**
64      * Copies a binary file.
65      *
66      * @param source the source file to copy.
67      * @param dest the destination file to write.
68      * @throws FileNotFoundException if the source file doesn't exist.
69      * @throws IOException if there's a problem reading or writing the file.
70      */
copyFile(File source, File dest)71     public abstract void copyFile(File source, File dest) throws IOException;
72 
73     /**
74      * Checks whether 2 binary files are the same.
75      *
76      * @param source the source file to copy
77      * @param destination the destination file to write
78      * @throws FileNotFoundException if the source files don't exist.
79      * @throws IOException if there's a problem reading the files.
80      */
isSameFile(File source, File destination)81     public abstract boolean isSameFile(File source, File destination)
82             throws IOException;
83 
84     /** Invokes {@link File#exists()} on the given {@code file}. */
exists(File file)85     public abstract boolean exists(File file);
86 
87     /** Invokes {@link File#isFile()} on the given {@code file}. */
isFile(File file)88     public abstract boolean isFile(File file);
89 
90     /** Invokes {@link File#isDirectory()} on the given {@code file}. */
isDirectory(File file)91     public abstract boolean isDirectory(File file);
92 
93     /** Invokes {@link File#length()} on the given {@code file}. */
length(File file)94     public abstract long length(File file);
95 
96     /**
97      * Invokes {@link File#delete()} on the given {@code file}.
98      * Note: for a recursive folder version, consider {@link #deleteFileOrFolder(File)}.
99      */
delete(File file)100     public abstract boolean delete(File file);
101 
102     /** Invokes {@link File#mkdirs()} on the given {@code file}. */
mkdirs(File file)103     public abstract boolean mkdirs(File file);
104 
105     /** Invokes {@link File#listFiles()} on the given {@code file}. */
listFiles(File file)106     public abstract File[] listFiles(File file);
107 
108     /** Invokes {@link File#renameTo(File)} on the given files. */
renameTo(File oldDir, File newDir)109     public abstract boolean renameTo(File oldDir, File newDir);
110 
111     /** Creates a new {@link FileOutputStream} for the given {@code file}. */
newFileOutputStream(File file)112     public abstract OutputStream newFileOutputStream(File file) throws FileNotFoundException;
113 
114 }
115