1 26 27 package net.sourceforge.groboutils.util.io.v1; 28 29 import java.io.InputStream ; 30 import java.io.IOException ; 31 import java.net.URL ; 32 33 34 44 public class URLInputStreamGenerator implements IInputStreamGenerator 45 { 46 private URL root; 47 private String orig; 48 49 53 public URLInputStreamGenerator( String originalName ) 54 throws IOException 55 { 56 this.orig = originalName; 57 URL url = new URL ( originalName ); 58 59 String path = url.getFile(); 61 int lastPos = path.lastIndexOf( '/' ); 62 if (lastPos >= 0) 63 { 64 path = path.substring( 0, lastPos+1 ); 65 } 66 this.root = new URL ( url.getProtocol(), url.getHost(), 67 url.getPort(), path ); 68 } 69 70 public URL getURL( String relativeName ) 71 throws IOException 72 { 73 URL url; 74 if (this.orig.equals( relativeName )) 75 { 76 url = new URL ( relativeName ); 77 } 78 else 79 { 80 url = new URL ( this.root, relativeName ); 81 } 82 return url; 83 } 84 85 86 public String getFullName( String relativeName ) 87 { 88 try 89 { 90 return getURL( relativeName ).toString(); 91 } 92 catch (IOException e) 93 { 94 return e.toString(); 95 } 96 } 97 98 99 public InputStream createInputStream( String relativeName ) 100 throws IOException 101 { 102 return getURL( relativeName ).openStream(); 103 } 104 } 105 | Popular Tags |