KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > drftpd > SFVFile


1 /*
2  * This file is part of DrFTPD, Distributed FTP Daemon.
3  *
4  * DrFTPD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * DrFTPD is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with DrFTPD; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package net.sf.drftpd;
19
20 import java.io.BufferedReader JavaDoc;
21 import java.io.FileNotFoundException JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.io.Serializable JavaDoc;
24 import java.util.Collection JavaDoc;
25 import java.util.Hashtable JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.Map JavaDoc;
28
29 import net.sf.drftpd.remotefile.LinkedRemoteFile;
30 import net.sf.drftpd.remotefile.LinkedRemoteFileInterface;
31
32 /**
33  * @author mog
34  * @version $Id: SFVFile.java,v 1.31 2004/04/22 02:10:10 mog Exp $
35  */

36 public class SFVFile implements Serializable JavaDoc {
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         /**
50          * Returns the number of files that don't exist or are 0byte.
51          * @return the number of files that don't exist or are 0byte.
52          */

53         public int getMissing() {
54             return _total - _present;
55         }
56
57         /**
58          * Returns the number of files that exist and are not 0 byte.
59          * @return the number of files that exist and are not 0 byte.
60          */

61         public int getPresent() {
62             return _present;
63         }
64
65         /**
66          * Returns the number of files that are available (online).
67          *
68          * If a file is online, it is of course is also present (exists).
69          * @return the number of files that are available (present & online)
70          */

71         public int getAvailable() {
72             return _present - _offline;
73         }
74
75         /**
76          * Returns the number of files that are offline.
77          * @return the number of files that are offline.
78          */

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     /**
93      * String fileName as key.
94      * Long checkSum as value.
95      */

96     private Hashtable JavaDoc _entries = new Hashtable JavaDoc();
97     /**
98      * Constructor for SFVFile.
99      */

100     public SFVFile(BufferedReader JavaDoc in) throws IOException JavaDoc {
101         String JavaDoc 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 JavaDoc fileName = line.substring(0, separator);
112             String JavaDoc checkSumString = line.substring(separator + 1);
113             Long JavaDoc checkSum;
114             try {
115                 checkSum = Long.valueOf(checkSumString, 16);
116             } catch (NumberFormatException JavaDoc e) {
117                 continue;
118             }
119             _entries.put(fileName, checkSum);
120         }
121     }
122
123     /**
124      * @deprecated use getStatus().getMissing()
125      */

126     public int filesLeft() {
127         return getStatus().getMissing();
128     }
129
130     /**
131      * @return the number of files in the dir that are in the .sfv and aren't 0 bytes
132      * @deprecated use getStatus()
133      */

134     public int finishedFiles() {
135         return size() - getStatus().getMissing();
136         // int good = 0;
137
// for (Iterator iter = getFiles().iterator(); iter.hasNext();) {
138
// LinkedRemoteFile file = (LinkedRemoteFile) iter.next();
139
// if (file.length() != 0)
140
// good++;
141
// }
142
// return good;
143
}
144
145     public SFVStatus getStatus() {
146         int offline = 0;
147         int present = 0;
148
149         for (Iterator JavaDoc 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 JavaDoc fileName) throws NoSFVEntryException {
162         Long JavaDoc checksum = (Long JavaDoc) _entries.get(fileName);
163         if (checksum == null)
164             throw new NoSFVEntryException();
165         return checksum.longValue();
166     }
167
168     /**
169      * Returns a map having <code>String filename</code> as key and <code>Long checksum</code> as value.
170      * @return a map having <code>String filename</code> as key and <code>Long checksum</code> as value.
171      */

172     public Map JavaDoc getEntries() {
173         return _entries;
174     }
175
176     public Map JavaDoc getEntriesFiles() {
177         LinkedRemoteFileInterface dir;
178         try {
179             dir = _companion.getParentFile();
180         } catch (FileNotFoundException JavaDoc e) {
181             throw new FatalException(e);
182         }
183
184         Map JavaDoc sfventries = getEntries();
185         Map JavaDoc ret = new Hashtable JavaDoc();
186
187         for (Iterator JavaDoc iter = sfventries.entrySet().iterator();
188             iter.hasNext();
189             ) {
190             Map.Entry JavaDoc element = (Map.Entry JavaDoc) iter.next();
191             String JavaDoc fileName = (String JavaDoc) element.getKey();
192
193             LinkedRemoteFile file;
194             try {
195                 file = (LinkedRemoteFile) dir.getFile(fileName);
196             } catch (FileNotFoundException JavaDoc e1) {
197                 continue;
198             }
199             ret.put(file, (Long JavaDoc) element.getValue());
200         }
201         return ret;
202     }
203
204     public Collection JavaDoc getFiles() {
205         return getEntriesFiles().keySet();
206     }
207
208     /**
209      * Returns the names of the files in this .sfv file
210      */

211     public Collection JavaDoc getNames() {
212         return getEntries().keySet();
213     }
214
215     public long getTotalBytes() {
216         long totalBytes = 0;
217         for (Iterator JavaDoc 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 JavaDoc 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 JavaDoc name) {
237         return getEntries().containsKey(name);
238     }
239
240     public void setCompanion(LinkedRemoteFileInterface companion) {
241         if (_companion != null)
242             throw new IllegalStateException JavaDoc("Can't overwrite companion");
243         _companion = companion;
244     }
245
246     /**
247      * @return Number of file entries in the .sfv
248      */

249     public int size() {
250         return _entries.size();
251     }
252 }
253
Popular Tags