Here you can find the source of slurp(final InputStream is, final int bufferSize)
public static byte[] slurp(final InputStream is, final int bufferSize) throws IOException
//package com.java2s; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.io.UnsupportedEncodingException; public class Main { public static String Slurp(final InputStream is, final int bufferSize) { final char[] buffer = new char[bufferSize]; final StringBuilder out = new StringBuilder(); try {// w ww . ja v a 2s .c o m final Reader in = new InputStreamReader(is, "UTF-8"); try { for (;;) { int rsz = in.read(buffer, 0, buffer.length); if (rsz < 0) break; out.append(buffer, 0, rsz); } } finally { in.close(); } } catch (UnsupportedEncodingException ex) { /* ... */ } catch (IOException ex) { /* ... */ } return out.toString(); } public static byte[] slurp(final InputStream is, final int bufferSize) throws IOException { // this dynamically extends to take the bytes you read ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream(); // this is storage overwritten on each iteration with bytes byte[] buffer = new byte[bufferSize]; // we need to know how may bytes were read to write them to the // byteBuffer int len = 0; while ((len = is.read(buffer)) != -1) { byteBuffer.write(buffer, 0, len); } // and then we can return your byte array. return byteBuffer.toByteArray(); } }