root / branches / Mobile_Compatible_Hito_1 / libFMap / src / es / prodevelop / gvsig / mobile / fmap / util / download / DownloadTask.java @ 21606
History | View | Annotate | Download (6.42 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2006 Prodevelop and Generalitat Valenciana.
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the Free Software
|
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
18 |
*
|
19 |
* For more information, contact:
|
20 |
*
|
21 |
* Generalitat Valenciana
|
22 |
* Conselleria d'Infraestructures i Transport
|
23 |
* Av. Blasco Ib??ez, 50
|
24 |
* 46010 VALENCIA
|
25 |
* SPAIN
|
26 |
*
|
27 |
* +34 963862235
|
28 |
* gvsig@gva.es
|
29 |
* http://www.gvsig.gva.es
|
30 |
*
|
31 |
* or
|
32 |
*
|
33 |
* Prodevelop Integraci?n de Tecnolog?as SL
|
34 |
* Conde Salvatierra de ?lava , 34-10
|
35 |
* 46004 Valencia
|
36 |
* Spain
|
37 |
*
|
38 |
* +34 963 510 612
|
39 |
* +34 963 510 968
|
40 |
* gis@prodevelop.es
|
41 |
* http://www.prodevelop.es
|
42 |
*
|
43 |
* or
|
44 |
*
|
45 |
* Instituto de Rob?tica
|
46 |
* Apartado de correos 2085
|
47 |
* 46071 Valencia
|
48 |
* (Spain)
|
49 |
*
|
50 |
* +34 963 543 577
|
51 |
* jjordan@robotica.uv.es
|
52 |
* http://robotica.uv.es
|
53 |
*
|
54 |
*/
|
55 |
|
56 |
package es.prodevelop.gvsig.mobile.fmap.util.download; |
57 |
|
58 |
import java.io.BufferedOutputStream; |
59 |
import java.io.DataInputStream; |
60 |
import java.io.DataOutputStream; |
61 |
import java.io.File; |
62 |
import java.io.FileOutputStream; |
63 |
import java.io.InputStream; |
64 |
import java.net.URL; |
65 |
import java.util.ArrayList; |
66 |
|
67 |
import org.apache.log4j.Logger; |
68 |
|
69 |
/**
|
70 |
* This class is used to download resources in a separate thread, so it can be cancelled.
|
71 |
*
|
72 |
* @see es.prodevelop.gvsig.mobile.fmap.util.download.Downloader
|
73 |
*
|
74 |
* @author jldominguez
|
75 |
*
|
76 |
*/
|
77 |
public class DownloadTask implements Runnable { |
78 |
|
79 |
private static Logger logger = Logger.getLogger(DownloadTask.class); |
80 |
|
81 |
public static final int BYTE_ARRAY = 0; |
82 |
public static final int FILE = 1; |
83 |
|
84 |
private URL url = null; |
85 |
private String ext = "tmp"; |
86 |
private File destFile = null; |
87 |
|
88 |
private Object returned_obj = null; |
89 |
private int down_type = FILE; |
90 |
|
91 |
private boolean go_on = true; |
92 |
private boolean done = false; |
93 |
|
94 |
/**
|
95 |
*
|
96 |
* @return whether the download is finished
|
97 |
*/
|
98 |
public boolean isDone() { |
99 |
return done;
|
100 |
} |
101 |
|
102 |
/**
|
103 |
* Constructor
|
104 |
* @param u the URL
|
105 |
* @param exte the local file extension
|
106 |
* @param _type whether it is a file download or an array of bytes
|
107 |
* @param dest_file destination file
|
108 |
*/
|
109 |
public DownloadTask(URL u, String exte, int _type, File dest_file) { |
110 |
url = u; |
111 |
ext = exte; |
112 |
down_type = _type; |
113 |
destFile = dest_file; |
114 |
} |
115 |
|
116 |
/**
|
117 |
* Switches the state to cancelled, so the download should be cancelled
|
118 |
*
|
119 |
*/
|
120 |
public void cancel() { |
121 |
go_on = false;
|
122 |
} |
123 |
|
124 |
/**
|
125 |
* @see java.lang.Runnable#run()
|
126 |
*/
|
127 |
public void run() { |
128 |
|
129 |
// ---------------------------------------- bytes
|
130 |
if (down_type == BYTE_ARRAY) {
|
131 |
|
132 |
ArrayList byteArraysList = new ArrayList(); |
133 |
|
134 |
try {
|
135 |
|
136 |
byte[] buffer = new byte[1024*4]; |
137 |
DataInputStream is = new DataInputStream(url.openStream()); |
138 |
int full_size = 0; |
139 |
|
140 |
for (int i = is.read(buffer); i>0; i = is.read(buffer)) { |
141 |
|
142 |
if (!go_on) {
|
143 |
returned_obj = null;
|
144 |
return;
|
145 |
} |
146 |
|
147 |
byteArraysList.add(new Integer(i)); |
148 |
byteArraysList.add(buffer.clone()); |
149 |
full_size = full_size + i; |
150 |
|
151 |
if (!go_on) {
|
152 |
returned_obj = null;
|
153 |
return;
|
154 |
} |
155 |
} |
156 |
|
157 |
byteArraysList.add(new Integer(full_size)); |
158 |
|
159 |
} catch (Exception e) { |
160 |
logger.error("While downloading: " + e.getMessage());
|
161 |
returned_obj = null;
|
162 |
done = true;
|
163 |
return;
|
164 |
} |
165 |
|
166 |
returned_obj = mergeBytes(byteArraysList); |
167 |
done = true;
|
168 |
return;
|
169 |
} |
170 |
// ---------------------------------------- file
|
171 |
if (down_type == FILE) {
|
172 |
|
173 |
logger.debug("Downloading to FILE this url: " + url.toString());
|
174 |
logger.debug("Destination path is: " + destFile.getAbsolutePath());
|
175 |
|
176 |
DataOutputStream dos;
|
177 |
|
178 |
if (!go_on) {
|
179 |
returned_obj = null;
|
180 |
return;
|
181 |
} |
182 |
|
183 |
try {
|
184 |
logger.debug("Getting file: File does not exist...");
|
185 |
if (!go_on) {
|
186 |
returned_obj = null;
|
187 |
return;
|
188 |
} |
189 |
|
190 |
if (!destFile.getParentFile().exists()) {
|
191 |
logger.debug("Parent did not exist!");
|
192 |
destFile.getParentFile().mkdirs(); |
193 |
} else {
|
194 |
logger.debug("Parent existed!");
|
195 |
} |
196 |
destFile.createNewFile(); |
197 |
|
198 |
dos = new DataOutputStream( new BufferedOutputStream(new FileOutputStream(destFile))); |
199 |
byte[] buffer = new byte[1024*4]; |
200 |
// ByteBuffer bff;
|
201 |
|
202 |
logger.debug("Opening stream...");
|
203 |
InputStream inp_str = url.openStream();
|
204 |
logger.debug("Opening stream... done");
|
205 |
|
206 |
DataInputStream is = new DataInputStream(inp_str); |
207 |
|
208 |
if (!go_on) {
|
209 |
returned_obj = null;
|
210 |
return;
|
211 |
} |
212 |
|
213 |
long read = 0; |
214 |
|
215 |
for (int i = is.read(buffer); i>0; i = is.read(buffer)) { |
216 |
|
217 |
if (!go_on) {
|
218 |
returned_obj = null;
|
219 |
return;
|
220 |
} |
221 |
|
222 |
dos.write(buffer, 0, i);
|
223 |
read += i; |
224 |
|
225 |
if (!go_on) {
|
226 |
returned_obj = null;
|
227 |
return;
|
228 |
} |
229 |
} |
230 |
|
231 |
inp_str.close(); |
232 |
dos.close(); is.close(); |
233 |
is = null; dos = null; |
234 |
inp_str = null;
|
235 |
|
236 |
} catch (Exception e) { |
237 |
logger.error("While downloading: " + e.getMessage());
|
238 |
returned_obj = null;
|
239 |
done = true;
|
240 |
return;
|
241 |
} |
242 |
|
243 |
returned_obj = destFile; |
244 |
done = true;
|
245 |
return;
|
246 |
|
247 |
} |
248 |
|
249 |
returned_obj = null;
|
250 |
done = true;
|
251 |
|
252 |
|
253 |
} |
254 |
|
255 |
/**
|
256 |
*
|
257 |
* @return the result object (file or byte array)
|
258 |
*/
|
259 |
public Object getDownloadedObject() { |
260 |
return returned_obj;
|
261 |
} |
262 |
|
263 |
private byte[] mergeBytes(ArrayList byteArraysList) { |
264 |
|
265 |
int array_size = byteArraysList.size();
|
266 |
int total_size = ((Integer) byteArraysList.get(array_size - 1)).intValue(); |
267 |
int n = array_size / 2; |
268 |
|
269 |
int item_size = 0; |
270 |
byte[] item_bytes = null; |
271 |
|
272 |
byte[] resp = new byte[total_size]; |
273 |
|
274 |
if (! go_on) {
|
275 |
return null; |
276 |
} |
277 |
|
278 |
int index = 0; |
279 |
for (int i=0; i<n; i++) { |
280 |
|
281 |
if (! go_on) {
|
282 |
return null; |
283 |
} |
284 |
|
285 |
item_size = ((Integer) byteArraysList.get(2 * i)).intValue(); |
286 |
item_bytes = (byte[]) byteArraysList.get(2 * i + 1); |
287 |
System.arraycopy(item_bytes, 0, resp, index, item_size); |
288 |
index = index + item_size; |
289 |
|
290 |
if (! go_on) {
|
291 |
return null; |
292 |
} |
293 |
|
294 |
} |
295 |
return resp;
|
296 |
} |
297 |
|
298 |
} |