1 19 20 package org.netbeans.core.startup.layers; 21 22 import java.io.FileNotFoundException ; 23 import java.io.IOException ; 24 import java.io.InputStream ; 25 import java.net.URL ; 26 import java.net.URLConnection ; 27 import java.net.URLStreamHandler ; 28 import java.net.URLStreamHandlerFactory ; 29 import java.net.UnknownServiceException ; 30 import org.openide.filesystems.FileObject; 31 import org.openide.util.Exceptions; 32 33 36 public class NbinstURLStreamHandlerFactory implements URLStreamHandlerFactory { 37 38 43 public URLStreamHandler createURLStreamHandler(String protocol) { 44 if (NbinstURLMapper.PROTOCOL.equals(protocol)) { 45 return new NbinstURLStreamHandler (); 46 } 47 return null; 48 } 49 50 53 private static class NbinstURLStreamHandler extends URLStreamHandler { 54 55 61 protected URLConnection openConnection(URL u) throws IOException { 62 return new NbinstURLConnection (u); 63 } 64 } 65 66 69 private static class NbinstURLConnection extends URLConnection { 70 71 private FileObject fo; 72 private InputStream iStream; 73 74 79 public NbinstURLConnection (URL url) { 80 super (url); 81 } 82 83 84 public void connect() throws IOException { 85 if (fo == null) { 86 FileObject[] decoded = NbinstURLMapper.decodeURL(this.url); 87 if (decoded != null && decoded.length>0) { 88 fo = decoded[0]; 89 } 90 else { 91 throw new FileNotFoundException ("Cannot find: " + url); } 93 } 94 if (fo.isFolder()) { 95 throw new UnknownServiceException (); 96 } 97 } 98 99 public int getContentLength() { 100 try { 101 this.connect(); 102 return (int) this.fo.getSize(); } catch (IOException e) { 104 return -1; 105 } 106 } 107 108 109 public InputStream getInputStream() throws IOException { 110 this.connect(); 111 if (iStream == null) { 112 iStream = fo.getInputStream(); 113 } 114 return iStream; 115 } 116 117 118 public String getHeaderField (String name) { 119 if ("content-type".equals(name)) { try { 121 this.connect(); 122 return fo.getMIMEType(); 123 } catch (IOException ioe) { 124 Exceptions.printStackTrace(ioe); 125 } 126 } 127 return super.getHeaderField(name); 128 } 129 } 130 } 131 | Popular Tags |