Revision 4265

View differences:

branches/v05/frameworks/_fwAndami/src/com/iver/andami/Utilities.java
44 44
import java.awt.Container;
45 45
import java.io.BufferedInputStream;
46 46
import java.io.BufferedOutputStream;
47
import java.io.BufferedReader;
47 48
import java.io.DataOutputStream;
48 49
import java.io.File;
49 50
import java.io.FileOutputStream;
50 51
import java.io.IOException;
51 52
import java.io.InputStream;
53
import java.io.InputStreamReader;
52 54
import java.io.OutputStream;
53 55
import java.net.ConnectException;
56
import java.net.HttpURLConnection;
54 57
import java.net.MalformedURLException;
55 58
import java.net.URL;
59
import java.net.URLConnection;
56 60
import java.net.UnknownHostException;
57 61
import java.util.Enumeration;
58 62
import java.util.Hashtable;
......
242 246
     * @throws ConnectException
243 247
     * @throws UnknownHostException
244 248
     */
245
    public static File downloadFile(URL url, String name) throws IOException,ConnectException, UnknownHostException{
249
    public static File downloadFile(URL url, String name) throws IOException, ConnectException, UnknownHostException{
246 250
    	File f = null;
247 251
    	long t1 = System.currentTimeMillis();
248
	    try{
249
	        if ((f=getPreviousDownloadedURL(url))==null){
250
	        	long t3 = System.currentTimeMillis();
251
	        	File tempDirectory = new File(tempDirectoryPath);
252
	        	if (!tempDirectory.exists())
253
	        		tempDirectory.mkdir();
254
	        	
255
	        	f = new File(tempDirectoryPath+"/"+name+System.currentTimeMillis());
256
	            
257
	            System.out.println("downloading '"+url.toString()+"' to: "+f.getAbsolutePath());
258
	            
259
	            f.deleteOnExit();
260
                DataOutputStream dos = new DataOutputStream( new BufferedOutputStream(new FileOutputStream(f)));
261
                byte[] buffer = new byte[1024*256];
262
                InputStream is = url.openStream();
263
                long readed = 0;
264
                for (int i = is.read(buffer); i>0; i = is.read(buffer)){
265
                    dos.write(buffer, 0, i);
266
                    readed += i;
267
                }
268
                dos.close();
269
                addDownloadedURL(url, f.getAbsolutePath());
270
                long t4 = System.currentTimeMillis();
271
        	    System.err.println("Download time: "+(t4-t3));
272
        	    
273
	        }
274
	    } catch (IOException io) {
275
	    	io.printStackTrace();
276
	    }
277
	    // Avoid possible conflits caused by multiple application instances.
278
	    if (!f.exists()) {
279
	    	downloadedFiles.remove(url);
280
	    	f = downloadFile(url, name);
281
	    }
282
	    long t2 = System.currentTimeMillis();
283
	    System.err.println("Total download method time: "+(t2-t1));
284
	    return f;
252
    	if ((f=getPreviousDownloadedURL(url))==null){
253
    		
254
    		File tempDirectory = new File(tempDirectoryPath);
255
    		if (!tempDirectory.exists())
256
    			tempDirectory.mkdir();
257
    		
258
    		f = new File(tempDirectoryPath+"/"+name+System.currentTimeMillis());
259
    		
260
    		System.out.println("downloading '"+url.toString()+"' to: "+f.getAbsolutePath());
261
    		
262
    		f.deleteOnExit();
263
    		DataOutputStream dos = new DataOutputStream( new BufferedOutputStream(new FileOutputStream(f)));
264
    		byte[] buffer = new byte[1024*256];
265
    		URLConnection connection = url.openConnection();
266
    		InputStream is = null;
267
    		try {
268
    			is = connection.getInputStream();
269
    		} catch (IOException e) {
270
    			// Ops! The server gave us an error. I'm piping the error
271
    			// channel to the input channel to lay it out to the user.
272
    			
273
    			if (!(connection instanceof HttpURLConnection)) throw e;
274
    			InputStream err = ((HttpURLConnection) connection).getErrorStream();
275
    			
276
    			if (err == null) throw e;
277
    			is = err;
278
    		}
279
    		long readed = 0;
280
    		for (int i = is.read(buffer); i>0; i = is.read(buffer)){
281
    			dos.write(buffer, 0, i);
282
    			readed += i;
283
    		}
284
    		dos.close();
285
    		addDownloadedURL(url, f.getAbsolutePath());
286
    	}
287
    	
288
    	// Avoid possible conflits caused by multiple application instances.
289
    	if (!f.exists()) {
290
    		downloadedFiles.remove(url);
291
    		f = downloadFile(url, name);
292
    	}
293
    	long t2 = System.currentTimeMillis();
294
    	System.err.println("Total download method time: "+(t2-t1));
295
    	return f;
285 296
	}
286 297

  
287 298
    /**

Also available in: Unified diff