messif.utility
Class DirectoryInputStream

java.lang.Object
  extended by java.io.InputStream
      extended by messif.utility.DirectoryInputStream
All Implemented Interfaces:
java.io.Closeable

public class DirectoryInputStream
extends java.io.InputStream

This class provides a continous InputStream over all files from a given directory (and subdirectories if specified). The file names can be filtered using a FileFilter and automatic decompression of GZIPInputStream is used on files that have "gz" suffix.


Constructor Summary
DirectoryInputStream(java.util.Collection<java.io.File> dirs, java.io.FileFilter filter, boolean descendDirectories)
          Creates a new DirectoryInputStream over files that match filter in dirsdirectories.
DirectoryInputStream(java.io.File dir, java.io.FileFilter filter, boolean descendDirectories)
          Creates a new DirectoryInputStream over files that match filter in the directory dir.
DirectoryInputStream(java.lang.String dir, java.io.FileFilter filter, boolean descendDirectories)
          Creates a new DirectoryInputStream over files that match filter in the directory dir.
 
Method Summary
 int available()
           
 void close()
           
 void mark(int readlimit)
           
 boolean markSupported()
           
static java.io.InputStream open(java.io.File path)
          Open input stream for the specified path.
static java.io.InputStream open(java.io.File path, java.io.FileFilter filter)
          Open input stream for the specified path.
static java.io.InputStream open(java.lang.String path)
          Open input stream for the specified path.
 int read()
           
 int read(byte[] b)
           
 int read(byte[] b, int off, int len)
           
 void reset()
           
static java.util.Collection<java.io.File> searchFiles(java.util.Collection<java.io.File> files, java.io.File dir, java.io.FileFilter filter, boolean descendDirectories)
          Search the directory for files that match the filter.
 long skip(long n)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DirectoryInputStream

public DirectoryInputStream(java.util.Collection<java.io.File> dirs,
                            java.io.FileFilter filter,
                            boolean descendDirectories)
                     throws java.lang.NullPointerException,
                            java.io.FileNotFoundException,
                            java.io.IOException
Creates a new DirectoryInputStream over files that match filter in dirsdirectories.

Parameters:
dirs - the collection of directories where to search for files
filter - the filter to apply on the files; all files are accepted if it is null
descendDirectories - if true, the subdirectories are descended and searched for matching files
Throws:
java.lang.NullPointerException - if one of the specified dirs was null
java.io.FileNotFoundException - if some of the specified dirs was not found or was not a directory
java.io.IOException - if there was a problem opening file from the directories

DirectoryInputStream

public DirectoryInputStream(java.io.File dir,
                            java.io.FileFilter filter,
                            boolean descendDirectories)
                     throws java.lang.NullPointerException,
                            java.io.FileNotFoundException,
                            java.io.IOException
Creates a new DirectoryInputStream over files that match filter in the directory dir.

Parameters:
dir - the directory where to search for files
filter - the filter to apply on the files; all files are accepted if it is null
descendDirectories - if true, the subdirectories are descended and searched for matching files
Throws:
java.lang.NullPointerException - if one of the specified dirs was null
java.io.FileNotFoundException - if some of the specified dirs was not found or was not a directory
java.io.IOException - if there was a problem opening file from the directories

DirectoryInputStream

public DirectoryInputStream(java.lang.String dir,
                            java.io.FileFilter filter,
                            boolean descendDirectories)
                     throws java.lang.NullPointerException,
                            java.io.FileNotFoundException,
                            java.io.IOException
Creates a new DirectoryInputStream over files that match filter in the directory dir.

Parameters:
dir - the directory where to search for files
filter - the filter to apply on the files; all files are accepted if it is null
descendDirectories - if true, the subdirectories are descended and searched for matching files
Throws:
java.lang.NullPointerException - if one of the specified dirs was null
java.io.FileNotFoundException - if some of the specified dirs was not found or was not a directory
java.io.IOException - if there was a problem opening file from the directories
Method Detail

open

public static java.io.InputStream open(java.lang.String path)
                                throws java.io.FileNotFoundException,
                                       java.io.IOException
Open input stream for the specified path. If any of the file names end with "gz", GZIP stream decompression is automatically used.

Parameters:
path - the path to open
Returns:
a new instance of InputStream
Throws:
java.io.FileNotFoundException - if the specified path was not found
java.io.IOException - if there was a problem opening file from the directories

open

public static java.io.InputStream open(java.io.File path)
                                throws java.io.FileNotFoundException,
                                       java.io.IOException
Open input stream for the specified path. If the path is null, the System.in is returned. If the path is a directory, all the files in this directory and its subdirectories will be merged into a single input stream that is returned. Otherwise, a FileInputStream is returned.

If any of the file names end with "gz", GZIP stream decompression is automatically used.

Parameters:
path - the path to open
Returns:
a new instance of InputStream
Throws:
java.io.FileNotFoundException - if the specified path was not found
java.io.IOException - if there was a problem opening file from the directories

open

public static java.io.InputStream open(java.io.File path,
                                       java.io.FileFilter filter)
                                throws java.io.FileNotFoundException,
                                       java.io.IOException
Open input stream for the specified path. If the path is null, the System.in is returned. If the path is a directory, all the files in this directory and its subdirectories will be merged into a single input stream that is returned. Otherwise, a FileInputStream is returned.

If any of the file names end with "gz", GZIP stream decompression is automatically used.

Parameters:
path - the path to open
filter - the filter to apply on the files; all files are accepted if it is null
Returns:
a new instance of InputStream
Throws:
java.io.FileNotFoundException - if the specified path was not found
java.io.IOException - if there was a problem opening file from the directories

searchFiles

public static java.util.Collection<java.io.File> searchFiles(java.util.Collection<java.io.File> files,
                                                             java.io.File dir,
                                                             java.io.FileFilter filter,
                                                             boolean descendDirectories)
                                                      throws java.io.FileNotFoundException,
                                                             java.lang.NullPointerException
Search the directory for files that match the filter. If the descendDirectories parameter is true, the subdirectories are descended and searched too.

The found files are added to the files collection. There is no guarantee that the files in the resulting collection will appear in any specific order; they are not, in particular, guaranteed to appear in alphabetical order.

Parameters:
files - the collection that receives the files found; if null, a new ArrayList is created
dir - the directory to search for files
filter - the filter to apply on the files; all files are accepted if it is null
descendDirectories - if true, the subdirectories are descended and searched for matching files
Returns:
the collection of files found, i.e. the collection provided in files argument
Throws:
java.io.FileNotFoundException - if the specified dir was not found or it is not a directory
java.lang.NullPointerException - if the specified dir was null

read

public int read()
         throws java.io.IOException
Specified by:
read in class java.io.InputStream
Throws:
java.io.IOException

read

public int read(byte[] b)
         throws java.io.IOException
Overrides:
read in class java.io.InputStream
Throws:
java.io.IOException

read

public int read(byte[] b,
                int off,
                int len)
         throws java.io.IOException
Overrides:
read in class java.io.InputStream
Throws:
java.io.IOException

skip

public long skip(long n)
          throws java.io.IOException
Overrides:
skip in class java.io.InputStream
Throws:
java.io.IOException

available

public int available()
              throws java.io.IOException
Overrides:
available in class java.io.InputStream
Throws:
java.io.IOException

close

public void close()
           throws java.io.IOException
Specified by:
close in interface java.io.Closeable
Overrides:
close in class java.io.InputStream
Throws:
java.io.IOException

mark

public void mark(int readlimit)
Overrides:
mark in class java.io.InputStream

reset

public void reset()
           throws java.io.IOException
Overrides:
reset in class java.io.InputStream
Throws:
java.io.IOException

markSupported

public boolean markSupported()
Overrides:
markSupported in class java.io.InputStream