001: /*
002: * This program is free software; you can redistribute it and/or modify
003: * it under the terms of the GNU General Public License as published by
004: * the Free Software Foundation; either version 2 of the License, or
005: * (at your option) any later version.
006: *
007: * This program is distributed in the hope that it will be useful,
008: * but WITHOUT ANY WARRANTY; without even the implied warranty of
009: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
010: * GNU Library General Public License for more details.
011: *
012: * You should have received a copy of the GNU General Public License
013: * along with this program; if not, write to the Free Software
014: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
015: */
016: package javazoom.download.util;
017:
018: import java.io.BufferedReader;
019: import java.io.BufferedWriter;
020: import java.io.File;
021: import java.io.FileReader;
022: import java.io.FileWriter;
023: import java.io.Serializable;
024: import java.util.List;
025:
026: public class FileInfo implements Serializable {
027:
028: public static String PATH = "";
029:
030: public FileInfo(String fileName) {
031: this .filename = fileName;
032: loadData();
033: }
034:
035: public FileInfo(String s, String s1, String s2, String s3,
036: String s4, boolean flag, int i, String s5, String s6,
037: String s7) {
038: path = null;
039: filename = null;
040: urlMapping = null;
041: contentType = null;
042: contentDisposition = null;
043: contentDispositionHeader = null;
044: customField = null;
045: zipEnabled = false;
046: maxDownload = -1;
047: login = null;
048: password = null;
049: download = 0;
050: incompleteDownload = 0;
051: size = -1L;
052: path = s;
053: filename = s1;
054: urlMapping = s2;
055: contentType = s3;
056: contentDisposition = s4;
057: zipEnabled = flag;
058: maxDownload = i;
059: login = s5;
060: password = s6;
061: customField = s7;
062: //load the saving data
063: loadData();
064: }
065:
066: public static FileInfo getFileInfo(List files, String filename) {
067: for (int i = 0; i < files.size(); i++) {
068: FileInfo fi = (FileInfo) files.get(i);
069: Debug.getInstance().trace(Debug.PANIC,
070: filename + "," + fi.getFilename());
071: if (filename.equals(fi.getFilename()))
072: return fi;
073: }
074: return null;
075: }
076:
077: protected void loadData() {
078: String fn = filename + ".sav";
079: BufferedReader reader = null;
080: try {
081: reader = new BufferedReader(new FileReader(PATH
082: + File.separator + fn));
083: String line = reader.readLine();
084: int idx = line.indexOf(',');
085: download = Integer.parseInt(line.substring(0, idx));
086: incompleteDownload = Integer.parseInt(line
087: .substring(idx + 1));
088: Debug.getInstance().trace(
089: Debug.PANIC,
090: "filename=" + filename + "download=" + download
091: + ",incompleteDownload="
092: + incompleteDownload);
093: } catch (Exception e) {
094: e.printStackTrace();
095: } finally {
096: try {
097: reader.close();
098: } catch (Exception e) {
099: }
100: }
101: }
102:
103: protected void saveData(int d1, int d2) {
104: String fn = filename + ".sav";
105: BufferedWriter writer = null;
106: try {
107: writer = new BufferedWriter(new FileWriter(PATH
108: + File.separator + fn));
109: writer.write(d1 + "," + d2);
110: } catch (Exception e) {
111: e.printStackTrace();
112: } finally {
113: try {
114: writer.close();
115: } catch (Exception e) {
116: }
117: }
118: }
119:
120: public void addDownload() {
121: download++;
122: saveData(download, incompleteDownload);
123: }
124:
125: public void addIncompleteDownload() {
126: incompleteDownload++;
127: saveData(download, incompleteDownload);
128: }
129:
130: public boolean checkLogin(String s) {
131: return login.equals(s);
132: }
133:
134: public boolean checkPassword(String s) {
135: return password.equals(s);
136: }
137:
138: public String getContentDisposition() {
139: return contentDisposition;
140: }
141:
142: public String getContentDispositionHeader() {
143: return contentDispositionHeader;
144: }
145:
146: public String getContentType() {
147: return contentType;
148: }
149:
150: public String getCustomField() {
151: return customField;
152: }
153:
154: public static String getExtension(String s) {
155: String s1 = "";
156: int i = s.lastIndexOf(".");
157: if (i != -1)
158: s1 = s.substring(i + 1, s.length());
159: return s1;
160: }
161:
162: public String getFilename() {
163: return filename;
164: }
165:
166: public String getFilenameExtension() {
167: return getExtension(filename);
168: }
169:
170: public String getLogin() {
171: return login;
172: }
173:
174: public int getMaxDownload() {
175: return maxDownload;
176: }
177:
178: public String getPassword() {
179: return password;
180: }
181:
182: public String getPath() {
183: return path;
184: }
185:
186: public String getRelativeFilename() {
187: int i = filename.lastIndexOf("/");
188: if (i != -1)
189: return filename.substring(i + 1, filename.length());
190: else
191: return filename;
192: }
193:
194: public String getRelativePath() {
195: int i = filename.lastIndexOf("/");
196: if (i != -1)
197: return filename.substring(0, i);
198: else
199: return "";
200: }
201:
202: public long getSize() {
203: return size;
204: }
205:
206: public int getTotalDownload() {
207: return download;
208: }
209:
210: public int getTotalIncompleteDownload() {
211: return incompleteDownload;
212: }
213:
214: public String getUrlMapping() {
215: return urlMapping;
216: }
217:
218: public boolean isZipEnabled() {
219: return zipEnabled;
220: }
221:
222: public void resetTotalDownload() {
223: download = 0;
224: }
225:
226: public void resetTotalIncompleteDownload() {
227: incompleteDownload = 0;
228: }
229:
230: public void setContentDisposition(String s) {
231: contentDisposition = s;
232: }
233:
234: public void setContentDispositionHeader(String s) {
235: contentDispositionHeader = s;
236: }
237:
238: public void setContentType(String s) {
239: contentType = s;
240: }
241:
242: public void setSize(long l) {
243: size = l;
244: }
245:
246: public void updateDownload(int i) {
247: System.out.println("updateDownload i=" + i);
248: download = i;
249: }
250:
251: public void updateIncompleteDownload(int i) {
252: System.out.println("updateIncompleteDownload i=" + i);
253: incompleteDownload = i;
254: }
255:
256: private String contentDisposition;
257:
258: private String contentDispositionHeader;
259:
260: private String contentType;
261:
262: private String customField;
263:
264: private boolean zipEnabled;
265:
266: private String filename;
267:
268: private String login;
269:
270: private int maxDownload;
271:
272: private String password;
273:
274: private String path;
275:
276: private long size;
277:
278: private int download;
279:
280: private int incompleteDownload;
281:
282: private String urlMapping;
283: }
|