📄 resource.java
字号:
/** * 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 { base=URIUtil.canonicalPath(base); if (base==null || !isDirectory()) return null; String[] ls = list(); if (ls==null) return null; Arrays.sort(ls); String decodedBase = URIUtil.decodePath(base); String title = "Directory: "+deTag(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>\n<TABLE BORDER=0>\n"); 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"); } String defangedBase = defangURI(base); DateFormat dfmt=DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM); for (int i=0 ; i< ls.length ; i++) { Resource item = addPath(ls[i]); buf.append("\n<TR><TD><A HREF=\""); String path=URIUtil.addPaths(defangedBase,URIUtil.encodePath(ls[i])); buf.append(path); if (item.isDirectory() && !path.endsWith("/")) buf.append(URIUtil.SLASH); // URIUtil.encodePath(buf,path); buf.append("\">"); buf.append(deTag(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>"); } buf.append("</TABLE>\n"); buf.append("</BODY></HTML>\n"); return buf.toString(); } /** * Defang any characters that could break the URI string in an HREF. * Such as <a href="/path/to;<script>Window.alert("XSS"+'%20'+"here");</script>">Link</a> * * The above example would parse incorrectly on various browsers as the "<" or '"' characters * would end the href attribute value string prematurely. * * @param raw the raw text to defang. * @return the defanged text. */ private static String defangURI(String raw) { StringBuffer buf = null; if (buf==null) { for (int i=0;i<raw.length();i++) { char c=raw.charAt(i); switch(c) { case '\'': case '"': case '<': case '>': buf=new StringBuffer(raw.length()<<1); break; } } if (buf==null) return raw; } for (int i=0;i<raw.length();i++) { char c=raw.charAt(i); switch(c) { case '"': buf.append("%22"); continue; case '\'': buf.append("%27"); continue; case '<': buf.append("%3C"); continue; case '>': buf.append("%3E"); continue; default: buf.append(c); continue; } } return buf.toString(); } private static String deTag(String raw) { return StringUtil.replace( StringUtil.replace(raw,"<","<"), ">", ">"); } /* ------------------------------------------------------------ */ /** * @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 + -