Class FileUtil
- Version:
- 2.0
- Author:
- Nicklas
- Last modified
- $Date$
-
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic boolean
checkMagicNumber
(PushbackInputStream pin, byte[] bCheck) Take a peek at an input stream and check if the first few bytes matches the bCheck parameter.static void
Close aCloseable
without throwing an exception.static void
close
(InputStream in) Close an input stream without throwing an exception.static void
close
(OutputStream out) Close an output stream without throwing an exception.static void
Close aAutoCloseable
without throwing an exception.static long
copy
(InputStream in, OutputStream out) Copy from the input stream to the output stream until end of file is reached.static long
copy
(InputStream in, OutputStream out, AbsoluteProgressReporter progress) Copy from the input stream to the output stream until end of file is reached.static long
Copy from the reader to the writer until end of file is reached.static long
copy
(Reader in, Writer out, AbsoluteProgressReporter progress) Copy from the reader to the writer until end of file is reached.static File
createTempDirectory
(String prefix, String suffix, File directory) Creates a temporary directory.static int
deleteEmptyDirectories
(File root, boolean deleteRoot) Deletes all empty subdirectories and, optionally, also the root directory.static int
deleteTempDirectory
(File root) Recursively delete all files and subdirectories within a given directory and the also the given directory itself.static void
doRecursively
(DbControl dc, Directory root, Filter<? super Directory> dirFilter, Filter<? super File> fileFilter) Do something recursively to all files and sub-directories in specified root directory.findFiles
(File directory, FileFilter filter) Find files matching a given filter in a directory.getDirectoryTree
(DbControl dc, Directory directory) Get the complete tree of sub-directories from a given directory.static InputStream
getInputStream
(File file) Get a bufferedInputStream
object reading from the specified file.static OutputStream
getOutputStream
(File file) Get a bufferedOutputStream
object writing to the specified file.loadMinimalDirectoryTree
(DbControl dc, Collection<Include> include, Directory... directories) Load a minimal directory tree that includes at least all the specified directories and their parent directories all the way up to the root or as far as the logged in user has read permission.loadMinimalDirectoryTree
(DbControl dc, Directory... directories) Load a minimal directory tree that includes at least all the specified directories and their parent directories all the way up to the root or as far as the logged in user has read permission.static long
read
(InputStream in) Read from the input stream until the end is reached.uploadFiles
(DbControl dc, Directory toDir, File fromDir, FileFilter filter, boolean overwrite, ProgressReporter progress) Upload multiple files to BASE.
-
Field Details
-
BUFFER_SIZE
private static final int BUFFER_SIZE- See Also:
-
-
Constructor Details
-
FileUtil
public FileUtil()
-
-
Method Details
-
copy
Copy from the input stream to the output stream until end of file is reached.- Parameters:
in
- TheInputStream
to read fromout
- TheOutputStream
to write to- Returns:
- The number of bytes copied
- Throws:
IOException
- This exception is thrown if there is an error
-
copy
public static long copy(InputStream in, OutputStream out, AbsoluteProgressReporter progress) throws IOException Copy from the input stream to the output stream until end of file is reached.- Parameters:
in
- TheInputStream
to read fromout
- TheOutputStream
to write toprogress
- An optional progress reporter- Returns:
- The number of bytes copied
- Throws:
IOException
- This exception is thrown if there is an error- Since:
- 2.5
-
copy
Copy from the reader to the writer until end of file is reached.- Parameters:
in
- TheReader
to read fromout
- TheWriter
to write to- Returns:
- The number of characters copied
- Throws:
IOException
- This exception is thrown if there is an error- Since:
- 2.12
-
copy
public static long copy(Reader in, Writer out, AbsoluteProgressReporter progress) throws IOException Copy from the reader to the writer until end of file is reached.- Parameters:
in
- TheReader
to read fromout
- TheWriter
to write toprogress
- An optional progress reporter- Returns:
- The number of characters copied
- Throws:
IOException
- This exception is thrown if there is an error- Since:
- 2.12
-
read
Read from the input stream until the end is reached.- Parameters:
in
- TheInputStream
to read from- Returns:
- The number of bytes copied
- Throws:
IOException
- This exception is thrown if there is an error- Since:
- 2.4
-
close
Close an input stream without throwing an exception.- Parameters:
in
- The input stream to close- Since:
- 2.9
-
close
Close an output stream without throwing an exception.- Parameters:
out
- The output stream to close- Since:
- 2.9
-
close
Close aCloseable
without throwing an exception.- Parameters:
c
- The object to close- Since:
- 3.5
-
close
Close aAutoCloseable
without throwing an exception.- Parameters:
c
- The object to close- Since:
- 3.18
-
getInputStream
Get a bufferedInputStream
object reading from the specified file.- Parameters:
file
- The path to the file to read from- Returns:
- A buffered
InputStream
- Throws:
IOException
- If the stream cannot be opened
-
getOutputStream
Get a bufferedOutputStream
object writing to the specified file.- Parameters:
file
- The path to the file to write to- Returns:
- A buffered
OutputStream
- Throws:
IOException
- If the stream cannot be opened
-
checkMagicNumber
Take a peek at an input stream and check if the first few bytes matches the bCheck parameter. After the check the bytes are pushed back to the stream again. This method is useful to check the file format of a file, which can often be done by checing the first few bytes at the start of the file. For example to check if the file is gzip file:bCheck = new byte[] { 0x1f, (byte)0x8b };
- Parameters:
pin
- The input stream to read from which must have large enough buffer to be able to unread the bytesbCheck
- The byte values to use for comparison- Returns:
- TRUE if the file matches the specified bytes, FALSE otherwise
- Throws:
IOException
- If there is an error reading or unreading the bytesNullPointerException
- If pin or bCheck is nullIllegalArgumentException
- If bCheck is zero length- Since:
- 2.4
-
getDirectoryTree
public static Map<Directory,List<Directory>> getDirectoryTree(DbControl dc, Directory directory) throws InvalidDataException, BaseException Get the complete tree of sub-directories from a given directory.NOTE!!! This method has very bad performance on large directory trees. Consider if it is not possible to use the
loadMinimalDirectoryTree(DbControl, Directory...)
instead together with lazy loading of subdirectories.- Parameters:
dc
- DbControl used to access the database.directory
- The directory to start with- Returns:
- A
Map
which maps a directory to a list of it's sub-directories. Directories without sub-directories are not included in the map. - Throws:
InvalidDataException
- If the directory is nullBaseException
- If there is another error
-
loadMinimalDirectoryTree
public static Map<Directory,List<Directory>> loadMinimalDirectoryTree(DbControl dc, Directory... directories) throws InvalidDataException, BaseException Load a minimal directory tree that includes at least all the specified directories and their parent directories all the way up to the root or as far as the logged in user has read permission.- Parameters:
dc
- A DbControl for accessing the databasedirectories
- An array of directories- Returns:
- A
Map
which maps a directory to a list of it's sub-directories. The map is guaranteed to contain an entry for each of the given directories, but the list may be empty if there are no subdirectories - Throws:
BaseException
- If there is an errorInvalidDataException
- Since:
- 2.11
-
loadMinimalDirectoryTree
public static Map<Directory,List<Directory>> loadMinimalDirectoryTree(DbControl dc, Collection<Include> include, Directory... directories) throws InvalidDataException, BaseException Load a minimal directory tree that includes at least all the specified directories and their parent directories all the way up to the root or as far as the logged in user has read permission.- Parameters:
dc
- A DbControl for accessing the databaseinclude
- Include settings for loading sub directoriesdirectories
- An array of directories- Returns:
- A
Map
which maps a directory to a list of it's sub-directories. The map is guaranteed to contain an entry for each of the given directories, but the list may be empty if there are no subdirectories - Throws:
BaseException
- If there is an errorInvalidDataException
- Since:
- 3.11
-
findFiles
Find files matching a given filter in a directory. If the filter returns sub-directories, this method will check those directories too. Use, for example, theRegexpFileFilter
to look for files matching a regular expression.- Parameters:
directory
- The parent directory to search infilter
- A filter which specifies which files to find, or null to find all files- Returns:
- A List with the files in directory that matches the filter, or null if the specified directory doesn't exists or isn't a directory
- Since:
- 2.5
- See Also:
-
deleteEmptyDirectories
Deletes all empty subdirectories and, optionally, also the root directory.- Parameters:
root
- The root directory to start looking indeleteRoot
- TRUE to also delete the root directory if it is empty, FALSE to leave it- Returns:
- The number of deleted directories
- Since:
- 2.13
-
doRecursively
public static void doRecursively(DbControl dc, Directory root, Filter<? super Directory> dirFilter, Filter<? super File> fileFilter) Do something recursively to all files and sub-directories in specified root directory. If no directory filter is specified, all subdirectories are processed, otherwise only those directories were theFilter.evaluate(Object)
method returns TRUE.Files are only loaded if a file filter is specified. The return value of the
evaluate
method is ignored.The files and directories may be processed in any order.
NOTE! The intention of this method is that the filter should have have a "side-effect" that does some work on the files/ sub-directories.
- Parameters:
dc
- A DbControl to use for database accessroot
- The root directorydirFilter
- A filter that selects/rejects sub-directoriesfileFilter
- A filter that does something with the file- Since:
- 2.13
-
createTempDirectory
public static File createTempDirectory(String prefix, String suffix, File directory) throws IOException Creates a temporary directory. This method uses the built-inFile.createTempFile(String, String, File)
method to first crete a temporary file, that is then deleted and made into a directory instead. This should ensure that a new empty directory is created each time this method is called. The "temporary" nature of this method doesn't involve cleaning up or removing the created directory. It is the responsibility of the calling code to remove every file and subdirectory as well as the returned directory. This can for example be done with thedeleteTempDirectory(File)
method.- Parameters:
prefix
- A prefix that is used in the directory name it must be at least 3 characters long.suffix
- An optional suffix for the directory namedirectory
- An optional parent directory in which the temporary directory should be created, or null to use the system temporary directory- Returns:
- A reference to the created directory
- Throws:
IOException
- If there is a problem with the directory creation- Since:
- 2.15
- See Also:
-
deleteTempDirectory
Recursively delete all files and subdirectories within a given directory and the also the given directory itself.- Parameters:
root
- The root directory to start looking in- Returns:
- The number of deleted files and directories
- Since:
- 2.15
-
uploadFiles
public static List<File> uploadFiles(DbControl dc, Directory toDir, File fromDir, FileFilter filter, boolean overwrite, ProgressReporter progress) throws IOException Upload multiple files to BASE.- Parameters:
toDir
- The directory in the BASE file system that the files should be uploaded tofromDir
- The directory in the local file system to scan for files to upload if the directory doesn't exists or is empty an empty list is returnedfilter
- An optional filter that decides which files that should be uploaded or not, if null all files, including subdirectories, are uploadedoverwrite
- TRUE if existing files should be overwritten, FALSE to only overwrite files that have been marked for removalprogress
- An optional progress reporter- Returns:
- A list with the new file items that were created
- Throws:
IOException
- Since:
- 2.16
-