title: Android Deflator Out of Memory Error
body: <p>I want to compress byte array before saving to the file.
  When I use Deflator to compress byte array, I get <code>OutOfMemoryError</code>: </p>
  
  <pre><code>ERROR/dalvikvm-heap(16065): Out of memory on a 921616-byte allocation.
  </code></pre>
  
  <p><a href="http://developer.android.com/intl/ru/reference/java/util/zip/Deflater.html" rel="nofollow">I check the code and it is the same as android developer</a>. But I added <code>DeflatorOutputStream</code> to reduce memory usage.</p>
  
  <p>My code:</p>
  
  <pre><code>public static byte[] compress(byte[] data) throws IOException {
  
      Deflater deflater = new Deflater();
      deflater.setInput(data);
      deflater.finish();
  
      ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);
      DeflaterOutputStream dos=new DeflaterOutputStream(outputStream);
  
      byte[] buffer = new byte[1024];
      while (!deflater.finished()) {
          int count=deflater.deflate(buffer); 
          // returns the generated code... index
          dos.write(buffer, 0, count);
      }
  
      deflater.end();
      byte[] output = outputStream.toByteArray();
  
      dos.finish();
      dos.close();
      outputStream.close();
  
      return output;
  }
  </code></pre>
  
  <p>I checked that error occurs in this line: <code>int count=deflater.deflate(buffer);</code></p>