001package ca.uhn.hl7v2.hoh.util;
002
003import java.io.ByteArrayInputStream;
004import java.io.ByteArrayOutputStream;
005import java.io.IOException;
006import java.util.zip.GZIPInputStream;
007import java.util.zip.GZIPOutputStream;
008
009/**
010 * Methods for dealing with GZip encoding
011 */
012public class GZipUtils {
013
014        /**
015         * Non instantiable
016         */
017        private GZipUtils() {
018                // nothing
019        }
020        
021        /**
022         * Compresses a byte array
023         */
024        public static byte[] compress(byte[] theBytes) throws IOException {
025                ByteArrayOutputStream bos = new ByteArrayOutputStream();
026                GZIPOutputStream gos = new GZIPOutputStream(bos);
027                gos.write(theBytes);
028                gos.close();
029                return bos.toByteArray();
030        }
031        
032        /**
033         * Compresses a byte array
034         */
035        public static byte[] uncompress(byte[] theBytes) throws IOException {
036                ByteArrayInputStream bis = new ByteArrayInputStream(theBytes);
037                GZIPInputStream gos = new GZIPInputStream(bis);
038                ByteArrayOutputStream bos = new ByteArrayOutputStream();
039                IOUtils.copy(gos, bos);         
040                return bos.toByteArray();
041        }
042
043}