Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8336681

Add GZIPOutputStream constructor to specify Deflater

XMLWordPrintable

    • Icon: CSR CSR
    • Resolution: Unresolved
    • Icon: P4 P4
    • 25
    • core-libs
    • None
    • source
    • minimal
    • The addition of a new constructor to the class should not affect the behavior of any existing code, whether source or binary.
    • Java API
    • SE

      Summary

      Add a new constructor to the GZIPOutputStream that allows the caller to provide a custom Deflater instance.

      Problem

      The class GZIPOutputStream extends DeflaterOutputStream, which is logical because the GZIP encoding is based on ZLIB "deflate" encoding.

      However, while DeflaterOutputStream provides constructors that take a custom Deflater argument supplied by the caller, GZIPOutputStream has no such constructors.

      As a result, it's not possible to do entirely reasonable customization, such as configuring a GZIPOutputStream for a non-default compression level.

      To quote Mark Reinhold, asking for such a constructor "is not an unreasonable request" (May 14, 2001).

      Solution

      Add a new constructor taking all of the existing parameters (size and syncFlush) and adding a Deflater parameter.

      Another option would be to add multiple constructors, with all combinations with/without size and syncFlush which is what DeflaterOutputStream does.

      However, that was deemed overkill. A single new constructor which takes all of the other optional parameters simply requires those users who need to supply a custom Deflater to also supply those parameters, which is not hard, especially if you are user who's sophisticated enough to know what a Deflater is.

      Specification

      diff --git a/src/java.base/share/classes/java/util/zip/GZIPOutputStream.java b/src/java.base/share/classes/java/util/zip/GZIPOutputStream.java
      index cea0880fca6..8d6ce5d8452 100644
      --- a/src/java.base/share/classes/java/util/zip/GZIPOutputStream.java
      +++ b/src/java.base/share/classes/java/util/zip/GZIPOutputStream.java
      @@ -59,6 +59,38 @@ public class GZIPOutputStream extends DeflaterOutputStream {
           // Represents the default "unknown" value for OS header, per RFC-1952
           private static final byte OS_UNKNOWN = (byte) 255;
      
      +    /**
      +     * Creates a new output stream with the specified compressor,
      +     * buffer size and flush mode.
      +     *
      +     * <p>
      +     * The given {@link Deflater} will not be closed by this instance. To release
      +     * the resources used by {@code def}, an application must explicitly close it
      +     * by invoking its {@link Deflater#end end()} or {@link Deflater#close close()}
      +     * method.
      +     *
      +     * @param out the output stream
      +     * @param def the compressor ("deflater")
      +     * @param size the output buffer size
      +     * @param syncFlush
      +     *        if {@code true} invocation of the inherited
      +     *        {@link DeflaterOutputStream#flush() flush()} method of
      +     *        this instance flushes the compressor with flush mode
      +     *        {@link Deflater#SYNC_FLUSH} before flushing the output
      +     *        stream, otherwise only flushes the output stream
      +     * @throws    IOException If an I/O error has occurred.
      +     * @throws    IllegalArgumentException if {@code size <= 0}
      +     *
      +     * @since 25
      +     */
      +    public GZIPOutputStream(OutputStream out, Deflater def, int size, boolean syncFlush)
      +        throws IOException
      +    {
      +        super(out, def, size, syncFlush);
      +        writeHeader();
      +        crc.reset();
      +    }
      +
           /**
            * Creates a new output stream with the specified buffer size.
            *

            acobbs Archie Cobbs
            kryansunw Kevin Ryan (Inactive)
            Chen Liang
            Votes:
            1 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: