Rev 28 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 28 | Rev 29 | ||
---|---|---|---|
Line 16... | Line 16... | ||
16 | * @see http://www.matheboard.de/archive/435725/thread.html |
16 | * @see http://www.matheboard.de/archive/435725/thread.html |
17 | */ |
17 | */ |
18 | public class ImmortableNumberSearch { |
18 | public class ImmortableNumberSearch { |
19 | 19 | ||
20 | private static final String SIGNATURE = "Immortable Number Report File Version 2.01"; |
20 | private static final String SIGNATURE = "Immortable Number Report File Version 2.01"; |
21 | private static final String SIGNATURE_MINOR = "Iterator GenX Java (100k save-interval, load-integrity-check, int32-r, array-object) r28"; |
21 | private static final String SIGNATURE_MINOR = "Iterator GenX Java (100k save-interval, load-integrity-check, int32-r, array-object) r29"; |
22 | private static final String END_SIG = "END OF REPORT"; |
22 | private static final String END_SIG = "END OF REPORT"; |
23 | private static final int SOFTBREAK = 76; |
23 | private static final int SOFTBREAK = 76; |
24 | 24 | ||
25 | private ByteArray a; |
25 | private ByteArray a; |
26 | private String filename; |
26 | private String filename; |
Line 31... | Line 31... | ||
31 | private String backupDir = "backup"; |
31 | private String backupDir = "backup"; |
32 | 32 | ||
33 | private static final int INITIAL_SIZE = 1000000; |
33 | private static final int INITIAL_SIZE = 1000000; |
34 | private static final int EXPANSION_SIZE = 1000000; |
34 | private static final int EXPANSION_SIZE = 1000000; |
35 | 35 | ||
- | 36 | private boolean do_integrity_test = false; |
|
- | 37 | private boolean verbose = false; |
|
- | 38 | ||
- | 39 | public boolean isDo_integrity_test() { |
|
- | 40 | return do_integrity_test; |
|
- | 41 | } |
|
- | 42 | ||
- | 43 | public void setDo_integrity_test(boolean doIntegrityTest) { |
|
- | 44 | do_integrity_test = doIntegrityTest; |
|
- | 45 | } |
|
- | 46 | ||
- | 47 | public boolean isVerbose() { |
|
- | 48 | return verbose; |
|
- | 49 | } |
|
- | 50 | ||
- | 51 | public void setVerbose(boolean verbose) { |
|
- | 52 | this.verbose = verbose; |
|
- | 53 | } |
|
- | 54 | ||
- | 55 | protected void log(String s) { |
|
- | 56 | if (verbose) { |
|
- | 57 | String timestamp = DateUtils.now("EEE, d MMM yyyy HH:mm:ss Z"); |
|
- | 58 | System.out.println(timestamp + " - " + s); // FURUE: In eine |
|
- | 59 | // Log-Datei schreiben |
|
- | 60 | } |
|
- | 61 | } |
|
- | 62 | ||
36 | public ImmortableNumberSearch(String filename) throws LoadException { |
63 | public ImmortableNumberSearch(String filename) throws LoadException { |
37 | this.filename = filename; |
64 | this.filename = filename; |
38 | load(); |
65 | load(); |
39 | } |
66 | } |
40 | 67 | ||
Line 86... | Line 113... | ||
86 | } catch (FileNotFoundException e) { |
113 | } catch (FileNotFoundException e) { |
87 | // Will not happen because of savePointExists(). |
114 | // Will not happen because of savePointExists(). |
88 | return; |
115 | return; |
89 | } |
116 | } |
90 | 117 | ||
91 | a.clear(); |
- | |
92 | u = -1; |
118 | u = -1; |
93 | r = -1; |
119 | r = -1; |
94 | 120 | ||
95 | try { |
121 | try { |
96 | try { |
122 | try { |
97 | String s = f.readLine(); |
123 | String s = f.readLine(); |
98 | if (!s.equals(SIGNATURE)) { |
124 | if (!s.equals(SIGNATURE)) { |
99 | throw new LoadException("Wrong signature"); |
125 | throw new LoadException("Corrupt: Wrong signature"); |
100 | } |
126 | } |
101 | 127 | ||
102 | f.readLine(); // Minor. signature |
128 | f.readLine(); // Minor. signature |
103 | f.readLine(); // "" |
129 | f.readLine(); // "" |
104 | 130 | ||
Line 146... | Line 172... | ||
146 | throw new LoadException(e); |
172 | throw new LoadException(e); |
147 | } |
173 | } |
148 | } |
174 | } |
149 | 175 | ||
150 | private void save(boolean integrityTest) throws SaveException { |
176 | private void save(boolean integrityTest) throws SaveException { |
151 | if (integrityTest) { |
177 | if ((integrityTest) && (do_integrity_test)) { |
152 | System.out.println("Beginne Selbsttest vor erster Speicherung..."); |
178 | log("Beginne Selbsttest vor erster Speicherung..."); |
153 | 179 | ||
154 | if (!integryTest()) { |
180 | if (!integryTest()) { |
155 | throw new SaveException( |
181 | throw new SaveException( |
156 | "Integrity test failed. (Loaded file broken?) Will not save."); |
182 | "Integrity test failed. (Loaded file broken?) Will not save."); |
157 | } |
183 | } |
158 | } |
184 | } |
159 | 185 | ||
- | 186 | log("Speichere bei u=" + (u + 1)); |
|
- | 187 | ||
160 | String timestamp = DateUtils.now("EEE, d MMM yyyy HH:mm:ss Z"); |
188 | String timestamp = DateUtils.now("EEE, d MMM yyyy HH:mm:ss Z"); |
161 | String timestamp_filename = DateUtils.now("dd-MM-yyyy_HH-mm-ss"); |
189 | String timestamp_filename = DateUtils.now("dd-MM-yyyy_HH-mm-ss"); |
162 | 190 | ||
163 | try { |
191 | try { |
164 | BufferedWriter f = new BufferedWriter(new FileWriter(filename)); |
192 | BufferedWriter f = new BufferedWriter(new FileWriter(filename)); |
Line 216... | Line 244... | ||
216 | FileUtils.copyFile(new File(filename), new File(bak_filename)); |
244 | FileUtils.copyFile(new File(filename), new File(bak_filename)); |
217 | } catch (IOException e) { |
245 | } catch (IOException e) { |
218 | throw new SaveException("Could not make backup", e); |
246 | throw new SaveException("Could not make backup", e); |
219 | } |
247 | } |
220 | 248 | ||
221 | if (integrityTest) { |
249 | if ((integrityTest) && (do_integrity_test)) { |
222 | System.out |
- | |
223 | .println("Erste Speicherung absolviert. Stabile Phase hat begonnen."); |
250 | log("Erste Speicherung absolviert. Stabile Phase hat begonnen."); |
224 | } |
251 | } |
225 | } |
252 | } |
226 | 253 | ||
227 | public void calcIterate(int amount) throws SaveException { |
254 | public void calcIterate(int amount) throws SaveException { |
228 | int s, m, k; |
255 | log("Beginn der Rechenarbeit..."); |
- | 256 | ||
229 | int cnt = 0; |
257 | int cnt = 0; |
230 | 258 | ||
231 | // Wir führen beim ersten Speichern einen weiteren |
259 | // Wir führen beim ersten Speichern einen weiteren |
232 | // integrity-Test durch. Grund: Wäre bei einer Fortsetzung einer |
260 | // integrity-Test durch. Grund: Wäre bei einer Fortsetzung einer |
233 | // Datei das "r" falsch (der Datenteil aber korrekt), dann würde |
261 | // Datei das "r" falsch (der Datenteil aber korrekt), dann würde |
Line 242... | Line 270... | ||
242 | u = 1; |
270 | u = 1; |
243 | r = 2; |
271 | r = 2; |
244 | cnt += 2; |
272 | cnt += 2; |
245 | } |
273 | } |
246 | 274 | ||
- | 275 | int s, m, k; |
|
- | 276 | ||
247 | do { |
277 | do { |
248 | r = (r - a.get(u)) / 10 + a.get(u); |
278 | r = (r /* - a.get(u) */) / 10 + a.get(u); |
249 | s = 0; |
279 | s = 0; |
250 | for (m = 1, k = u; m < k; m++, k--) { |
280 | for (m = 1, k = u; m < k; m++, k--) { |
251 | s += a.get(m) * a.get(k); |
281 | s += a.get(m) * a.get(k); |
252 | } |
282 | } |
253 | r += 2 * s; |
283 | r += 2 * s; |
Line 264... | Line 294... | ||
264 | } |
294 | } |
265 | } while (cnt != amount); |
295 | } while (cnt != amount); |
266 | 296 | ||
267 | // Wir brauchen nicht zwei Mal zu speichern |
297 | // Wir brauchen nicht zwei Mal zu speichern |
268 | if (cnt - 1 % saveInterval != 0) { |
298 | if (cnt - 1 % saveInterval != 0) { |
- | 299 | log("Letzte Speicherung vor Ende..."); |
|
269 | save(firstSave); |
300 | save(firstSave); |
270 | firstSave = false; |
301 | // firstSave = false; |
271 | } |
302 | } |
- | 303 | ||
- | 304 | log("Ende der Rechenarbeit..."); |
|
272 | } |
305 | } |
273 | 306 | ||
274 | public byte getDigitReverse(int u) { |
307 | public byte getDigitReverse(int u) { |
275 | return a.get(u); |
308 | return a.get(u); |
276 | } |
309 | } |