Statistics
| Revision:

root / branches / Mobile_Compatible_Hito_1 / libFMap / src / es / prodevelop / gvsig / mobile / fmap / util / download / Downloader.java @ 21606

History | View | Annotate | Download (5.6 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.DataInputStream;
59
import java.io.File;
60
import java.net.URL;
61
import java.util.ArrayList;
62

    
63
import org.apache.log4j.Logger;
64

    
65
import es.prodevelop.gvsig.mobile.fmap.util.Utils;
66

    
67

    
68
/**
69
 * Utility class to download resources from the internet.
70
 * 
71
 * @see es.prodevelop.gvsig.mobile.fmap.util.download.DownloadTask
72
 * @see es.prodevelop.gvsig.mobile.fmap.util.download.DownloadException
73
 * 
74
 * @author jldominguez
75
 *
76
 */
77
public class Downloader {
78
        
79
        private static final long MAX_TIME_ALLOWED = 12000;
80
        private static final long MAX_TIME_ALLOWED_STEP = 500;
81
        
82
        public static String CACHED_FILES_ROOT_PATH = File.separator;
83
        
84
        private static Logger logger = Logger.getLogger(Downloader.class);
85
        
86

    
87
        /**
88
         * This method downloads the resource into a local File or as an array of bytes in memory.
89
         * 
90
         * @param u the URL
91
         * @param exte the extension (used if it's going to be a file)
92
         * @param type download type (file/array)
93
         * @param cachedFilesSubfolder folder destination (used when it's a file)
94
         * @param filePath file path on the folder destination
95
         * @param use_hash whether a hash funcrtion must be used to code the file name
96
         * @return an object file or an array of bytes
97
         * @throws DownloadException
98
         */
99
        public static Object downloadObjectCancel(
100
                        URL u,
101
                        String exte,
102
                        int type,
103
                        String cachedFilesSubfolder,
104
                        String filePath,
105
                        boolean use_hash) throws DownloadException {
106
                
107
                if (u == null) {
108
                        throw new DownloadException("Bad URL");
109
                }
110
                
111
                File cachedFile = null;
112
                
113
                if (type == DownloadTask.FILE) {
114
                        
115
                        if (use_hash) {
116
                                cachedFile = Utils.getHashCacheFileForUrl(
117
                                                u, CACHED_FILES_ROOT_PATH, cachedFilesSubfolder, exte);
118
                        } else {
119
                                cachedFile = new File(CACHED_FILES_ROOT_PATH
120
                                                + File.separator
121
                                                + cachedFilesSubfolder
122
                                                + File.separator
123
                                                + filePath);
124
                        }
125
                        
126
                        if (cachedFile.exists()) {
127
                                logger.debug("CACHED FILE EXISTED !!!");
128
                                return cachedFile; 
129
                        }
130
                }
131

    
132
                DownloadTask task = new DownloadTask(u, exte, type, cachedFile);
133
                Thread down_thread = new Thread(task);
134
                
135
                down_thread.start();
136
                
137
                long time_out = MAX_TIME_ALLOWED;
138
                while ((time_out > 0) && (!task.isDone())) {
139
                        try { Thread.sleep(500); } catch (InterruptedException e) {}
140
                        time_out = time_out - MAX_TIME_ALLOWED_STEP;
141
                }
142
                
143
                if (task.isDone()) {
144
                        
145
                        Object resp = task.getDownloadedObject();
146
                        if (resp == null) {
147
                                task.cancel();
148
                                throw new DownloadException("error");
149
                        } else {
150
                                return resp;
151
                        }
152
                        
153
                } else {
154
                        task.cancel();
155
                        throw new DownloadException("timeout");
156
                }
157
        }
158
        
159
        /**
160
         * This method downloads a URL as a byte array. It does no participate in
161
         * the file cache, so should be avoided
162
         * 
163
         * @param u
164
         * @param exte
165
         * @return the resource as a bute array
166
         * @throws DownloadException
167
         */
168
        public static byte[] getByteArray(URL u, String exte) throws DownloadException {
169
                
170
                ArrayList byteArraysList = new ArrayList(); 
171

    
172
                try {
173
                        
174
                        byte[] buffer = new byte[1024*4];
175
                        DataInputStream is = new DataInputStream(u.openStream());
176
                        int full_size = 0;
177

    
178
                        for (int i = is.read(buffer); i>0; i = is.read(buffer)){
179
                                byteArraysList.add(new Integer(i));
180
                                byteArraysList.add(buffer.clone());
181
                                full_size = full_size + i;
182
                        }
183
                        
184
                        byteArraysList.add(new Integer(full_size));
185
                        
186
                } catch (Exception e) {
187
                        if (e.getMessage().indexOf("403") != -1) {
188
                                throw new DownloadException("403");
189
                        } else {
190
                                throw new DownloadException(e.getMessage());
191
                        }
192
                        
193
                }
194
                
195
                return mergeBytes(byteArraysList);
196
        }
197

    
198
        private static byte[] mergeBytes(ArrayList byteArraysList) {
199
                
200
                int array_size = byteArraysList.size(); 
201
                int total_size =  ((Integer) byteArraysList.get(array_size - 1)).intValue();
202
                int n = array_size / 2;
203
                
204
                int item_size = 0;
205
                byte[] item_bytes = null;
206

    
207
                byte[] resp = new byte[total_size];
208

    
209
                int index = 0;
210
                for (int i=0; i<n; i++) {
211
                        
212
                        logger.debug("MERGE BYTES: i = " + i);
213
                        item_size = ((Integer) byteArraysList.get(2 * i)).intValue();
214
                        item_bytes = (byte[]) byteArraysList.get(2 * i + 1);
215
                        System.arraycopy(item_bytes, 0, resp, index, item_size);
216
                        index = index + item_size;
217
                }
218
                return resp;
219
        }
220
        
221
        
222
}