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

com.hfg.util.io.GZIP Maven / Gradle / Ivy

There is a newer version: 20240423
Show newest version
package com.hfg.util.io;

// IMPORTS
import com.hfg.exception.CompressionException;

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

//------------------------------------------------------------------------------
/**
  Static methods to simplify GZIP compression/uncompression

  @author Alex Taylor
 */
//------------------------------------------------------------------------------

public class GZIP
{

   //##########################################################################
   // PUBLIC METHODS
   //##########################################################################


    //--------------------------------------------------------------------------
    public static byte[] compress(byte[] inValue)
    throws CompressionException
    {
        return compress(inValue, 0, inValue.length);
    }

    //--------------------------------------------------------------------------
    public static byte[] compress(byte[] inValue, int inOffset, int inLength)
    throws CompressionException
    {
        byte[] output;

        try
        {
            ByteArrayOutputStream outStream = new ByteArrayOutputStream();
            GZIPOutputStream zipStream = new GZIPOutputStream(outStream);

            zipStream.write(inValue, inOffset, inLength);
            zipStream.close();
            output = outStream.toByteArray();
        }
        catch (IOException e)
        {
            throw new CompressionException(e);
        }

        return output;
    }

    //--------------------------------------------------------------------------
    public static byte[] compress(String inValue)
    throws CompressionException
    {
        byte[] output = null;

        if (inValue != null)
        {
            output = compress(inValue.getBytes());
        }

        return output;
    }

   //--------------------------------------------------------------------------
   public static byte[] uncompress(byte[] inValue)
   throws CompressionException
   {
        byte[] output = null;
        
        try
        {
            ByteArrayInputStream inStream = new ByteArrayInputStream(inValue);
            GZIPInputStream zipStream = new GZIPInputStream(inStream);

            ByteArrayOutputStream outStream = new ByteArrayOutputStream();

            byte[] buffer = new byte[2048];
            int bytesRead;
            while ((bytesRead = zipStream.read(buffer, 0, buffer.length)) != -1)
            {
                outStream.write(buffer, 0, bytesRead);
            }

            output = outStream.toByteArray();
            inStream.close();
            outStream.close();
        }
        catch (IOException e)
        {
            throw new CompressionException(e);
        }
        
        return output;
   }

   //--------------------------------------------------------------------------
   public static String uncompressToString(byte[] inValue)
   throws CompressionException
   {
       return new String(uncompress(inValue));
   }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy