public static ByteBuffer drainInputStreamToBuffer(InputStream is) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream(4096);
    IOUtils.copyBytes(is, baos, 4096, true);
    ByteBuffer buffer = ByteBuffer.wrap(baos.toByteArray());
    buffer.rewind();
    return buffer;
  }
Copy from the InputStream to a new heap ByteBuffer until the InputStream is exhausted.
hbase-common/src/main/java/org/apache/hadoop/hbase/util/ByteBufferUtils.java