1 25 package org.snipsnap.util; 26 27 import java.io.*; 28 import java.net.URL ; 29 import java.util.*; 30 31 36 public class Checksum { 37 private String id; 38 private Map checksums; 39 40 public Checksum(File file) throws IOException { 41 load(file); 42 } 43 44 public Checksum(URL url) throws IOException { 45 load(url); 46 } 47 48 public Checksum(String id) { 49 this(id, new HashMap()); 50 } 51 52 public Checksum(String id, Map init) { 53 this.id = id; 54 this.checksums = init; 55 } 56 57 public String getId() { 58 return id; 59 } 60 61 public void add(String file, Long checksum) { 62 checksums.put(file, checksum); 63 } 64 65 public Long get(String file) { 66 return (Long ) 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 key = (String ) 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 key = (String ) 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 name = (String ) it.next(); 102 save.setProperty(name, Long.toHexString(((Long ) 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 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 key = (String ) it.next(); 125 if(!"ID".equals(key)) { 126 checksums.put(key, new Long (Long.parseLong(load.getProperty(key), 16))); 127 } 128 } 129 id = load.getProperty("ID"); 130 } 131 132 public String toString() { 133 return "Checksum[id=" + id + ", " + checksums.toString() + "]"; 134 } 135 } 136 | Popular Tags |