KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > util > Checksum


1 /*
2  * This file is part of "SnipSnap Wiki/Weblog".
3  *
4  * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel
5  * All Rights Reserved.
6  *
7  * Please visit http://snipsnap.org/ for updates and contact.
8  *
9  * --LICENSE NOTICE--
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  * --LICENSE NOTICE--
24  */

25 package org.snipsnap.util;
26
27 import java.io.*;
28 import java.net.URL JavaDoc;
29 import java.util.*;
30
31 /**
32  *
33  * @author Matthias L. Jugel
34  * @version $Id: Checksum.java 864 2003-05-23 10:47:26Z stephan $
35  */

36 public class Checksum {
37   private String JavaDoc id;
38   private Map checksums;
39
40   public Checksum(File file) throws IOException {
41     load(file);
42   }
43
44   public Checksum(URL JavaDoc url) throws IOException {
45     load(url);
46   }
47
48   public Checksum(String JavaDoc id) {
49     this(id, new HashMap());
50   }
51
52   public Checksum(String JavaDoc id, Map init) {
53     this.id = id;
54     this.checksums = init;
55   }
56
57   public String JavaDoc getId() {
58     return id;
59   }
60
61   public void add(String JavaDoc file, Long JavaDoc checksum) {
62     checksums.put(file, checksum);
63   }
64
65   public Long JavaDoc get(String JavaDoc file) {
66     return (Long JavaDoc) checksums.get(file);
67   }
68
69   public Set compareChanged(Checksum other) {
70     Set result = new TreeSet();
71     Iterator it = checksums.keySet().iterator();
72     while (it.hasNext()) {
73       String JavaDoc key = (String JavaDoc) it.next();
74       if (!checksums.get(key).equals(other.get(key))) {
75         result.add(key);
76       }
77     }
78     return result;
79   }
80
81   public Set getFileNames() {
82     return checksums.keySet();
83   }
84
85   public Set compareUnchanged(Checksum other) {
86     Set result = new TreeSet();
87     Iterator it = checksums.keySet().iterator();
88     while (it.hasNext()) {
89       String JavaDoc key = (String JavaDoc) it.next();
90       if (checksums.get(key).equals(other.get(key))) {
91         result.add(key);
92       }
93     }
94     return result;
95   }
96
97   public void store(File file) throws IOException {
98     Iterator it = checksums.keySet().iterator();
99     Properties save = new Properties();
100     while (it.hasNext()) {
101       String JavaDoc name = (String JavaDoc) it.next();
102       save.setProperty(name, Long.toHexString(((Long JavaDoc) checksums.get(name)).longValue()));
103     }
104     OutputStream out = new FileOutputStream(file);
105     save.setProperty("ID", id);
106     save.store(out, "checksums for " + id);
107     out.close();
108   }
109
110   public void load(File file) throws IOException {
111     load(new FileInputStream(file));
112   }
113
114   public void load(URL JavaDoc url) throws IOException {
115     load(url.openStream());
116   }
117
118   private void load(InputStream in) throws IOException {
119     checksums = new HashMap();
120     Properties load = new Properties();
121     load.load(in);
122     Iterator it = load.keySet().iterator();
123     while (it.hasNext()) {
124       String JavaDoc key = (String JavaDoc) it.next();
125       if(!"ID".equals(key)) {
126         checksums.put(key, new Long JavaDoc(Long.parseLong(load.getProperty(key), 16)));
127       }
128     }
129     id = load.getProperty("ID");
130   }
131
132   public String JavaDoc toString() {
133     return "Checksum[id=" + id + ", " + checksums.toString() + "]";
134   }
135 }
136
Popular Tags