/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2015 Lachlan Dowding
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package permafrost.tundra.zip;

import permafrost.tundra.io.StreamHelper;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

/**
 * A collection of convenience methods for working with GZIP compression.
 */
public class GZIPHelper {
    /**
     * Disallow instantiation of this class.
     */
    private GZIPHelper() {}

    /**
     * GZIP compresses the given data.
     * @param inputStream   The data to be compressed.
     * @return              The compressed data.
     * @throws IOException  If an I/O problem occurs when reading from the stream.
     */
    public static InputStream compress(InputStream inputStream) throws IOException {
        if (inputStream == null) return null;

        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        StreamHelper.copy(StreamHelper.normalize(inputStream), new GZIPOutputStream(byteArrayOutputStream));

        return StreamHelper.normalize(byteArrayOutputStream.toByteArray());
    }

    /**
     * GZIP compresses the given data.
     * @param bytes         The data to be compressed.
     * @return              The compressed data.
     * @throws IOException  If an I/O problem occurs when reading from the stream.
     */
    public static InputStream compress(byte[] bytes) throws IOException {
        return compress(StreamHelper.normalize(bytes));
    }

    /**
     * GZIP decompresses the given data.
     * @param inputStream   The compressed data to be decompressed.
     * @return              The decompressed data.
     * @throws IOException  If an I/O problem occurs when reading from the stream.
     */
    public static InputStream decompress(InputStream inputStream) throws IOException {
        if (inputStream == null) return null;

        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        StreamHelper.copy(new GZIPInputStream(StreamHelper.normalize(inputStream)), byteArrayOutputStream);

        return StreamHelper.normalize(byteArrayOutputStream.toByteArray());
    }

    /**
     * GZIP decompresses the given data.
     * @param bytes         The compressed data to be decompressed.
     * @return              The decompressed data.
     * @throws IOException  If an I/O problem occurs when reading from the stream.
     */
    public static InputStream decompress(byte[] bytes) throws IOException {
        return decompress(StreamHelper.normalize(bytes));
    }
}

@@ -1215,16 +1215,104 @@
 mHelper;
+%0Aimport permafrost.tundra.lang.BytesHelper;%0Aimport permafrost.tundra.lang.CharsetHelper;
 %0A%0Aimport
@@ -1391,32 +1391,65 @@
 io.InputStream;%0A
+import java.nio.charset.Charset;%0A
 import java.util
@@ -2729,32 +2729,1334 @@
 bytes));%0A    %7D%0A%0A
+    /**%0A     * GZIP compresses the given data.%0A     * @param string        The data to be compressed.%0A     * @param charset       The character set to use.%0A     * @return              The compressed data.%0A     * @throws IOException  If an I/O problem occurs when reading from the stream.%0A     */%0A    public static InputStream compress(String string, Charset charset) throws IOException %7B%0A        return compress(StreamHelper.normalize(string, charset));%0A    %7D%0A%0A    /**%0A     * GZIP compresses the given data.%0A     * @param string        The data to be compressed.%0A     * @param charsetName   The character set to use.%0A     * @return              The compressed data.%0A     * @throws IOException  If an I/O problem occurs when reading from the stream.%0A     */%0A    public static InputStream compress(String string, String charsetName) throws IOException %7B%0A        return compress(string, CharsetHelper.normalize(charsetName));%0A    %7D%0A%0A    /**%0A     * GZIP compresses the given data.%0A     * @param string        The data to be compressed.%0A     * @return              The compressed data.%0A     * @throws IOException  If an I/O problem occurs when reading from the stream.%0A     */%0A    public static InputStream compress(String string) throws IOException %7B%0A        return compress(string, (Charset)null);%0A    %7D%0A%0A
     /**%0A     * G
@@ -5034,32 +5034,32 @@
 s IOException %7B%0A
-
         return d
@@ -5105,11 +5105,466 @@
 );%0A    %7D
+%0A%0A    /**%0A     * GZIP decompresses the given data.%0A     * @param base64        The compressed data as a base64-encoded string to be decompressed.%0A     * @return              The decompressed data.%0A     * @throws IOException  If an I/O problem occurs when reading from the stream.%0A     */%0A    public static InputStream decompress(String base64) throws IOException %7B%0A        return decompress(StreamHelper.normalize(BytesHelper.base64Decode(base64)));%0A    %7D
 %0A%7D%0A

Add overrides to compress and decompress methods in GZIPHelper that accept the data as a String