com.android.repository.io
Class FileOpUtils

java.lang.Object
  extended by com.android.repository.io.FileOpUtils

public final class FileOpUtils
extends java.lang.Object

Some convenience methods for working with Files/FileOps.


Method Summary
static java.io.File append(java.io.File base, java.lang.String... segments)
          Appends the given segments to the base file.
static java.io.File append(java.lang.String base, java.lang.String... segments)
          Appends the given segments to the base file.
static FileOp create()
          The standard way to create a FileOp that interacts with the real filesystem.
static java.io.File getNewTempDir(java.lang.String base, FileOp fileOp)
          Creates a new subdirectory of the system temp directory.
static java.lang.String makeRelative(java.io.File baseDir, java.io.File toBeRelative, FileOp fop)
          Computes a relative path from "toBeRelative" relative to "baseDir".
static void recursiveCopy(java.io.File src, java.io.File dest, FileOp fop)
          Copies a file or directory tree to the given location.
static void safeRecursiveOverwrite(java.io.File src, java.io.File dest, FileOp fop, ProgressIndicator progress)
          Moves a file or directory from one location to another.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

create

@NonNull
public static FileOp create()
The standard way to create a FileOp that interacts with the real filesystem.


recursiveCopy

public static void recursiveCopy(@NonNull
                                 java.io.File src,
                                 @NonNull
                                 java.io.File dest,
                                 @NonNull
                                 FileOp fop)
                          throws java.io.IOException
Copies a file or directory tree to the given location. dest should not exist: with the file system currently looking like
     /
       dir1/
         a.txt
       dir2/
     
 
Running recursiveCopy(new File("/dir1"), new File("/dir2"), fOp) will result in an exception, while recursiveCopy(new File("/dir1"), new File("/dir2/foo") will result in
     /
       dir1/
         a.txt
       dir2/
         foo/
           a.txt
     
 
This is equivalent to the behavior of cp -r when the target does not exist.

Parameters:
src - File to copy
dest - 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.

safeRecursiveOverwrite

public static void safeRecursiveOverwrite(@NonNull
                                          java.io.File src,
                                          @NonNull
                                          java.io.File dest,
                                          @NonNull
                                          FileOp fop,
                                          @NonNull
                                          ProgressIndicator progress)
                                   throws java.io.IOException
Moves 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 move
dest - Destination. Follows the same rules as recursiveCopy(File, File, FileOp)}.
fop - The FileOp to use for file operations.
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.io.File getNewTempDir(@NonNull
                                                  java.lang.String base,
                                                  @NonNull
                                                  FileOp fileOp)
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.


append

@NonNull
public static java.io.File append(@NonNull
                                          java.io.File base,
                                          @NonNull
                                          java.lang.String... segments)
Appends the given segments to the base file.

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 given segments to the base file.

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,
                                                    FileOp fop)
                                     throws java.io.IOException
Computes 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.
fop - FileOp, in this case just to determine the platform.
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.