public static byte[] readBytes(InputStream stream, int chunkSize) throws IOException {
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();

        int nRead;
        byte[] data = new byte[chunkSize];
        while ((nRead = stream.read(data, 0, data.length)) != -1)
            buffer.write(data, 0, nRead);
        buffer.flush();
        return buffer.toByteArray();
    }
Reads all the content of the given input stream, in chunks of at max chunkSize
vespajlib/src/main/java/com/yahoo/io/IOUtils.java