Package org.apache.cassandra.io.util
Class FileUtils
- java.lang.Object
-
- org.apache.cassandra.io.util.FileUtils
-
public final class FileUtils extends java.lang.Object
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static voidappend(java.io.File file, java.lang.String... lines)static voidappendAndSync(java.io.File file, java.lang.String... lines)static voidclean(java.nio.ByteBuffer buffer)static voidclose(java.io.Closeable... cs)static voidclose(java.lang.Iterable<? extends java.io.Closeable> cs)static voidcloseQuietly(java.io.Closeable c)static voidcloseQuietly(java.lang.AutoCloseable c)static voidcloseQuietly(java.lang.Iterable<? extends java.lang.AutoCloseable> cs)static voidcopyTo(java.io.DataInput in, java.io.OutputStream out, int length)static voidcopyWithConfirm(java.io.File from, java.io.File to)static voidcopyWithConfirm(java.lang.String from, java.lang.String to)static voidcopyWithOutConfirm(java.lang.String from, java.lang.String to)static java.io.FilecreateDeletableTempFile(java.lang.String prefix, java.lang.String suffix)static voidcreateDirectory(java.io.File directory)static voidcreateDirectory(java.lang.String directory)static voidcreateHardLink(java.io.File from, java.io.File to)static voidcreateHardLink(java.lang.String from, java.lang.String to)static voidcreateHardLinkWithConfirm(java.io.File from, java.io.File to)static voidcreateHardLinkWithConfirm(java.lang.String from, java.lang.String to)static voidcreateHardLinkWithoutConfirm(java.lang.String from, java.lang.String to)static java.io.FilecreateTempFile(java.lang.String prefix, java.lang.String suffix)static java.io.FilecreateTempFile(java.lang.String prefix, java.lang.String suffix, java.io.File directory)Pretty much likeFile.createTempFile(String, String, File), but with the guarantee that the "random" part of the generated file name betweenprefixandsuffixis a positive, increasinglongvalue.static voiddelete(java.io.File... files)static booleandelete(java.lang.String file)static voiddeleteAsync(java.lang.String file)static voiddeleteChildrenRecursive(java.io.File dir)Deletes all files and subdirectories under "dir".static voiddeleteDirectoryIfEmpty(java.nio.file.Path path)Deletes the specified directory if it is emptystatic voiddeleteRecursive(java.io.File dir)Deletes the specified directory after having deleted its content.static voiddeleteRecursiveOnExit(java.io.File dir)Schedules deletion of all file and subdirectories under "dir" on JVM shutdown.static voiddeleteRecursiveWithThrottle(java.io.File dir, com.google.common.util.concurrent.RateLimiter rateLimiter)Deletes all files and subdirectories under "dir".static voiddeleteWithConfirm(java.io.File file)static java.lang.ThrowabledeleteWithConfirm(java.io.File file, java.lang.Throwable accumulate)static java.lang.ThrowabledeleteWithConfirm(java.io.File file, java.lang.Throwable accumulate, com.google.common.util.concurrent.RateLimiter rateLimiter)static voiddeleteWithConfirm(java.lang.String file)static java.lang.ThrowabledeleteWithConfirm(java.lang.String filePath, java.lang.Throwable accumulate)static voiddeleteWithConfirmWithThrottle(java.io.File file, com.google.common.util.concurrent.RateLimiter rateLimiter)static longfolderSize(java.io.File folder)Get the size of a directory in bytesstatic java.lang.StringgetCanonicalPath(java.io.File file)static java.lang.StringgetCanonicalPath(java.lang.String filename)static java.nio.file.FileStoregetFileStore(java.nio.file.Path path)Returns theFileStorerepresenting the file store where a file is located.static longgetFreeSpace(java.io.File file)Returns the number of unallocated bytes on the specified partition.static java.lang.StringgetRelativePath(java.lang.String basePath, java.lang.String path)Convert absolute path into a path relative to the base pathstatic java.io.FilegetTempDir()static longgetTotalSpace(java.io.File file)Returns the size of the specified partition.static longgetUsableSpace(java.io.File file)Returns the number of available bytes on the specified partition.static voidhandleCorruptSSTable(CorruptSSTableException e)static voidhandleFSError(FSError e)static voidhandleFSErrorAndPropagate(FSError e)handleFSErrorAndPropagate will invoke the disk failure policy error handler, which may or may not stop the daemon or transports.static booleanisContained(java.io.File folder, java.io.File file)Return true if file is contained in folderstatic booleanisSubDirectory(java.io.File parent, java.io.File child)static voidmoveRecursively(java.nio.file.Path source, java.nio.file.Path target)Moves the contents of a directory to another directory.static longparseFileSize(java.lang.String value)static java.util.List<java.lang.String>readLines(java.io.File file)static voidrenameWithConfirm(java.io.File from, java.io.File to)static voidrenameWithConfirm(java.lang.String from, java.lang.String to)static voidrenameWithOutConfirm(java.lang.String from, java.lang.String to)static voidreplace(java.io.File file, java.lang.String... lines)static voidsetFSErrorHandler(FSErrorHandler handler)static java.lang.StringstringifyFileSize(double value)static voidtruncate(java.lang.String path, long size)static voidwrite(java.io.File file, java.util.List<java.lang.String> lines, java.nio.file.StandardOpenOption... options)Write lines to a file adding a newline to the end of each supplied line using the provided open options.
-
-
-
Field Detail
-
CHARSET
public static final java.nio.charset.Charset CHARSET
-
ONE_KB
public static final long ONE_KB
- See Also:
- Constant Field Values
-
ONE_MB
public static final long ONE_MB
- See Also:
- Constant Field Values
-
ONE_GB
public static final long ONE_GB
- See Also:
- Constant Field Values
-
ONE_TB
public static final long ONE_TB
- See Also:
- Constant Field Values
-
-
Method Detail
-
getTempDir
public static java.io.File getTempDir()
-
createTempFile
public static java.io.File createTempFile(java.lang.String prefix, java.lang.String suffix, java.io.File directory)Pretty much likeFile.createTempFile(String, String, File), but with the guarantee that the "random" part of the generated file name betweenprefixandsuffixis a positive, increasinglongvalue.
-
createTempFile
public static java.io.File createTempFile(java.lang.String prefix, java.lang.String suffix)
-
createDeletableTempFile
public static java.io.File createDeletableTempFile(java.lang.String prefix, java.lang.String suffix)
-
createHardLink
public static void createHardLink(java.lang.String from, java.lang.String to)
-
createHardLink
public static void createHardLink(java.io.File from, java.io.File to)
-
createHardLinkWithConfirm
public static void createHardLinkWithConfirm(java.io.File from, java.io.File to)
-
createHardLinkWithConfirm
public static void createHardLinkWithConfirm(java.lang.String from, java.lang.String to)
-
createHardLinkWithoutConfirm
public static void createHardLinkWithoutConfirm(java.lang.String from, java.lang.String to)
-
deleteWithConfirm
public static java.lang.Throwable deleteWithConfirm(java.lang.String filePath, java.lang.Throwable accumulate)
-
deleteWithConfirm
public static java.lang.Throwable deleteWithConfirm(java.io.File file, java.lang.Throwable accumulate)
-
deleteWithConfirm
public static java.lang.Throwable deleteWithConfirm(java.io.File file, java.lang.Throwable accumulate, com.google.common.util.concurrent.RateLimiter rateLimiter)
-
deleteWithConfirm
public static void deleteWithConfirm(java.lang.String file)
-
deleteWithConfirm
public static void deleteWithConfirm(java.io.File file)
-
deleteWithConfirmWithThrottle
public static void deleteWithConfirmWithThrottle(java.io.File file, com.google.common.util.concurrent.RateLimiter rateLimiter)
-
copyWithOutConfirm
public static void copyWithOutConfirm(java.lang.String from, java.lang.String to)
-
copyWithConfirm
public static void copyWithConfirm(java.lang.String from, java.lang.String to)
-
copyWithConfirm
public static void copyWithConfirm(java.io.File from, java.io.File to)
-
renameWithOutConfirm
public static void renameWithOutConfirm(java.lang.String from, java.lang.String to)
-
renameWithConfirm
public static void renameWithConfirm(java.lang.String from, java.lang.String to)
-
renameWithConfirm
public static void renameWithConfirm(java.io.File from, java.io.File to)
-
truncate
public static void truncate(java.lang.String path, long size)
-
closeQuietly
public static void closeQuietly(java.io.Closeable c)
-
closeQuietly
public static void closeQuietly(java.lang.AutoCloseable c)
-
close
public static void close(java.io.Closeable... cs) throws java.io.IOException- Throws:
java.io.IOException
-
close
public static void close(java.lang.Iterable<? extends java.io.Closeable> cs) throws java.io.IOException- Throws:
java.io.IOException
-
closeQuietly
public static void closeQuietly(java.lang.Iterable<? extends java.lang.AutoCloseable> cs)
-
getCanonicalPath
public static java.lang.String getCanonicalPath(java.lang.String filename)
-
getCanonicalPath
public static java.lang.String getCanonicalPath(java.io.File file)
-
isContained
public static boolean isContained(java.io.File folder, java.io.File file)Return true if file is contained in folder
-
getRelativePath
public static java.lang.String getRelativePath(java.lang.String basePath, java.lang.String path)Convert absolute path into a path relative to the base path
-
clean
public static void clean(java.nio.ByteBuffer buffer)
-
createDirectory
public static void createDirectory(java.lang.String directory)
-
createDirectory
public static void createDirectory(java.io.File directory)
-
delete
public static boolean delete(java.lang.String file)
-
delete
public static void delete(java.io.File... files)
-
deleteAsync
public static void deleteAsync(java.lang.String file)
-
parseFileSize
public static long parseFileSize(java.lang.String value)
-
stringifyFileSize
public static java.lang.String stringifyFileSize(double value)
-
deleteRecursiveWithThrottle
public static void deleteRecursiveWithThrottle(java.io.File dir, com.google.common.util.concurrent.RateLimiter rateLimiter)Deletes all files and subdirectories under "dir".- Parameters:
dir- Directory to be deleted- Throws:
FSWriteError- if any part of the tree cannot be deleted
-
deleteRecursive
public static void deleteRecursive(java.io.File dir)
Deletes the specified directory after having deleted its content.- Parameters:
dir- Directory to be deleted- Throws:
FSWriteError- if any part of the tree cannot be deleted
-
deleteChildrenRecursive
public static void deleteChildrenRecursive(java.io.File dir)
Deletes all files and subdirectories under "dir".- Parameters:
dir- Directory to be deleted- Throws:
FSWriteError- if any part of the tree cannot be deleted
-
deleteRecursiveOnExit
public static void deleteRecursiveOnExit(java.io.File dir)
Schedules deletion of all file and subdirectories under "dir" on JVM shutdown.- Parameters:
dir- Directory to be deleted
-
handleCorruptSSTable
public static void handleCorruptSSTable(CorruptSSTableException e)
-
handleFSError
public static void handleFSError(FSError e)
-
handleFSErrorAndPropagate
public static void handleFSErrorAndPropagate(FSError e)
handleFSErrorAndPropagate will invoke the disk failure policy error handler, which may or may not stop the daemon or transports. However, if we don't exit, we still want to propagate the exception to the caller in case they have custom exception handling- Parameters:
e- A filesystem error
-
folderSize
public static long folderSize(java.io.File folder)
Get the size of a directory in bytes- Parameters:
folder- The directory for which we need size.- Returns:
- The size of the directory
-
copyTo
public static void copyTo(java.io.DataInput in, java.io.OutputStream out, int length) throws java.io.IOException- Throws:
java.io.IOException
-
isSubDirectory
public static boolean isSubDirectory(java.io.File parent, java.io.File child) throws java.io.IOException- Throws:
java.io.IOException
-
append
public static void append(java.io.File file, java.lang.String... lines)
-
appendAndSync
public static void appendAndSync(java.io.File file, java.lang.String... lines)
-
replace
public static void replace(java.io.File file, java.lang.String... lines)
-
write
public static void write(java.io.File file, java.util.List<java.lang.String> lines, java.nio.file.StandardOpenOption... options)Write lines to a file adding a newline to the end of each supplied line using the provided open options. If open option sync or dsync is provided this will not open the file with sync or dsync since it might end up syncing many times for a lot of lines. Instead it will write all the lines and sync once at the end. Since the file is never returned there is not much difference from the perspective of the caller.- Parameters:
file-lines-options-
-
readLines
public static java.util.List<java.lang.String> readLines(java.io.File file)
-
setFSErrorHandler
public static void setFSErrorHandler(FSErrorHandler handler)
-
getTotalSpace
public static long getTotalSpace(java.io.File file)
Returns the size of the specified partition.This method handles large file system by returning
Long.MAX_VALUEif the size overflow. See JDK-8179320 for more information.- Parameters:
file- the partition- Returns:
- the size, in bytes, of the partition or
0Lif the abstract pathname does not name a partition
-
getFreeSpace
public static long getFreeSpace(java.io.File file)
Returns the number of unallocated bytes on the specified partition.This method handles large file system by returning
Long.MAX_VALUEif the number of unallocated bytes overflow. See JDK-8179320 for more information- Parameters:
file- the partition- Returns:
- the number of unallocated bytes on the partition or
0Lif the abstract pathname does not name a partition.
-
getUsableSpace
public static long getUsableSpace(java.io.File file)
Returns the number of available bytes on the specified partition.This method handles large file system by returning
Long.MAX_VALUEif the number of available bytes overflow. See JDK-8179320 for more information- Parameters:
file- the partition- Returns:
- the number of available bytes on the partition or
0Lif the abstract pathname does not name a partition.
-
getFileStore
public static java.nio.file.FileStore getFileStore(java.nio.file.Path path) throws java.io.IOExceptionReturns theFileStorerepresenting the file store where a file is located. ThisFileStorehandles large file system by returningLong.MAX_VALUEfromFileStore#getTotalSpace(),FileStore#getUnallocatedSpace()andFileStore#getUsableSpace()it the value is bigger thanLong.MAX_VALUE. See JDK-8162520 for more information.- Parameters:
path- the path to the file- Returns:
- the file store where the file is stored
- Throws:
java.io.IOException
-
moveRecursively
public static void moveRecursively(java.nio.file.Path source, java.nio.file.Path target) throws java.io.IOExceptionMoves the contents of a directory to another directory.Once a file has been copied to the target directory it will be deleted from the source directory. If a file already exists in the target directory a warning will be logged and the file will not be deleted.
- Parameters:
source- the directory containing the files to movetarget- the directory where the files must be moved- Throws:
java.io.IOException
-
deleteDirectoryIfEmpty
public static void deleteDirectoryIfEmpty(java.nio.file.Path path) throws java.io.IOExceptionDeletes the specified directory if it is empty- Parameters:
path- the path to the directory- Throws:
java.io.IOException
-
-