Clover coverage report -
Coverage timestamp: Mon May 5 2008 11:56:20 GMT-05:00
file stats: LOC: 219   Methods: 8
NCLOC: 126   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
CompressionUtil.java - 100% 100% 100%
coverage
 1    /*
 2    * Copyright 2005 The Regents of the University of Michigan
 3    *
 4    * Licensed under the Apache License, Version 2.0 (the "License");
 5    * you may not use this file except in compliance with the License.
 6    * You may obtain a copy of the License at
 7    *
 8    * http://www.apache.org/licenses/LICENSE-2.0
 9    *
 10    * Unless required by applicable law or agreed to in writing, software
 11    * distributed under the License is distributed on an "AS IS" BASIS,
 12    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13    * See the License for the specific language governing permissions and
 14    * limitations under the License.
 15    */
 16    package org.proteomecommons.tranche.util;
 17   
 18    import SevenZip.LzmaAlone;
 19    import java.io.ByteArrayInputStream;
 20    import java.io.ByteArrayOutputStream;
 21    import java.io.File;
 22    import java.io.FileInputStream;
 23    import java.io.FileOutputStream;
 24    import java.io.IOException;
 25    import java.util.List;
 26    import java.util.zip.GZIPInputStream;
 27    import java.util.zip.GZIPOutputStream;
 28    import org.apache.tools.bzip2.CBZip2InputStream;
 29    import org.apache.tools.bzip2.CBZip2OutputStream;
 30   
 31    /**
 32    * Helper methods for using various compression encodings. Includes methods that use temporary files and methods that don't.
 33    * @author Jayson Falkner - jfalkner@umich.edu
 34    */
 35    public class CompressionUtil {
 36   
 37    /**
 38    *GZIPs the in memory and returns the compressed bytes. Avoids any time penalties associated with making/using files.
 39    */
 40  1 public static final byte[] gzipCompress(byte[] dataBytes, byte[] padding) throws IOException {
 41    // transfer the file
 42  1 ByteArrayInputStream fis = null;
 43  1 ByteArrayOutputStream fos = null;
 44  1 GZIPOutputStream gos = null;
 45  1 try {
 46    // make the streams
 47  1 fis = new ByteArrayInputStream(dataBytes);
 48  1 fos = new ByteArrayOutputStream();
 49  1 gos = new GZIPOutputStream(fos);
 50    // write out the content
 51  1 IOUtil.getBytes(fis, gos);
 52  1 gos.write(padding);
 53    } finally {
 54  1 IOUtil.safeClose(fis);
 55  1 IOUtil.safeClose(gos);
 56  1 IOUtil.safeClose(fos);
 57    }
 58    // return the gzip'd content
 59  1 return fos.toByteArray();
 60    }
 61   
 62    /**
 63    *Decompresses the input bytes assuming that it is GZIP'd. Completely avoids the use of temporary files, which saves associated time.
 64    */
 65  328 public static final byte[] gzipDecompress(byte[] dataBytes) throws IOException {
 66    // transfer the file
 67  328 ByteArrayInputStream fis = null;
 68  328 ByteArrayOutputStream fos = null;
 69  328 GZIPInputStream gis = null;
 70  328 try {
 71    // make the streams
 72  328 fis = new ByteArrayInputStream(dataBytes);
 73  328 fos = new ByteArrayOutputStream();
 74  328 gis = new GZIPInputStream(fis);
 75    // write out the content
 76  328 IOUtil.getBytes(gis, fos);
 77    } finally {
 78  328 IOUtil.safeClose(gis);
 79  328 IOUtil.safeClose(fis);
 80  328 IOUtil.safeClose(fos);
 81    }
 82    // return the gzip'd content
 83  328 return fos.toByteArray();
 84    }
 85   
 86   
 87    /**
 88    *GZIPs the input file and returns a pointer to a file that is GZIP compressed.
 89    */
 90  2 public static final File gzipCompress(File input) throws IOException {
 91    // make a temp file
 92  2 File gzip = TempFileUtil.createTemporaryFile(".gzip");
 93    // transfer the file
 94  2 FileInputStream fis = null;
 95  2 FileOutputStream fos = null;
 96  2 GZIPOutputStream gos = null;
 97  2 try {
 98    // make the streams
 99  2 fis = new FileInputStream(input);
 100  2 fos = new FileOutputStream(gzip);
 101  2 gos = new GZIPOutputStream(fos);
 102    // write out the content
 103  2 IOUtil.getBytes(fis, gos);
 104    } finally {
 105  2 IOUtil.safeClose(fis);
 106  2 IOUtil.safeClose(gos);
 107  2 IOUtil.safeClose(fos);
 108    }
 109    // return the gzip'd content
 110  2 return gzip;
 111    }
 112   
 113    /**
 114    *Decompresses the input file assuming that it is GZIP'd.
 115    */
 116  86 public static final File gzipDecompress(File input) throws IOException {
 117    // make a temp file
 118  86 File gzip = TempFileUtil.createTemporaryFile();
 119    // transfer the file
 120  86 FileInputStream fis = null;
 121  86 FileOutputStream fos = null;
 122  86 GZIPInputStream gis = null;
 123  86 try {
 124    // make the streams
 125  86 fis = new FileInputStream(input);
 126  86 fos = new FileOutputStream(gzip);
 127  86 gis = new GZIPInputStream(fis);
 128    // write out the content
 129  86 IOUtil.getBytes(gis, fos);
 130    } finally {
 131  86 IOUtil.safeClose(gis);
 132  86 IOUtil.safeClose(fis);
 133  86 IOUtil.safeClose(fos);
 134    }
 135    // return the gzip'd content
 136  86 return gzip;
 137    }
 138   
 139   
 140    /**
 141    *bzip2 compresses the input file and returns a reference to the compressed file.
 142    */
 143  1 public static final File bzip2Compress(File input) throws IOException {
 144    // make a temp file
 145  1 File compressed = TempFileUtil.createTemporaryFile();
 146    // transfer the file
 147  1 FileInputStream fis = null;
 148  1 FileOutputStream fos = null;
 149  1 CBZip2OutputStream gos = null;
 150  1 try {
 151    // make the streams
 152  1 fis = new FileInputStream(input);
 153  1 fos = new FileOutputStream(compressed);
 154  1 gos = new CBZip2OutputStream(fos);
 155    // write out the content
 156  1 IOUtil.getBytes(fis, gos);
 157    } finally {
 158  1 IOUtil.safeClose(fis);
 159  1 IOUtil.safeClose(gos);
 160  1 IOUtil.safeClose(fos);
 161    }
 162    // return the gzip'd content
 163  1 return compressed;
 164    }
 165   
 166    /**
 167    *Decompresses the input file assuming that it is bzip2 compressed.
 168    */
 169  1 public static final File bzip2Decompress(File input) throws IOException {
 170    // make a temp file
 171  1 File decompressed = TempFileUtil.createTemporaryFile();
 172    // transfer the file
 173  1 FileInputStream fis = null;
 174  1 FileOutputStream fos = null;
 175  1 CBZip2InputStream gis = null;
 176  1 try {
 177    // make the streams
 178  1 fis = new FileInputStream(input);
 179  1 fos = new FileOutputStream(decompressed);
 180  1 gis = new CBZip2InputStream(fis);
 181    // write out the content
 182  1 IOUtil.getBytes(gis, fos);
 183    } finally {
 184  1 IOUtil.safeClose(gis);
 185  1 IOUtil.safeClose(fis);
 186  1 IOUtil.safeClose(fos);
 187    }
 188    // return the gzip'd content
 189  1 return decompressed;
 190    }
 191   
 192    /**
 193    *Decompresses the input file assuming that it is LZMA compressed..
 194    */
 195  1 public static final File lzmaDecompress(File input) throws Exception {
 196    // make a temp file
 197  1 File decompressed = TempFileUtil.createTemporaryFile();
 198   
 199    // decompress using LZMA
 200  1 LzmaAlone.main(new String[] {"d", input.getCanonicalPath(), decompressed.getCanonicalPath()});
 201   
 202    // return the gzip'd content
 203  1 return decompressed;
 204    }
 205   
 206    /**
 207    *Compresses the input file using LZMA.
 208    */
 209  1 public static final File lzmaCompress(File input) throws Exception {
 210    // make a temp file
 211  1 File compressed = TempFileUtil.createTemporaryFile();
 212   
 213    // compress using LZMA
 214  1 LzmaAlone.main(new String[] {"e", input.getCanonicalPath(), compressed.getCanonicalPath()});
 215   
 216    // return the gzip'd content
 217  1 return compressed;
 218    }
 219    }