1 18 package net.sf.drftpd; 19 20 import java.io.BufferedReader ; 21 import java.io.FileNotFoundException ; 22 import java.io.IOException ; 23 import java.io.Serializable ; 24 import java.util.Collection ; 25 import java.util.Hashtable ; 26 import java.util.Iterator ; 27 import java.util.Map ; 28 29 import net.sf.drftpd.remotefile.LinkedRemoteFile; 30 import net.sf.drftpd.remotefile.LinkedRemoteFileInterface; 31 32 36 public class SFVFile implements Serializable { 37 38 public static class SFVStatus { 39 private int _present; 40 private int _offline; 41 private int _total; 42 43 public SFVStatus(int total, int offline, int present) { 44 _total = total; 45 _offline = offline; 46 _present = present; 47 } 48 49 53 public int getMissing() { 54 return _total - _present; 55 } 56 57 61 public int getPresent() { 62 return _present; 63 } 64 65 71 public int getAvailable() { 72 return _present - _offline; 73 } 74 75 79 public int getOffline() { 80 return _offline; 81 } 82 83 public boolean isFinished() { 84 return getMissing() == 0; 85 } 86 } 87 88 static final long serialVersionUID = 5381510163578487722L; 89 90 private transient LinkedRemoteFileInterface _companion; 91 92 96 private Hashtable _entries = new Hashtable (); 97 100 public SFVFile(BufferedReader in) throws IOException { 101 String line; 102 while ((line = in.readLine()) != null) { 103 if (line.length() == 0) 104 continue; 105 if (line.charAt(0) == ';') 106 continue; 107 int separator = line.indexOf(" "); 108 if (separator == -1) 109 continue; 110 111 String fileName = line.substring(0, separator); 112 String checkSumString = line.substring(separator + 1); 113 Long checkSum; 114 try { 115 checkSum = Long.valueOf(checkSumString, 16); 116 } catch (NumberFormatException e) { 117 continue; 118 } 119 _entries.put(fileName, checkSum); 120 } 121 } 122 123 126 public int filesLeft() { 127 return getStatus().getMissing(); 128 } 129 130 134 public int finishedFiles() { 135 return size() - getStatus().getMissing(); 136 } 144 145 public SFVStatus getStatus() { 146 int offline = 0; 147 int present = 0; 148 149 for (Iterator iter = getFiles().iterator(); iter.hasNext();) { 150 LinkedRemoteFileInterface file = (LinkedRemoteFileInterface) iter.next(); 151 if (file.length() != 0) { 152 present++; 153 if (!file.isAvailable()) { 154 offline++; 155 } 156 } 157 } 158 return new SFVStatus(size(), offline, present); 159 } 160 161 public long getChecksum(String fileName) throws NoSFVEntryException { 162 Long checksum = (Long ) _entries.get(fileName); 163 if (checksum == null) 164 throw new NoSFVEntryException(); 165 return checksum.longValue(); 166 } 167 168 172 public Map getEntries() { 173 return _entries; 174 } 175 176 public Map getEntriesFiles() { 177 LinkedRemoteFileInterface dir; 178 try { 179 dir = _companion.getParentFile(); 180 } catch (FileNotFoundException e) { 181 throw new FatalException(e); 182 } 183 184 Map sfventries = getEntries(); 185 Map ret = new Hashtable (); 186 187 for (Iterator iter = sfventries.entrySet().iterator(); 188 iter.hasNext(); 189 ) { 190 Map.Entry element = (Map.Entry ) iter.next(); 191 String fileName = (String ) element.getKey(); 192 193 LinkedRemoteFile file; 194 try { 195 file = (LinkedRemoteFile) dir.getFile(fileName); 196 } catch (FileNotFoundException e1) { 197 continue; 198 } 199 ret.put(file, (Long ) element.getValue()); 200 } 201 return ret; 202 } 203 204 public Collection getFiles() { 205 return getEntriesFiles().keySet(); 206 } 207 208 211 public Collection getNames() { 212 return getEntries().keySet(); 213 } 214 215 public long getTotalBytes() { 216 long totalBytes = 0; 217 for (Iterator iter = getFiles().iterator(); iter.hasNext();) { 218 totalBytes += ((LinkedRemoteFileInterface) iter.next()).length(); 219 } 220 return totalBytes; 221 } 222 223 public long getTotalXfertime() { 224 long totalXfertime = 0; 225 for (Iterator iter = getFiles().iterator(); iter.hasNext();) { 226 totalXfertime += ((LinkedRemoteFileInterface) iter.next()).getXfertime(); 227 } 228 return totalXfertime; 229 } 230 231 public long getXferspeed() { 232 if (getTotalXfertime()/1000 == 0) 233 return 0; 234 return getTotalBytes() / (getTotalXfertime() / 1000); 235 } 236 public boolean hasFile(String name) { 237 return getEntries().containsKey(name); 238 } 239 240 public void setCompanion(LinkedRemoteFileInterface companion) { 241 if (_companion != null) 242 throw new IllegalStateException ("Can't overwrite companion"); 243 _companion = companion; 244 } 245 246 249 public int size() { 250 return _entries.size(); 251 } 252 } 253 | Popular Tags |