public static void transfer(InputStream in, OutputStream out, long len)
            throws IOException {
        byte[] buf = new byte[4096];
        while (len != 0) {
            int count = len < 0 || buf.length < len ? buf.length : (int)len;
            count = in.read(buf, 0, count);
            if (count == -1) {
                if (len > 0)
                    throw new IOException("unexpected end of stream");
                break;
            }
            out.write(buf, 0, count);
            len -= len > 0 ? count : 0;
        }
    }