⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 directoryreader.java

📁 dump3 morpheus 0.2.9 src
💻 JAVA
字号:
/**
 * DuMP3 version morpheus_0.2.9 - a duplicate/similar file finder in Java<BR>
 * Copyright 2005 Alexander Gr&auml;sser<BR>
 * All Rights Reserved, http://dump3.sourceforge.net/<BR>
 * <BR>
 * This file is part of DuMP3.<BR>
 * <BR>
 * DuMP3 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 2 of the License, or (at your option) any later version.<BR>
 * <BR>
 * DuMP3 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.<BR>
 * <BR>
 * You should have received a copy of the GNU General Public License along with DuMP3; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
 * Fifth Floor, Boston, MA 02110-1301 USA
 */
package net.za.grasser.duplicate.file;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Observable;
import java.util.regex.Pattern;
import net.za.grasser.duplicate.Configure;
import net.za.grasser.duplicate.util.Constants;
import org.apache.log4j.Logger;

/**
 * @author <a href="http://sourceforge.net/sendmessage.php?touser=733840">pyropunk at sourceforge dot net</a>
 * @version $Revision: 1.12 $
 * @modelguid {72365C3C-CC96-4FB5-BE11-F10F3930021F}
 */
public class DirectoryReader extends Observable implements Runnable {
  /** @modelguid {4126BD5F-2D12-456B-888D-A6210E80C812} */
  static Logger log = Logger.getLogger(DirectoryReader.class);
  /**
   * <code>recurs</code> DirectoryReader - recursively process subdirectories
   */
  private boolean recurs = true;
  /**
   * <code>matchdirs</code> DirectoryReader - match directories to wildcardFilter (Unix style)
   */
  private boolean matchdirs = false;
  /**
   * <code>casesensitive</code> DirectoryReader - case sensitivley match files
   */
  private boolean casesensitive = false;
  /**
   * <code>regex</code> DirectoryReader - use regular expressions and not OS wildcards
   */
  private boolean regex = false;
  /**
   * <code>running</code> DirectoryReader - start or stop the thread.
   */
  private boolean running = false;
  /**
   * <code>wildcardFilter</code> DirectoryReader -
   */
  private WildcardFilter wildcardFilter = null;
  /**
   * <code>dir</code> DirectoryReader -
   */
  private DirectoryInfo dir = new DirectoryInfo(".");
  static {
    Configure.load();
  }

  /**
   * DirectoryReader contructor
   * 
   * @param pstrWildcard String
   * @param pDir DirectoryInfo
   * @throws FileNotFoundException
   */
  public DirectoryReader(final String pstrWildcard, final DirectoryInfo pDir) throws FileNotFoundException {
    super();
    // defaults from config file
    recurs = Configure.getBooleanProperty("recursive", recurs, DirectoryReader.class.getName());
    matchdirs = Configure.getBooleanProperty("match dirs", matchdirs, DirectoryReader.class.getName());
    casesensitive = Configure.getBooleanProperty("case sensitive", casesensitive, DirectoryReader.class.getName());
    regex = Configure.getBooleanProperty("regex wildcard", regex, DirectoryReader.class.getName());
    setDir(pDir);
    setWildcard(pstrWildcard, regex);
  }

  /**
   * start the thread.
   */
  public void start() {
    if (!running) {
      running = true;
      final Thread thread = new Thread(this, Constants.getClassName(this) + ':' + dir.getPath());
      thread.setDaemon(true);
      thread.setPriority(Thread.MIN_PRIORITY);
      thread.start();
    }
  }

  /**
   * Checks whether the directory is a directory and not a file. If it is a file it changes the wildcard to the filename and sets the directory to the real
   * directory.
   * 
   * @throws FileNotFoundException
   */
  private void checkPath() throws FileNotFoundException {
    final String strDir = dir.getPath();
    final File file = new File(strDir);
    if (!file.isDirectory()) {
      log.warn(file + " is not a dir");
      setDir(new DirectoryInfo(file.getParent()));
      setWildcard(file.getName(), regex);
    } else {
      log.info("Dir: " + dir.getPath() + ", Wildcard: " + wildcardFilter);
    }
  }

  /**
   * Creates a Wildcard filter from a string.
   * 
   * @param pstrWildcard
   * @param pRegex
   * @throws FileNotFoundException
   */
  public void setWildcard(final String pstrWildcard, final boolean pRegex) throws FileNotFoundException {
    regex = pRegex;
    String lWildcard = pstrWildcard;
    // check whether correct values were supplied
    if (lWildcard == null) {
      lWildcard = regex ? ".*" : "*";
    }
    log.debug("Wild: " + lWildcard);
    // create a filter
    if (regex) {
      wildcardFilter = new WildcardFilter(Pattern.compile(lWildcard));
    } else {
      wildcardFilter = new WildcardFilter(lWildcard);
    }
    wildcardFilter.setMatchDirs(matchdirs);
    wildcardFilter.setCaseSensitive(casesensitive);
    checkPath();
  }

  /**
   * This method will scan the filesystem for files (and subdirectories) that match the wildcard and add them to pDir.
   * 
   * @param pDir
   * @modelguid {ACDBBD89-3440-4250-9BA5-611E8C1A9B8D}
   */
  protected void read(final DirectoryInfo pDir) {
    final String pstrDir = pDir.getPath();
    final String[] list = new File(pstrDir).list(wildcardFilter);
    if (list == null) {
      return;
    }
    log.debug("Found " + list.length + " files/dirs");
    final ArrayList<FingerprintFile> files = new ArrayList<FingerprintFile>();
    final ArrayList<DirectoryInfo> dirs = new ArrayList<DirectoryInfo>();
    final StringBuffer path = new StringBuffer(12);
    for (int i = 0; i < list.length && running; i++) {
      path.setLength(0);
      path.append(pstrDir).append(File.separatorChar).append(list[i]);
      final File f = new File(path.toString());
      if (f.exists()) {
        if (f.isDirectory()) {
          final DirectoryInfo lDir = new DirectoryInfo(f.getName());
          lDir.setParent(pDir);
          dirs.add(lDir);
          if (recurs) {
            read(lDir);
          }
        } else {
          final FingerprintFile ff = new FingerprintFile(list[i], f.length(), f.lastModified(), pDir);
          files.add(ff);
          setChanged();
          super.notifyObservers(ff);
        }
      }
    }
    pDir.setFiles(files);
    pDir.setSubDirectories(dirs);
  }

  /**
   * @param pDir
   * @modelguid {ACDBBD89-3440-4250-9BA5-611E8C1A9B8D}
   */
  protected void filter(final DirectoryInfo pDir) {
    if (pDir.getSubDirectories() != null) {
      for (final DirectoryInfo lDir : pDir.getSubDirectories()) {
        if (recurs && wildcardFilter.accept(new File(pDir.getPath()), lDir.getName())) {
          filter(lDir);
        }
      }
    }
    if (pDir.getFiles() != null) {
      for (final FingerprintFile lFile : pDir.getFiles()) {
        if (wildcardFilter.accept(new File(pDir.getPath()), lFile.getName())) {
          setChanged();
          super.notifyObservers(lFile);
        }
      }
    }
  }

  /**
   * @see java.lang.Runnable#run()
   */
  public void run() {
    log.info("Directory reading started");
    if (dir.getFiles() != null || dir.getSubDirectories() != null) {
      filter(dir);
    } else {
      read(dir);
    }
    log.debug("notify of stoppage");
    setChanged();
    super.notifyObservers("stop");
    log.info("Directory reading stopped");
    setRunning(false);
  }

  /**
   * @return boolean - Returns the running.
   */
  public synchronized boolean isRunning() {
    return running;
  }

  /**
   * Sets the running to the value of pRunning.
   * 
   * @param pRunning boolean - The new value.
   */
  public synchronized void setRunning(final boolean pRunning) {
    running = pRunning;
  }

  /**
   * @return boolean - Returns the casesensitive.
   */
  public boolean isCasesensitive() {
    return casesensitive;
  }

  /**
   * Sets the casesensitive to the value of pCasesensitive.
   * 
   * @param pCasesensitive boolean - The new value.
   */
  public void setCasesensitive(final boolean pCasesensitive) {
    casesensitive = pCasesensitive;
    wildcardFilter.setCaseSensitive(casesensitive);
  }

  /**
   * @return boolean - Returns the matchdirs.
   */
  public boolean isMatchdirs() {
    return matchdirs;
  }

  /**
   * Sets the matchdirs to the value of pMatchdirs.
   * 
   * @param pMatchdirs boolean - The new value.
   */
  public void setMatchdirs(final boolean pMatchdirs) {
    matchdirs = pMatchdirs;
    wildcardFilter.setMatchDirs(matchdirs);
  }

  /**
   * @return boolean - Returns the recurs.
   */
  public boolean isRecurs() {
    return recurs;
  }

  /**
   * Sets the recurs to the value of pRecurs.
   * 
   * @param pRecurs boolean - The new value.
   */
  public void setRecurs(final boolean pRecurs) {
    recurs = pRecurs;
  }

  /**
   * @return DirectoryInfo - Returns the dir.
   */
  public DirectoryInfo getDir() {
    return dir;
  }

  /**
   * Sets the dir to the value of pDir.
   * 
   * @param pDir DirectoryInfo - The new value.
   * @throws FileNotFoundException
   */
  private void setDir(final DirectoryInfo pDir) throws FileNotFoundException {
    dir = pDir;
    if (!new File(dir.getPath()).exists()) {
      throw new FileNotFoundException(dir.getPath() + " does not exist!");
    }
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -