Class BufferedGZipOutputStream
- java.lang.Object
-
- java.io.OutputStream
-
- java.io.FilterOutputStream
-
- java.io.BufferedOutputStream
-
- com.activeviam.io.file.impl.ABufferedArchiveOutputStream
-
- com.activeviam.io.file.impl.BufferedGZipOutputStream
-
- All Implemented Interfaces:
IArchiveOutputStream
,Closeable
,Flushable
,AutoCloseable
public class BufferedGZipOutputStream extends ABufferedArchiveOutputStream
Managed stream implementation ofGZIPOutputStream
for ensuring an optimal generation of a single-entry archive file.NOTICE - How to use this?... Once an instance of this stream has been created, simply use
write(byte[], int, int)
(or equivalent write methods) to append more content into the stream. Lastly, useABufferedArchiveOutputStream.close()
to properly finalize the archive file generation.We use buffered intermediary streams after the GZip output stream logic itself. All writing operations happen into a temporary file. We rename this temporary file into the final archive file (specified when creating this instance) upon closure of this stream.
We use an intermediary buffered stream AFTER the ZIP logic, because
GZIPOutputStream
is delegating to an underlying output stream for the final writing ofbyte[]
content into system resources: this is eitherRandomAccessFile
(if default constructor ofGZIPOutputStream
is used and instantiation ofRandomAccessFile
possible) orFileOutputStream
(in all other cases).Now, when the
Deflater
output (e.g. compressed output) is ready,GZIPOutputStream
is using an intermediary buffer to read thebyte[]
of "compressed content" and pass that to the underlying output stream'swrite(byte[], int, int)
(or equivalent). This buffer is of sizeBUFFER_SIZE = 512
, which is quite small.This means that (since
RandomAccessFile
andFileOutputStream
are not buffered) we would be going to write to the system resource by blocks ofBUFFER_SIZE
bytes, which might be sub-optimal. So, instead, we use aBufferedOutputStream
(piping into aFileOutputStream
) as underlying output stream for theGZIPOutputStream
that this stream wraps. The size of this output buffer is by default equal toABufferedArchiveOutputStream.DEFAULT_SYSTEM_OUTPUT_BUFFER_SIZE
(which is the default size suggested inBufferedOutputStream
implementation) but this can be specified differently at instantiation time.- Author:
- ActiveViam
- See Also:
GZIPOutputStream
,ABufferedArchiveOutputStream
-
-
Field Summary
-
Fields inherited from class com.activeviam.io.file.impl.ABufferedArchiveOutputStream
DEFAULT_SYSTEM_OUTPUT_BUFFER_SIZE, TMP_FILE_SUFFIX, ZIP_DEFLATER_INPUT_BUFFER_SIZE
-
Fields inherited from class java.io.BufferedOutputStream
buf, count
-
Fields inherited from class java.io.FilterOutputStream
out
-
-
Constructor Summary
Constructors Constructor Description BufferedGZipOutputStream(File archiveFile, boolean keepOutputFileIfFailure)
Default constructor.BufferedGZipOutputStream(File outputFile, int systemOutputBufferSize, boolean keepOutputFileIfFailure)
Alternative constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description long
getBytesWritten()
void
write(byte[] b)
void
write(byte[] b, int off, int len)
void
write(int b)
-
Methods inherited from class com.activeviam.io.file.impl.ABufferedArchiveOutputStream
beforeClose, close, getCurrentArchiveFile, getFinalArchiveFile, setOutputSuccess
-
Methods inherited from class java.io.BufferedOutputStream
flush
-
Methods inherited from class java.io.OutputStream
nullOutputStream
-
-
-
-
Constructor Detail
-
BufferedGZipOutputStream
public BufferedGZipOutputStream(File archiveFile, boolean keepOutputFileIfFailure) throws IOException
Default constructor.Archive will be created in ZIP32 mode and the system output buffer (buffer used for writing to filesystem resource) will be sized with
ABufferedArchiveOutputStream.DEFAULT_SYSTEM_OUTPUT_BUFFER_SIZE
.- Parameters:
archiveFile
- abstract representation of the target archive file pathnamekeepOutputFileIfFailure
- TRUE if the generated file needs to be kept even in case of failure of the extraction- Throws:
IOException
- in case of stream creation failure
-
BufferedGZipOutputStream
public BufferedGZipOutputStream(File outputFile, int systemOutputBufferSize, boolean keepOutputFileIfFailure) throws IOException
Alternative constructor.- Parameters:
outputFile
- abstract representation of the final archive file pathname (seeABufferedArchiveOutputStream.getCurrentArchiveFile()
)systemOutputBufferSize
- size of the intermediary buffer used to write compressed results into the system resources (e.g. final archive file(s) on the filesystem)keepOutputFileIfFailure
- TRUE if the generated file needs to be kept even in case of failure of the extraction- Throws:
IOException
- in case of stream creation failure
-
-
Method Detail
-
getBytesWritten
public long getBytesWritten()
- Returns:
- the number of bytes written into this stream (a.k.a. the volume of uncompressed data that has been processed). If the stream is still open when this is called this will return the bytes written so far (depends on what has been passed for writing and on the buffer fill/flush cycles), otherwise (called after stream closure) it will return the final number of bytes written.
-
write
public void write(byte[] b) throws IOException
- Overrides:
write
in classFilterOutputStream
- Throws:
IOException
-
write
public void write(byte[] b, int off, int len) throws IOException
- Overrides:
write
in classBufferedOutputStream
- Throws:
IOException
-
write
public void write(int b) throws IOException
- Overrides:
write
in classBufferedOutputStream
- Throws:
IOException
-
-