edu.mayo.bsi.ngsportal.server
Class Compressor

java.lang.Object
  extended by edu.mayo.bsi.ngsportal.server.Compressor

public class Compressor
extends Object

Class that seamlessly handles reading and writing Zipped, Gzipped, and Bzipped files, using Java's built in code for dealing with Zipped files, and Apache's code for dealing with BZipped and Gzipped files. It does this by looking at the file suffix, returning a plain Buffered Reader / Writer if the file has a suffix that is not recognized as a compressed file.

Author:
Greg Dougherty

Field Summary
static int kBZipCompression
          Signifies a file using BZip compression.
static int kGZipCompression
          Signifies a file using GZip compression.
static int kNoCompression
          Signifies an uncompressed file, or at least one whose compression we don't understand.
static int kZipCompression
          Signifies a file using Zip compression.
 
Constructor Summary
Compressor(File inFile, File outFile)
           
Compressor(File inFile, File outFile, boolean append)
           
 
Method Summary
static int compressionUsed(File testFile)
          Static function to tell the caller if the file is / will be compressed
 int getCompression()
          Accessor function to return the type of compression this file is using
 BufferedReader getReader()
           
static String getSuffix(File file)
          Extract the suffix from the file name and return it
static String getSuffix(int compression)
          Return the suffix used by files with this type of compression, or an empty string if no compression.
static String getSuffix(String name)
          Extract the suffix from the name and return it
 BufferedWriter getWriter()
           
 BufferedReader resetReader()
          Routine to close the current reader, and get a new reader at the start of the file.
 void setInputStream(InputStream inStream, String name)
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

kNoCompression

public static final int kNoCompression
Signifies an uncompressed file, or at least one whose compression we don't understand.

See Also:
Constant Field Values

kZipCompression

public static final int kZipCompression
Signifies a file using Zip compression.

See Also:
Constant Field Values

kGZipCompression

public static final int kGZipCompression
Signifies a file using GZip compression.

See Also:
Constant Field Values

kBZipCompression

public static final int kBZipCompression
Signifies a file using BZip compression.

See Also:
Constant Field Values
Constructor Detail

Compressor

public Compressor(File inFile,
                  File outFile)
           throws IOException
Parameters:
inFile - File to read from, that might be zip, bzip or gzip compressed, or null
outFile - File to write to, that might be zip, bzip or gzip compressed, or null
Throws:
IOException - if there's a problem building the file accessors

Compressor

public Compressor(File inFile,
                  File outFile,
                  boolean append)
           throws IOException
Parameters:
inFile - File to read from, that might be zip, bzip or gzip compressed, or null
outFile - File to write to, that might be zip, bzip or gzip compressed, or null
append - If true, then if writing to a non-compressed file will append data to the file rather than overwriting it.
Throws:
IOException - if there's a problem building the file accessors
Method Detail

setInputStream

public void setInputStream(InputStream inStream,
                           String name)
Parameters:
inStream - Stream to read from, that might be zip, gzip or bzip compressed
name - Name of the item behind the stream, so we can figure out the compression

getReader

public final BufferedReader getReader()
Returns:
the reader created for the input file

getWriter

public final BufferedWriter getWriter()
Returns:
the writer created for the output file

getCompression

public final int getCompression()
Accessor function to return the type of compression this file is using

Returns:
an int describing the compression, if any

compressionUsed

public static final int compressionUsed(File testFile)
Static function to tell the caller if the file is / will be compressed

Parameters:
testFile - File to test
Returns:
kZipCompression if a zip, kGZipCompression if gZipped, kBZipCompression if bZipped, else kNoCompression

resetReader

public final BufferedReader resetReader()
                                 throws IOException
Routine to close the current reader, and get a new reader at the start of the file.

Returns:
A new buffered reader aimed at the start of the file
Throws:
IOException

getSuffix

public static final String getSuffix(int compression)
Return the suffix used by files with this type of compression, or an empty string if no compression.

Parameters:
compression - type of compression
Returns:
String containing the suffix. Empty string if no compression

getSuffix

public static final String getSuffix(File file)
Extract the suffix from the file name and return it

Parameters:
file - File to process
Returns:
The file's suffix, or an empty string if no suffix

getSuffix

public static final String getSuffix(String name)
Extract the suffix from the name and return it

Parameters:
name - Name to process
Returns:
The name's suffix, or an empty string if no suffix