All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.cedarsoftware.util.AdjustableGZIPOutputStream Maven / Gradle / Ivy

The newest version!
package com.cedarsoftware.util;

import java.io.IOException;
import java.io.OutputStream;
import java.util.zip.GZIPOutputStream;

/**
 * A customizable extension of {@link GZIPOutputStream} that allows users to specify the compression level.
 * 

* {@code AdjustableGZIPOutputStream} enhances the functionality of {@code GZIPOutputStream} by providing * constructors that let users configure the compression level, enabling control over the trade-off between * compression speed and compression ratio. *

* *

Key Features

*
    *
  • Supports all compression levels defined by {@link java.util.zip.Deflater}, including: *
      *
    • {@link java.util.zip.Deflater#DEFAULT_COMPRESSION}
    • *
    • {@link java.util.zip.Deflater#BEST_SPEED}
    • *
    • {@link java.util.zip.Deflater#BEST_COMPRESSION}
    • *
    • Specific levels from 0 (no compression) to 9 (maximum compression).
    • *
    *
  • *
  • Provides constructors to set both the compression level and buffer size.
  • *
  • Fully compatible with the standard {@code GZIPOutputStream} API.
  • *
* *

Usage Example

*
{@code
 * try (OutputStream fileOut = Files.newOutputStream(Paths.get("compressed.gz"));
 *      AdjustableGZIPOutputStream gzipOut = new AdjustableGZIPOutputStream(fileOut, Deflater.BEST_COMPRESSION)) {
 *     gzipOut.write("Example data to compress".getBytes(StandardCharsets.UTF_8));
 * }
 * }
* *

Additional Notes

*
    *
  • If the specified compression level is invalid, a {@link java.lang.IllegalArgumentException} will be thrown.
  • *
  • The default compression level is {@link java.util.zip.Deflater#DEFAULT_COMPRESSION} when not specified.
  • *
  • The {@code AdjustableGZIPOutputStream} inherits all thread-safety properties of {@code GZIPOutputStream}.
  • *
* * @see GZIPOutputStream * @see java.util.zip.Deflater * * @author John DeRegnaucourt ([email protected]) *
* Copyright (c) Cedar Software LLC *

* Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at *

* License *

* Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ public class AdjustableGZIPOutputStream extends GZIPOutputStream { public AdjustableGZIPOutputStream(OutputStream out, int level) throws IOException { super(out); def.setLevel(level); } public AdjustableGZIPOutputStream(OutputStream out, int size, int level) throws IOException { super(out, size); def.setLevel(level); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy