public class FileUtils extends Object
| Modifier and Type | Class and Description |
|---|---|
static class |
FileUtils.FileCopyResult
Keeps results of a file copy, including children and total size of the resultant files.
|
static interface |
FileUtils.InputStreamSupplier
Like
ByteSource, but this is an interface, which allows use of lambdas. |
static class |
FileUtils.LinkOrCopyResult |
static interface |
FileUtils.OutputStreamConsumer<T> |
| Modifier and Type | Field and Description |
|---|---|
static com.google.common.base.Predicate<Throwable> |
IS_EXCEPTION
Useful for retry functionality that doesn't want to stop Throwables, but does want to retry on Exceptions
|
| Constructor and Description |
|---|
FileUtils() |
| Modifier and Type | Method and Description |
|---|---|
static long |
copyLarge(FileUtils.InputStreamSupplier inputSource,
File outFile,
byte[] fetchBuffer,
com.google.common.base.Predicate<Throwable> retryCondition,
int numTries,
String messageOnRetry)
Copy a potentially large amount of data from an input source to a file.
|
static <T> long |
copyLarge(T object,
ObjectOpenFunction<T> objectOpenFunction,
File outFile,
byte[] fetchBuffer,
com.google.common.base.Predicate<Throwable> retryCondition,
int numTries,
String messageOnRetry)
Copies data from the InputStream opened with objectOpenFunction to the given file.
|
static File |
createTempDir()
Creates a temporary directory inside the configured temporary space (java.io.tmpdir).
|
static File |
createTempDir(String prefix)
Creates a temporary directory inside the configured temporary space (java.io.tmpdir).
|
static void |
deleteDirectory(File directory)
Equivalent to
FileUtils.deleteDirectory(File). |
static long |
getFileSize(File file)
Computes the size of the file.
|
static FileUtils.LinkOrCopyResult |
linkOrCopy(File src,
File dest)
Hard-link "src" as "dest", if possible.
|
static MappedByteBufferHandler |
map(File file)
Fully maps a file read-only in to memory as per
FileChannel.map(FileChannel.MapMode, long, long). |
static MappedByteBufferHandler |
map(File file,
long offset,
long length)
Fully maps a file read-only in to memory as per
FileChannel.map(FileChannel.MapMode, long, long). |
static MappedByteBufferHandler |
map(RandomAccessFile randomAccessFile,
long offset,
long length)
Fully maps a file read-only in to memory as per
FileChannel.map(FileChannel.MapMode, long, long). |
static void |
mkdirp(File directory)
Create "directory" and all intermediate directories as needed.
|
static FileUtils.FileCopyResult |
retryCopy(com.google.common.io.ByteSource byteSource,
File outFile,
com.google.common.base.Predicate<Throwable> shouldRetry,
int maxAttempts)
Copy input byte source to outFile.
|
static <T> T |
writeAtomically(File file,
File tmpDir,
FileUtils.OutputStreamConsumer<T> f)
Write to a file atomically, by first writing to a temporary file in given tmpDir directory and then moving it to
the target location.
|
static <T> T |
writeAtomically(File file,
FileUtils.OutputStreamConsumer<T> f)
Write to a file atomically, by first writing to a temporary file in the same directory and then moving it to
the target location.
|
public static final com.google.common.base.Predicate<Throwable> IS_EXCEPTION
public static FileUtils.FileCopyResult retryCopy(com.google.common.io.ByteSource byteSource, File outFile, com.google.common.base.Predicate<Throwable> shouldRetry, int maxAttempts)
byteSource - Supplier for an input stream that is to be copied. The resulting stream is closed each iterationoutFile - Where the file should be written to.shouldRetry - Predicate indicating if an error is recoverable and should be retried.maxAttempts - The maximum number of assumed recoverable attempts to try before completely failing.RuntimeException - wrapping the inner exception on failure.public static MappedByteBufferHandler map(File file) throws IOException
FileChannel.map(FileChannel.MapMode, long, long).
Files are mapped from offset 0 to its length.
This only works for files <= Integer.MAX_VALUE bytes.
Similar to Files.map(File), but returns MappedByteBufferHandler, that
makes it easier to unmap the buffer within try-with-resources pattern:
try (MappedByteBufferHandler fileMappingHandler = FileUtils.map(file)) {
ByteBuffer fileMapping = fileMappingHandler.get();
// use mapped buffer
}file - the file to mapMappedByteBufferHandler, wrapping a read-only buffer reflecting fileFileNotFoundException - if the file does not existIOException - if an I/O error occursIllegalArgumentException - if length is greater than Integer.MAX_VALUEFileChannel.map(FileChannel.MapMode, long, long)public static MappedByteBufferHandler map(File file, long offset, long length) throws IOException
FileChannel.map(FileChannel.MapMode, long, long).file - the file to mapoffset - starting offset for the mmaplength - length for the mmapMappedByteBufferHandler, wrapping a read-only buffer reflecting fileFileNotFoundException - if the file does not existIOException - if an I/O error occursIllegalArgumentException - if length is greater than Integer.MAX_VALUEFileChannel.map(FileChannel.MapMode, long, long)public static MappedByteBufferHandler map(RandomAccessFile randomAccessFile, long offset, long length) throws IOException
FileChannel.map(FileChannel.MapMode, long, long).randomAccessFile - the file to map. The file will not be closed.offset - starting offset for the mmaplength - length for the mmapMappedByteBufferHandler, wrapping a read-only buffer reflecting randomAccessFileIOException - if an I/O error occursIllegalArgumentException - if length is greater than Integer.MAX_VALUEFileChannel.map(FileChannel.MapMode, long, long)public static <T> T writeAtomically(File file, FileUtils.OutputStreamConsumer<T> f) throws IOException
writeAtomically(File, File, OutputStreamConsumer) .IOExceptionpublic static <T> T writeAtomically(File file, File tmpDir, FileUtils.OutputStreamConsumer<T> f) throws IOException
IOExceptionpublic static <T> long copyLarge(T object,
ObjectOpenFunction<T> objectOpenFunction,
File outFile,
byte[] fetchBuffer,
com.google.common.base.Predicate<Throwable> retryCondition,
int numTries,
String messageOnRetry)
throws IOException
object - object to openobjectOpenFunction - function to open the given objectoutFile - file to write datafetchBuffer - a buffer to copy data from the input stream to the fileretryCondition - condition which should be satisfied for retrynumTries - max number of retriesmessageOnRetry - log message on retryIOExceptionpublic static long copyLarge(FileUtils.InputStreamSupplier inputSource, File outFile, byte[] fetchBuffer, com.google.common.base.Predicate<Throwable> retryCondition, int numTries, String messageOnRetry) throws IOException
IOExceptionpublic static long getFileSize(File file)
public static File createTempDir()
Files.createTempDir() from Guava, but has nicer error messages.IllegalStateException - if the directory could not be createdpublic static File createTempDir(@Nullable String prefix)
Files.createTempDir() from Guava, but has nicer error messages.prefix - base directory name; if null/empty then this method will use "druid"IllegalStateException - if the directory could not be createdpublic static void mkdirp(File directory) throws IOException
File.mkdirs(), and more reliable since it is safe from races where two threads try
to create the same directory at the same time.
The name is inspired by UNIX mkdir -p, which has the same behavior.IOExceptionpublic static void deleteDirectory(File directory) throws IOException
FileUtils.deleteDirectory(File). Exists here mostly so callers
can avoid dealing with our FileUtils and the Commons FileUtils having the same name.IOExceptionpublic static FileUtils.LinkOrCopyResult linkOrCopy(File src, File dest) throws IOException
IOException - if something went wrongCopyright © 2011–2023 The Apache Software Foundation. All rights reserved.