Class StreamCompressor
java.lang.Object
org.apache.commons.compress.archivers.zip.StreamCompressor
- All Implemented Interfaces:
Closeable
,AutoCloseable
- Direct Known Subclasses:
StreamCompressor.DataOutputCompressor
,StreamCompressor.OutputStreamCompressor
,StreamCompressor.ScatterGatherBackingStoreCompressor
,StreamCompressor.SeekableByteChannelCompressor
Encapsulates a
Deflater
and crc calculator, handling multiple types of output streams. Currently ZipEntry.DEFLATED
and
ZipEntry.STORED
are the only supported compression methods.- Since:
- 1.10
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static final class
private static final class
private static final class
private static final class
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final int
private final CRC32
private final Deflater
private static final int
Apparently Deflater.setInput gets slowed down a lot on Sun JVMs when it gets handed a huge buffer.private final byte[]
private final byte[]
private long
private long
private long
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
static StreamCompressor
create
(int compressionLevel, ScatterGatherBackingStore bs) Creates a stream compressor with the given compression level.(package private) static StreamCompressor
create
(DataOutput os, Deflater deflater) Creates a stream compressor with the given compression level.(package private) static StreamCompressor
create
(OutputStream os) Creates a stream compressor with the default compression level.(package private) static StreamCompressor
create
(OutputStream os, Deflater deflater) Creates a stream compressor with the given compression level.(package private) static StreamCompressor
create
(SeekableByteChannel os, Deflater deflater) Creates a stream compressor with the given compression level.static StreamCompressor
Creates a stream compressor with the default compression level.(package private) void
deflate()
void
deflate
(InputStream source, int method) Deflates the given source using the supplied compression methodprivate void
(package private) void
long
Gets the number of bytes read from the source streamlong
Gets the number of bytes written to the output for the last entrylong
getCrc32()
Gets the crc32 of the last deflated filelong
Gets the total number of bytes written to the output for all files(package private) void
reset()
(package private) long
write
(byte[] b, int offset, int length, int method) Writes bytes to ZIP entry.void
writeCounted
(byte[] data) void
writeCounted
(byte[] data, int offset, int length) private void
writeDeflated
(byte[] b, int offset, int length) protected abstract void
writeOut
(byte[] data, int offset, int length)
-
Field Details
-
DEFLATER_BLOCK_SIZE
private static final int DEFLATER_BLOCK_SIZEApparently 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:
-
BUFFER_SIZE
private static final int BUFFER_SIZE- See Also:
-
deflater
-
crc
-
writtenToOutputStreamForLastEntry
private long writtenToOutputStreamForLastEntry -
sourcePayloadLength
private long sourcePayloadLength -
totalWrittenToOutputStream
private long totalWrittenToOutputStream -
outputBuffer
private final byte[] outputBuffer -
readerBuf
private final byte[] readerBuf
-
-
Constructor Details
-
StreamCompressor
StreamCompressor(Deflater deflater)
-
-
Method Details
-
create
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
Creates a stream compressor with the given compression level.- Parameters:
compressionLevel
- TheDeflater
compression levelbs
- The ScatterGatherBackingStore to receive output- Returns:
- A stream compressor
-
create
Creates a stream compressor with the default compression level.- Parameters:
os
- The stream to receive output- Returns:
- A stream compressor
-
create
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
Creates a stream compressor with the default compression level.- Parameters:
bs
- The ScatterGatherBackingStore to receive output- Returns:
- A stream compressor
-
create
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
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Throws:
IOException
-
deflate
- Throws:
IOException
-
deflate
Deflates the given source using the supplied compression method- Parameters:
source
- The source to compressmethod
- The #ZipArchiveEntry compression method- Throws:
IOException
- When failures happen
-
deflateUntilInputIsNeeded
- Throws:
IOException
-
flushDeflater
- Throws:
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
Writes 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:
IOException
- on error
-
writeCounted
- Throws:
IOException
-
writeCounted
- Throws:
IOException
-
writeDeflated
- Throws:
IOException
-
writeOut
- Throws:
IOException
-