1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
|
package net.ess3.storage;
import java.io.*;
import java.math.BigInteger;
import java.security.DigestInputStream;
import java.security.DigestOutputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.logging.Level;
import static net.ess3.I18n._;
import net.ess3.api.IEssentials;
import org.bukkit.Bukkit;
public class ManagedFile
{
private final static int BUFFERSIZE = 1024 * 8;
private final File file;
public ManagedFile(final String filename, final IEssentials ess)
{
file = new File(ess.getPlugin().getDataFolder(), filename);
if (file.exists())
{
try
{
if (checkForVersion(file, ess.getPlugin().getVersion()) && !file.delete())
{
throw new IOException("Could not delete file " + file.toString());
}
}
catch (IOException ex)
{
Bukkit.getLogger().log(Level.SEVERE, ex.getMessage(), ex);
}
}
if (!file.exists())
{
try
{
copyResourceAscii("/" + filename, file);
}
catch (IOException ex)
{
Bukkit.getLogger().log(Level.SEVERE, _("Could not load items.csv!"), ex);
}
}
}
public static void copyResourceAscii(final String resourceName, final File file) throws IOException
{
final InputStreamReader reader = new InputStreamReader(ManagedFile.class.getResourceAsStream(resourceName));
try
{
final MessageDigest digest = getDigest();
final DigestOutputStream digestStream = new DigestOutputStream(new FileOutputStream(file), digest);
try
{
final OutputStreamWriter writer = new OutputStreamWriter(digestStream);
try
{
final char[] buffer = new char[BUFFERSIZE];
do
{
final int length = reader.read(buffer);
if (length >= 0)
{
writer.write(buffer, 0, length);
}
else
{
break;
}
}
while (true);
writer.write("\n");
writer.flush();
final BigInteger hashInt = new BigInteger(1, digest.digest());
digestStream.on(false);
digestStream.write('#');
digestStream.write(hashInt.toString(16).getBytes());
}
finally
{
writer.close();
}
}
finally
{
digestStream.close();
}
}
finally
{
reader.close();
}
}
public static boolean checkForVersion(final File file, final String version) throws IOException
{
if (file.length() < 33)
{
return false;
}
final BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
try
{
final byte[] buffer = new byte[(int)file.length()];
int position = 0;
do
{
final int length = bis.read(buffer, position, Math.min((int)file.length() - position, BUFFERSIZE));
if (length < 0)
{
break;
}
position += length;
}
while (position < file.length());
final ByteArrayInputStream bais = new ByteArrayInputStream(buffer);
if (bais.skip(file.length() - 33) != file.length() - 33)
{
return false;
}
final BufferedReader reader = new BufferedReader(new InputStreamReader(bais));
try
{
String hash = reader.readLine();
if (hash != null && hash.matches("#[a-f0-9]{32}"))
{
hash = hash.substring(1);
bais.reset();
final String versionline = reader.readLine();
if (versionline != null && versionline.matches("#version: .+"))
{
final String versioncheck = versionline.substring(10);
if (!versioncheck.equalsIgnoreCase(version))
{
bais.reset();
final MessageDigest digest = getDigest();
final DigestInputStream digestStream = new DigestInputStream(bais, digest);
try
{
final byte[] bytes = new byte[(int)file.length() - 33];
digestStream.read(bytes);
final BigInteger correct = new BigInteger(hash, 16);
final BigInteger test = new BigInteger(1, digest.digest());
if (correct.equals(test))
{
return true;
}
else
{
Bukkit.getLogger().warning(
"File " + file.toString() + " has been modified by user and file version differs, please update the file manually.");
}
}
finally
{
digestStream.close();
}
}
}
}
}
finally
{
reader.close();
}
}
finally
{
bis.close();
}
return false;
}
public static MessageDigest getDigest() throws IOException
{
try
{
return MessageDigest.getInstance("MD5");
}
catch (NoSuchAlgorithmException ex)
{
throw new IOException(ex);
}
}
public List<String> getLines()
{
try
{
final BufferedReader reader = new BufferedReader(new FileReader(file));
try
{
final List<String> lines = new ArrayList<String>();
do
{
final String line = reader.readLine();
if (line == null)
{
break;
}
else
{
lines.add(line);
}
}
while (true);
return lines;
}
finally
{
reader.close();
}
}
catch (IOException ex)
{
Bukkit.getLogger().log(Level.SEVERE, ex.getMessage(), ex);
return Collections.emptyList();
}
}
}
|