📄 urlfilesystem.java
字号:
/**
* $RCSfile$
* $Revision: 18470 $
* $Date: 2005-02-15 19:34:51 -0800 (Tue, 15 Feb 2005) $
*
* Copyright (C) 2004-2008 Jive Software. All rights reserved.
*
* This software is published under the terms of the GNU Public License (GPL),
* a copy of which is included in this distribution, or a commercial license
* agreement with Jive.
*/
package org.jivesoftware.xmpp.workgroup.utils;
import org.xmpp.component.ComponentManagerFactory;
import java.io.*;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLConnection;
/**
* <code>URLFileSystem</code> class handles some of the most common
* functionallity when working with URLs.
*
* @version 1.0, 03/12/14
*/
public class URLFileSystem {
public static void main(String args[]) {
}
/**
* Copies the contents at <CODE>src</CODE> to <CODE>dst</CODE>.
*/
public static void copy(URL src, File dst) throws IOException {
InputStream in = null;
OutputStream out = null;
try {
in = src.openStream();
out = new FileOutputStream(dst);
dst.mkdirs();
copy(in, out);
}
finally {
try {
if (in != null) in.close();
}
catch (IOException e) {
}
try {
if (out != null) out.close();
}
catch (IOException e) {
}
}
}
/**
* Common code for copy routines. By convention, the streams are
* closed in the same method in which they were opened. Thus,
* this method does not close the streams when the copying is done.
*/
private static void copy(InputStream in, OutputStream out)
throws IOException {
final byte[] buffer = new byte[4096];
while (true) {
final int bytesRead = in.read(buffer);
if (bytesRead < 0) {
break;
}
out.write(buffer, 0, bytesRead);
}
}
/**
* If a dot ('.') occurs in the path portion of the {@link URL}, then
* all of the text starting at the last dot is returned, including
* the dot. If the last dot is also the last character in the path,
* then the dot by itself is returned. If there is no dot in the
* path, then the empty string is returned.
*/
public static String getSuffix(URL url) {
final String path = url.getPath();
int lastDot = path.lastIndexOf('.');
return (lastDot >= 0) ? path.substring(lastDot) : "";
}
//--------------------------------------------------------------------------
// URLFileSystemHelper public API...
//--------------------------------------------------------------------------
/**
* Returns a canonical form of the {@link URL}, if one is available.
* <P>
* <p/>
* The default implementation just returns the specified {@link URL}
* as-is.
*/
public URL canonicalize(URL url) {
return url;
}
/**
* Tests whether the application can read the resource at the
* specified {@link URL}.
*
* @return <CODE>true</CODE> if and only if the specified
* {@link URL} points to a resource that exists <EM>and</EM> can be
* read by the application; <CODE>false</CODE> otherwise.
*/
public boolean canRead(URL url) {
try {
final URLConnection urlConnection = url.openConnection();
return urlConnection.getDoInput();
}
catch (Exception e) {
return false;
}
}
/**
* Tests whether the application can modify the resource at the
* specified {@link URL}.
*
* @return <CODE>true</CODE> if and only if the specified
* {@link URL} points to a file that exists <EM>and</EM> the
* application is allowed to write to the file; <CODE>false</CODE>
* otherwise.
*/
public boolean canWrite(URL url) {
try {
final URLConnection urlConnection = url.openConnection();
return urlConnection.getDoOutput();
}
catch (Exception e) {
return false;
}
}
/**
* Tests whether the application can create the resource at the specified
* {@link URL}.
*
* @return <CODE>true</CODE> if the resource at the specified {@link URL}
* exists or can be created; <CODE>false</CODE> otherwise.
*/
public boolean canCreate(URL url) {
return true;
}
/**
* Tests whether the specified {@link URL} is valid. If the resource
* pointed by the {@link URL} exists the method returns <CODE>true</CODE>.
* If the resource does not exist, the method tests that all components
* of the path can be created.
*
* @return <CODE>true</CODE> if the {@link URL} is valid.
*/
public boolean isValid(URL url) {
if (exists(url)) {
return true;
}
return canCreate(url);
}
/**
* Returns <CODE>true</CODE> if the specified {@link URL} points to a
* resource that currently exists; returns <CODE>false</CODE>
* otherwise.<P>
* <p/>
* The default implementation simply returns <CODE>false</CODE>
* without doing anything.
*/
public static boolean exists(URL url) {
return url2File(url).exists();
}
public static boolean mkdirs(URL url) {
final File file = url2File(url);
if (!file.exists()) {
return file.mkdirs();
}
return true;
}
/**
* Returns the name of the file contained by the {@link URL}, not
* including any protocol, hostname authentication, directory path,
* anchor, or query. This simply returns the simple filename. For
* example, if you pass in an {@link URL} whose string representation
* is:
* <p/>
* <BLOCKQUOTE><CODE>
* protocol://host:1010/dir1/dir2/file.ext#anchor?query
* </CODE></BLOCKQUOTE>
* <p/>
* the returned value is "<CODE>file.ext</CODE>" (without the
* quotes).<P>
* <p/>
* The returned file name should only be used for display purposes
* and not for opening streams or otherwise trying to locate the
* resource indicated by the {@link URL}.
*/
public static String getFileName(URL url) {
if (url == null) {
return "";
}
final String path = url.getPath();
if (path.equals("/")) {
return "/";
}
final int lastSep = path.lastIndexOf('/');
if (lastSep == path.length() - 1) {
final int lastSep2 = path.lastIndexOf('/', lastSep - 1);
return path.substring(lastSep2 + 1, lastSep);
}
else {
return path.substring(lastSep + 1);
}
}
/**
* Returns the number of bytes contained in the resource that the
* specified {@link URL} points to. If the length cannot be
* determined, <CODE>-1</CODE> is returned.<P>
* <p/>
* The default implementation attempts to get the content length from
* the {@link URLConnection} associated with the {@link URL}. If that
* fails for some reason (e.g. the resource does not exist, there was
* some other an I/O exception, etc.), <CODE>-1</CODE> is returned.
*
* @see URLConnection
*/
public long getLength(URL url) {
try {
final URLConnection urlConnection = url.openConnection();
return urlConnection.getContentLength();
}
catch (Exception e) {
return -1;
}
}
/**
* Returns the name of the file contained by the {@link URL}, not
* including any protocol, hostname authentication, directory path,
* anchor, or query. This simply returns the simple filename. For
* example, if you pass in an {@link URL} whose string representation
* is:
* <p/>
* <BLOCKQUOTE><CODE>
* protocol://host:1010/dir1/dir2/file.ext1.ext2#anchor?query
* </CODE></BLOCKQUOTE>
* <p/>
* the returned value is "<CODE>file</CODE>" (without the quotes).<P>
* <p/>
* The returned file name should only be used for display purposes
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -