public static void copyDirectory(File sourceLocation, File targetLocation, int maxRecurseLevel, FilenameFilter filter) throws IOException {
        if ( ! sourceLocation.exists()) throw new IllegalArgumentException(sourceLocation.getAbsolutePath() + " does not exist");

        if ( ! sourceLocation.isDirectory()) { // copy file
            InputStream in=null;
            OutputStream out=null;
            try {
                in = new FileInputStream(sourceLocation);
                out = new FileOutputStream(targetLocation);
                // Copy the bits from instream to outstream
                byte[] buf = new byte[1024];
                int len;
                while ((len = in.read(buf)) > 0) {
                    out.write(buf, 0, len);
                }
            }
            finally {
                closeInputStream(in);
                closeOutputStream(out);
            }
        }
        else if (maxRecurseLevel!=0) { // copy directory if allowed
            if (!targetLocation.exists())
                targetLocation.mkdirs();

            String[] children = sourceLocation.list(filter);
            for (int i=0; i<children.length; i++)
                copyDirectory(new File(sourceLocation, children[i]),
                              new File(targetLocation, children[i]),
                              maxRecurseLevel-1);
        }
    }
Copies all files and subdirectories in a directory to another.
Any existing files are overwritten.

@param sourceLocation the source directory
@param targetLocation the target directory
@param maxRecurseLevel if this is 1, only files immediately in sourceLocation are copied,
       if it is 2, then files contained in immediate subdirectories are copied, etc.
       If it is 0, sourceLocation will only be copied if it is a file, not a directory.
       If it is negative, recursion is infinite.
@param filter Only copy files passing through filter.
@throws IOException if copying any file fails. This will typically result in some files being copied and
        others not, i.e this method is not exception safe
vespajlib/src/main/java/com/yahoo/io/IOUtils.java