Top Description Fields Constructors Methods
org.apache.commons.compress.compressors.gzip

public Class GzipCompressorOutputStream

extends CompressorOutputStream
Class Inheritance
Imports
java.io.IOException, .OutputStream, java.net.URI, .URISyntaxException, java.nio.ByteBuffer, .ByteOrder, java.nio.charset.StandardCharsets, java.util.zip.CRC32, .Deflater, .GZIPInputStream, .GZIPOutputStream, org.apache.commons.compress.compressors.CompressorOutputStream

Compressed output stream using the gzip format. This implementation improves over the standard GZIPOutputStream class by allowing the configuration of the compression level and the header metadata (file name, comment, modification time, operating system and extra flags).
See Also
GZIP File Format Specification

Field Summary

Modifier and TypeField and Description
private boolean
closed

Indicates if the stream has been closed

private final CRC32
crc

The checksum of the uncompressed data

private final byte[]
deflateBuffer

The buffer receiving the compressed data from the deflater

private final Deflater
deflater

Deflater used to compress the data

private static final int
FCOMMENT

Header flag indicating a comment follows the header

private static final int
FNAME

Header flag indicating a file name follows the header

private final OutputStream
out

The underlying stream

Constructor Summary

AccessConstructor and Description
public
GzipCompressorOutputStream(final OutputStream
the stream to compress to
out
)

Creates a gzip compressed output stream with the default parameters.

public
GzipCompressorOutputStream(final OutputStream
the stream to compress to
out
,
final GzipParameters
the parameters to use
parameters
)

Creates a gzip compressed output stream with the specified parameters.

Method Summary

Modifier and TypeMethod and Description
public void
close()

Overrides java.io.OutputStream.close.

Implements java.io.Closeable.close.

Closes this output stream and releases any system resources associated with this stream.
private void
public void
finish()

Finishes writing compressed data to the underlying stream without closing it.

public void
flush()

Overrides java.io.OutputStream.flush.

Implements java.io.Flushable.flush.

Flushes this output stream and forces any buffered output bytes to be written out.
private byte[]
getBytes(final String
The string to encode.
string
)

Gets the bytes encoded in the GzipUtils#GZIP_ENCODING Charset.

public void
write(final byte[]
the data.
buffer
)

Overrides java.io.OutputStream.write.

Writes b.length bytes from the specified byte array to this output stream.
public void
write(final byte[]
the data.
buffer
,
final int
the start offset in the data.
offset
,
final int
the number of bytes to write.
length
)

Overrides java.io.OutputStream.write.

Writes len bytes from the specified byte array starting at offset off to this output stream.
public void
write(final int
the byte.
b
)

Implements abstract java.io.OutputStream.write.

Writes the specified byte to this output stream.
private void
writeHeader(final GzipParameters parameters)

private void

Field Detail

closedback to summary
private boolean closed

Indicates if the stream has been closed

crcback to summary
private final CRC32 crc

The checksum of the uncompressed data

deflateBufferback to summary
private final byte[] deflateBuffer

The buffer receiving the compressed data from the deflater

deflaterback to summary
private final Deflater deflater

Deflater used to compress the data

FCOMMENTback to summary
private static final int FCOMMENT

Header flag indicating a comment follows the header

FNAMEback to summary
private static final int FNAME

Header flag indicating a file name follows the header

outback to summary
private final OutputStream out

The underlying stream

Constructor Detail

GzipCompressorOutputStreamback to summary
public GzipCompressorOutputStream(final OutputStream out) throws IOException

Creates a gzip compressed output stream with the default parameters.

Parameters
out:OutputStream

the stream to compress to

Exceptions
IOException:
if writing fails
GzipCompressorOutputStreamback to summary
public GzipCompressorOutputStream(final OutputStream out, final GzipParameters parameters) throws IOException

Creates a gzip compressed output stream with the specified parameters.

Parameters
out:OutputStream

the stream to compress to

parameters:GzipParameters

the parameters to use

Exceptions
IOException:
if writing fails
Since
1.7

Method Detail

closeback to summary
public void close() throws IOException

Overrides java.io.OutputStream.close.

Implements java.io.Closeable.close.

Doc from java.io.OutputStream.close.

Closes this output stream and releases any system resources associated with this stream. The general contract of close is that it closes the output stream. A closed stream cannot perform output operations and cannot be reopened.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs.
deflateback to summary
private void deflate() throws IOException
finishback to summary
public void finish() throws IOException

Finishes writing compressed data to the underlying stream without closing it.

Exceptions
IOException:
on error
Since
1.7
flushback to summary
public void flush() throws IOException

Overrides java.io.OutputStream.flush.

Implements java.io.Flushable.flush.

Doc from java.io.OutputStream.flush.

Flushes this output stream and forces any buffered output bytes to be written out. The general contract of flush is that calling it is an indication that, if any bytes previously written have been buffered by the implementation of the output stream, such bytes should immediately be written to their intended destination.

If the intended destination of this stream is an abstraction provided by the underlying operating system, for example a file, then flushing the stream guarantees only that bytes previously written to the stream are passed to the operating system for writing; it does not guarantee that they are actually written to a physical device such as a disk drive.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs.
Since
1.7
getBytesback to summary
private byte[] getBytes(final String string) throws IOException

Gets the bytes encoded in the GzipUtils#GZIP_ENCODING Charset.

If the string cannot be encoded directly with GzipUtils#GZIP_ENCODING, then use URI-style percent encoding.

Parameters
string:String

The string to encode.

writeback to summary
public void write(final byte[] buffer) throws IOException

Overrides java.io.OutputStream.write.

Doc from java.io.OutputStream.write.

Writes b.length bytes from the specified byte array to this output stream. The general contract for write(b) is that it should have exactly the same effect as the call write(b, 0, b.length).

Parameters
buffer:byte[]

the data.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs.
Since
1.1
writeback to summary
public void write(final byte[] buffer, final int offset, final int length) throws IOException

Overrides java.io.OutputStream.write.

Doc from java.io.OutputStream.write.

Writes len bytes from the specified byte array starting at offset off to this output stream. The general contract for write(b, off, len) is that some of the bytes in the array b are written to the output stream in order; element b[off] is the first byte written and b[off+len-1] is the last byte written by this operation.

If b is null, a NullPointerException is thrown.

If off is negative, or len is negative, or off+len is greater than the length of the array b, then an IndexOutOfBoundsException is thrown.

Parameters
buffer:byte[]

the data.

offset:int

the start offset in the data.

length:int

the number of bytes to write.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs. In particular, an IOException is thrown if the output stream is closed.
Since
1.1
writeback to summary
public void write(final int b) throws IOException

Implements abstract java.io.OutputStream.write.

Doc from java.io.OutputStream.write.

Writes the specified byte to this output stream. The general contract for write is that one byte is written to the output stream. The byte to be written is the eight low-order bits of the argument b. The 24 high-order bits of b are ignored.

Parameters
b:int

the byte.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs. In particular, an IOException may be thrown if the output stream has been closed.
writeHeaderback to summary
private void writeHeader(final GzipParameters parameters) throws IOException
writeTrailerback to summary
private void writeTrailer() throws IOException