<commit_before>package org.jvnet.hudson.plugins.backup.utils;

import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.zip.ZipEntry;

import org.apache.commons.io.DirectoryWalker;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.input.AutoCloseInputStream;
import org.jvnet.hudson.plugins.backup.BackupException;
import org.jvnet.hudson.plugins.backup.utils.compress.Archiver;
import org.jvnet.hudson.plugins.backup.utils.compress.ArchiverException;
import org.jvnet.hudson.plugins.backup.utils.compress.CompressionMethodEnum;

public class BackupEngine extends DirectoryWalker {
	private BackupLogger logger;
	private File source;
	private int nbFiles = 0;
	private int nbErrors = 0;

	/**
	 * the length of the source string, to speed up walk
	 */
	private int sourceLength;
	// private ZipOutputStream target;
	private Archiver archiver;

	public BackupEngine(BackupLogger logger, String sourceDirectory,
			String targetName, FileFilter filter) throws BackupException {
		super(filter, -1);
		this.logger = logger;
		this.source = new File(sourceDirectory);
		this.sourceLength = sourceDirectory.length();

		File targetFile = new File(targetName);
		logger.info("Full backup file name : " + targetFile.getAbsolutePath());
		
		// TODO parameterized this
		archiver = CompressionMethodEnum.ZIP.getArchiver();
		
		try {
			archiver.init(targetFile);
		} catch (ArchiverException e) {
			throw new BackupException(e);
		}
		
	}

	@Override
	protected void handleFile(File file, int depth, Collection results)
			throws IOException {

		if (!file.exists()) {
			logger.warn("inconsistent file " + file.getAbsolutePath());
			nbErrors++;
			super.handleFile(file, depth, results);
			return;
		}

		String name = getInArchiveName(file.getAbsolutePath());

		logger.debug(name + " file");

		try {
			archiver.addFile(name, file);
		} catch(ArchiverException e) {
			logger.error(e.getMessage());
			throw new IOException("Unable to add " + name, e);
		}

		nbFiles++;

		super.handleFile(file, depth, results);
	}

	@Override
	protected void handleEnd(Collection results) throws IOException {
		try {
			archiver.close();
		} catch(ArchiverException e) {
			logger.error(e.getMessage());
			throw new IOException(e.getMessage());
		}
		logger.info("Saved files : " + nbFiles);
		logger.info("Number of errors : " + nbErrors);<commit_msg>[backup-engine] made compliant with jdk1.5<commit_after>package org.jvnet.hudson.plugins.backup.utils;

import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.zip.ZipEntry;

import org.apache.commons.io.DirectoryWalker;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.input.AutoCloseInputStream;
import org.jvnet.hudson.plugins.backup.BackupException;
import org.jvnet.hudson.plugins.backup.utils.compress.Archiver;
import org.jvnet.hudson.plugins.backup.utils.compress.ArchiverException;
import org.jvnet.hudson.plugins.backup.utils.compress.CompressionMethodEnum;

public class BackupEngine extends DirectoryWalker {
	private BackupLogger logger;
	private File source;
	private int nbFiles = 0;
	private int nbErrors = 0;

	/**
	 * the length of the source string, to speed up walk
	 */
	private int sourceLength;
	// private ZipOutputStream target;
	private Archiver archiver;

	public BackupEngine(BackupLogger logger, String sourceDirectory,
			String targetName, FileFilter filter) throws BackupException {
		super(filter, -1);
		this.logger = logger;
		this.source = new File(sourceDirectory);
		this.sourceLength = sourceDirectory.length();

		File targetFile = new File(targetName);
		logger.info("Full backup file name : " + targetFile.getAbsolutePath());
		
		// TODO parameterized this
		archiver = CompressionMethodEnum.ZIP.getArchiver();
		
		try {
			archiver.init(targetFile);
		} catch (ArchiverException e) {
			throw new BackupException(e);
		}
		
	}

	@Override
	protected void handleFile(File file, int depth, Collection results)
			throws IOException {

		if (!file.exists()) {
			logger.warn("inconsistent file " + file.getAbsolutePath());
			nbErrors++;
			super.handleFile(file, depth, results);
			return;
		}

		String name = getInArchiveName(file.getAbsolutePath());

		logger.debug(name + " file");

		try {
			archiver.addFile(name, file);
		} catch(ArchiverException e) {
			logger.error(e.getMessage());
			throw new IOException("Unable to add " + name);
		}

		nbFiles++;

		super.handleFile(file, depth, results);
	}

	@Override
	protected void handleEnd(Collection results) throws IOException {
		try {
			archiver.close();
		} catch(ArchiverException e) {
			logger.error(e.getMessage());
			throw new IOException(e.getMessage());
		}
		logger.info("Saved files : " + nbFiles);
		logger.info("Number of errors : " + nbErrors);
{'fraction_non_alphanumeric': 0.11434799841458582, 'fraction_numerical': 0.0015854141894569957, 'mean_word_length': 4.558370044052864, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '7151703', 'n_tokens_mistral': 1692, 'n_tokens_neox': 1527, 'n_words': 356}