final int BUFSIZ = 1024; byte[] data = new byte[BUFSIZ]; int total = 0, c = 0, x = 0; while ((c = in.read(data, total, x)) != -1) { total += c; x -= c; if (x == 0) { byte[] tmp = new byte[total + BUFSIZ]; System.arraycopy(data, 0, tmp, 0, total); data = tmp; x = BUFSIZ; }
ByteArrayOutputStream bout = new ByteArrayOutputStream(); int read = -1; byte[] buf = new byte[1024 * 50]; while ((read = in.read(buf)) != -1) { bout.write(buf, 0, read);
    public static byte[] bytesFromInputStream(InputStream in) throws IOException {        if (in == null) throw new IllegalArgumentException("null input stream");        final int BUFSIZ = 1024;        byte[] data = new byte[BUFSIZ];        int total = 0, c = 0, x = 0;        while ((c = in.read(data, total, x)) != -1) {            total += c;            x -= c;            if (x == 0) { // Need more buffer                byte[] tmp = new byte[total + BUFSIZ];                System.arraycopy(data, 0, tmp, 0, total);                data = tmp; x = BUFSIZ;            }        }        return data;    }