FileUtil

public class FileUtil
extends Object

java.lang.Object
   ↳ com.android.tradefed.util.FileUtil


A helper class for file related operations

Summary

Nested classes

class FileUtil.LowDiskSpaceException

Thrown if usable disk space is below minimum threshold. 

Constants

int FILESYSTEM_FILENAME_MAX_LENGTH

Public constructors

FileUtil()

Public methods

static String calculateBase64Md5(File file)

Helper method to calculate base64 md5 for a file.

static long calculateCrc32(File file)

Helper method to calculate CRC-32 for a file.

static String calculateMd5(File file)

Helper method to calculate md5 for a file.

static boolean chmod(File file, String perms)
static boolean chmodGroupRW(File file)

Performs a best effort attempt to make given file group readable and writable.

static boolean chmodGroupRWX(File file)

Performs a best effort attempt to make given file group executable, readable, and writable.

static boolean chmodRWXRecursively(File file)
static void closeZip(ZipFile zipFile)

This method is deprecated. Moved to ZipUtil#closeZip(ZipFile).

static collectJars(File dir)

Returns all jar files found in given directory

static boolean compareFileContents(File file1, File file2)

Utility method to do byte-wise content comparison of two files.

static long convertSizeToBytes(String sizeString)

The inverse of convertToReadableSize(long).

static IOException convertToDiskSpaceIfNeeded(IOException e)

Wraps error into a disk space error if needed.

static String convertToReadableSize(long sizeLong)

Convert the given file size in bytes to a more readable format in X.Y[KMGT] format.

static void copyFile(File origFile, File destFile)

A helper method that copies a file's contents to a local file

static File createNamedTempDir(File parentDir, String name)

Helper function to create a named directory inside a foldere.

static File createNamedTempDir(String name)

Helper function to create a named directory inside your temp folder.

static File createTempDir(String prefix)

Helper function to create a temp directory in the system default temporary file directory.

static File createTempDir(String prefix, File parentDir)

Helper function to create a temp directory.

static File createTempFile(String prefix, String suffix, File parentDir)

Helper wrapper function around ERROR(/File#createTempFile(String, String, File)) that audits for potential out of disk space scenario.

static File createTempFile(String prefix, String suffix)

Helper wrapper function around ERROR(/File#createTempFile(String, String)) that audits for potential out of disk space scenario.

static File createTempFileForRemote(String remoteFilePath, File parentDir)

Helper method which constructs a unique file on temporary disk, whose name corresponds as closely as possible to the file name given by the remote file path

static File createZip(File dir)

This method is deprecated. Moved to ZipUtil#createZip(File).

static void createZip(File dir, File zipFile)

This method is deprecated. Moved to ZipUtil#createZip(File, File).

static void deleteFile(File file)

Try to delete a file.

static boolean ensureGroupRWX(File file)

Performs a best effort attempt to ensure given file group executable, readable, and writable.

static File extractFileFromZip(ZipFile zipFile, String filePath)

This method is deprecated. Moved to ZipUtil#extractFileFromZip(ZipFile, String).

static void extractZip(ZipFile zipFile, File destDir)

This method is deprecated. Moved to ZipUtil#extractZip(ZipFile, File).

static File findDirectory(String dirName, File... dirs)

Search and return the first directory ERROR(/File) among other directories.

static findDirsUnder(File rootDir, File relativeParent)

Recursively find all directories under the given rootDir

static File findFile(String fileName, IAbi abi, File... dirs)

Get all file paths of files in the given directory with name matching the given filter and also filter the found file by abi arch if abi is not null.

static File findFile(File dir, String fileName)

Recursively search given directory for first file with given name

static findFiles(String fileName, IAbi abi, boolean includeDirectory, File... dirs)

Get all files in the given directory with name matching the given filter and also filter the found files by abi arch if abi is not null.

static findFiles(File dir, String filter)

Get all file paths of files in the given directory with name matching the given filter

static findFilesObject(File dir, String filter)

Get all file paths of files in the given directory with name matching the given filter

static findFilesObject(File dir, String filter, boolean includeDirectory)

Get all file paths of files in the given directory with name matching the given filter

static String getBaseName(String fileName)

Gets the base name, without extension, of given file name.

static String getContentType(String filePath)

Get file's content type based it's extension.

static String getExtension(String fileName)

Gets the extension for given file name.

static File getFileForPath(File parentDir, String... pathSegments)

Helper method to build a system-dependent File

static String getPath(String... pathSegments)

Helper method to build a system-dependent relative path

static void gzipFile(File file, File gzipFile)

This method is deprecated. Moved to ZipUtil#gzipFile(File, File).

static void hardlinkFile(File origFile, File destFile)

A helper method that hardlinks a file to another file.

static void hardlinkFile(File origFile, File destFile, boolean ignoreExistingFile)

A helper method that hardlinks a file to another file.

static boolean isDiskSpaceError(String message)

Returns true if the message is an disk space error.

static boolean mkdirsRWX(File file)

Method to create a chain of directories, and set them all group execute/read/writable as they are created, by calling chmodGroupRWX(File).

static String readStringFromFile(File sourceFile, long startOffset, long length)

A helper method for reading partial string data from a file

static String readStringFromFile(File sourceFile)

A helper method for reading string data from a file

static void recursiveCopy(File sourceDir, File destDir)

Recursively copy folder contents.

static void recursiveDelete(File rootDir)

Recursively delete given file or directory and all its contents.

static void recursiveHardlink(File sourceDir, File destDir)

Recursively hardlink folder contents.

static void recursiveHardlink(File sourceDir, File destDir, boolean ignoreExistingFile, copyInsteadofHardlink)

Recursively hardlink folder contents.

static void recursiveHardlink(File sourceDir, File destDir, boolean ignoreExistingFile)

Recursively hardlink folder contents.

static void recursiveSymlink(File sourceDir, File destDir)

Recursively symlink folder contents.

static File saveResourceFile(InputStream resourceStream, File destDir, String targetFileName)

Save a resource file to a directory.

static void setReadableRecursive(File file)

Recursively set read and exec (if folder) permissions for given file.

static Long sizeOfDirectory(File directory)

Returns the size reported by the directory.

static void symlinkFile(File origFile, File destFile)

A helper method that symlinks a file to another file

static unixModeToPosix(int mode)

Converts an integer representing unix mode to a set of ERROR(/PosixFilePermission)s

static void writeToFile(String inputString, File destFile, boolean append)

A helper method for writing or appending string data to file

static void writeToFile(String inputString, File destFile)

A helper method for writing string data to file

static void writeToFile(InputStream input, File destFile, boolean append, long startOffset, long size)

A helper method for writing stream data to file

static void writeToFile(InputStream input, File destFile)

A helper method for writing stream data to file

static void writeToFile(InputStream input, File destFile, boolean append)

A helper method for writing stream data to file

Protected methods

static boolean chmodExists()

Internal helper to determine if 'chmod' is available on the system OS.

static void setChmodBinary(String chmodName)

Exposed for testing.

Constants

FILESYSTEM_FILENAME_MAX_LENGTH

public static final int FILESYSTEM_FILENAME_MAX_LENGTH

Constant Value: 255 (0x000000ff)

Public constructors

FileUtil

public FileUtil ()

Public methods

calculateBase64Md5

public static String calculateBase64Md5 (File file)

Helper method to calculate base64 md5 for a file.

Returns
String md5 of the file

calculateCrc32

public static long calculateCrc32 (File file)

Helper method to calculate CRC-32 for a file.

Returns
long CRC-32 of the file

Throws
IOException

calculateMd5

public static String calculateMd5 (File file)

Helper method to calculate md5 for a file.

Returns
String md5 of the file

chmod

public static boolean chmod (File file, 
                String perms)

Parameters
file File

perms String

Returns
boolean

chmodGroupRW

public static boolean chmodGroupRW (File file)

Performs a best effort attempt to make given file group readable and writable.

Note that the execute permission is required to make directories accessible. See chmodGroupRWX(File).

If 'chmod' system command is not supported by underlying OS, will set file to writable by all.

Parameters
file File: the ERROR(/File) to make owner and group writable

Returns
boolean true if file was successfully made group writable, false otherwise

chmodGroupRWX

public static boolean chmodGroupRWX (File file)

Performs a best effort attempt to make given file group executable, readable, and writable.

If 'chmod' system command is not supported by underlying OS, will attempt to set permissions for all users.

Parameters
file File: the ERROR(/File) to make owner and group writable

Returns
boolean true if permissions were set successfully, false otherwise

chmodRWXRecursively

public static boolean chmodRWXRecursively (File file)

Parameters
file File

Returns
boolean

closeZip

public static void closeZip (ZipFile zipFile)

This method is deprecated.
Moved to ZipUtil#closeZip(ZipFile).

Close an open ERROR(/ZipFile), ignoring any exceptions.

Parameters
zipFile ZipFile: the file to close

collectJars

public static  collectJars (File dir)

Returns all jar files found in given directory

Parameters
dir File

Returns

compareFileContents

public static boolean compareFileContents (File file1, 
                File file2)

Utility method to do byte-wise content comparison of two files.

Parameters
file1 File

file2 File

Returns
boolean true if file contents are identical

convertSizeToBytes

public static long convertSizeToBytes (String sizeString)

The inverse of convertToReadableSize(long). Converts the readable format described in convertToReadableSize(long) to a byte value.

Parameters
sizeString String: the string description of the size.

Returns
long the size in bytes

Throws
IllegalArgumentException if cannot recognize size

convertToDiskSpaceIfNeeded

public static IOException convertToDiskSpaceIfNeeded (IOException e)

Wraps error into a disk space error if needed.

Parameters
e IOException

Returns
IOException

convertToReadableSize

public static String convertToReadableSize (long sizeLong)

Convert the given file size in bytes to a more readable format in X.Y[KMGT] format.

Parameters
sizeLong long: file size in bytes

Returns
String descriptive string of file size

copyFile

public static void copyFile (File origFile, 
                File destFile)

A helper method that copies a file's contents to a local file

Parameters
origFile File: the original file to be copied

destFile File: the destination file

Throws
if failed to copy file

createNamedTempDir

public static File createNamedTempDir (File parentDir, 
                String name)

Helper function to create a named directory inside a foldere.

This directory will not have its name randomized. If the directory already exists it will be returned.

Parameters
parentDir File: the directory where to create the dir. If null, will be in /tmp

name String: The name of the directory to create in the parent folder

Returns
File the created directory

createNamedTempDir

public static File createNamedTempDir (String name)

Helper function to create a named directory inside your temp folder.

This directory will not have its name randomized. If the directory already exists it will be returned.

Parameters
name String: The name of the directory to create in your tmp folder.

Returns
File the created directory

createTempDir

public static File createTempDir (String prefix)

Helper function to create a temp directory in the system default temporary file directory.

Parameters
prefix String: The prefix string to be used in generating the file's name; must be at least three characters long

Returns
File the created directory

Throws
if file could not be created

createTempDir

public static File createTempDir (String prefix, 
                File parentDir)

Helper function to create a temp directory.

Parameters
prefix String: The prefix string to be used in generating the file's name; must be at least three characters long

parentDir File: The parent directory in which the directory is to be created. If null the system default temp directory will be used.

Returns
File the created directory

Throws
if file could not be created

createTempFile

public static File createTempFile (String prefix, 
                String suffix, 
                File parentDir)

Helper wrapper function around ERROR(/File#createTempFile(String, String, File)) that audits for potential out of disk space scenario.

Parameters
prefix String

suffix String

parentDir File

Returns
File

Throws
FileUtil.LowDiskSpaceException if disk space on partition is lower than minimum allowed

createTempFile

public static File createTempFile (String prefix, 
                String suffix)

Helper wrapper function around ERROR(/File#createTempFile(String, String)) that audits for potential out of disk space scenario.

Parameters
prefix String

suffix String

Returns
File

Throws
FileUtil.LowDiskSpaceException if disk space on temporary partition is lower than minimum allowed

createTempFileForRemote

public static File createTempFileForRemote (String remoteFilePath, 
                File parentDir)

Helper method which constructs a unique file on temporary disk, whose name corresponds as closely as possible to the file name given by the remote file path

Parameters
remoteFilePath String: the '/' separated remote path to construct the name from

parentDir File: the parent directory to create the file in. null to use the default temporary directory

Returns
File

createZip

public static File createZip (File dir)

This method is deprecated.
Moved to ZipUtil#createZip(File).

Utility method to create a temporary zip file containing the given directory and all its contents.

Parameters
dir File: the directory to zip

Returns
File a temporary zip ERROR(/File) containing directory contents

Throws
if failed to create zip file

createZip

public static void createZip (File dir, 
                File zipFile)

This method is deprecated.
Moved to ZipUtil#createZip(File, File).

Utility method to create a zip file containing the given directory and all its contents.

Parameters
dir File: the directory to zip

zipFile File: the zip file to create - it should not already exist

Throws
if failed to create zip file

deleteFile

public static void deleteFile (File file)

Try to delete a file. Intended for use when cleaning up in finally stanzas.

Parameters
file File: may be null.

ensureGroupRWX

public static boolean ensureGroupRWX (File file)

Performs a best effort attempt to ensure given file group executable, readable, and writable.

If 'chmod' system command is not supported by underlying OS, will attempt to set permissions for all users. The operation is synchronized to prevent race condition introduced by accessing files from a cache, e.g., GCSFileDownloader.

Parameters
file File: the ERROR(/File) to make owner and group writable

Returns
boolean true if permissions were set successfully, false otherwise

extractFileFromZip

public static File extractFileFromZip (ZipFile zipFile, 
                String filePath)

This method is deprecated.
Moved to ZipUtil#extractFileFromZip(ZipFile, String).

Utility method to extract one specific file from zip file into a tmp file

Parameters
zipFile ZipFile: the ERROR(/ZipFile) to extract

filePath String: the filePath of to extract

Returns
File the ERROR(/File) or null if not found

Throws
if failed to extract file

extractZip

public static void extractZip (ZipFile zipFile, 
                File destDir)

This method is deprecated.
Moved to ZipUtil#extractZip(ZipFile, File).

Utility method to extract entire contents of zip file into given directory

Parameters
zipFile ZipFile: the ERROR(/ZipFile) to extract

destDir File: the local dir to extract file to

Throws
if failed to extract file

findDirectory

public static File findDirectory (String dirName, 
                File... dirs)

Search and return the first directory ERROR(/File) among other directories.

Parameters
dirName String: The directory name we are looking for.

dirs File: The list of directories we are searching.

Returns
File a ERROR(/File) with the directory found or Null if not found.

Throws
IOException

findDirsUnder

public static  findDirsUnder (File rootDir, 
                File relativeParent)

Recursively find all directories under the given rootDir

Parameters
rootDir File: the root directory to search in

relativeParent File: An optional parent for all ERROR(/File)s returned. If not specified, all ERROR(/File)s will be relative to rootDir.

Returns
An set of ERROR(/File)s, representing all directories under rootDir, including rootDir itself. If rootDir is null, an empty set is returned.

findFile

public static File findFile (String fileName, 
                IAbi abi, 
                File... dirs)

Get all file paths of files in the given directory with name matching the given filter and also filter the found file by abi arch if abi is not null. Return the first match file found.

Parameters
fileName String: String of the regex to match file path

abi IAbi: IAbi object of the abi to match the target

dirs File: a varargs array of ERROR(/File) object of the directories to search for files

Returns
File the ERROR(/File) or null if it could not be found

findFile

public static File findFile (File dir, 
                String fileName)

Recursively search given directory for first file with given name

Parameters
dir File: the directory to search

fileName String: the name of the file to search for

Returns
File the ERROR(/File) or null if it could not be found

findFiles

public static  findFiles (String fileName, 
                IAbi abi, 
                boolean includeDirectory, 
                File... dirs)

Get all files in the given directory with name matching the given filter and also filter the found files by abi arch if abi is not null.

Parameters
fileName String: String of the regex to match file path

abi IAbi: IAbi object of the abi to match the target

includeDirectory boolean: whether to include directories in the search result

dirs File: an array of ERROR(/File) object of the directories to search for files

Returns
a set of ERROR(/File)s or empty if it could not be found

findFiles

public static  findFiles (File dir, 
                String filter)

Get all file paths of files in the given directory with name matching the given filter

Parameters
dir File: ERROR(/File) object of the directory to search for files recursively

filter String: String of the regex to match file names

Returns
a set of String of the file paths

findFilesObject

public static  findFilesObject (File dir, 
                String filter)

Get all file paths of files in the given directory with name matching the given filter

Parameters
dir File: ERROR(/File) object of the directory to search for files recursively

filter String: String of the regex to match file names

Returns
a set of ERROR(/File) of the file objects. @See findFiles(File, String)

findFilesObject

public static  findFilesObject (File dir, 
                String filter, 
                boolean includeDirectory)

Get all file paths of files in the given directory with name matching the given filter

Parameters
dir File: ERROR(/File) object of the directory to search for files recursively

filter String: String of the regex to match file names

includeDirectory boolean: whether to include directories in the search result

Returns
a set of ERROR(/File) of the file objects. @See findFiles(File, String)

getBaseName

public static String getBaseName (String fileName)

Gets the base name, without extension, of given file name.

e.g. getBaseName("file.txt") will return "file"

Returns
String the base name

getContentType

public static String getContentType (String filePath)

Get file's content type based it's extension.

Parameters
filePath String: the file path

Returns
String content type

getExtension

public static String getExtension (String fileName)

Gets the extension for given file name.

Returns
String the extension or empty String if file has no extension

getFileForPath

public static File getFileForPath (File parentDir, 
                String... pathSegments)

Helper method to build a system-dependent File

Parameters
parentDir File: the parent directory to use.

pathSegments String: the relative path segments to use

Returns
File the ERROR(/File) representing given path, with each pathSegment separated by ERROR(/File#separatorChar)

getPath

public static String getPath (String... pathSegments)

Helper method to build a system-dependent relative path

Parameters
pathSegments String: the relative path segments to use

Returns
String the String representing given path, with each pathSegment separated by ERROR(/File#separatorChar)

gzipFile

public static void gzipFile (File file, 
                File gzipFile)

This method is deprecated.
Moved to ZipUtil#gzipFile(File, File).

Helper method to create a gzipped version of a single file.

Parameters
file File: the original file

gzipFile File: the file to place compressed contents in

Throws
IOException

hardlinkFile

public static void hardlinkFile (File origFile, 
                File destFile)

A helper method that hardlinks a file to another file. Fallback to copy in case of cross partition linking.

Parameters
origFile File: the original file

destFile File: the destination file

Throws
if failed to hardlink file

hardlinkFile

public static void hardlinkFile (File origFile, 
                File destFile, 
                boolean ignoreExistingFile)

A helper method that hardlinks a file to another file. Fallback to copy in case of cross partition linking.

Parameters
origFile File: the original file

destFile File: the destination file

ignoreExistingFile boolean: If True and the file being linked already exists, skip the exception.

Throws
if failed to hardlink file

isDiskSpaceError

public static boolean isDiskSpaceError (String message)

Returns true if the message is an disk space error.

Parameters
message String

Returns
boolean

mkdirsRWX

public static boolean mkdirsRWX (File file)

Method to create a chain of directories, and set them all group execute/read/writable as they are created, by calling chmodGroupRWX(File). Essentially a version of ERROR(/File#mkdirs()) that also runs chmod(File, String).

Parameters
file File: the name of the directory to create, possibly with containing directories that don't yet exist.

Returns
boolean true if file exists and is a directory, false otherwise.

readStringFromFile

public static String readStringFromFile (File sourceFile, 
                long startOffset, 
                long length)

A helper method for reading partial string data from a file

Parameters
sourceFile File: the file to read from

startOffset long: the start offset to read from the file.

length long: the number of bytes to read of the file.

Returns
String

Throws
IOException
FileNotFoundException

readStringFromFile

public static String readStringFromFile (File sourceFile)

A helper method for reading string data from a file

Parameters
sourceFile File: the file to read from

Returns
String

Throws
IOException
FileNotFoundException

recursiveCopy

public static void recursiveCopy (File sourceDir, 
                File destDir)

Recursively copy folder contents.

Only supports copying of files and directories - symlinks are not copied. If the destination directory does not exist, it will be created.

Parameters
sourceDir File: the folder that contains the files to copy

destDir File: the destination folder

Throws
IOException

recursiveDelete

public static void recursiveDelete (File rootDir)

Recursively delete given file or directory and all its contents.

Parameters
rootDir File: the directory or file to be deleted; can be null

recursiveHardlink

public static void recursiveHardlink (File sourceDir, 
                File destDir)

Recursively hardlink folder contents.

Only supports copying of files and directories - symlinks are not copied. If the destination directory does not exist, it will be created.

Parameters
sourceDir File: the folder that contains the files to copy

destDir File: the destination folder

Throws
IOException

recursiveHardlink

public static void recursiveHardlink (File sourceDir, 
                File destDir, 
                boolean ignoreExistingFile, 
                 copyInsteadofHardlink)

Recursively hardlink folder contents.

Only supports copying of files and directories - symlinks are not copied. If the destination directory does not exist, it will be created.

Parameters
sourceDir File: the folder that contains the files to copy

destDir File: the destination folder

ignoreExistingFile boolean: If True and the file being linked already exists, skip the exception.

copyInsteadofHardlink : Set of files that needs to be copied instead of linked.

Throws
IOException

recursiveHardlink

public static void recursiveHardlink (File sourceDir, 
                File destDir, 
                boolean ignoreExistingFile)

Recursively hardlink folder contents.

Only supports copying of files and directories - symlinks are not copied. If the destination directory does not exist, it will be created.

Parameters
sourceDir File: the folder that contains the files to copy

destDir File: the destination folder

ignoreExistingFile boolean: If True and the file being linked already exists, skip the exception.

Throws
IOException

recursiveSymlink

public static void recursiveSymlink (File sourceDir, 
                File destDir)

Recursively symlink folder contents.

Only supports copying of files and directories - symlinks are not copied. If the destination directory does not exist, it will be created.

Parameters
sourceDir File: the folder that contains the files to copy

destDir File: the destination folder

Throws
IOException

saveResourceFile

public static File saveResourceFile (InputStream resourceStream, 
                File destDir, 
                String targetFileName)

Save a resource file to a directory.

Parameters
resourceStream InputStream: a {link InputStream} object to the resource to be saved.

destDir File: a ERROR(/File) object of a directory to where the resource file will be saved.

targetFileName String: a String for the name of the file to be saved to.

Returns
File a ERROR(/File) object of the file saved.

Throws
if the file failed to be saved.

setReadableRecursive

public static void setReadableRecursive (File file)

Recursively set read and exec (if folder) permissions for given file.

Parameters
file File

sizeOfDirectory

public static Long sizeOfDirectory (File directory)

Returns the size reported by the directory.

Parameters
directory File

Returns
Long

symlinkFile

public static void symlinkFile (File origFile, 
                File destFile)

A helper method that symlinks a file to another file

Parameters
origFile File: the original file

destFile File: the destination file

Throws
if failed to symlink file

unixModeToPosix

public static  unixModeToPosix (int mode)

Converts an integer representing unix mode to a set of ERROR(/PosixFilePermission)s

Parameters
mode int

Returns

writeToFile

public static void writeToFile (String inputString, 
                File destFile, 
                boolean append)

A helper method for writing or appending string data to file

Parameters
inputString String: the input String

destFile File: the destination file to write or append to

append boolean: append to end of file if true, overwrite otherwise

writeToFile

public static void writeToFile (String inputString, 
                File destFile)

A helper method for writing string data to file

Parameters
inputString String: the input String

destFile File: the destination file to write to

writeToFile

public static void writeToFile (InputStream input, 
                File destFile, 
                boolean append, 
                long startOffset, 
                long size)

A helper method for writing stream data to file

Parameters
input InputStream: the unbuffered input stream

destFile File: the destination file to write or append to

append boolean: append to end of file if true, overwrite otherwise

startOffset long: the start offset of the input stream to retrieve data

size long: number of bytes to retrieve from the input stream, set it to a negative value to retrieve all content starting at the given offset.

writeToFile

public static void writeToFile (InputStream input, 
                File destFile)

A helper method for writing stream data to file

Parameters
input InputStream: the unbuffered input stream

destFile File: the destination file to write to

writeToFile

public static void writeToFile (InputStream input, 
                File destFile, 
                boolean append)

A helper method for writing stream data to file

Parameters
input InputStream: the unbuffered input stream

destFile File: the destination file to write or append to

append boolean: append to end of file if true, overwrite otherwise

Protected methods

chmodExists

protected static boolean chmodExists ()

Internal helper to determine if 'chmod' is available on the system OS.

Returns
boolean

setChmodBinary

protected static void setChmodBinary (String chmodName)

Exposed for testing. Allows to modify the chmod binary name we look for, in order to tests system with no chmod support.

Parameters
chmodName String