001package org.nuxeo.runtime.tomcat.dev;
002
003import java.io.File;
004import java.io.FileInputStream;
005import java.io.FileOutputStream;
006import java.io.IOException;
007import java.io.InputStream;
008import java.io.OutputStream;
009import java.util.List;
010
011public class IOUtils {
012
013    public static void deleteTree(File dir) {
014        for (File file : dir.listFiles()) {
015            if (file.isDirectory()) {
016                deleteTree(file);
017            } else {
018                file.delete();
019            }
020        }
021        dir.delete();
022    }
023
024    public static void copyTree(File source, File target) throws IOException {
025        if (source.isDirectory()) {
026            if (!target.exists()) {
027                target.mkdir();
028            }
029            for (File child : source.listFiles()) {
030                copyTree(child, new File(target, child.getName()));
031            }
032        } else {
033            copyContent(new FileInputStream(source), new FileOutputStream(target));
034        }
035    }
036
037    public static void copyContent(InputStream is, OutputStream out) throws IOException {
038        int data;
039        while (is.available() > 0 && (data = is.read()) != -1) {
040            out.write(data);
041        }
042    }
043
044    public static void appendResourceBundleFragments(String name, List<File> files, File target) throws IOException {
045        File l10n = new File(target, name);
046        File backup = new File(target, name + "~bak");
047        if (!backup.exists()) {
048            backup.createNewFile();
049            IOUtils.copyContent(new FileInputStream(l10n), new FileOutputStream(backup));
050        }
051        IOUtils.copyContent(new FileInputStream(backup), new FileOutputStream(l10n));
052        for (File file : files) {
053            InputStream in = new FileInputStream(file);
054            OutputStream out = new FileOutputStream(l10n, true);
055            IOUtils.copyContent(in, out);
056        }
057    }
058
059}