int length = in.remaining();
deflater.setInput(in.array(), in.arrayOffset() + in.position(), length);
deflater.finish();
int outSize = 0;
int offset = out.arrayOffset() + out.position();
while (!deflater.finished() && (length > outSize)) {
int size = deflater.deflate(out.array(), offset, out.remaining());
out.position(size + out.position());
outSize += size;
offset += size;
// if we run out of space in the out buffer, use the overflow