View Javadoc
1   package ca.uhn.hl7v2.hoh.util;
2   
3   import java.io.ByteArrayInputStream;
4   import java.io.ByteArrayOutputStream;
5   import java.io.IOException;
6   import java.util.zip.GZIPInputStream;
7   import java.util.zip.GZIPOutputStream;
8   
9   /**
10   * Methods for dealing with GZip encoding
11   */
12  public class GZipUtils {
13  
14  	/**
15  	 * Non instantiable
16  	 */
17  	private GZipUtils() {
18  		// nothing
19  	}
20  	
21  	/**
22  	 * Compresses a byte array
23  	 */
24  	public static byte[] compress(byte[] theBytes) throws IOException {
25  		ByteArrayOutputStream bos = new ByteArrayOutputStream();
26  		GZIPOutputStream gos = new GZIPOutputStream(bos);
27  		gos.write(theBytes);
28  		gos.close();
29  		return bos.toByteArray();
30  	}
31  	
32  	/**
33  	 * Compresses a byte array
34  	 */
35  	public static byte[] uncompress(byte[] theBytes) throws IOException {
36  		ByteArrayInputStream bis = new ByteArrayInputStream(theBytes);
37  		GZIPInputStream gos = new GZIPInputStream(bis);
38  		ByteArrayOutputStream bos = new ByteArrayOutputStream();
39  		IOUtils.copy(gos, bos);		
40  		return bos.toByteArray();
41  	}
42  
43  }