Mercurial > dive4elements > river
annotate flys-backend/src/main/java/de/intevation/flys/utils/FileTools.java @ 1212:dd948530fd57
Schema: Added missing columns in HYK tables.
flys-backend/trunk@2338 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Fri, 15 Jul 2011 14:33:12 +0000 |
parents | 7121a40671ff |
children | cc88db4a5b34 |
rev | line source |
---|---|
177
31895d24387e
Importer: Added info gew parser.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
1 package de.intevation.flys.utils; |
31895d24387e
Importer: Added info gew parser.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
2 |
31895d24387e
Importer: Added info gew parser.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
3 import java.io.File; |
1206
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
4 import java.io.IOException; |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
5 import java.io.InputStream; |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
6 import java.io.FileInputStream; |
177
31895d24387e
Importer: Added info gew parser.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
7 |
31895d24387e
Importer: Added info gew parser.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
8 import java.util.Stack; |
1206
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
9 import java.util.List; |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
10 import java.util.Set; |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
11 import java.util.HashSet; |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
12 import java.util.ArrayList; |
177
31895d24387e
Importer: Added info gew parser.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
13 |
31895d24387e
Importer: Added info gew parser.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
14 import org.apache.log4j.Logger; |
31895d24387e
Importer: Added info gew parser.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
15 |
1206
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
16 import java.security.MessageDigest; |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
17 import java.security.NoSuchAlgorithmException; |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
18 |
177
31895d24387e
Importer: Added info gew parser.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
19 public class FileTools |
31895d24387e
Importer: Added info gew parser.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
20 { |
31895d24387e
Importer: Added info gew parser.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
21 private static Logger log = Logger.getLogger(FileTools.class); |
31895d24387e
Importer: Added info gew parser.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
22 |
1206
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
23 public static final String DIGEST = |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
24 System.getProperty("flys.backend.file.cmp.digest", "MD5"); |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
25 |
177
31895d24387e
Importer: Added info gew parser.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
26 private FileTools() { |
31895d24387e
Importer: Added info gew parser.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
27 } |
31895d24387e
Importer: Added info gew parser.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
28 |
31895d24387e
Importer: Added info gew parser.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
29 public static File repair(File file) { |
31895d24387e
Importer: Added info gew parser.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
30 file = file.getAbsoluteFile(); |
31895d24387e
Importer: Added info gew parser.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
31 if (file.exists()) { |
31895d24387e
Importer: Added info gew parser.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
32 return file; |
31895d24387e
Importer: Added info gew parser.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
33 } |
31895d24387e
Importer: Added info gew parser.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
34 Stack<String> parts = new Stack<String>(); |
31895d24387e
Importer: Added info gew parser.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
35 File curr = file; |
31895d24387e
Importer: Added info gew parser.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
36 while (curr != null) { |
31895d24387e
Importer: Added info gew parser.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
37 String name = curr.getName(); |
31895d24387e
Importer: Added info gew parser.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
38 if (name.length() > 0) { |
31895d24387e
Importer: Added info gew parser.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
39 parts.push(curr.getName()); |
31895d24387e
Importer: Added info gew parser.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
40 } |
31895d24387e
Importer: Added info gew parser.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
41 curr = curr.getParentFile(); |
31895d24387e
Importer: Added info gew parser.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
42 } |
31895d24387e
Importer: Added info gew parser.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
43 |
31895d24387e
Importer: Added info gew parser.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
44 curr = null; |
31895d24387e
Importer: Added info gew parser.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
45 OUTER: while (!parts.isEmpty()) { |
31895d24387e
Importer: Added info gew parser.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
46 String f = parts.pop(); |
31895d24387e
Importer: Added info gew parser.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
47 log.debug("fixing: '" + f + "'"); |
31895d24387e
Importer: Added info gew parser.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
48 if (curr == null) { |
31895d24387e
Importer: Added info gew parser.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
49 // XXX: Not totaly correct because there |
31895d24387e
Importer: Added info gew parser.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
50 // more than one root on none unix systems. |
31895d24387e
Importer: Added info gew parser.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
51 for (File root: File.listRoots()) { |
31895d24387e
Importer: Added info gew parser.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
52 File [] files = root.listFiles(); |
31895d24387e
Importer: Added info gew parser.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
53 if (files == null) { |
31895d24387e
Importer: Added info gew parser.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
54 log.warn("cannot list '" + root); |
31895d24387e
Importer: Added info gew parser.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
55 continue; |
31895d24387e
Importer: Added info gew parser.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
56 } |
31895d24387e
Importer: Added info gew parser.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
57 for (File candidate: files) { |
31895d24387e
Importer: Added info gew parser.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
58 if (candidate.getName().equalsIgnoreCase(f)) { |
31895d24387e
Importer: Added info gew parser.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
59 curr = new File(root, candidate.getName()); |
31895d24387e
Importer: Added info gew parser.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
60 continue OUTER; |
31895d24387e
Importer: Added info gew parser.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
61 } |
31895d24387e
Importer: Added info gew parser.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
62 } |
31895d24387e
Importer: Added info gew parser.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
63 } |
31895d24387e
Importer: Added info gew parser.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
64 break; |
31895d24387e
Importer: Added info gew parser.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
65 } |
31895d24387e
Importer: Added info gew parser.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
66 else { |
31895d24387e
Importer: Added info gew parser.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
67 File [] files = curr.listFiles(); |
31895d24387e
Importer: Added info gew parser.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
68 if (files == null) { |
31895d24387e
Importer: Added info gew parser.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
69 log.warn("cannot list: '" + curr + "'"); |
31895d24387e
Importer: Added info gew parser.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
70 return file; |
31895d24387e
Importer: Added info gew parser.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
71 } |
31895d24387e
Importer: Added info gew parser.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
72 for (File candidate: files) { |
31895d24387e
Importer: Added info gew parser.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
73 if (candidate.getName().equalsIgnoreCase(f)) { |
31895d24387e
Importer: Added info gew parser.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
74 curr = new File(curr, candidate.getName()); |
31895d24387e
Importer: Added info gew parser.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
75 continue OUTER; |
31895d24387e
Importer: Added info gew parser.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
76 } |
31895d24387e
Importer: Added info gew parser.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
77 } |
31895d24387e
Importer: Added info gew parser.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
78 curr = null; |
31895d24387e
Importer: Added info gew parser.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
79 break; |
31895d24387e
Importer: Added info gew parser.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
80 } |
31895d24387e
Importer: Added info gew parser.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
81 } |
31895d24387e
Importer: Added info gew parser.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
82 |
31895d24387e
Importer: Added info gew parser.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
83 if (curr == null) { |
31895d24387e
Importer: Added info gew parser.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
84 log.warn("cannot repair path '" + file + "'"); |
31895d24387e
Importer: Added info gew parser.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
85 return file; |
31895d24387e
Importer: Added info gew parser.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
86 } |
31895d24387e
Importer: Added info gew parser.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
87 |
31895d24387e
Importer: Added info gew parser.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
88 return curr; |
31895d24387e
Importer: Added info gew parser.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
89 } |
1206
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
90 |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
91 public static class HashedFile |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
92 implements Comparable<HashedFile> |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
93 { |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
94 protected File file; |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
95 protected long length; |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
96 protected byte [] hash; |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
97 |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
98 public HashedFile(File file) { |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
99 this.file = file; |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
100 length = file.length(); |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
101 } |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
102 |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
103 public File getFile() { |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
104 return file; |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
105 } |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
106 |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
107 protected byte [] getHash() { |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
108 if (hash == null) { |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
109 InputStream in = null; |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
110 |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
111 try { |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
112 in = new FileInputStream(file); |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
113 |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
114 MessageDigest digest = MessageDigest.getInstance(DIGEST); |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
115 |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
116 byte [] buf = new byte[40*1024]; |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
117 int r; |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
118 |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
119 while ((r = in.read(buf)) >= 0) { |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
120 digest.update(buf, 0, r); |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
121 } |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
122 |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
123 hash = digest.digest(); |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
124 } |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
125 catch (IOException ioe) { |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
126 log.error(ioe); |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
127 hash = new byte[0]; |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
128 } |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
129 catch (NoSuchAlgorithmException nsae) { |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
130 log.error(nsae); |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
131 hash = new byte[0]; |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
132 } |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
133 finally { |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
134 if (in != null) { |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
135 try { |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
136 in.close(); |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
137 } |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
138 catch (IOException ioe) { |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
139 log.error(ioe); |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
140 } |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
141 } |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
142 } |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
143 } |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
144 return hash; |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
145 } |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
146 |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
147 @Override |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
148 public int compareTo(HashedFile other) { |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
149 if (length < other.length) return -1; |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
150 if (length > other.length) return +1; |
1207
7121a40671ff
HashedFile: Forget to call the file hashing so only the file lengths were compared.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1206
diff
changeset
|
151 return compare(getHash(), other.getHash()); |
1206
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
152 } |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
153 |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
154 private static int compare(byte [] a, byte [] b) { |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
155 if (a.length < b.length) return -1; |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
156 if (a.length > b.length) return +1; |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
157 for (int i = 0; i < a.length; ++i) { |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
158 int x = a[i] & 0xff; |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
159 int y = b[i] & 0xff; |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
160 if (x < y) return -1; |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
161 if (x > y) return +1; |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
162 } |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
163 return 0; |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
164 } |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
165 |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
166 @Override |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
167 public boolean equals(Object other) { |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
168 return other instanceof HashedFile |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
169 && ((HashedFile)other).compareTo(this) == 0; |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
170 } |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
171 |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
172 @Override |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
173 public int hashCode() { |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
174 return (int)(length ^ (length >>> 32)); |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
175 } |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
176 } // class HashedFile |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
177 |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
178 public static List<File> uniqueFiles(List<File> files) { |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
179 |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
180 Set<HashedFile> set = new HashSet<HashedFile>(); |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
181 |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
182 for (File file: files) { |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
183 if (!set.add(new HashedFile(file))) { |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
184 log.warn("file '" + file + "' is a duplicate."); |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
185 } |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
186 } |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
187 |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
188 ArrayList<File> out = new ArrayList<File>(set.size()); |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
189 for (HashedFile hf: set) { |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
190 out.add(hf.file); |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
191 } |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
192 |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
193 return out; |
c7370734b872
Prevent parsing and storing PRF duplicates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
177
diff
changeset
|
194 } |
177
31895d24387e
Importer: Added info gew parser.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
195 } |
31895d24387e
Importer: Added info gew parser.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
196 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |