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: InputStream input;
26: if (resource != null) {
27: input = resource.openStream();
28: } else if (new File(source).exists()) {
29: input = new FileInputStream(source);
30: } else {
31: return false;
32: }
33: long sourceSum = checkSum(input);
34: long destinationSum = checkSum(new FileInputStream(
35: destinationFile()));
36: return sourceSum != destinationSum;
37: }
38: }
39:
40: private long checkSum(InputStream input) throws IOException {
41: long sum = 0;
42: int b;
43: while ((b = input.read()) != -1)
44: sum += b;
45: input.close();
46:
47: return sum;
48: }
49: }
|