public static byte[] toByteArray(InputStream stream) throws IOException {
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        int nRead;
        byte[] data = new byte[16384];

        while ((nRead = stream.read(data, 0, data.length)) != -1) {
            buffer.write(data, 0, nRead);
        }
        return buffer.toByteArray();

    }
@Test
    public void toByteArray_big() throws Exception {
        int size = 100000; //bigger that internal buffer size
        byte[] input = new byte[size];
        Arrays.fill(input, (byte) 1);
        byte[] output = IOUtils.toByteArray(new ByteArrayInputStream(input));
        assertThat(output).isEqualTo(input);
    }