abstractfingerprint.java

来自「dump3 morpheus 0.2.9 src」· Java 代码 · 共 200 行

JAVA
200
字号
/**
 * 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.fingerprint;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.text.DateFormat;
import java.util.Date;
import java.util.Map;
import net.za.grasser.duplicate.file.FingerprintFile;
import net.za.grasser.duplicate.file.Status;
import net.za.grasser.duplicate.util.Constants;
import net.za.grasser.duplicate.util.HexArray;
import org.apache.log4j.Logger;

/**
 * @author <a href="http://sourceforge.net/sendmessage.php?touser=733840">pyropunk at sourceforge dot net</a>
 * @version $Revision: 1.16 $
 * @modelguid {57599672-FC9B-486D-A969-9A645E7DEFB7}
 */
public abstract class AbstractFingerprint {
  /** @modelguid {55FFD1FB-DF09-4575-A86F-A2B682A46924} */
  private static final Logger log = Logger.getLogger(AbstractFingerprint.class);

  /**
   * compare two byte arrays.
   * 
   * @param one
   * @param two
   * @return boolean
   */
  public static boolean equals(final byte[] one, final byte[] two) {
    if (one.length != two.length) {
      return false;
    }
    for (int i = 0; i < two.length; i++) {
      if (one[i] != two[i]) {
        return false;
      }
    }
    return true;
  }

  /** @modelguid {A39E4D60-B2CB-48BE-86CD-7D1131937D40} */
  private FingerprintFile file = null;
  /** @modelguid {E082CCC0-956D-4C56-AD02-1F8F9FF22509} */
  protected byte[] fingerprint = null;

  /**
   * @param fi FingerprintFile
   * @modelguid {A31937E1-7614-4FDC-9E7D-F7F1A9C5A96F}
   */
  protected AbstractFingerprint(final FingerprintFile fi) {
    file = fi;
    fingerprint = null;
  }

  /**
   * This method adds additional info to the FingerprintFile
   */
  public void addInfo() {
    final Map<String, String> map = file.getInfo();
    map.put("file size", String.valueOf(file.getLength()));
    map.put("file date", DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM).format(new Date(file.getLastModified())));
    map.put("file name", file.getPath());
  }

  /**
   * @return String
   * @modelguid {E440866E-6C37-49CC-8B8A-E6645E19C3B1}
   */
  public String getClassName() {
    return Constants.getClassName(this);
  }

  /**
   * @return FingerprintFile - Returns the file.
   */
  public FingerprintFile getFile() {
    return file;
  }

  /**
   * calculate fingerprint using algorithm defined by the subclass.
   * 
   * @return byte[]
   * @modelguid {052A3023-EDEE-4FBC-8C53-B0B625FB5B08}
   */
  public byte[] getFingerprint() {
    if (fingerprint == null && (file.getStatus() == Status.FILE_OK || file.getStatus() == Status.FILE_SIGNATURE_MISMATCH)) {
      makeFingerprintFromFile();
    }
    return fingerprint;
  }

  /**
   * @return threshhold at witch fingerprints are considered the same
   */
  public abstract float getSimilarityThreshhold();

  /**
   * @modelguid {25267CE6-3715-43F5-9C06-C44C0F5CC416}
   */
  protected void makeFingerprintFromFile() {
    BufferedInputStream bis = null;
    final File f = new File(file.getPath());
    try {
      bis = new BufferedInputStream(new FileInputStream(f));
      long len = f.length();
      preRead();
      // check whether file is small enough to read in one operation (2MB)
      if (len <= Constants.BUFFER_SIZE) {
        final byte[] buffer = new byte[((int)len)];
        bis.read(buffer);
        update(buffer, (int)len);
      } else {
        final byte[] buffer = new byte[Constants.BUFFER_SIZE];
        while ((len = bis.read(buffer)) > -1) {
          update(buffer, (int)len);
        }
      }
      postRead();
    } catch (final Exception e) {
      file.setStatus(Status.FILE_NOT_READABLE);
      fingerprint = null;
      log.warn("oops", e);
    } finally {
      if (bis != null) {
        try {
          bis.close();
        } catch (final Exception e) {
          // ignore
        }
      }
    }
  }

  /**
   * @param fi AbstractFingerprint
   * @return boolean
   * @modelguid {B275D8C6-611D-424F-B031-DB737C07FD11}
   */
  public abstract float matches(AbstractFingerprint fi);

  /**
   * teardown phase after reading the file<BR>
   * fingerprint must be computed here
   */
  protected abstract void postRead();

  /**
   * setup phase before reading the file
   */
  protected abstract void preRead();

  /**
   * @see java.lang.Object#toString()
   * @modelguid {15F45C2A-3906-479F-99D1-6F7ADEBA5C81}
   */
  @Override
  public String toString() {
    if (getFingerprint() == null) {
      return "(null)";
    }
    return "(" + HexArray.makeString(getFingerprint()) + ")";
  }

  /**
   * update the checksum with a byte array.
   * 
   * @param pBuffer
   * @param pLength
   */
  protected abstract void update(byte[] pBuffer, int pLength);

  /**
   * Sets the fingerprint to the value of pFingerprint.
   * 
   * @param pFingerprint byte[] - The new value.
   */
  public void setFingerprint(final byte[] pFingerprint) {
    fingerprint = pFingerprint;
  }
}

⌨️ 快捷键说明

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