<commit_before>/**
 * 
 */
package atg.test.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.zip.Adler32;
import java.util.zip.CheckedInputStream;

import org.apache.log4j.Logger;

/**
 * @author robert
 * 
 */
public class FileUtil {

  private static Logger log = Logger.getLogger(FileUtil.class);

  private static final File TMP_FILE = new File(System
      .getProperty("java.io.tmpdir")
      + File.separator + "atg-dust-rh.tmp");

  /**
   * 
   * @param srcDir
   * @param dstDir
   * @param excludes
   * @param isSmartCopyWithGlobalScopeForce
   * @throws IOException
   */
  public static void copyDirectory(String srcDir, String dstDir,
      final List<String> excludes) throws IOException {
    new File(dstDir).mkdirs();
    for (final String file : new File(srcDir).list()) {
      final String source = srcDir + File.separator + file, destination = dstDir
          + File.separator + file;
      boolean dir = new File(source).isDirectory();
      if (dir && !excludes.contains(file)) {
        copyDirectory(source, destination, excludes);
      }
      else {
        if (!excludes.contains(file)) {
          copyFile(source, destination);
        }
      }
    }
  }

  /**
   * 
   * @param src
   * @param dst
   * @throws IOException
   */
  public static void copyFile(final String src, final String dst)
      throws IOException {
    final FileChannel srcChannel = new FileInputStream(src).getChannel();
    final FileChannel dstChannel = new FileOutputStream(dst).getChannel();
    dstChannel.transferFrom(srcChannel, 0, srcChannel.size());
    dstChannel.close();
    srcChannel.close();
    log.debug(String.format("Copied %s to %s", src, dst));
  }

  /**
   * 
   * @param componentName
   *          The name of the nucleus component
   * @param configurationStagingLocation
   *          A valid not <code>null</code> directory.
   * @param clazz
   *          The class implementing the nucleus component
   * @param settings
   *          An implementation of {@link Map} containing all needed properties
   *          the component is depended on (eg key = username, value = test).
   *          Can be <code>null</code> or empty.
   * @throws IOException
   */
  public static void createPropertyFile(final String componentName,
      final File configurationStagingLocation, final Class<?> clazz,
      final Map<String, String> settings) throws IOException {

    configurationStagingLocation.mkdirs();
    final File propertyFile = new File(configurationStagingLocation,
        componentName.replace("/", File.separator) + ".properties");
    new File(propertyFile.getParent()).mkdirs();
    propertyFile.createNewFile();
    final BufferedWriter out = new BufferedWriter(new FileWriter(propertyFile));

    try {<commit_msg>Made copying hopefully a bit smarter and faster<commit_after>/**
 * 
 */
package atg.test.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.zip.Adler32;
import java.util.zip.CheckedInputStream;

import org.apache.log4j.Logger;

/**
 * @author robert
 * 
 */
public class FileUtil {

  private static Logger log = Logger.getLogger(FileUtil.class);

  private static final File TMP_FILE = new File(System
      .getProperty("java.io.tmpdir")
      + File.separator + "atg-dust-rh.tmp");

  /**
   * 
   * @param srcDir
   * @param dstDir
   * @param excludes
   * @param isSmartCopyWithGlobalScopeForce
   * @throws IOException
   */
  public static void copyDirectory(String srcDir, String dstDir,
      final List<String> excludes) throws IOException {
    new File(dstDir).mkdirs();
    for (final String file : new File(srcDir).list()) {
      final String source = srcDir + File.separator + file, destination = dstDir
          + File.separator + file;
      boolean dir = new File(source).isDirectory();
      if (dir && !excludes.contains(file)) {
        copyDirectory(source, destination, excludes);
      }
      else {
        if (!excludes.contains(file)) {
          copyFile(source, destination);
        }
      }
    }
  }

  /**
   * 
   * @param src
   * @param dst
   * @throws IOException
   */
  public static void copyFile(final String src, final String dst)
      throws IOException {
    final File srcFile = new File(src);
    final File dstFile = new File(dst);

    if (srcFile.lastModified() >= dstFile.lastModified() - 2500) {
      if (log.isDebugEnabled()) {
        log.debug("Same files no copy (2500m/s threshold)");
      }
    }
    else {
      if (log.isDebugEnabled()) {
        log.debug(String.format("Src file %s ts %s : ", src, srcFile
            .lastModified()));
        log.debug(String.format("Dst file %s ts %s : ", dst, dstFile
            .lastModified()));
      }
      final FileChannel srcChannel = new FileInputStream(src).getChannel();
      final FileChannel dstChannel = new FileOutputStream(dst).getChannel();
      dstChannel.transferFrom(srcChannel, 0, srcChannel.size());
      dstChannel.close();
      srcChannel.close();
      if (log.isDebugEnabled()) {
        log.debug(String.format("Copied %s to %s", src, dst));
      }
    }
  }

  /**
   * 
   * @param componentName
   *          The name of the nucleus component
   * @param configurationStagingLocation
   *          A valid not <code>null</code> directory.
   * @param clazz
   *          The class implementing the nucleus component
   * @param settings
   *          An implementation of {@link Map} containing all needed properties
   *          the component is depended on (eg key = username, value = test).
   *          Can be <code>null</code> or empty.
   * @throws IOException
   */
  public static void createPropertyFile(final String componentName,
      final File configurationStagingLocation, final Class<?> clazz,
      final Map<String, String> settings) throws IOException {

    configurationStagingLocation.mkdirs();
    final File propertyFile = new File(configurationStagingLocation,
        componentName.replace("/", File.separator) + ".properties");
    new File(propertyFile.getParent()).mkdirs();
    propertyFile.createNewFile();
    final BufferedWriter out = new BufferedWriter(new FileWriter(propertyFile));

    try {
{'fraction_non_alphanumeric': 0.11241515837104073, 'fraction_numerical': 0.0022624434389140274, 'mean_word_length': 3.1026682134570764, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7308505', 'n_tokens_mistral': 2054, 'n_tokens_neox': 1987, 'n_words': 538}