Package ca.cgjennings.algo.compression
Class AbstractCompressor
- java.lang.Object
-
- ca.cgjennings.algo.compression.AbstractCompressor
-
- All Implemented Interfaces:
Compressor
- Direct Known Subclasses:
BZip2Compressor
,DeflateCompressor
,GZIPCompressor
,LZMACompressor
public abstract class AbstractCompressor extends java.lang.Object implements Compressor
An abstract base class forCompressor
implementations. Concrete classes need only implement the following methods:
Compressor.compress(java.io.InputStream, java.io.OutputStream)
and
Compressor.decompress(java.io.InputStream, java.io.OutputStream)
- Since:
- 3.0
- Author:
- Chris Jennings
-
-
Constructor Summary
Constructors Constructor Description AbstractCompressor()
Creates a compressor that defaults to the maximum compression level.AbstractCompressor(int compressionLevel)
Creates a compressor with the requested compression level.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
compress(java.io.File in, java.io.File out)
Compresses a file, writing the result to another file.void
compress(java.io.File in, java.io.File out, ProgressListener li)
Compresses a file, writing the result to another file.void
decompress(java.io.File in, java.io.File out)
Decompresses a file, writing the result to another file.void
decompress(java.io.File in, java.io.File out, ProgressListener li)
Decompresses a file, writing the result to another file.void
decompress(java.io.InputStream in, java.io.OutputStream out)
Reads compressed data from the input stream, decompresses it, and writes it to the output stream.int
getCompressionLevel()
Returns the current compression level.protected void
pumpStream(java.io.InputStream in, java.io.OutputStream out, boolean allowNIO)
This convenience method is provided for concrete subclasses to copy the content of one stream to another stream.void
setCompressionLevel(int level)
Sets the compression level for future compression operations.protected java.io.InputStream
wrap(java.io.InputStream src)
Wraps the provided input stream with a buffer if necessary.protected java.io.OutputStream
wrap(java.io.OutputStream src)
Wraps the provided output stream with a buffer if necessary.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface ca.cgjennings.algo.compression.Compressor
compress, filter, filter
-
-
-
-
Constructor Detail
-
AbstractCompressor
public AbstractCompressor()
Creates a compressor that defaults to the maximum compression level.
-
AbstractCompressor
public AbstractCompressor(int compressionLevel)
Creates a compressor with the requested compression level.- Parameters:
compressionLevel
- the compression level, from 0-9
-
-
Method Detail
-
decompress
public void decompress(java.io.InputStream in, java.io.OutputStream out) throws java.io.IOException
Description copied from interface:Compressor
Reads compressed data from the input stream, decompresses it, and writes it to the output stream.- Specified by:
decompress
in interfaceCompressor
- Parameters:
in
- the source of data to decompressout
- the sink for decompressed data- Throws:
java.io.IOException
- if an error occurs during decompression
-
compress
public void compress(java.io.File in, java.io.File out) throws java.io.IOException
Description copied from interface:Compressor
Compresses a file, writing the result to another file.- Specified by:
compress
in interfaceCompressor
- Parameters:
in
- the file to compressout
- the destination- Throws:
java.io.IOException
- if an error occurs
-
compress
public void compress(java.io.File in, java.io.File out, ProgressListener li) throws java.io.IOException
Description copied from interface:Compressor
Compresses a file, writing the result to another file.- Specified by:
compress
in interfaceCompressor
- Parameters:
in
- the file to compressout
- the destinationli
- a listener that will be updated with compression progress- Throws:
java.io.IOException
- if an error occurs
-
decompress
public void decompress(java.io.File in, java.io.File out) throws java.io.IOException
Description copied from interface:Compressor
Decompresses a file, writing the result to another file.- Specified by:
decompress
in interfaceCompressor
- Parameters:
in
- the file to decompressout
- the destination- Throws:
java.io.IOException
- if an error occurs
-
decompress
public void decompress(java.io.File in, java.io.File out, ProgressListener li) throws java.io.IOException
Description copied from interface:Compressor
Decompresses a file, writing the result to another file.- Specified by:
decompress
in interfaceCompressor
- Parameters:
in
- the file to decompressout
- the destinationli
- a listener that will be updated with the decompression progress- Throws:
java.io.IOException
- if an error occurs
-
getCompressionLevel
public final int getCompressionLevel()
Description copied from interface:Compressor
Returns the current compression level.- Specified by:
getCompressionLevel
in interfaceCompressor
- Returns:
- the current compression level, from 0 to 9 inclusive
-
setCompressionLevel
public final void setCompressionLevel(int level)
Description copied from interface:Compressor
Sets the compression level for future compression operations. The compression level is an integer between 0 and 9 inclusive that indicates whether the compressor should favour low resource requirements (such as CPU time and memory) or high compression ratios (9=maximum compression). For some implementations, this value may have no effect.Each compressor has its own default compression level chosen to balance the amount of compression against the resources required to compress and/or decompress the data. Setting the compression level higher than its default may result in a sharp increase in resource use, particularly during compression.
- Specified by:
setCompressionLevel
in interfaceCompressor
- Parameters:
level
- the compression level from 0 to 9, with higher values suggesting that the algorithm should try harder to compress the data
-
pumpStream
protected void pumpStream(java.io.InputStream in, java.io.OutputStream out, boolean allowNIO) throws java.io.IOException
This convenience method is provided for concrete subclasses to copy the content of one stream to another stream. A typical use case is to create a compression (decompression) stream filter on the output (input) stream and then use this method to complete the compression (decompression) process.- Parameters:
in
- the sourceout
- the sinkallowNIO
- iftrue
, use NIO to accelerate stream copying- Throws:
java.io.IOException
- if an I/O exception occurs during copying
-
wrap
protected java.io.InputStream wrap(java.io.InputStream src)
Wraps the provided input stream with a buffer if necessary.- Parameters:
src
- the source stream- Returns:
- the (possibly wrapped) source stream
-
wrap
protected java.io.OutputStream wrap(java.io.OutputStream src)
Wraps the provided output stream with a buffer if necessary.- Parameters:
src
- the source stream- Returns:
- the (possibly wrapped) source stream
-
-