Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2 | daniel-mar | 1 | package de.viathinksoft.distributed.apps.immortal.iterator.sequential; |
2 | |||
3 | import java.io.BufferedReader; |
||
4 | import java.io.BufferedWriter; |
||
5 | import java.io.FileReader; |
||
6 | import java.io.FileWriter; |
||
7 | import java.io.IOException; |
||
8 | import java.math.BigInteger; |
||
9 | import java.security.NoSuchAlgorithmException; |
||
10 | import java.util.ArrayList; |
||
11 | |||
12 | import de.viathinksoft.distributed.apps.immortal.exception.FileContentsBadException; |
||
13 | import de.viathinksoft.utils.security.MD5; |
||
14 | |||
15 | public class ImmortalSequenceList extends ArrayList<BigInteger> { |
||
16 | |||
17 | private static final long serialVersionUID = 3701716850542275075L; |
||
18 | |||
19 | protected static final String SEPARATOR = ";"; |
||
20 | |||
21 | public ImmortalSequenceList() { |
||
22 | super(); |
||
23 | } |
||
24 | |||
25 | public ImmortalSequenceList(String filename) |
||
26 | throws NoSuchAlgorithmException, IOException, |
||
27 | FileContentsBadException { |
||
28 | loadFromFile(filename); |
||
29 | } |
||
30 | |||
31 | @Override |
||
32 | public String toString() { |
||
33 | String s = ""; |
||
34 | for (BigInteger m : this) { |
||
35 | if (s.equals("")) { |
||
36 | s = m.toString(); |
||
37 | } else { |
||
38 | s = s.concat(SEPARATOR).concat(m.toString()); |
||
39 | } |
||
40 | } |
||
41 | return s; |
||
42 | } |
||
43 | |||
44 | public BigInteger getLastNumber() { |
||
45 | return this.get(this.size() - 1); |
||
46 | } |
||
47 | |||
48 | public BigInteger getFirstNumber() { |
||
49 | return this.get(0); |
||
50 | } |
||
51 | |||
52 | // TODO equals, clone |
||
53 | |||
54 | private final String SIGNATURE = "Immortal Number Sequence List V001"; |
||
55 | private final String SALT = "AFasoifjsiugdaskdgnakjsdmoi2jtuijwqoikoafojdnsvhfbqimlkwmfkSFSDFADFA>SFasfAQ"; |
||
56 | |||
57 | public void saveToFile(String filename) throws IOException, |
||
58 | NoSuchAlgorithmException { |
||
59 | BufferedWriter x = new BufferedWriter(new FileWriter(filename)); |
||
60 | |||
61 | String s; |
||
62 | x.write(SIGNATURE); |
||
63 | x.write("\r\n"); |
||
64 | |||
65 | s = this.toString(); |
||
66 | x.write(s); |
||
67 | x.write("\r\n"); |
||
68 | x.write(MD5.digest(s, SALT)); |
||
69 | x.write("\r\n"); |
||
70 | |||
71 | x.close(); |
||
72 | } |
||
73 | |||
74 | public void loadFromFile(String filename) throws IOException, |
||
75 | NoSuchAlgorithmException, FileContentsBadException { |
||
76 | BufferedReader r = new BufferedReader(new FileReader(filename)); |
||
77 | |||
78 | String f_sig = r.readLine(); |
||
79 | if ((f_sig == null) || (!f_sig.equals(SIGNATURE))) { |
||
80 | throw new FileContentsBadException(); |
||
81 | } |
||
82 | |||
83 | String f_commatext = r.readLine(); |
||
84 | if ((f_commatext == null) |
||
85 | || (!MD5.digest(f_commatext, SALT).equals(r.readLine()))) { |
||
86 | throw new FileContentsBadException(); |
||
87 | } |
||
88 | |||
89 | r.close(); |
||
90 | |||
91 | this.clear(); |
||
92 | |||
93 | String[] ary = f_commatext.split(SEPARATOR); |
||
94 | for (String s : ary) { |
||
95 | this.add(new BigInteger(s)); |
||
96 | } |
||
97 | } |
||
98 | |||
99 | } |