01: // Copyright (C) 2003,2004,2005 by Object Mentor, Inc. All rights reserved.
02: // Released under the terms of the GNU General Public License version 2 or later.
03: package fitnesse.updates;
04:
05: import java.io.*;
06: import java.net.URL;
07:
08: public class ReplacingFileUpdate extends FileUpdate {
09: public ReplacingFileUpdate(Updater updater, String source,
10: String destination) throws Exception {
11: super (updater, source, destination);
12: }
13:
14: public void doUpdate() throws Exception {
15: if (destinationFile().exists())
16: destinationFile().delete();
17: super .doUpdate();
18: }
19:
20: public boolean shouldBeApplied() throws Exception {
21: if (super .shouldBeApplied())
22: return true;
23: else {
24: URL resource = getResource(source);
25: if (resource != null) {
26: long sourceSum = checkSum(resource.openStream());
27: long destinationSum = checkSum(new FileInputStream(
28: destinationFile()));
29:
30: return sourceSum != destinationSum;
31: } else
32: return false;
33: }
34: }
35:
36: private long checkSum(InputStream input) throws IOException {
37: long sum = 0;
38: int b;
39: while ((b = input.read()) != -1)
40: sum += b;
41: input.close();
42:
43: return sum;
44: }
45: }
|