Revision 5536 trunk/libraries/libRemoteServices/src/org/gvsig/remoteClient/utils/Utilities.java

View differences:

Utilities.java
68 68
 */
69 69
public class Utilities {
70 70
	private static String characters;
71
	static boolean canceled;
72
	static final long latency = 500;
71 73
	/**
72 74
     * <b>key</b>: URL, <b>value</b>: path to the downloaded file.
73 75
     */
74 76
    private static Hashtable downloadedFiles;
77
	static Exception downloadException;
75 78
    private static final String tempDirectoryPath = System.getProperty("java.io.tmpdir")+"/tmp-andami";
76 79
	
77 80
	
......
431 434
     * @param url
432 435
     * @param filePath
433 436
     */
434
    private static void addDownloadedURL(URL url, String filePath){
437
    static void addDownloadedURL(URL url, String filePath){
435 438
        if (downloadedFiles==null)
436 439
            downloadedFiles = new Hashtable();
437 440
        String fileName = (String) downloadedFiles.put(url, filePath);
......
458 461
     * @throws ConnectException
459 462
     * @throws UnknownHostException
460 463
     */
461
    public static File downloadFile(URL url, String name, ICancellable cancel) throws IOException,ConnectException, UnknownHostException{
464
    public static synchronized File downloadFile(URL url, String name, ICancellable cancel) throws IOException,ConnectException, UnknownHostException{
462 465
    	File f = null;
463

  
464
	    try{
465
	        if ((f=getPreviousDownloadedURL(url))==null){
466
	        	File tempDirectory = new File(tempDirectoryPath);
467
	        	if (!tempDirectory.exists())
468
	        		tempDirectory.mkdir();
469
	        	
470
	        	f = new File(tempDirectoryPath+"/"+name+System.currentTimeMillis());
471
	            
472
	            System.out.println("downloading '"+url.toString()+"' to: "+f.getAbsolutePath());
473
	            
474
	            f.deleteOnExit();
475
                DataOutputStream dos = new DataOutputStream( new BufferedOutputStream(new FileOutputStream(f)));
476
                byte[] buffer = new byte[1024*4]; 
477
                DataInputStream is = new DataInputStream(url.openStream());
478
                long readed = 0;
479
                boolean canceled = (cancel!=null) ? cancel.isCanceled() : false;
480
                for (int i = is.read(buffer); !canceled && i>0; i = is.read(buffer)){
481
                    dos.write(buffer, 0, i);
482
                    readed += i;
483
                    if (cancel!=null)
484
                    	canceled = cancel.isCanceled();
485
                    
486
                }
487
                dos.close();
488
                is.close();
489
            	is = null;
490
            	dos = null;
491
                if (canceled) {
492
                	System.err.println("[RemoteClients] '"+url+"' CANCELED.");
493
                	f.delete();
494
                	f= null;
495
                } else {
496
                	addDownloadedURL(url, f.getAbsolutePath());
497
                }
498
	        }
499
	    } catch (IOException io) {
500
	    	io.printStackTrace();
501
	    }
502
	    
503
	    return f;
466
    	
467
    	if ((f=getPreviousDownloadedURL(url))==null){
468
    		File tempDirectory = new File(tempDirectoryPath);
469
    		if (!tempDirectory.exists())
470
    			tempDirectory.mkdir();
471
    		
472
    		f = new File(tempDirectoryPath+"/"+name+System.currentTimeMillis());
473
    		
474
    		if (cancel == null) {
475
    			cancel = new ICancellable() {
476
					public boolean isCanceled() {
477
						return false;
478
					}
479
    			};
480
    		}
481
    		Thread downloader = new Thread(new Downloader(url, f));
482
    		Thread monitor = new Thread(new Monitor(cancel));
483
    		monitor.start();
484
    		downloader.start();
485
    		while(!canceled && downloader.isAlive()) {
486
    			try {
487
					Thread.sleep(latency);
488
				} catch (InterruptedException e) {
489
					// TODO Auto-generated catch block
490
					e.printStackTrace();
491
				}
492
    		}
493
    		downloader = null;
494
    		monitor = null;
495
    		if (Utilities.downloadException!=null) {
496
    			Exception e = Utilities.downloadException;
497
    			if (e instanceof IOException)
498
    				throw (IOException) e;
499
    			else if (e instanceof ConnectException)
500
    				throw (ConnectException) e;
501
    			else if (e instanceof UnknownHostException)
502
    				throw (UnknownHostException) e;
503
    		}
504
    	}
505
    	
506
    	return f;
504 507
	}
505 508

  
506 509
    /**
......
545 548
		if (downloadedFiles != null && downloadedFiles.containsKey(url))
546 549
			downloadedFiles.remove(url);
547 550
	}
551

  
552
	
548 553
}
554

  
555
final class Monitor implements Runnable {
556
	ICancellable c;
557
	public Monitor(ICancellable cancel) {
558
		Utilities.canceled = false;
559
		this.c = cancel;
560
	}
561
	public void run() {
562
		while (!c.isCanceled()) {
563
			try {
564
				Thread.sleep(Utilities.latency);
565
			} catch (InterruptedException e) {
566
				// TODO Auto-generated catch block
567
				e.printStackTrace();
568
			}
569
		}
570
		Utilities.canceled = true;
571
	}
572
}
573

  
574
final class Downloader implements Runnable {
575
	private URL url;
576
	private File dstFile;
577

  
578
	public Downloader(URL url, File dstFile) {
579
		this.url = url;
580
		this.dstFile = dstFile;
581
		Utilities.downloadException = null;
582
	}
583

  
584
	public void run() {
585
		System.out.println("downloading '"+url.toString()+"' to: "+dstFile.getAbsolutePath());
586
		
587
		DataOutputStream dos;
588
		try {
589
			dos = new DataOutputStream( new BufferedOutputStream(new FileOutputStream(dstFile)));
590
			byte[] buffer = new byte[1024*4]; 
591
			DataInputStream is = new DataInputStream(url.openStream());
592
			long readed = 0;
593
			for (int i = is.read(buffer); !Utilities.canceled && i>0; i = is.read(buffer)){
594
				dos.write(buffer, 0, i);
595
				readed += i;
596
				
597
			}
598
			dos.close();
599
			is.close();
600
			is = null;
601
			dos = null;
602
			if (Utilities.canceled) {
603
				System.err.println("[RemoteClients] '"+url+"' CANCELED.");
604
				dstFile.delete();
605
				dstFile= null;
606
			} else {
607
				Utilities.addDownloadedURL(url, dstFile.getAbsolutePath());
608
			}
609
		} catch (Exception e) {
610
			Utilities.downloadException = e;
611
		}
612
		
613
	}
614
}

Also available in: Unified diff