1 19 20 package org.openide.filesystems; 21 22 import java.io.File ; 23 import java.io.FileNotFoundException ; 24 import java.io.FilePermission ; 25 import java.io.IOException ; 26 import java.io.InputStream ; 27 import java.io.OutputStream ; 28 import java.io.StringReader ; 29 import java.net.InetAddress ; 30 import java.net.URL ; 31 import java.net.URLConnection ; 32 import java.net.URLStreamHandler ; 33 import java.net.UnknownServiceException ; 34 import java.security.Permission ; 35 36 40 final class FileURL extends URLConnection { 41 42 public static final String PROTOCOL = "nbfs"; 44 46 static URLStreamHandler HANDLER = new URLStreamHandler () { 47 51 public URLConnection openConnection(URL u) 52 throws IOException { 53 return new FileURL(u); 54 } 55 56 protected synchronized InetAddress getHostAddress(URL u) { 57 return null; 58 } 59 }; 60 61 62 InputStream iStream = null; 63 64 65 OutputStream oStream = null; 66 67 68 private FileObject fo; 69 70 74 private FileURL(URL u) { 75 super(u); 76 } 77 78 83 public static URL encodeFileObject(FileObject fo) throws FileStateInvalidException { 84 return NbfsUtil.getURL(fo); 85 } 86 87 91 public static FileObject decodeURL(URL u) { 92 return NbfsUtil.getFileObject(u); 93 } 94 95 97 public void connect() throws IOException { 98 if (fo != null) { 99 return; 100 } 101 102 fo = decodeURL(url); 103 104 if (fo == null) { 105 throw new FileNotFoundException ("Cannot find: " + url); } 107 } 108 109 112 public InputStream getInputStream() throws IOException , UnknownServiceException { 113 connect(); 114 115 if (iStream == null) { 116 try { 117 if (fo.isFolder()) { 118 iStream = new FIS(fo); 119 } else { 120 iStream = fo.getInputStream(); 121 } 122 } catch (FileNotFoundException e) { 123 ExternalUtil.exception(e); 124 throw e; 125 } 126 } 127 128 return iStream; 129 } 130 131 134 public OutputStream getOutputStream() throws IOException , UnknownServiceException { 135 connect(); 136 137 if (fo.isFolder()) { 138 throw new UnknownServiceException (); 139 } 140 141 if (oStream == null) { 142 FileLock flock = fo.lock(); 143 oStream = new LockOS(fo.getOutputStream(flock), flock); 144 } 145 146 return oStream; 147 } 148 149 152 public int getContentLength() { 153 try { 154 connect(); 155 156 return (int) fo.getSize(); 157 } catch (IOException ex) { 158 return 0; 159 } 160 } 161 162 166 public String getHeaderField(String name) { 167 if (name.equalsIgnoreCase("content-type")) { 169 try { 170 connect(); 171 172 if (fo.isFolder()) { 173 return "text/html"; } else { 175 return fo.getMIMEType(); 176 } 177 } catch (IOException e) { 178 } 179 } 180 181 return super.getHeaderField(name); 182 } 183 184 public Permission getPermission() throws IOException { 188 if (fo != null) { 191 File f = FileUtil.toFile(fo); 192 193 if (f != null) { 194 return new FilePermission (f.getAbsolutePath(), "read"); } 196 197 try { 198 FileSystem fs = fo.getFileSystem(); 199 200 if (fs instanceof JarFileSystem) { 201 return new FilePermission (((JarFileSystem) fs).getJarFile().getAbsolutePath(), "read"); } 203 204 } catch (FileStateInvalidException fsie) { 206 } 208 } 209 210 return new FilePermission ("<<ALL FILES>>", "read"); } 213 214 216 private static class LockOS extends java.io.BufferedOutputStream { 217 218 private FileLock flock; 219 220 224 public LockOS(OutputStream os, FileLock lock) throws IOException { 225 super(os); 226 flock = lock; 227 } 228 229 230 public void close() throws IOException { 231 flock.releaseLock(); 232 super.close(); 233 } 234 } 235 236 242 private static final class FIS extends InputStream { 243 244 private StringReader reader; 245 246 249 public FIS(FileObject folder) throws IOException { 250 reader = new StringReader (createDocument(folder)); 251 } 252 253 254 private String createDocument(FileObject folder) 255 throws IOException { 256 StringBuffer buff = new StringBuffer (150); 257 StringBuffer lit = new StringBuffer (15); 258 FileObject[] fobia = folder.getChildren(); 259 String name; 260 261 buff.append("<HTML>\n"); buff.append("<BODY>\n"); 264 FileObject parent = folder.getParent(); 265 266 if (parent != null) { 267 buff.append("<P>"); buff.append("<A HREF=").append("..").append(">").append("..").append("</A>").append("\n"); buff.append("</P>"); } 273 274 for (int i = 0; i < fobia.length; i++) { 275 lit.setLength(0); 276 lit.append(fobia[i].getNameExt()); 277 name = lit.toString(); 278 279 if (fobia[i].isFolder()) { 280 lit.append('/'); } 282 283 buff.append("<P>"); buff.append("<A HREF=").append((Object ) lit).append(">").append(name).append("</A>").append("\n"); buff.append("</P>"); } 287 288 buff.append("</BODY>\n"); buff.append("</HTML>\n"); 291 return buff.toString(); 292 } 293 294 public int read() throws IOException { 296 return reader.read(); 297 } 298 299 public int read(byte[] b, int off, int len) throws IOException { 300 char[] ch = new char[len]; 301 int r = reader.read(ch, 0, len); 302 303 for (int i = 0; i < r; i++) 304 b[off + i] = (byte) ch[i]; 305 306 return r; 307 } 308 309 public long skip(long skip) throws IOException { 310 return reader.skip(skip); 311 } 312 313 public void close() throws IOException { 314 reader.close(); 315 } 316 317 public void reset() throws IOException { 318 reader.reset(); 319 } 320 321 public boolean markSupported() { 322 return false; 323 } 324 } 325 } 327 | Popular Tags |