title: How to improve GZIP performance
body: <p>Currently I do have the problem that this piece of code will be called >500k of times. The size of the compressed <code>byte[]</code> is less than 1KB. Every time the method is called all of the streams has to been created. So I am looking for a way to improve this code.</p>
  
  <pre><code>private byte[] unzip(byte[] data) throws IOException, DataFormatException {
  
      byte[] unzipData = new byte[4096];
  
      try (ByteArrayInputStream in = new ByteArrayInputStream(data);
           GZIPInputStream gzipIn = new GZIPInputStream(in);
           ByteArrayOutputStream out = new ByteArrayOutputStream()) {
  
          int read = 0;
          while( (read = gzipIn.read(unzipData)) != -1) {
              out.write(unzipData, 0, read);
          }
  
          return out.toByteArray();
      }
  }
  </code></pre>
  
  <p>I already <a href="http://java-performance.com/" rel="noreferrer">tried</a> to replace <code>ByteArrayOutputStream</code> with a <code>ByteBuffer</code>, but at the time of creation I don't know how many bytes I need to allocate.</p>
  
  <p>Also, I tried to use <code>Inflater</code> but I stumbled across the problem descriped <a href="https://stackoverflow.com/questions/11076060/decompressing-gzipped-data-with-inflater-in-java">here</a>.</p>
  
  <p>Any other idea what I could do to improve the perfomance of this code.</p>
  
  <p><strong>UPDATE#1</strong></p>
  
  <ul>
  <li>Maybe <a href="https://github.com/jasonk000/fastzlib" rel="noreferrer">this</a> lib helps someone.</li>
  <li>Also there is an open <a href="https://bugs.openjdk.java.net/browse/JDK-8142920" rel="noreferrer">JDK-Bug</a>.</li>
  </ul>