KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > filesystems > FileURL


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.openide.filesystems;
21
22 import java.io.File JavaDoc;
23 import java.io.FileNotFoundException JavaDoc;
24 import java.io.FilePermission JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.InputStream JavaDoc;
27 import java.io.OutputStream JavaDoc;
28 import java.io.StringReader JavaDoc;
29 import java.net.InetAddress JavaDoc;
30 import java.net.URL JavaDoc;
31 import java.net.URLConnection JavaDoc;
32 import java.net.URLStreamHandler JavaDoc;
33 import java.net.UnknownServiceException JavaDoc;
34 import java.security.Permission JavaDoc;
35
36 /** Special URL connection directly accessing an internal file object.
37 *
38 * @author Ales Novak, Petr Hamernik, Jan Jancura, Jaroslav Tulach
39 */

40 final class FileURL extends URLConnection JavaDoc {
41     /** Protocol name for this type of URL. */
42     public static final String JavaDoc PROTOCOL = "nbfs"; // NOI18N
43

44     /** Default implemenatation of handler for this type of URL.
45      */

46     static URLStreamHandler JavaDoc HANDLER = new URLStreamHandler JavaDoc() {
47             /**
48             * @param u - URL to open connection to.
49             * @return new URLConnection.
50             */

51             public URLConnection JavaDoc openConnection(URL JavaDoc u)
52             throws IOException JavaDoc {
53                 return new FileURL(u);
54             }
55
56             protected synchronized InetAddress JavaDoc getHostAddress(URL JavaDoc u) {
57                 return null;
58             }
59         };
60
61     /** 1 URLConnection == 1 InputSteam*/
62     InputStream JavaDoc iStream = null;
63
64     /** 1 URLConnection == 1 OutputSteam*/
65     OutputStream JavaDoc oStream = null;
66
67     /** FileObject that we want to connect to. */
68     private FileObject fo;
69
70     /**
71     * Create a new connection to a {@link FileObject}.
72     * @param u URL of the connection. Please use {@link #encodeFileObject(FileObject)} to create the URL.
73     */

74     private FileURL(URL JavaDoc u) {
75         super(u);
76     }
77
78     /** Provides a URL to access a file object.
79     * @param fo the file object
80     * @return a URL using the correct syntax and {@link #PROTOCOL protocol}
81     * @exception FileStateInvalidException if the file object is not valid (typically, if its filesystem is inconsistent or no longer present)
82     */

83     public static URL JavaDoc encodeFileObject(FileObject fo) throws FileStateInvalidException {
84         return NbfsUtil.getURL(fo);
85     }
86
87     /** Retrieves the file object specified by an internal URL.
88     * @param u the url to decode
89     * @return the file object that is represented by the URL, or <code>null</code> if the URL is somehow invalid or the file does not exist
90     */

91     public static FileObject decodeURL(URL JavaDoc u) {
92         return NbfsUtil.getFileObject(u);
93     }
94
95     /* A method for connecting to a FileObject.
96     */

97     public void connect() throws IOException JavaDoc {
98         if (fo != null) {
99             return;
100         }
101
102         fo = decodeURL(url);
103
104         if (fo == null) {
105             throw new FileNotFoundException JavaDoc("Cannot find: " + url); // NOI18N
106
}
107     }
108
109     /*
110     * @return InputStream or given FileObject.
111     */

112     public InputStream JavaDoc getInputStream() throws IOException JavaDoc, UnknownServiceException JavaDoc {
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 JavaDoc e) {
123                 ExternalUtil.exception(e);
124                 throw e;
125             }
126         }
127
128         return iStream;
129     }
130
131     /*
132     * @return OutputStream for given FileObject.
133     */

134     public OutputStream JavaDoc getOutputStream() throws IOException JavaDoc, UnknownServiceException JavaDoc {
135         connect();
136
137         if (fo.isFolder()) {
138             throw new UnknownServiceException JavaDoc();
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     /*
150     * @return length of FileObject.
151     */

152     public int getContentLength() {
153         try {
154             connect();
155
156             return (int) fo.getSize();
157         } catch (IOException JavaDoc ex) {
158             return 0;
159         }
160     }
161
162     /** Get a header field (currently, content type only).
163     * @param name the header name. Only <code>content-type</code> is guaranteed to be present.
164     * @return the value (i.e., MIME type)
165     */

166     public String JavaDoc getHeaderField(String JavaDoc name) {
167         if (name.equalsIgnoreCase("content-type")) { // NOI18N
168

169             try {
170                 connect();
171
172                 if (fo.isFolder()) {
173                     return "text/html"; // NOI18N
174
} else {
175                     return fo.getMIMEType();
176                 }
177             } catch (IOException JavaDoc e) {
178             }
179         }
180
181         return super.getHeaderField(name);
182     }
183
184     // #13038: URLClassPath is going to check this.
185
// Better not return AllPermission!
186
// SocketPermission on localhost might also work.
187
public Permission JavaDoc getPermission() throws IOException JavaDoc {
188         // Note this is normally called by URLClassPath with an unconnected
189
// URLConnection, so the fo will probably be null anyway.
190
if (fo != null) {
191             File JavaDoc f = FileUtil.toFile(fo);
192
193             if (f != null) {
194                 return new FilePermission JavaDoc(f.getAbsolutePath(), "read"); // NOI18N
195
}
196
197             try {
198                 FileSystem fs = fo.getFileSystem();
199
200                 if (fs instanceof JarFileSystem) {
201                     return new FilePermission JavaDoc(((JarFileSystem) fs).getJarFile().getAbsolutePath(), "read"); // NOI18N
202
}
203
204                 // [PENDING] could do XMLFileSystem too...
205
} catch (FileStateInvalidException fsie) {
206                 // ignore
207
}
208         }
209
210         // fallback
211
return new FilePermission JavaDoc("<<ALL FILES>>", "read"); // NOI18N
212
}
213
214     /** Stream that also closes the lock, if closed.
215      */

216     private static class LockOS extends java.io.BufferedOutputStream JavaDoc {
217         /** lock */
218         private FileLock flock;
219
220         /**
221         * @param os is an OutputStream for writing in
222         * @param lock is a lock for the stream
223         */

224         public LockOS(OutputStream JavaDoc os, FileLock lock) throws IOException JavaDoc {
225             super(os);
226             flock = lock;
227         }
228
229         /** overriden */
230         public void close() throws IOException JavaDoc {
231             flock.releaseLock();
232             super.close();
233         }
234     }
235
236     /** The class allows reading of folder via URL. Because of html
237     * oriented user interface the document has html format.
238     *
239     * @author Ales Novak
240     * @version 0.10 May 15, 1998
241     */

242     private static final class FIS extends InputStream JavaDoc {
243         /** delegated reader that reads the document */
244         private StringReader JavaDoc reader;
245
246         /**
247         * @param folder is a folder
248         */

249         public FIS(FileObject folder) throws IOException JavaDoc {
250             reader = new StringReader JavaDoc(createDocument(folder));
251         }
252
253         /** creates html document as string */
254         private String JavaDoc createDocument(FileObject folder)
255         throws IOException JavaDoc {
256             StringBuffer JavaDoc buff = new StringBuffer JavaDoc(150);
257             StringBuffer JavaDoc lit = new StringBuffer JavaDoc(15);
258             FileObject[] fobia = folder.getChildren();
259             String JavaDoc name;
260
261             buff.append("<HTML>\n"); // NOI18N
262
buff.append("<BODY>\n"); // NOI18N
263

264             FileObject parent = folder.getParent();
265
266             if (parent != null) {
267                 // lit.setLength(0);
268
// lit.append('/').append(parent.getPackageName('/'));
269
buff.append("<P>"); // NOI18N
270
buff.append("<A HREF=").append("..").append(">").append("..").append("</A>").append("\n"); // NOI18N
271
buff.append("</P>"); // NOI18N
272
}
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('/'); // NOI18N
281
}
282
283                 buff.append("<P>"); // NOI18N
284
buff.append("<A HREF=").append((Object JavaDoc) lit).append(">").append(name).append("</A>").append("\n"); // NOI18N
285
buff.append("</P>"); // NOI18N
286
}
287
288             buff.append("</BODY>\n"); // NOI18N
289
buff.append("</HTML>\n"); // NOI18N
290

291             return buff.toString();
292         }
293
294         //************************************** stream methods **********
295
public int read() throws IOException JavaDoc {
296             return reader.read();
297         }
298
299         public int read(byte[] b, int off, int len) throws IOException JavaDoc {
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 JavaDoc {
310             return reader.skip(skip);
311         }
312
313         public void close() throws IOException JavaDoc {
314             reader.close();
315         }
316
317         public void reset() throws IOException JavaDoc {
318             reader.reset();
319         }
320
321         public boolean markSupported() {
322             return false;
323         }
324     }
325      // end of FIS
326
}
327
Popular Tags