Package com.android.repository.io
Class FileOpUtils
- java.lang.Object
-
- com.android.repository.io.FileOpUtils
-
public final class FileOpUtils extends java.lang.ObjectSome convenience methods for working withFiles/FileOps.
-
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static java.io.Fileappend(java.io.File base, java.lang.String... segments)Appends the givensegmentsto thebasefile.static java.io.Fileappend(java.lang.String base, java.lang.String... segments)Appends the givensegmentsto thebasefile.static FileOpcreate()Deprecated.UsePaths,CancellableFileIoand (for testing)InMemoryFileSystemsdirectly.static FileOpcreate(java.nio.file.Path path)Deprecated.UsePaths,CancellableFileIoand (for testing)InMemoryFileSystemsdirectly.static booleandeleteFileOrFolder(java.nio.file.Path fileOrFolder)Helper to delete a file or a directory.static booleandeleteIfExists(java.io.File file, FileOp fop)Deprecated.UseFiles.deleteIfExists(Path).static java.nio.file.PathgetNewTempDir(java.lang.String base, FileOp fileOp)Deprecated.static java.nio.file.PathgetNewTempDir(java.lang.String base, java.nio.file.FileSystem fileSystem)static booleanisWindows()static java.nio.file.Path[]listFiles(java.nio.file.Path file)InvokesCancellableFileIo.list(Path)on the givenfile.static java.lang.StringmakeRelative(java.io.File baseDir, java.io.File toBeRelative)Computes a relative path from "toBeRelative" relative to "baseDir".static voidrecursiveCopy(java.io.File src, java.io.File dest, FileOp fop, ProgressIndicator progress)Deprecated.static voidrecursiveCopy(java.nio.file.Path src, java.nio.file.Path dest, boolean merge, ProgressIndicator progress)static voidsafeRecursiveOverwrite(java.nio.file.Path src, java.nio.file.Path dest, ProgressIndicator progress)Moves a file or directory from one location to another.static voidsetExecutablePermission(java.nio.file.Path path)static java.io.FiletoFile(java.nio.file.Path path)Convenience method to allow Path-based code to interop with legacy File-based code.static java.io.FiletoFileUnsafe(java.nio.file.Path path)Temporary functionality to help with File-to-Path migration.
-
-
-
Method Detail
-
create
@NonNull public static FileOp create()
Deprecated.UsePaths,CancellableFileIoand (for testing)InMemoryFileSystemsdirectly.The standard way to create aFileOpthat interacts with the real filesystem.
-
create
@NonNull public static FileOp create(@Nullable java.nio.file.Path path)
Deprecated.UsePaths,CancellableFileIoand (for testing)InMemoryFileSystemsdirectly.
-
recursiveCopy
public static void recursiveCopy(@NonNull java.io.File src, @NonNull java.io.File dest, @NonNull FileOp fop, @NonNull ProgressIndicator progress) throws java.io.IOExceptionDeprecated.Copies a file or directory tree to the given location.destshould not exist: with the file system currently looking like
Running/ dir1/ a.txt dir2/recursiveCopy(new File("/dir1"), new File("/dir2"), fOp)will result in an exception, whilerecursiveCopy(new File("/dir1"), new File("/dir2/foo")will result in
This is equivalent to the behavior of/ dir1/ a.txt dir2/ foo/ a.txtcp -rwhen the target does not exist.- Parameters:
src- File to copydest- Destination.fop- The FileOp to use for file operations.- Throws:
java.io.IOException- If the destination already exists, or if there is a problem copying the files or creating directories.
-
recursiveCopy
public static void recursiveCopy(@NonNull java.nio.file.Path src, @NonNull java.nio.file.Path dest, boolean merge, @NonNull ProgressIndicator progress) throws java.io.IOException- Throws:
java.io.IOException
-
listFiles
@NonNull public static java.nio.file.Path[] listFiles(@NonNull java.nio.file.Path file)InvokesCancellableFileIo.list(Path)on the givenfile. Contrary to the Java API, this returns an empty array instead of null when the directory does not exist.
-
safeRecursiveOverwrite
public static void safeRecursiveOverwrite(@NonNull java.nio.file.Path src, @NonNull java.nio.file.Path dest, @NonNull ProgressIndicator progress) throws java.io.IOExceptionMoves a file or directory from one location to another. If the destination exists, it is moved away, and once the operation has completed successfully, deleted. If there is a problem during the copy, the original files are moved back into place.- Parameters:
src- File to movedest- Destination. Follows the same rules asrecursiveCopy(File, File, FileOp, ProgressIndicator)}.progress- Currently only used for error logging.- Throws:
java.io.IOException- If some problem occurs during copies or directory creation.
-
getNewTempDir
@Nullable public static java.nio.file.Path getNewTempDir(@NonNull java.lang.String base, @NonNull FileOp fileOp)Deprecated.Creates a new subdirectory of the system temp directory. The directory will be named<base> + NN, where NN makes the directory distinct from any existing directories.
-
getNewTempDir
@Nullable public static java.nio.file.Path getNewTempDir(@NonNull java.lang.String base, @NonNull java.nio.file.FileSystem fileSystem)
-
append
@NonNull public static java.io.File append(@NonNull java.io.File base, @NonNull java.lang.String... segments)Appends the givensegmentsto thebasefile.- Parameters:
base- A base file, non-null.segments- Individual folder or filename segments to append to the base file.- Returns:
- A new file representing the concatenation of the base path with all the segments.
-
append
@NonNull public static java.io.File append(@NonNull java.lang.String base, @NonNull java.lang.String... segments)Appends the givensegmentsto thebasefile.- Parameters:
base- A base file path, non-empty and non-null.segments- Individual folder or filename segments to append to the base path.- Returns:
- A new file representing the concatenation of the base path with all the segments.
-
makeRelative
@NonNull public static java.lang.String makeRelative(@NonNull java.io.File baseDir, @NonNull java.io.File toBeRelative) throws java.io.IOExceptionComputes a relative path from "toBeRelative" relative to "baseDir".Rule: - let relative2 = makeRelative(path1, path2) - then pathJoin(path1 + relative2) == path2 after canonicalization.
Principle: - let base = /c1/c2.../cN/a1/a2../aN - let toBeRelative = /c1/c2.../cN/b1/b2../bN - result is removes the common paths, goes back from aN to cN then to bN: - result = ../..../../1/b2../bN
- Parameters:
baseDir- The base directory to be relative to.toBeRelative- The file or directory to make relative to the base.- Returns:
- A path that makes toBeRelative relative to baseDir.
- Throws:
java.io.IOException- If drive letters don't match on Windows or path canonicalization fails.
-
deleteIfExists
public static boolean deleteIfExists(java.io.File file, FileOp fop)Deprecated.UseFiles.deleteIfExists(Path).Deletes the given file if it exists. Does nothing and returns successfully if the file didn't exist to begin with.- Returns:
- true if the file no longer exists, false if we failed to delete it
-
isWindows
public static boolean isWindows()
-
deleteFileOrFolder
public static boolean deleteFileOrFolder(@NonNull java.nio.file.Path fileOrFolder)Helper to delete a file or a directory. For a directory, recursively deletes all of its content. It's ok for the file or folder to not exist at all.- Returns:
- true if the delete was successful
-
toFileUnsafe
@NonNull public static java.io.File toFileUnsafe(@NonNull java.nio.file.Path path)Temporary functionality to help with File-to-Path migration. Should only be called when a FileOp is not available and with a Path that is backed by the default FileSystem (notably not in the context of any tests that use MockFileOp or jimfs).- Throws:
java.lang.UnsupportedOperationException- if the Path is backed by a non-default FileSystem.
-
setExecutablePermission
public static void setExecutablePermission(@NonNull java.nio.file.Path path) throws java.io.IOException- Throws:
java.io.IOException
-
toFile
@NonNull public static java.io.File toFile(@NonNull java.nio.file.Path path)Convenience method to allow Path-based code to interop with legacy File-based code.Note that the resulting File may not refer to an actual file on the default FileSystem.
-
-