code: public static ByteBuffer minimalBufferFor(ByteBuffer buf)
      {
          return buf.capacity() > buf.remaining() || !buf.hasArray() ? ByteBuffer.wrap(getArray(buf)) : buf;
      }
docs: trims size of bytebuffer to exactly number of bytes in it, to do not hold too much memory