org.at4j.zip.comp
Class DeflatedCompressionMethod

java.lang.Object
  extended by org.at4j.zip.comp.DeflatedCompressionMethod
All Implemented Interfaces:
ZipEntryCompressionMethod

public class DeflatedCompressionMethod
extends Object
implements ZipEntryCompressionMethod

This is the "Deflated" compression method.

Use the getCompressionLevel() to get the compression level for a deflated Zip file entry.

Important note when reading deflated Zip file entries: Never try to read more data than what is available in the stream. This sometimes causes Java's InflaterOutputStream to throw an EOFException with the message "Unexpected end of ZLIB input stream". See this bug report.

Since:
1.0
Author:
Karl Gustafsson

Field Summary
static UnsignedShort CODE
          The unique code for this compression method.
static DeflatedCompressionMethod FAST_COMPRESSION
          A singleton instance that uses the "fast" compression level.
static int FAST_COMPRESSION_LEVEL
          The "fast" compression level.
static DeflatedCompressionMethod FASTEST_COMPRESSION
          A singleton instance that uses the fastest compression level.
static int FASTEST_COMPRESSION_LEVEL
          The fastest compression level.
static DeflatedCompressionMethod MAXIMUM_COMPRESSION
          A singleton instance that uses the maximum compression level.
static int MAXIMUM_COMPRESSION_LEVEL
          The maximum compression level.
static String NAME
           
static DeflatedCompressionMethod NORMAL_COMPRESSION
          A singleton instance that uses the "normal" compression level.
static int NORMAL_COMPRESSION_LEVEL
          The "normal" compression level.
static UnsignedShort VERSION_NEEDED_TO_EXTRACT
          The PK-Zip version needed to extract entries compressed with this compression method.
 
Constructor Summary
DeflatedCompressionMethod()
          Create a deflated compression method using the default compression level (6).
DeflatedCompressionMethod(CompressionLevel level)
          Create a deflated compression method using the specified compression level.
DeflatedCompressionMethod(int compressionLevel)
          Create a deflated compression method using the specified compression level.
 
Method Summary
 InputStream createInputStream(InputStream is, long compressedSize, long uncompressedSize)
          Create an InputStream that clients can use to read uncompressed file data from.
 OutputStream createOutputStream(OutputStream os)
          Create an OutputStream that compresses the data written to it and writes it to the wrapped output stream.
 RandomAccess createRandomAccess(RandomAccess ra, long compressedSize, long uncompressedSize)
          This method always throws an UnsupportedOperationException.
 ZipEntryCompressionMethod createWithCompressionLevel(CompressionLevel level)
          Create a new ZipEntryCompressionMethod object that will have the supplied compression level.
 UnsignedShort getCode()
          Get the unique code that is used to identify this compression method in a Zip entry's header.
 int getCompressionLevel()
          Get the compression level between 1 (fastest) and 9 (best) used by this compression method.
 String getName()
          Get a human-readable name for the compression method.
 UnsignedShort getVersionNeededToExtract()
          Get the PK-Zip version needed to extract Zip entries compressed by this compression method.
 boolean isRandomAccessSupported()
          This method always returns false
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

CODE

public static final UnsignedShort CODE
The unique code for this compression method. The code is used in the Zip entry's header to specify the compression method that is used for compressing the entry's file data.


NAME

public static final String NAME
See Also:
Constant Field Values

VERSION_NEEDED_TO_EXTRACT

public static final UnsignedShort VERSION_NEEDED_TO_EXTRACT
The PK-Zip version needed to extract entries compressed with this compression method. See ZipEntryCompressionMethod.getVersionNeededToExtract().


NORMAL_COMPRESSION_LEVEL

public static final int NORMAL_COMPRESSION_LEVEL
The "normal" compression level.

See Also:
Constant Field Values

FAST_COMPRESSION_LEVEL

public static final int FAST_COMPRESSION_LEVEL
The "fast" compression level.

See Also:
Constant Field Values

FASTEST_COMPRESSION_LEVEL

public static final int FASTEST_COMPRESSION_LEVEL
The fastest compression level.

See Also:
Constant Field Values

MAXIMUM_COMPRESSION_LEVEL

public static final int MAXIMUM_COMPRESSION_LEVEL
The maximum compression level.

See Also:
Constant Field Values

FASTEST_COMPRESSION

public static final DeflatedCompressionMethod FASTEST_COMPRESSION
A singleton instance that uses the fastest compression level.


FAST_COMPRESSION

public static final DeflatedCompressionMethod FAST_COMPRESSION
A singleton instance that uses the "fast" compression level.


NORMAL_COMPRESSION

public static final DeflatedCompressionMethod NORMAL_COMPRESSION
A singleton instance that uses the "normal" compression level.


MAXIMUM_COMPRESSION

public static final DeflatedCompressionMethod MAXIMUM_COMPRESSION
A singleton instance that uses the maximum compression level.

Constructor Detail

DeflatedCompressionMethod

public DeflatedCompressionMethod()
Create a deflated compression method using the default compression level (6).


DeflatedCompressionMethod

public DeflatedCompressionMethod(int compressionLevel)
                          throws IllegalArgumentException
Create a deflated compression method using the specified compression level.

Parameters:
compressionLevel - The compression level, between 1 (fastest) and 9 (best compression}.
Throws:
IllegalArgumentException - If the compression level is not in the range 1 to 9 (inclusive).

DeflatedCompressionMethod

public DeflatedCompressionMethod(CompressionLevel level)
Create a deflated compression method using the specified compression level.

Parameters:
level - The compression level.
Since:
1.0.2
Method Detail

getCode

public UnsignedShort getCode()
Description copied from interface: ZipEntryCompressionMethod
Get the unique code that is used to identify this compression method in a Zip entry's header.

Specified by:
getCode in interface ZipEntryCompressionMethod
Returns:
The compression method's unique code.

getName

public String getName()
Description copied from interface: ZipEntryCompressionMethod
Get a human-readable name for the compression method.

Specified by:
getName in interface ZipEntryCompressionMethod
Returns:
The compression method's name.

getVersionNeededToExtract

public UnsignedShort getVersionNeededToExtract()
Description copied from interface: ZipEntryCompressionMethod
Get the PK-Zip version needed to extract Zip entries compressed by this compression method.

Specified by:
getVersionNeededToExtract in interface ZipEntryCompressionMethod
Returns:
An UnsignedShort representing the version number. 10 corresponds to version 1.0, 62 to 6.2, etc.

getCompressionLevel

public int getCompressionLevel()
Get the compression level between 1 (fastest) and 9 (best) used by this compression method.

Returns:
The compression level.

createInputStream

public InputStream createInputStream(InputStream is,
                                     long compressedSize,
                                     long uncompressedSize)
Description copied from interface: ZipEntryCompressionMethod
Create an InputStream that clients can use to read uncompressed file data from.

Specified by:
createInputStream in interface ZipEntryCompressionMethod
Parameters:
is - An open InputStream on the compressed file data.
compressedSize - The total size of the compressed data.
uncompressedSize - The total size of the uncompressed data.
Returns:
An open InputStream containing uncompressed file data.

createOutputStream

public OutputStream createOutputStream(OutputStream os)
                                throws IOException,
                                       UnsupportedCompressionMethodException
Description copied from interface: ZipEntryCompressionMethod
Create an OutputStream that compresses the data written to it and writes it to the wrapped output stream. This is used when building Zip files.

Specified by:
createOutputStream in interface ZipEntryCompressionMethod
Parameters:
os - The wrapped output stream.
Returns:
A compressing output stream.
Throws:
IOException - On I/O errors.
UnsupportedCompressionMethodException - If the compression method is not supported.

createRandomAccess

public RandomAccess createRandomAccess(RandomAccess ra,
                                       long compressedSize,
                                       long uncompressedSize)
                                throws UnsupportedOperationException
This method always throws an UnsupportedOperationException.

Specified by:
createRandomAccess in interface ZipEntryCompressionMethod
Parameters:
ra - An open random access object on the file entry's data.
compressedSize - The total size of the compressed data.
uncompressedSize - The total size of the uncompressed data.
Returns:
On open RandomAccess that can be used to read uncompressed data.
Throws:
UnsupportedOperationException - Always.

isRandomAccessSupported

public boolean isRandomAccessSupported()
This method always returns false

Specified by:
isRandomAccessSupported in interface ZipEntryCompressionMethod
Returns:
false, always.

toString

public String toString()
Overrides:
toString in class Object

createWithCompressionLevel

public ZipEntryCompressionMethod createWithCompressionLevel(CompressionLevel level)
Description copied from interface: ZipEntryCompressionMethod
Create a new ZipEntryCompressionMethod object that will have the supplied compression level.

The reason for this method is that it makes it easier to set a custom compression level for a compression method, without having to create a new compression method object from scratch manually.

Specified by:
createWithCompressionLevel in interface ZipEntryCompressionMethod
Parameters:
level - The compression level.
Returns:
A new compression method object. If the new object would have been created with the same settings as this object, this method may return this since ZipEntryCompressionMethod objects are immutable.