org.jivesoftware.spark.util
Class URLFileSystem

java.lang.Object
  extended by org.jivesoftware.spark.util.URLFileSystem

public class URLFileSystem
extends java.lang.Object

URLFileSystem class handles some of the most common functionallity when working with URLs.

Version:
1.0, 03/12/14

Constructor Summary
URLFileSystem()
           
 
Method Summary
 boolean canCreate(java.net.URL url)
          Tests whether the application can create the resource at the specified URL.
 java.net.URL canonicalize(java.net.URL url)
          Returns a canonical form of the URL, if one is available.
 boolean canRead(java.net.URL url)
          Tests whether the application can read the resource at the specified URL.
 boolean canWrite(java.net.URL url)
          Tests whether the application can modify the resource at the specified URL.
static void copy(java.io.InputStream in, java.io.OutputStream out)
          Common code for copy routines.
static void copy(java.net.URL src, java.io.File dst)
          Copies the contents at src to dst.
static boolean exists(java.net.URL url)
          Returns true if the specified URL points to a resource that currently exists; returns false otherwise.
static java.lang.String getContents(java.io.File file)
          Returns the contents of a file.
static java.lang.String getContents(java.io.InputStream is)
           
static java.lang.String getContents(java.net.URL url)
          Returns the contents of a given URL.
static java.lang.String getFileName(java.net.URL url)
          Returns the name of the file contained by the URL, not including any protocol, hostname authentication, directory path, anchor, or query.
 long getLength(java.net.URL url)
          Returns the number of bytes contained in the resource that the specified URL points to.
static java.lang.String getName(java.net.URL url)
          Returns the name of the file contained by the URL, not including any protocol, hostname authentication, directory path, anchor, or query.
static java.net.URL getParent(java.net.URL url)
           
 java.lang.String getPath(java.net.URL url)
          Returns the path part of the URL.
 java.lang.String getPathNoExt(java.net.URL url)
          Returns the path part of the URL without the last file extension.
 java.lang.String getPlatformPathName(java.net.URL url)
          Returns the platform-dependent String representation of the URL; the returned string should be considered acceptable for users to read.
static java.lang.String getSuffix(java.net.URL url)
          If a dot ('.') occurs in the path portion of the URL, then all of the text starting at the last dot is returned, including the dot.
 boolean isValid(java.net.URL url)
          Tests whether the specified URL is valid.
static boolean mkdirs(java.net.URL url)
           
static java.net.URL newFileURL(java.io.File file)
           
static java.net.URL newFileURL(java.lang.String filePath)
           
static java.net.URL newURL(java.lang.String protocol, java.lang.String path)
           
static java.net.URL newURL(java.lang.String protocol, java.lang.String userinfo, java.lang.String host, int port, java.lang.String path, java.lang.String query, java.lang.String ref)
          Creates a new URL whose parts have the exact values that are specified.
static java.io.File url2File(java.net.URL url)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

URLFileSystem

public URLFileSystem()
Method Detail

getContents

public static java.lang.String getContents(java.net.URL url)
Returns the contents of a given URL.

Parameters:
url - the url.
Returns:
the string contents of a url.

getContents

public static java.lang.String getContents(java.io.File file)
Returns the contents of a file.

Parameters:
file - the file whose contents you wish to retrieve.
Returns:
the string contents of a file.

getContents

public static java.lang.String getContents(java.io.InputStream is)

copy

public static void copy(java.net.URL src,
                        java.io.File dst)
                 throws java.io.IOException
Copies the contents at src to dst.

Throws:
java.io.IOException

copy

public static void copy(java.io.InputStream in,
                        java.io.OutputStream out)
                 throws java.io.IOException
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.

Throws:
java.io.IOException

getSuffix

public static java.lang.String getSuffix(java.net.URL url)
If a dot ('.') occurs in the path portion of the 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.


canonicalize

public java.net.URL canonicalize(java.net.URL url)
Returns a canonical form of the URL, if one is available.

The default implementation just returns the specified URL as-is.


canRead

public boolean canRead(java.net.URL url)
Tests whether the application can read the resource at the specified URL.

Returns:
true if and only if the specified URL points to a resource that exists and can be read by the application; false otherwise.

canWrite

public boolean canWrite(java.net.URL url)
Tests whether the application can modify the resource at the specified URL.

Returns:
true if and only if the specified URL points to a file that exists and the application is allowed to write to the file; false otherwise.

canCreate

public boolean canCreate(java.net.URL url)
Tests whether the application can create the resource at the specified URL.

Returns:
true if the resource at the specified URL exists or can be created; false otherwise.

isValid

public boolean isValid(java.net.URL url)
Tests whether the specified URL is valid. If the resource pointed by the URL exists the method returns true. If the resource does not exist, the method tests that all components of the path can be created.

Returns:
true if the URL is valid.

exists

public static boolean exists(java.net.URL url)
Returns true if the specified URL points to a resource that currently exists; returns false otherwise.

The default implementation simply returns false without doing anything.


mkdirs

public static boolean mkdirs(java.net.URL url)

getFileName

public static java.lang.String getFileName(java.net.URL url)
Returns the name of the file contained by the 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 URL whose string representation is:

protocol://host:1010/dir1/dir2/file.ext#anchor?query

the returned value is "file.ext" (without the quotes).

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 URL.


getLength

public long getLength(java.net.URL url)
Returns the number of bytes contained in the resource that the specified URL points to. If the length cannot be determined, -1 is returned.

The default implementation attempts to get the content length from the URLConnection associated with the URL. If that fails for some reason (e.g. the resource does not exist, there was some other an I/O exception, etc.), -1 is returned.

See Also:
URLConnection

getName

public static java.lang.String getName(java.net.URL url)
Returns the name of the file contained by the 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 URL whose string representation is:

protocol://host:1010/dir1/dir2/file.ext1.ext2#anchor?query

the returned value is "file" (without the quotes).

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 URL.

The default implementation first calls getFileName(URL) to get the file name part. Then all characters starting with the first occurrence of '.' are removed. The remaining string is then returned.


getPath

public java.lang.String getPath(java.net.URL url)
Returns the path part of the URL.

The default implementation delegates to URL.getPath().


getPathNoExt

public java.lang.String getPathNoExt(java.net.URL url)
Returns the path part of the URL without the last file extension. To clarify, the following examples demonstrate the different cases that come up:

Path part of input URL
Output String
/dir/file.ext /dir/file
/dir/file.ext1.ext2 /dir/file.ext1
/dir1.ext1/dir2.ext2/file.ext1.ext2 /dir1.ext1/dir2.ext2/file.ext1
/file.ext /file
/dir.ext/file /dir.ext/file
/dir/file /dir/file
/file /file
/.ext /

The default implementation gets the path from getPath(URL) and then trims off all of the characters beginning with the last "." in the path, if and only if the last "." comes after the last "/" in the path. If the last "." comes before the last "/" or if there is no "." at all, then the entire path is returned.


getPlatformPathName

public java.lang.String getPlatformPathName(java.net.URL url)
Returns the platform-dependent String representation of the URL; the returned string should be considered acceptable for users to read. In general, the returned string should omit as many parts of the URL as possible. For the "file" protocol, therefore, the platform pathname should just be the pathname alone (no protocol) using the appropriate file separator character for the current platform. For other protocols, it may be necessary to reformat the URL string into a more human-readable form. That decision is left to each URLFileSystemHelper implementor.

The default implementation returns url.toString(). If the URL is null, the empty string is returned.

Returns:
The path portion of the specified URL in platform-dependent notation. This value should only be used for display purposes and not for opening streams or otherwise trying to locate the document.

newFileURL

public static java.net.URL newFileURL(java.io.File file)

newFileURL

public static java.net.URL newFileURL(java.lang.String filePath)

newURL

public static java.net.URL newURL(java.lang.String protocol,
                                  java.lang.String path)

newURL

public static java.net.URL newURL(java.lang.String protocol,
                                  java.lang.String userinfo,
                                  java.lang.String host,
                                  int port,
                                  java.lang.String path,
                                  java.lang.String query,
                                  java.lang.String ref)
Creates a new URL whose parts have the exact values that are specified. In general, you should avoid calling this method directly.

This method is the ultimate place where all of the other URLFactory methods end up when creating an URL.

Non-sanitizing.


url2File

public static final java.io.File url2File(java.net.URL url)

getParent

public static java.net.URL getParent(java.net.URL url)