📄 resource.java
字号:
URL url=Resource.class.getResource(name); if (url==null) { try { url=Loader.getResource(Resource.class,name,checkParents); } catch(ClassNotFoundException e) { url=ClassLoader.getSystemResource(name); } } if (url==null) return null; return newResource(url,useCaches); } /* ------------------------------------------------------------ */ protected void finalize() { release(); } /* ------------------------------------------------------------ */ /** Release any resources held by the resource. */ public abstract void release(); /* ------------------------------------------------------------ */ /** * Returns true if the respresened resource exists. */ public abstract boolean exists(); /* ------------------------------------------------------------ */ /** * Returns true if the respresenetd resource is a container/directory. * If the resource is not a file, resources ending with "/" are * considered directories. */ public abstract boolean isDirectory(); /* ------------------------------------------------------------ */ /** * Returns the last modified time */ public abstract long lastModified(); /* ------------------------------------------------------------ */ /** * Return the length of the resource */ public abstract long length(); /* ------------------------------------------------------------ */ /** * Returns an URL representing the given resource */ public abstract URL getURL(); /* ------------------------------------------------------------ */ /** * Returns an File representing the given resource or NULL if this * is not possible. */ public abstract File getFile() throws IOException; /* ------------------------------------------------------------ */ /** * Returns the name of the resource */ public abstract String getName(); /* ------------------------------------------------------------ */ /** * Returns an input stream to the resource */ public abstract InputStream getInputStream() throws java.io.IOException; /* ------------------------------------------------------------ */ /** * Returns an output stream to the resource */ public abstract OutputStream getOutputStream() throws java.io.IOException, SecurityException; /* ------------------------------------------------------------ */ /** * Deletes the given resource */ public abstract boolean delete() throws SecurityException; /* ------------------------------------------------------------ */ /** * Rename the given resource */ public abstract boolean renameTo( Resource dest) throws SecurityException; /* ------------------------------------------------------------ */ /** * Returns a list of resource names contained in the given resource * The resource names are not URL encoded. */ public abstract String[] list(); /* ------------------------------------------------------------ */ /** * Returns the resource contained inside the current resource with the * given name. * @param path The path segment to add, which should be encoded by the * encode method. */ public abstract Resource addPath(String path) throws IOException,MalformedURLException; /* ------------------------------------------------------------ */ /** Encode according to this resource type. * The default implementation calls URI.encodePath(uri) * @param uri * @return String encoded for this resource type. */ public String encode(String uri) { return URIUtil.encodePath(uri); } /* ------------------------------------------------------------ */ public Object getAssociate() { return _associate; } /* ------------------------------------------------------------ */ public void setAssociate(Object o) { _associate=o; } /* ------------------------------------------------------------ */ /** * @return The canonical Alias of this resource or null if none. */ public URL getAlias() { return null; } /* ------------------------------------------------------------ */ /** Get the resource list as a HTML directory listing. * @param base The base URL * @param parent True if the parent directory should be included * @return String of HTML */ public String getListHTML(String base, boolean parent) throws IOException { if (!isDirectory()) return null; String[] ls = list(); if (ls==null) return null; Arrays.sort(ls); String decodedBase = URIUtil.decodePath(base); String title = "Directory: "+decodedBase; StringBuffer buf=new StringBuffer(4096); buf.append("<HTML><HEAD><TITLE>"); buf.append(title); buf.append("</TITLE></HEAD><BODY>\n<H1>"); buf.append(title); buf.append("</H1><TABLE BORDER=0>"); if (parent) { buf.append("<TR><TD><A HREF="); buf.append(URIUtil.addPaths(base,"../")); buf.append(">Parent Directory</A></TD><TD></TD><TD></TD></TR>\n"); } DateFormat dfmt=DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM); for (int i=0 ; i< ls.length ; i++) { String encoded=URIUtil.encodePath(ls[i]); Resource item = addPath(ls[i]); buf.append("<TR><TD><A HREF=\""); String path=URIUtil.addPaths(base,encoded); if (item.isDirectory() && !path.endsWith("/")) path=URIUtil.addPaths(path,URIUtil.SLASH); buf.append(path); buf.append("\">"); buf.append(StringUtil.replace(StringUtil.replace(ls[i],"<","<"),">",">")); buf.append(" "); buf.append("</TD><TD ALIGN=right>"); buf.append(item.length()); buf.append(" bytes </TD><TD>"); buf.append(dfmt.format(new Date(item.lastModified()))); buf.append("</TD></TR>\n"); } buf.append("</TABLE>\n"); buf.append("</BODY></HTML>\n"); return buf.toString(); } /* ------------------------------------------------------------ */ /** * @param out * @param start First byte to write * @param count Bytes to write or -1 for all of them. */ public void writeTo(OutputStream out,long start,long count) throws IOException { InputStream in = getInputStream(); try { in.skip(start); if (count<0) IO.copy(in,out); else IO.copy(in,out,count); } finally { in.close(); } } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -