filedatabase.java
来自「dump3 morpheus 0.2.9 src」· Java 代码 · 共 183 行
JAVA
183 行
/**
* DuMP3 version morpheus_0.2.9 - a duplicate/similar file finder in Java<BR>
* Copyright 2005 Alexander Grä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.persist;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.List;
import org.apache.log4j.Logger;
import net.za.grasser.duplicate.Configure;
import net.za.grasser.duplicate.file.FingerprintFile;
import net.za.grasser.duplicate.fingerprint.AbstractFingerprint;
/**
* This class ...
*
* @author <a href="http://sourceforge.net/sendmessage.php?touser=733840">pyropunk at sourceforge dot net</a>
* @version $Revision: 1.8 $
*/
public class FileDatabase implements Database {
/**
* <code>log</code> FileDatabase -
*/
private static final Logger log = Logger.getLogger(FileDatabase.class);
/**
* <code>filename</code> SQLDatabase - filename of saved map
*/
static String filename = null;
/**
* <code>files</code> SQLDatabase - Map of fingerprint files (used when db is not available)
*/
static Map<String, DbElement> files = null;
static {
Configure.load();
// Load a database driver
filename = Configure.getProperty("save file", "./database.dat", SQLDatabase.class.getName());
}
/**
* Constructor
*/
FileDatabase() {
super();
}
/**
* @see net.za.grasser.duplicate.persist.Database#connect()
*/
@SuppressWarnings("unchecked")
public boolean connect() {
log.debug("connect");
final File f = new File(filename);
if (f.exists()) {
ObjectInputStream ois = null;
try {
ois = new ObjectInputStream(new BufferedInputStream(new FileInputStream(f)));
files = (HashMap<String, DbElement>)ois.readObject();
} catch (final ClassCastException t) {
t.printStackTrace();
} catch (final ClassNotFoundException cnfe) {
log.error("Class could not be found.", cnfe);
} catch (final FileNotFoundException fnfe) {
log.error("Database file could not be found.", fnfe);
} catch (final IOException ioe) {
log.error("Database file could not be read.", ioe);
} finally {
try {
if (ois != null) {
ois.close();
}
} catch (final IOException e) {
// ignore we are closing.
}
}
}
return true;
}
/**
* @see net.za.grasser.duplicate.persist.Database#close()
*/
public void close() {
log.debug("close");
final File f = new File(filename);
f.delete();
if (!f.exists() && files != null) {
ObjectOutputStream oos = null;
try {
oos = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(f)));
oos.writeObject(files);
} catch (final IOException ioe) {
log.error("Database file could not be written.", ioe);
} finally {
try {
if (oos != null) {
oos.flush();
oos.close();
}
} catch (final IOException e) {
// ignore we are closing.
}
}
}
}
/**
* @see net.za.grasser.duplicate.persist.Database#put(net.za.grasser.duplicate.file.FingerprintFile)
*/
public synchronized void put(final FingerprintFile pBf) {
final Map<String, List<Byte>> prints = new HashMap<String, List<Byte>>();
final Iterator<Map.Entry<String, AbstractFingerprint>> it = pBf.getFingerprints().entrySet().iterator();
log.debug("put key=[" + pBf.getKey() + "]");
while (it.hasNext()) {
final Map.Entry<String, AbstractFingerprint> print = it.next();
final byte[] afb = print.getValue().getFingerprint();
if (afb != null) {
final List<Byte> a = new ArrayList<Byte>();
for (final byte element : afb) {
a.add(new Byte(element));
}
prints.put(print.getKey(), a);
}
}
if (files == null) {
files = new HashMap<String, DbElement>();
}
final DbElement e = new DbElement();
e.prints = prints;
e.info = new HashMap<String, String>();
e.info.putAll(pBf.getInfo());
e.status = pBf.getStatus();
files.put(pBf.getKey(), e);
log.debug("put return");
log.debug("Memory: " + Runtime.getRuntime().freeMemory() + "/" + Runtime.getRuntime().maxMemory() + "=" + Runtime.getRuntime().freeMemory()
/ Runtime.getRuntime().maxMemory() * 100 + "% used");
}
/**
* @see net.za.grasser.duplicate.persist.Database#get(net.za.grasser.duplicate.file.FingerprintFile)
*/
public synchronized boolean get(final FingerprintFile pBf) {
log.debug("get key=[" + pBf.getKey() + "]");
if (files != null && files.containsKey(pBf.getKey())) {
final DbElement el = files.get(pBf.getKey());
pBf.setStatus(el.status);
pBf.getInfo().putAll(el.info);
final Iterator<Map.Entry<String, AbstractFingerprint>> it = pBf.getFingerprints().entrySet().iterator();
while (it.hasNext()) {
byte[] b = null;
final Map.Entry<String, AbstractFingerprint> print = it.next();
final List<Byte> al = el.prints.get(print.getKey());
if (al != null) {
b = new byte[al.size()];
for (int i = 0; i < b.length; i++) {
b[i] = al.get(i).byteValue();
}
}
print.getValue().setFingerprint(b);
}
log.debug("get return [found]");
return true;
}
log.debug("get return [not found]");
return false;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?