public class URLFileSystem
extends java.lang.Object
URLFileSystem
class handles some of the most common
functionallity when working with URLs.Constructor and Description |
---|
URLFileSystem() |
Modifier and Type | Method and Description |
---|---|
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 void |
copyDir(java.io.File src,
java.io.File dst)
Allows for the copying of entire directories/sub-dirs and their files.
|
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) |
static java.lang.String |
getContents(java.io.InputStream is) |
static java.lang.String |
getContents(java.net.URL 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.io.File file)
If a dot ('.') occurs in the path portion of the
File , then
all of the text starting at the last dot is returned, including
the dot. |
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 void |
main(java.lang.String[] args) |
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) |
public static void main(java.lang.String[] args)
public static java.lang.String getContents(java.net.URL url)
public static java.lang.String getContents(java.io.InputStream is)
public static java.lang.String getContents(java.io.File file)
public static void copy(java.net.URL src, java.io.File dst) throws java.io.IOException
src
to dst
.src
- URL to copy to local file.dst
- File to pull information from to copy to.java.io.IOException
- if there is an error during copy.public static void copy(java.io.InputStream in, java.io.OutputStream out) throws java.io.IOException
in
- Source streamout
- Destination streamjava.io.IOException
- if there is an error during copy.public static java.lang.String getSuffix(java.net.URL url)
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.url
- the URL.public static java.lang.String getSuffix(java.io.File file)
File
, 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.file
- the File.public java.net.URL canonicalize(java.net.URL url)
URL
, if one is available.
The default implementation just returns the specified URL
as-is.url
- URL to convert.public boolean canRead(java.net.URL url)
URL
.url
- URL to check if we can read from it.true
if and only if the specified
URL
points to a resource that exists and can be
read by the application; false
otherwise.public boolean canWrite(java.net.URL url)
URL
.url
- URL to check if we can write to.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.public boolean canCreate(java.net.URL url)
URL
.url
- URL to check if we can create things at.true
if the resource at the specified URL
exists or can be created; false
otherwise.public boolean isValid(java.net.URL url)
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.url
- URL to check for validity.true
if the URL
is valid.public static boolean exists(java.net.URL url)
true
if the specified URL
points to a
resource that currently exists; returns false
otherwise.The default implementation simply returns
false
without doing anything.url
- URL to test for existancepublic static boolean mkdirs(java.net.URL url)
public static java.lang.String getFileName(java.net.URL url)
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
.url
- URL of resource to get filename ofpublic long getLength(java.net.URL url)
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.url
- URL to get length of resource ofURLConnection
public static java.lang.String getName(java.net.URL url)
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.url
- URL of resourcepublic java.lang.String getPath(java.net.URL url)
URL
.
The default implementation delegates to URL.getPath()
.url
- URL of resourcepublic java.lang.String getPathNoExt(java.net.URL url)
URL
without the last file
extension. To clarify, the following examples demonstrate the
different cases that come up:
URL |
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 |
/ |
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.url
- URL of resourcepublic java.lang.String getPlatformPathName(java.net.URL url)
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.url
- URL of resourceURL
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.public static java.net.URL newFileURL(java.io.File file)
public static java.net.URL newFileURL(java.lang.String filePath)
public static java.net.URL newURL(java.lang.String protocol, java.lang.String path)
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)
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.protocol
- Protocol portion of uriuserinfo
- Username/Password portion of urihost
- Host portion of uriport
- Port portion of uripath
- Path portion of uriquery
- Query portion of uriref
- Ref portion of uripublic static java.io.File url2File(java.net.URL url)
public static java.net.URL getParent(java.net.URL url)
public static void copyDir(java.io.File src, java.io.File dst) throws java.io.IOException
src
- the root directory to copy.dst
- the destination directory.java.io.IOException
- thrown if there is an issue copying over the files.