1 7 package fr.jayasoft.ivy.repository.url; 8 9 import java.io.IOException ; 10 import java.io.InputStream ; 11 import java.net.MalformedURLException ; 12 import java.net.URL ; 13 14 import fr.jayasoft.ivy.repository.Resource; 15 import fr.jayasoft.ivy.url.URLHandlerRegistry; 16 import fr.jayasoft.ivy.url.URLHandler.URLInfo; 17 18 public class URLResource implements Resource { 19 private URL _url; 20 private boolean _init = false; 21 private long _lastModified; 22 private long _contentLength; 23 private boolean _exists; 24 25 public URLResource(URL url) { 26 _url = url; 27 } 28 29 public String getName() { 30 return _url.toExternalForm(); 31 } 32 33 public Resource clone(String cloneName) { 34 try { 35 return new URLResource(new URL (cloneName)); 36 } catch (MalformedURLException e) { 37 throw new IllegalArgumentException ("bad clone name provided: not suitable for an URLResource: "+cloneName); 38 } 39 } 40 41 public long getLastModified() { 42 if (!_init) { 43 init(); 44 } 45 return _lastModified; 46 } 47 48 private void init() { 49 URLInfo info = URLHandlerRegistry.getDefault().getURLInfo(_url); 50 _contentLength = info.getContentLength(); 51 _lastModified = info.getLastModified(); 52 _exists = info.isReachable(); 53 _init = true; 54 } 55 56 public long getContentLength() { 57 if (!_init) { 58 init(); 59 } 60 return _contentLength; 61 } 62 63 public boolean exists() { 64 if (!_init) { 65 init(); 66 } 67 return _exists; 68 } 69 70 public URL getURL() { 71 return _url; 72 } 73 public String toString() { 74 return getName(); 75 } 76 77 public boolean isLocal() { 78 return false; 79 } 80 81 public InputStream openStream() throws IOException { 82 return _url.openStream(); 83 } 84 } 85 | Popular Tags |