package org.opencb.commons.utils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.DataFormatException;
import java.util.zip.Deflater;
import java.util.zip.Inflater;

/**
 * @author Cristina Yenyxe Gonzalez Garcia <cyenyxe@ebi.ac.uk>
 */
public final class CompressionUtils {

    private CompressionUtils() {
    }

    public static byte[] compress(byte[] data) throws IOException {
        Deflater deflater = new Deflater();
        deflater.setInput(data);

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);

        deflater.finish();
        byte[] buffer = new byte[1024];
        while (!deflater.finished()) {
            int count = deflater.deflate(buffer); // returns the generated code... index
            outputStream.write(buffer, 0, count);
        }
        outputStream.close();
        byte[] output = outputStream.toByteArray();

        Logger.getLogger(CompressionUtils.class.getName()).log(Level.FINE, "Original: {0} Kb", data.length / 1024);
        Logger.getLogger(CompressionUtils.class.getName()).log(Level.FINE, "Compressed: {0} Kb", output.length / 1024);
        return output;
    }

    public static byte[] decompress(byte[] data) throws IOException, DataFormatException {
        Inflater inflater = new Inflater();
        inflater.setInput(data);

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);
        byte[] buffer = new byte[1024];
        while (!inflater.finished()) {
            int count = inflater.inflate(buffer);
            outputStream.write(buffer, 0, count);
        }
        outputStream.close();
        byte[] output = outputStream.toByteArray();

        Logger.getLogger(CompressionUtils.class.getName()).log(Level.FINE, "Original: {0}", data.length);
        Logger.getLogger(CompressionUtils.class.getName()).log(Level.FINE, "Compressed: {0}", output.length);
        return output;
    }
}

@@ -474,32 +474,33 @@
 s IOException %7B%0A
+%0A
         Deflater
@@ -539,42 +539,13 @@
     
-deflater.setInput(data);%0A%0A        
+try (
 Byte
@@ -615,19 +615,61 @@
 .length)
-;%0A%0A
+) %7B%0A            deflater.setInput(data);%0A    
         
@@ -683,24 +683,28 @@
 r.finish();%0A
+    
         byte
@@ -731,32 +731,36 @@
 %5B1024%5D;%0A        
+  
+  
 while (!deflater
@@ -766,32 +766,36 @@
 r.finished()) %7B%0A
+    
             int 
@@ -871,32 +871,36 @@
 dex%0A            
+    
 outputStream.wri
@@ -933,40 +933,18 @@
     
-%7D%0A
     
+%7D%0A
     
-outputStream.close();%0A
     
@@ -984,32 +984,36 @@
 toByteArray();%0A%0A
+    
         Logger.g
@@ -1112,32 +1112,36 @@
  1024);%0A        
+ 
+   
 Logger.getLogger
@@ -1236,32 +1236,36 @@
  1024);%0A        
+   
+ 
 return output;%0A 
@@ -1259,24 +1259,82 @@
 urn output;%0A
+        %7D finally %7B%0A            deflater.end();%0A        %7D%0A
     %7D%0A%0A    p
@@ -1463,24 +1463,25 @@
 ater();%0A
+%0A
         
 inflater
@@ -1476,42 +1476,13 @@
     
-inflater.setInput(data);%0A%0A        
+try (
 Byte
@@ -1544,34 +1544,77 @@
 eam(data.length)
-;%0A
+) %7B%0A            inflater.setInput(data);%0A    
         byte%5B%5D b
@@ -1629,32 +1629,36 @@
 new byte%5B1024%5D;%0A
+    
         while (!
@@ -1684,32 +1684,36 @@
 ) %7B%0A            
+    
 int count = infl
@@ -1734,16 +1734,20 @@
 uffer);%0A
+    
         
@@ -1800,40 +1800,18 @@
     
-%7D%0A
     
+%7D%0A
     
-outputStream.close();%0A
     
@@ -1859,32 +1859,36 @@
 ray();%0A%0A        
+ 
+   
 Logger.getLogger
@@ -1969,32 +1969,36 @@
 ength);%0A        
+   
+ 
 Logger.getLogger
@@ -2083,32 +2083,36 @@
 ength);%0A        
+    
 return output;%0A 
@@ -2106,20 +2106,78 @@
 urn output;%0A
+        %7D finally %7B%0A            inflater.end();%0A        %7D%0A
     %7D%0A%7D%0A

Fix CompressionUtils. Close Deflater and Inflater