Class ZCompressorOutputStream
java.lang.Object
java.io.OutputStream
java.io.FilterOutputStream
com.ibm.jzos.ZCompressorOutputStream
- All Implemented Interfaces:
Closeable, Flushable, AutoCloseable
A subclass of
OutputStream that will compress data
as it writes to another OutputStream. Not thread safe.
The class implements AutoCloseable. It can be used with the AutoCloseable with a try-with-resources
block or without, as shown in the two examples below:
Example 1: Allocate and close a ZFile when a try-with-resources block is not used.
The an explicit call to the close() close the ZFile object and release the
associated resources. The close() method is called in a finally block to
assure that the close is performed after the try block completes.
ZFile zfile = new new ZFile(fileName, options);
try {
byte[] recBuf = new byte[lrecl];
// read the records sequentially
while (zfile.read(recBuf) != -1) {
String record = new String(recBuf);
System.out.println("Record=" + record);
}
}
finally {
zfile.close();
}
Example 2: Allocate a ZFile when a try-with-resources block is used. The method close() is not needed.
Upon exiting a try-with-resources block, the AutoCloseable will automatically call the close()
method which will lose the ZFile object and release the associated resources.
try (ZFile zfile = new new ZFile(fileName, options)) {
byte[] recBuf = new byte[lrecl];
// read the records sequentially
while (zfile.read(recBuf) != -1) {
String record = new String(recBuf);
System.out.println("Record=" + record);
}
}
...
- Since:
- 2.4.4
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionZCompressorOutputStream(ZCompressor compressor, OutputStream out) Create a new output stream with default buffer size.ZCompressorOutputStream(ZCompressor compressor, OutputStream out, int buflen) Create a new output stream with the specified buffer size. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()This method also flushes data, including any partially compressed last byte.voidflush()This flushes the buffer of all compressed data, excluding a possible partial last byte.longAnswer the total number of compressed bytes written so far.longAnswer the total number of input (uncompressed) bytes written so far.voidreleaseZCompressorOnClose(boolean value) Sets a flag that will cause the ZCompressor resources to be released when this OutputStream is closed.voidwrite(byte[] b, int off, int len) voidwrite(int b) Methods inherited from class FilterOutputStream
writeMethods inherited from class OutputStream
nullOutputStream
-
Constructor Details
-
ZCompressorOutputStream
Create a new output stream with default buffer size.- Parameters:
compressor- the ZCompressor object used to compress dataout- the target OutputStream for compressed data
-
ZCompressorOutputStream
Create a new output stream with the specified buffer size.- Parameters:
compressor- the ZCompressor object used to compress dataout- the target OutputStream for compressed databuflen- internal buffer for compressed data
-
-
Method Details
-
releaseZCompressorOnClose
public void releaseZCompressorOnClose(boolean value) Sets a flag that will cause the ZCompressor resources to be released when this OutputStream is closed. Defaults tofalse. -
getTotalSourceBytes
public long getTotalSourceBytes()Answer the total number of input (uncompressed) bytes written so far.- Returns:
- the total number of input (uncompressed) bytes written
-
getTotalCompressedBytes
public long getTotalCompressedBytes()Answer the total number of compressed bytes written so far.- Returns:
- the total number of compressed bytes written
-
write
- Overrides:
writein classFilterOutputStream- Throws:
IOException- See Also:
-
write
- Overrides:
writein classFilterOutputStream- Throws:
IOException- See Also:
-
close
This method also flushes data, including any partially compressed last byte.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classFilterOutputStream- Throws:
IOException- See Also:
-
flush
This flushes the buffer of all compressed data, excluding a possible partial last byte. Remaining unused bits in such a partial byte may be used for compressing more data, and is only written when the stream is closed.- Specified by:
flushin interfaceFlushable- Overrides:
flushin classFilterOutputStream- Throws:
IOException- See Also:
-