| 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public boolean compress(ByteBuffer in, ByteBuffer out, |
| 123 | ByteBuffer overflow, |
| 124 | Options options) { |
| 125 | ZlibOptions zlo = (ZlibOptions) options; |
| 126 | int length = in.remaining(); |
| 127 | int outSize = 0; |
| 128 | Deflater deflater = new Deflater(zlo.level, true); |
| 129 | try { |
| 130 | deflater.setStrategy(zlo.strategy); |
| 131 | deflater.setInput(in.array(), in.arrayOffset() + in.position(), length); |
| 132 | deflater.finish(); |
| 133 | int offset = out.arrayOffset() + out.position(); |
| 134 | while (!deflater.finished() && (length > outSize)) { |
| 135 | int size = deflater.deflate(out.array(), offset, out.remaining()); |
| 136 | out.position(size + out.position()); |
| 137 | outSize += size; |
| 138 | offset += size; |
| 139 | // if we run out of space in the out buffer, use the overflow |
| 140 | if (out.remaining() == 0) { |
| 141 | if (overflow == null) { |
| 142 | return false; |
| 143 | } |
| 144 | out = overflow; |
| 145 | offset = out.arrayOffset() + out.position(); |
| 146 | } |
| 147 | } |
| 148 | } finally { |
| 149 | deflater.end(); |
| 150 | } |
| 151 | return length > outSize; |
| 152 | } |
| 153 | |
| 154 | @Override |
| 155 | public void decompress(ByteBuffer in, ByteBuffer out) throws IOException { |