Added README file
[metux-java.git] / util / FileStock.java
blob5f42a9166bc48c9092f06b3f108110c6529f517b
2 package org.de.metux.util;
4 import java.util.Hashtable;
5 import java.util.Enumeration;
6 import java.io.IOException;
7 import java.io.File;
8 import java.io.FileNotFoundException;
9 import java.io.BufferedReader;
10 import java.io.FileReader;
12 public class FileStock
14 class record
16 String filename;
17 String md5;
20 Hashtable ht;
22 public FileStock()
24 ht = new Hashtable();
27 public String getmd5(String fn)
29 return getmd5(new File(fn));
32 public String getmd5(File f)
34 return FileOps.md5(f);
37 private void add(record r_new)
39 record r_old;
41 if ((r_old = (record)ht.get(r_new.md5))==null)
43 ht.put(r_new.md5,r_new);
44 return;
47 if (r_new.md5.equals(r_old.md5))
48 return;
50 throw new RuntimeException("COLLISSION "+r_old.filename+" vs. "+r_new.filename);
53 public void add(String [] fns)
54 throws FileNotFoundException, IOException
56 if (fns==null)
57 return;
59 for (int x=0; x<fns.length; x++)
60 add(fns[x]);
63 public void add(String fn)
64 throws FileNotFoundException, IOException
66 record r = new record();
67 r.filename = fn;
68 r.md5 = getmd5(fn);
69 add(r);
72 public String[] getFilenames()
74 int count = ht.size();
75 String fns[] = new String[count];
77 int x = 0;
78 for (Enumeration e=ht.keys(); e.hasMoreElements(); )
80 record r = (record)ht.get((String)e.nextElement());
81 fns[x++] = r.filename;
83 System.runFinalization();
84 return fns;