Java tutorial
//package com.java2s; import java.io.InputStream; public class Main { public static int readInputStream(InputStream input, byte[] buffer, int offset, int length) throws Exception { int n, o; if (buffer == null) throw new NullPointerException(); if (offset < 0 || length < 0 || offset + length > buffer.length) throw new IndexOutOfBoundsException(); if (length == 0) return 0; if ((n = input.read(buffer, offset++, 1)) < 0) return -1; if (--length > 0 && (o = input.available()) > 0) { n += input.read(buffer, offset, o > length ? length : o); } return n; } }