2.9.1: 2008-12-18

net.sf.basedb.util
Class FileUtil

java.lang.Object
  extended by net.sf.basedb.util.FileUtil

public class FileUtil
extends Object

This class collects some useful methods for file and stream handling.

Version:
2.0
Author:
Nicklas
Last modified
$Date$

Field Summary
private static int BUFFER_SIZE
           
 
Constructor Summary
FileUtil()
           
 
Method Summary
static 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(InputStream in)
          Close an input stream without throwing an exception.
static void close(OutputStream out)
          Close an output stream 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 List<File> findFiles(File directory, FileFilter filter)
          Find files matching a given filter in a directory.
static Map<Directory,List<Directory>> getDirectoryTree(DbControl dc, Directory directory)
          Get the complete tree of sub-directories from a given directory.
static InputStream getInputStream(File file)
          Get a buffered InputStream object reading from the specified file.
static OutputStream getOutputStream(File file)
          Get a buffered OutputStream object writing to the specified file.
static long read(InputStream in)
          Read from the input stream until the end is reached.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

BUFFER_SIZE

private static final int BUFFER_SIZE
See Also:
Constant Field Values
Constructor Detail

FileUtil

public FileUtil()
Method Detail

copy

public static long copy(InputStream in,
                        OutputStream out)
                 throws IOException
Copy from the input stream to the output stream until end of file is reached.

Parameters:
in - The InputStream to read from
out - The OutputStream 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 - The InputStream to read from
out - The OutputStream to write to
progress - An optional progress reporter
Returns:
The number of bytes copied
Throws:
IOException - This exception is thrown if there is an error
Since:
2.5

read

public static long read(InputStream in)
                 throws IOException
Read from the input stream until the end is reached.

Parameters:
in - The InputStream to read from
Returns:
The number of bytes copied
Throws:
IOException - This exception is thrown if there is an error
Since:
2.4

close

public static void close(InputStream in)
Close an input stream without throwing an exception.

Parameters:
in - The input stream to close
Since:
2.9

close

public static void close(OutputStream out)
Close an output stream without throwing an exception.

Parameters:
out - The output stream to close
Since:
2.9

getInputStream

public static InputStream getInputStream(File file)
                                  throws IOException
Get a buffered InputStream 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

public static OutputStream getOutputStream(File file)
                                    throws IOException
Get a buffered OutputStream 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

public static boolean checkMagicNumber(PushbackInputStream pin,
                                       byte[] bCheck)
                                throws IOException
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 bytes
bCheck - 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 bytes
NullPointerException - If pin or bCheck is null
IllegalArgumentException - 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.

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 null
BaseException - If there is another error

findFiles

public static List<File> findFiles(File directory,
                                   FileFilter filter)
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, the RegexpFileFilter to look for files matching a regular expression.

Parameters:
directory - The parent directory to search in
filter - 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:
RegexpFileFilter

2.9.1: 2008-12-18