com.qwerky.tools.foldersync.Analyser.java Source code

Java tutorial

Introduction

Here is the source code for com.qwerky.tools.foldersync.Analyser.java

Source

/**
 *  Copyright (C) 2012  Neil Taylor (https://github.com/qwerky/)
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.qwerky.tools.foldersync;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.commons.codec.digest.DigestUtils;

public class Analyser {

    private File inRoot;

    private File outRoot;

    private List<WorkUnit> results;

    /**
     * Constructor.
     * 
     * @param in
     * @param out
     */
    public Analyser(File in, File out) {
        this.inRoot = in;
        this.outRoot = out;
        this.results = new ArrayList<WorkUnit>();
    }

    public void analyse() {
        validateInput();
        validateOuput();
        analyseDir(inRoot);
    }

    private void analyseDir(File in) {
        for (File file : in.listFiles()) {
            if (file.isDirectory()) {
                analyseDir(file);
            } else {
                checkFile(file);
            }
        }
    }

    private void checkFile(File file) {
        File out = buildTarget(file);
        if (requireSync(file, out)) {
            results.add(new WorkUnit(file, out));
        }
    }

    /**
     * Builds the required File in the output dir, given an input file.
     * 
     * @param file
     * @return
     */
    private File buildTarget(File file) {
        String in = file.getAbsolutePath();
        String relative = in.replace(inRoot.getAbsolutePath(), "");
        return new File(outRoot, relative);
    }

    private boolean requireSync(File in, File out) {
        if (out.exists()) {
            try {
                byte[] inmd5 = DigestUtils.md5(new FileInputStream(in));
                byte[] outmd5 = DigestUtils.md5(new FileInputStream(out));

                return (in.length() != out.length()) || (in.lastModified() > out.lastModified())
                        || !(Arrays.equals(inmd5, outmd5));
            } catch (IOException ioe) {
                return true;
            }
        }
        return true;
    }

    public List<WorkUnit> getResults() {
        return results;
    }

    private void validateInput() {
        if (!inRoot.exists())
            throw new IllegalArgumentException("In folder " + inRoot.getAbsolutePath() + " not found.");
        if (!inRoot.isDirectory())
            throw new IllegalArgumentException(
                    "In folder " + inRoot.getAbsolutePath() + " is a file, not a directory.");
    }

    private void validateOuput() {
        if (!outRoot.exists()) {
            outRoot.mkdirs();
        }
        if (outRoot.isFile())
            throw new IllegalArgumentException(
                    "Out folder " + outRoot.getAbsolutePath() + " is a file, not a directory.");
    }
}