1 /*
2 Bloof - visualize the evolution of your software project
3 Copyright ( C ) 2003 Lukasz Pekacki <lukasz@pekacki.de>
4 http://bloof.sf.net/
5
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License.
8
9 This program 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 along with
15 this program; if not, write to the Free Software Foundation, Inc.,
16 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17
18 $RCSfile: FileDownload.java,v $
19 Created on $Date: 2003/06/02 15:58:08 $
20 */
21 package net.sf.bloof.util;
22
23 import java.io.File;
24 import java.io.FileOutputStream;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.net.ConnectException;
28 import java.net.HttpURLConnection;
29 import java.net.URL;
30 import java.net.URLConnection;
31 import java.util.Iterator;
32 import java.util.Vector;
33 import java.util.logging.Logger;
34
35 /***
36 * Downloads a file
37 * @author Lukasz Pekacki <pekacki@users.sourceforge.net>
38 * @version $Id: FileDownload.java,v 1.5 2003/06/02 15:58:08 pekacki Exp $
39 */
40 public class FileDownload {
41 /***
42 * Main constructor
43 * @param aSource URL of the source file
44 * @param aDestFile destination file
45 */
46 public FileDownload(URL aSource, File aDestFile) {
47 this.mSource = aSource;
48 this.mDestFile = aDestFile;
49 }
50 /***
51 * Registers a Listener for the download
52 * @param aFlistener the listener
53 */
54 public void addDownloadListener(FileDownloadListener aFlistener) {
55 mDownloadListeners.add(aFlistener);
56 }
57 /***
58 * Does the work.
59 */
60 private void getAndSave() throws IOException {
61 //set up the URL connection
62 URLConnection connection = mSource.openConnection();
63 int totalLenght = connection.getContentLength();
64 //connect to the remote site ( may take some time )
65 connection.connect();
66 HttpURLConnection httpConnection = (HttpURLConnection) connection;
67 if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_UNAUTHORIZED) {
68 throw new ConnectException("HTTP Authorization failure");
69 }
70 propagateDownloadEvent(
71 new DownloadEvent(DownloadEvent.START, totalLenght, 0, mDestFile.getName(), mSource));
72 InputStream is = null;
73 for (int i = 0; i < 3; i++) {
74 try {
75 is = connection.getInputStream();
76 break;
77 } catch (IOException ex) {
78 sLogger.info("Error opening connection " + ex);
79 }
80 }
81 if (is == null) {
82 sLogger.info("Can't get " + mSource + " to " + mDestFile);
83 throw new ConnectException("Can't get " + mSource + " to " + mDestFile);
84 }
85
86 FileOutputStream fos = new FileOutputStream(mDestFile);
87 mFinished = false;
88 byte[] buffer = new byte[100 * 1024];
89 int length;
90 while ((length = is.read(buffer)) >= 0) {
91 fos.write(buffer, 0, length);
92 propagateDownloadEvent(
93 new DownloadEvent(
94 DownloadEvent.RUNNING,
95 totalLenght,
96 length,
97 mDestFile.getName(),
98 mSource));
99 }
100 mFinished = true;
101 if (fos != null) {
102 fos.close();
103 }
104 is.close();
105 propagateDownloadEvent(
106 new DownloadEvent(DownloadEvent.END, totalLenght, totalLenght, mDestFile.getName(), mSource));
107
108 }
109
110 private void propagateDownloadEvent(DownloadEvent aDe) {
111 for (Iterator iter = mDownloadListeners.iterator(); iter.hasNext();) {
112 FileDownloadListener element = (FileDownloadListener) iter.next();
113 element.downloadEventOccured(aDe);
114
115 }
116 }
117 /***
118 * Downloads the file and saves it to the destination file
119 * if the download fails or is interrupted, the destination
120 * file is deleted
121 * @throws IOException if an error occurs
122 */
123 public void startDownload() throws IOException {
124 sLogger.fine("Start downloading " + mSource);
125 try {
126 getAndSave();
127 } catch (IOException e) {
128 if (mDestFile.exists()) {
129 mDestFile.delete();
130 }
131 throw new IOException("An error occured on downloading or saving the file.");
132 }
133 sLogger.fine("Saved downloaded file to " + mDestFile);
134 }
135 private static Logger sLogger = Logger.getLogger("net.sf.bloof.util.FileDownload");
136 private File mDestFile;
137 private Vector mDownloadListeners = new Vector();
138 private boolean mFinished = false;
139 private URL mSource;
140 }
This page was automatically generated by Maven