byte[] output = new byte[input.length]; ByteBuffer buf = new ByteBuffer(); int compressedDataLength; int fullCompressedDataLength = 0; while (!deflater.finished()) { compressedDataLength = deflater.deflate(output); fullCompressedDataLength += compressedDataLength; buf.append(output,0,compressedDataLength);
buffer[0] = (byte) 0x78;  if (level <= 1) buffer[1] = (byte) 0x01; else if (level < 6) buffer[1] = (byte) 0x5e; else if (level == 6) buffer[1] = (byte) 0x9c; else buffer[1] = (byte) 0xda;  out.write(buffer, 0, 2, false);  while (! isFinished) { while (! isFinished && deflater.needsInput()) { int len = data.read(buffer, 0, buffer.length);  if (len > 0) { crc.update(buffer, 0, len); deflater.setInput(buffer, 0, len); } else { isFinished = true; deflater.finish(); } }  int len;  while ((len = deflater.deflate(buffer, 0, buffer.length)) > 0) { out.write(buffer, 0, len, false); }
  public Value gzcompress(String data,                          @Optional("-1") int level)    throws DataFormatException, IOException  {    if (level == -1)      level = Deflater.DEFAULT_COMPRESSION;    Deflater deflater = new Deflater(level);    byte[] input = data.getBytes();    deflater.setInput(input);    deflater.finish();    byte[] output = new byte[input.length];    ByteBuffer buf = new ByteBuffer();    int compressedDataLength;    int fullCompressedDataLength = 0;    while (!deflater.finished()) {      compressedDataLength = deflater.deflate(output);      fullCompressedDataLength += compressedDataLength;      buf.append(output,0,compressedDataLength);    }    ByteArrayInputStream result = new ByteArrayInputStream(buf.getBuffer(),0,fullCompressedDataLength);    ReadStream readStream = new ReadStream(new VfsStream(result,null));    return new TempBufferStringValue(TempBuffer.copyFromStream(readStream));  }