title: Java byte array compression
body: <p>I'm trying to use the java <code>DeflaterOutputStream</code> and <code>InflaterOutputStream</code> classes to compress a byte array, but both appear to not be working correctly. I assume I'm incorrectly implementing them.</p>
  
  <pre><code>public static byte[] compress(byte[] in) {
      try {
          ByteArrayOutputStream out = new ByteArrayOutputStream();
          DeflaterOutputStream defl = new DeflaterOutputStream(out);
          defl.write(in);
          defl.flush();
          defl.close();
  
          return out.toByteArray();
      } catch (Exception e) {
          e.printStackTrace();
          System.exit(150);
          return null;
      }
  }
  
  public static byte[] decompress(byte[] in) {
      try {
          ByteArrayOutputStream out = new ByteArrayOutputStream();
          InflaterOutputStream infl = new InflaterOutputStream(out);
          infl.write(in);
          infl.flush();
          infl.close();
  
          return out.toByteArray();
      } catch (Exception e) {
          e.printStackTrace();
          System.exit(150);
          return null;
      }
  }
  </code></pre>
  
  <p>Here's the two methods I'm using to compress and decompress the byte array. Most implementations I've seen online use a fixed size buffer array for the decompression portion, but I'd prefer to avoid that if possible, because I'd need to make that buffer array have a size of one if I want to have any significant compression.</p>
  
  <p>If anyone can explain to me what I'm doing wrong it would be appreciated. Also, to explain why I know these methods aren't working correctly: The "compressed" byte array that it outputs is always larger than the uncompressed one, no matter what size byte array I attempt to provide it.</p>