Package java.util.zip

Class DeflaterOutputStream

All Implemented Interfaces:
Closeable, Flushable, AutoCloseable
Direct Known Subclasses:
GZIPOutputStream, ZipOutputStream

public class DeflaterOutputStream
extends FilterOutputStream
This class provides an implementation of FilterOutputStream that compresses data using the DEFLATE algorithm. Basically it wraps the Deflater class and takes care of the buffering.
See Also:
Deflater
  • Field Details

    • buf

      protected byte[] buf
      The buffer for the data to be written to.
    • def

      protected Deflater def
      The deflater used.
  • Constructor Details

    • DeflaterOutputStream

      public DeflaterOutputStream​(OutputStream os)
      Constructs a new instance with a default-constructed Deflater.
    • DeflaterOutputStream

      public DeflaterOutputStream​(OutputStream os, Deflater def)
      Constructs a new instance with the given Deflater.
    • DeflaterOutputStream

      public DeflaterOutputStream​(OutputStream os, Deflater def, int bufferSize)
      Constructs a new instance with the given Deflater and buffer size.
    • DeflaterOutputStream

      public DeflaterOutputStream​(OutputStream os, boolean syncFlush)
      Constructs a new instance with the given flushing behavior (see flush()).
      Since:
      1.7
    • DeflaterOutputStream

      public DeflaterOutputStream​(OutputStream os, Deflater def, boolean syncFlush)
      Constructs a new instance with the given Deflater and flushing behavior (see flush()).
      Since:
      1.7
    • DeflaterOutputStream

      public DeflaterOutputStream​(OutputStream os, Deflater def, int bufferSize, boolean syncFlush)
      Constructs a new instance with the given Deflater, buffer size, and flushing behavior (see flush()).
      Since:
      1.7
  • Method Details

    • deflate

      protected void deflate() throws IOException
      Compress the data in the input buffer and write it to the underlying stream.
      Throws:
      IOException - If an error occurs during deflation.
    • close

      public void close() throws IOException
      Writes any unwritten compressed data to the underlying stream, the closes all underlying streams. This stream can no longer be used after close() has been called.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Overrides:
      close in class FilterOutputStream
      Throws:
      IOException - If an error occurs while closing the data compression process.
    • finish

      public void finish() throws IOException
      Writes any unwritten data to the underlying stream. Does not close the stream.
      Throws:
      IOException - If an error occurs.
    • write

      public void write​(int i) throws IOException
      Description copied from class: FilterOutputStream
      Writes one byte to the target stream. Only the low order byte of the integer oneByte is written.
      Overrides:
      write in class FilterOutputStream
      Parameters:
      i - the byte to be written.
      Throws:
      IOException - if an I/O error occurs while writing to this stream.
    • write

      public void write​(byte[] buffer, int offset, int byteCount) throws IOException
      Compresses byteCount bytes of data from buf starting at offset and writes it to the underlying stream.
      Overrides:
      write in class FilterOutputStream
      Parameters:
      buffer - the buffer to write.
      offset - the index of the first byte in buffer to write.
      byteCount - the number of bytes in buffer to write.
      Throws:
      IOException - If an error occurs during writing.
    • flush

      public void flush() throws IOException
      Flushes the underlying stream. This flushes only the bytes that can be compressed at the highest level.

      For deflater output streams constructed with the syncFlush parameter set to true, this first flushes all outstanding data so that it may be immediately read by its recipient. Doing so may degrade compression but improve interactive behavior.

      Specified by:
      flush in interface Flushable
      Overrides:
      flush in class FilterOutputStream
      Throws:
      IOException - if an error occurs attempting to flush this stream.