com.hfg.util.io.GZIP Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of com_hfg Show documentation
Show all versions of com_hfg Show documentation
com.hfg xml, html, svg, and bioinformatics utility library
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
{
byte[] output = null;
try
{
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
GZIPOutputStream zipStream = new GZIPOutputStream(outStream);
zipStream.write(inValue, 0, inValue.length);
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));
}
}