- java.lang.Object
-
- kala.compress.archivers.zip.StreamCompressor
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable
- Direct Known Subclasses:
StreamCompressor.DataOutputCompressor,StreamCompressor.OutputStreamCompressor,StreamCompressor.ScatterGatherBackingStoreCompressor,StreamCompressor.SeekableByteChannelCompressor
public abstract class StreamCompressor extends java.lang.Object implements java.io.CloseableEncapsulates aDeflaterand crc calculator, handling multiple types of output streams. CurrentlyZipEntry.DEFLATEDandZipEntry.STOREDare the only supported compression methods.- Since:
- 1.10
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classStreamCompressor.DataOutputCompressorprivate static classStreamCompressor.OutputStreamCompressorprivate static classStreamCompressor.ScatterGatherBackingStoreCompressorprivate static classStreamCompressor.SeekableByteChannelCompressor
-
Field Summary
Fields Modifier and Type Field Description private static intBUFFER_SIZEprivate java.util.zip.CRC32crcprivate java.util.zip.Deflaterdeflaterprivate static intDEFLATER_BLOCK_SIZEApparently Deflater.setInput gets slowed down a lot on Sun JVMs when it gets handed a huge buffer.private byte[]outputBufferprivate byte[]readerBufprivate longsourcePayloadLengthprivate longtotalWrittenToOutputStreamprivate longwrittenToOutputStreamForLastEntry
-
Constructor Summary
Constructors Constructor Description StreamCompressor(java.util.zip.Deflater deflater)
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description voidclose()static StreamCompressorcreate(int compressionLevel, ScatterGatherBackingStore bs)Creates a stream compressor with the given compression level.(package private) static StreamCompressorcreate(java.io.DataOutput os, java.util.zip.Deflater deflater)Creates a stream compressor with the given compression level.(package private) static StreamCompressorcreate(java.io.OutputStream os)Creates a stream compressor with the default compression level.(package private) static StreamCompressorcreate(java.io.OutputStream os, java.util.zip.Deflater deflater)Creates a stream compressor with the given compression level.(package private) static StreamCompressorcreate(java.nio.channels.SeekableByteChannel os, java.util.zip.Deflater deflater)Creates a stream compressor with the given compression level.static StreamCompressorcreate(ScatterGatherBackingStore bs)Creates a stream compressor with the default compression level.(package private) voiddeflate()voiddeflate(java.io.InputStream source, int method)Deflates the given source using the supplied compression methodprivate voiddeflateUntilInputIsNeeded()(package private) voidflushDeflater()longgetBytesRead()Gets the number of bytes read from the source streamlonggetBytesWrittenForLastEntry()Gets the number of bytes written to the output for the last entrylonggetCrc32()Gets the crc32 of the last deflated filelonggetTotalBytesWritten()Gets the total number of bytes written to the output for all files(package private) voidreset()(package private) longwrite(byte[] b, int offset, int length, int method)Writes bytes to ZIP entry.voidwriteCounted(byte[] data)voidwriteCounted(byte[] data, int offset, int length)private voidwriteDeflated(byte[] b, int offset, int length)protected abstract voidwriteOut(byte[] data, int offset, int length)
-
-
-
Field Detail
-
DEFLATER_BLOCK_SIZE
private static final int DEFLATER_BLOCK_SIZE
Apparently Deflater.setInput gets slowed down a lot on Sun JVMs when it gets handed a huge buffer. See https://issues.apache.org/bugzilla/show_bug.cgi?id=45396 Using a buffer size of 8192 bytes proved to be a good compromise- See Also:
- Constant Field Values
-
BUFFER_SIZE
private static final int BUFFER_SIZE
- See Also:
- Constant Field Values
-
deflater
private final java.util.zip.Deflater deflater
-
crc
private final java.util.zip.CRC32 crc
-
writtenToOutputStreamForLastEntry
private long writtenToOutputStreamForLastEntry
-
sourcePayloadLength
private long sourcePayloadLength
-
totalWrittenToOutputStream
private long totalWrittenToOutputStream
-
outputBuffer
private final byte[] outputBuffer
-
readerBuf
private final byte[] readerBuf
-
-
Method Detail
-
create
static StreamCompressor create(java.io.DataOutput os, java.util.zip.Deflater deflater)
Creates a stream compressor with the given compression level.- Parameters:
os- The DataOutput to receive outputdeflater- The deflater to use for the compressor- Returns:
- A stream compressor
-
create
public static StreamCompressor create(int compressionLevel, ScatterGatherBackingStore bs)
Creates a stream compressor with the given compression level.- Parameters:
compressionLevel- TheDeflatercompression levelbs- The ScatterGatherBackingStore to receive output- Returns:
- A stream compressor
-
create
static StreamCompressor create(java.io.OutputStream os)
Creates a stream compressor with the default compression level.- Parameters:
os- The stream to receive output- Returns:
- A stream compressor
-
create
static StreamCompressor create(java.io.OutputStream os, java.util.zip.Deflater deflater)
Creates a stream compressor with the given compression level.- Parameters:
os- The stream to receive outputdeflater- The deflater to use- Returns:
- A stream compressor
-
create
public static StreamCompressor create(ScatterGatherBackingStore bs)
Creates a stream compressor with the default compression level.- Parameters:
bs- The ScatterGatherBackingStore to receive output- Returns:
- A stream compressor
-
create
static StreamCompressor create(java.nio.channels.SeekableByteChannel os, java.util.zip.Deflater deflater)
Creates a stream compressor with the given compression level.- Parameters:
os- The SeekableByteChannel to receive outputdeflater- The deflater to use for the compressor- Returns:
- A stream compressor
- Since:
- 1.13
-
close
public void close() throws java.io.IOException- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Throws:
java.io.IOException
-
deflate
void deflate() throws java.io.IOException- Throws:
java.io.IOException
-
deflate
public void deflate(java.io.InputStream source, int method) throws java.io.IOExceptionDeflates the given source using the supplied compression method- Parameters:
source- The source to compressmethod- The #ZipArchiveEntry compression method- Throws:
java.io.IOException- When failures happen
-
deflateUntilInputIsNeeded
private void deflateUntilInputIsNeeded() throws java.io.IOException- Throws:
java.io.IOException
-
flushDeflater
void flushDeflater() throws java.io.IOException- Throws:
java.io.IOException
-
getBytesRead
public long getBytesRead()
Gets the number of bytes read from the source stream- Returns:
- The number of bytes read, never negative
-
getBytesWrittenForLastEntry
public long getBytesWrittenForLastEntry()
Gets the number of bytes written to the output for the last entry- Returns:
- The number of bytes, never negative
-
getCrc32
public long getCrc32()
Gets the crc32 of the last deflated file- Returns:
- the crc32
-
getTotalBytesWritten
public long getTotalBytesWritten()
Gets the total number of bytes written to the output for all files- Returns:
- The number of bytes, never negative
-
reset
void reset()
-
write
long write(byte[] b, int offset, int length, int method) throws java.io.IOExceptionWrites bytes to ZIP entry.- Parameters:
b- the byte array to writeoffset- the start position to write fromlength- the number of bytes to writemethod- the comrpession method to use- Returns:
- the number of bytes written to the stream this time
- Throws:
java.io.IOException- on error
-
writeCounted
public void writeCounted(byte[] data) throws java.io.IOException- Throws:
java.io.IOException
-
writeCounted
public void writeCounted(byte[] data, int offset, int length) throws java.io.IOException- Throws:
java.io.IOException
-
writeDeflated
private void writeDeflated(byte[] b, int offset, int length) throws java.io.IOException- Throws:
java.io.IOException
-
writeOut
protected abstract void writeOut(byte[] data, int offset, int length) throws java.io.IOException- Throws:
java.io.IOException
-
-