PageRenderTime 48ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/netbeans-7.3/openide.filesystems/src/org/openide/filesystems/FileURL.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 357 lines | 191 code | 55 blank | 111 comment | 25 complexity | 7185633dbfceebeeca39756cd2670c21 MD5 | raw file
  1. /*
  2. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  3. *
  4. * Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved.
  5. *
  6. * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
  7. * Other names may be trademarks of their respective owners.
  8. *
  9. * The contents of this file are subject to the terms of either the GNU
  10. * General Public License Version 2 only ("GPL") or the Common
  11. * Development and Distribution License("CDDL") (collectively, the
  12. * "License"). You may not use this file except in compliance with the
  13. * License. You can obtain a copy of the License at
  14. * http://www.netbeans.org/cddl-gplv2.html
  15. * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
  16. * specific language governing permissions and limitations under the
  17. * License. When distributing the software, include this License Header
  18. * Notice in each file and include the License file at
  19. * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
  20. * particular file as subject to the "Classpath" exception as provided
  21. * by Oracle in the GPL Version 2 section of the License file that
  22. * accompanied this code. If applicable, add the following below the
  23. * License Header, with the fields enclosed by brackets [] replaced by
  24. * your own identifying information:
  25. * "Portions Copyrighted [year] [name of copyright owner]"
  26. *
  27. * Contributor(s):
  28. *
  29. * The Original Software is NetBeans. The Initial Developer of the Original
  30. * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
  31. * Microsystems, Inc. All Rights Reserved.
  32. *
  33. * If you wish your version of this file to be governed by only the CDDL
  34. * or only the GPL Version 2, indicate your decision by adding
  35. * "[Contributor] elects to include this software in this distribution
  36. * under the [CDDL or GPL Version 2] license." If you do not indicate a
  37. * single choice of license, a recipient has the option to distribute
  38. * your version of this file under either the CDDL, the GPL Version 2 or
  39. * to extend the choice of license to its licensees as provided above.
  40. * However, if you add GPL Version 2 code and therefore, elected the GPL
  41. * Version 2 license, then the option applies only if the new code is
  42. * made subject to such option by the copyright holder.
  43. */
  44. package org.openide.filesystems;
  45. import java.io.File;
  46. import java.io.FileNotFoundException;
  47. import java.io.FilePermission;
  48. import java.io.IOException;
  49. import java.io.InputStream;
  50. import java.io.OutputStream;
  51. import java.io.StringReader;
  52. import java.net.InetAddress;
  53. import java.net.URL;
  54. import java.net.URLConnection;
  55. import java.net.URLStreamHandler;
  56. import java.net.UnknownServiceException;
  57. import java.security.Permission;
  58. import org.openide.util.URLStreamHandlerRegistration;
  59. /** Special URL connection directly accessing an internal file object.
  60. *
  61. * @author Ales Novak, Petr Hamernik, Jan Jancura, Jaroslav Tulach
  62. */
  63. class FileURL extends URLConnection {
  64. /** Protocol name for this type of URL. */
  65. public static final String PROTOCOL = "nbfs"; // NOI18N
  66. /** Default implemenatation of handler for this type of URL.
  67. */
  68. @URLStreamHandlerRegistration(protocol=PROTOCOL)
  69. public static class Handler extends URLStreamHandler {
  70. public URLConnection openConnection(URL u) throws IOException {
  71. return new FileURL(u);
  72. }
  73. protected @Override synchronized InetAddress getHostAddress(URL u) {
  74. return null;
  75. }
  76. }
  77. /** 1 URLConnection == 1 InputSteam*/
  78. InputStream iStream = null;
  79. /** 1 URLConnection == 1 OutputSteam*/
  80. OutputStream oStream = null;
  81. /** FileObject that we want to connect to. */
  82. protected FileObject fo;
  83. /**
  84. * Create a new connection to a {@link FileObject}.
  85. * @param u URL of the connection. Please use {@link #encodeFileObject(FileObject)} to create the URL.
  86. */
  87. protected FileURL(URL u) {
  88. super(u);
  89. }
  90. /** Provides a URL to access a file object.
  91. * @param fo the file object
  92. * @return a URL using the correct syntax and {@link #PROTOCOL protocol}
  93. */
  94. public static URL encodeFileObject(FileObject fo) {
  95. return NbfsUtil.getURL(fo);
  96. }
  97. /** Retrieves the file object specified by an internal URL.
  98. * @param u the url to decode
  99. * @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
  100. */
  101. public static FileObject decodeURL(URL u) {
  102. return NbfsUtil.getFileObject(u);
  103. }
  104. /* A method for connecting to a FileObject.
  105. */
  106. public void connect() throws IOException {
  107. if (fo != null) {
  108. return;
  109. }
  110. fo = decodeURL(url);
  111. if (fo == null) {
  112. throw new FileNotFoundException("Cannot find: " + url); // NOI18N
  113. }
  114. }
  115. /*
  116. * @return InputStream or given FileObject.
  117. */
  118. public InputStream getInputStream() throws IOException, UnknownServiceException {
  119. connect();
  120. if (iStream == null) {
  121. try {
  122. if (fo.isFolder()) {
  123. iStream = new FIS(fo);
  124. } else {
  125. iStream = fo.getInputStream();
  126. }
  127. } catch (FileNotFoundException e) {
  128. ExternalUtil.exception(e);
  129. throw e;
  130. }
  131. }
  132. return iStream;
  133. }
  134. /*
  135. * @return OutputStream for given FileObject.
  136. */
  137. public OutputStream getOutputStream() throws IOException, UnknownServiceException {
  138. connect();
  139. if (fo.isFolder()) {
  140. throw new UnknownServiceException();
  141. }
  142. if (oStream == null) {
  143. FileLock flock = fo.lock();
  144. oStream = new LockOS(fo.getOutputStream(flock), flock);
  145. }
  146. return oStream;
  147. }
  148. /*
  149. * @return length of FileObject.
  150. */
  151. public int getContentLength() {
  152. try {
  153. connect();
  154. return (int) fo.getSize();
  155. } catch (IOException ex) {
  156. return 0;
  157. }
  158. }
  159. /** Get a header field (currently, content type only).
  160. * @param name the header name. Only <code>content-type</code> is guaranteed to be present.
  161. * @return the value (i.e., MIME type)
  162. */
  163. public String getHeaderField(String name) {
  164. if (name.equalsIgnoreCase("content-type")) { // NOI18N
  165. try {
  166. connect();
  167. if (fo.isFolder()) {
  168. return "text/html"; // NOI18N
  169. } else {
  170. return fo.getMIMEType();
  171. }
  172. } catch (IOException e) {
  173. }
  174. }
  175. return super.getHeaderField(name);
  176. }
  177. public @Override long getHeaderFieldDate(String name, long Default) {
  178. if (name.equalsIgnoreCase("last-modified")) { // NOI18N
  179. try {
  180. connect();
  181. return fo.lastModified().getTime();
  182. } catch (IOException e) {
  183. }
  184. }
  185. return super.getHeaderFieldDate(name, Default);
  186. }
  187. // #13038: URLClassPath is going to check this.
  188. // Better not return AllPermission!
  189. // SocketPermission on localhost might also work.
  190. public Permission getPermission() throws IOException {
  191. // Note this is normally called by URLClassPath with an unconnected
  192. // URLConnection, so the fo will probably be null anyway.
  193. if (fo != null) {
  194. File f = FileUtil.toFile(fo);
  195. if (f != null) {
  196. return new FilePermission(f.getAbsolutePath(), "read"); // NOI18N
  197. }
  198. try {
  199. FileSystem fs = fo.getFileSystem();
  200. if (fs instanceof JarFileSystem) {
  201. return new FilePermission(((JarFileSystem) fs).getJarFile().getAbsolutePath(), "read"); // NOI18N
  202. }
  203. // [PENDING] could do XMLFileSystem too...
  204. } catch (FileStateInvalidException fsie) {
  205. // ignore
  206. }
  207. }
  208. // fallback
  209. return new FilePermission("<<ALL FILES>>", "read"); // NOI18N
  210. }
  211. /** Stream that also closes the lock, if closed.
  212. */
  213. private static class LockOS extends java.io.BufferedOutputStream {
  214. /** lock */
  215. private FileLock flock;
  216. /**
  217. * @param os is an OutputStream for writing in
  218. * @param lock is a lock for the stream
  219. */
  220. public LockOS(OutputStream os, FileLock lock) throws IOException {
  221. super(os);
  222. flock = lock;
  223. }
  224. /** overriden */
  225. public void close() throws IOException {
  226. flock.releaseLock();
  227. super.close();
  228. }
  229. }
  230. /** The class allows reading of folder via URL. Because of html
  231. * oriented user interface the document has html format.
  232. *
  233. * @author Ales Novak
  234. * @version 0.10 May 15, 1998
  235. */
  236. private static final class FIS extends InputStream {
  237. /** delegated reader that reads the document */
  238. private StringReader reader;
  239. /**
  240. * @param folder is a folder
  241. */
  242. public FIS(FileObject folder) throws IOException {
  243. reader = new StringReader(createDocument(folder));
  244. }
  245. /** creates html document as string */
  246. private String createDocument(FileObject folder)
  247. throws IOException {
  248. StringBuffer buff = new StringBuffer(150);
  249. StringBuffer lit = new StringBuffer(15);
  250. FileObject[] fobia = folder.getChildren();
  251. String name;
  252. buff.append("<HTML>\n"); // NOI18N
  253. buff.append("<BODY>\n"); // NOI18N
  254. FileObject parent = folder.getParent();
  255. if (parent != null) {
  256. // lit.setLength(0);
  257. // lit.append('/').append(parent.getPackageName('/'));
  258. buff.append("<P>"); // NOI18N
  259. buff.append("<A HREF=").append("..").append(">").append("..").append("</A>").append("\n"); // NOI18N
  260. buff.append("</P>"); // NOI18N
  261. }
  262. for (int i = 0; i < fobia.length; i++) {
  263. lit.setLength(0);
  264. lit.append(fobia[i].getNameExt());
  265. name = lit.toString();
  266. if (fobia[i].isFolder()) {
  267. lit.append('/'); // NOI18N
  268. }
  269. buff.append("<P>"); // NOI18N
  270. buff.append("<A HREF=").append((Object) lit).append(">").append(name).append("</A>").append("\n"); // NOI18N
  271. buff.append("</P>"); // NOI18N
  272. }
  273. buff.append("</BODY>\n"); // NOI18N
  274. buff.append("</HTML>\n"); // NOI18N
  275. return buff.toString();
  276. }
  277. //************************************** stream methods **********
  278. public int read() throws IOException {
  279. return reader.read();
  280. }
  281. public int read(byte[] b, int off, int len) throws IOException {
  282. char[] ch = new char[len];
  283. int r = reader.read(ch, 0, len);
  284. for (int i = 0; i < r; i++)
  285. b[off + i] = (byte) ch[i];
  286. return r;
  287. }
  288. public long skip(long skip) throws IOException {
  289. return reader.skip(skip);
  290. }
  291. public void close() throws IOException {
  292. reader.close();
  293. }
  294. public void reset() throws IOException {
  295. reader.reset();
  296. }
  297. public boolean markSupported() {
  298. return false;
  299. }
  300. }
  301. // end of FIS
  302. }