static void copyResourceIntoFile()

in freemarker-docgen-core/src/main/java/org/freemarker/docgen/core/FileUtil.java [61:101]


    static void copyResourceIntoFile(
            Class<?> srcBaseClass, String srcBaseDir,
            String srcRelativePath, File destDir)
            throws IOException {
        File dstFile = new File(
                destDir,
                srcRelativePath.replace('/', File.separatorChar));

        File curDestDir = dstFile.getParentFile();
        if (!curDestDir.isDirectory() && !curDestDir.mkdirs()) {
            throw new IOException("Failed to create destination directory: "
                    + curDestDir.getAbsolutePath());
        }

        byte[] buffer = new byte[COPY_BUFFER_SIZE];
        String finalResourcePath;
        if (srcBaseDir == null || srcBaseDir.length() == 0) {
            finalResourcePath = srcRelativePath;
        } else {
            finalResourcePath = srcBaseDir + "/" + srcRelativePath;
        }
        InputStream in = srcBaseClass.getResourceAsStream(finalResourcePath);
        if (in == null) {
            throw new IOException("Failed to open class-loader resource: "
                    + finalResourcePath + " relatively to "
                    + Transform.class.getPackage().getName());
        }
        try {
            OutputStream out = new FileOutputStream(dstFile);
            try {
                int ln;
                while ((ln = in.read(buffer)) != -1) {
                    out.write(buffer, 0, ln);
                }
            } finally {
                out.close();
            }
        } finally {
            in.close();
        }
    }