private byte[] readStream(InputStream fin) throws IOException {
        byte[] buf = new byte[4096];
        int size = 0;
        int len = 0;
        do {
            size += len;
            if (buf.length - size <= 0) {
                byte[] newbuf = new byte[buf.length * 2];
                System.arraycopy(buf, 0, newbuf, 0, size);
                buf = newbuf;
            }

            len = fin.read(buf, size, buf.length - size);
        } while (len >= 0);

        byte[] result = new byte[size];
        System.arraycopy(buf, 0, result, 0, size);
        return result;
    }