public final class NetUtil extends Object
The GNU Lesser General Public License for details.
Modifier and Type | Class and Description |
---|---|
static class |
NetUtil.IsDirectoryURIFilter
Check that the directories in the version directory really represent
versions.
|
static class |
NetUtil.URIFilterFilenameFilter
Quick implementation of FilenameFilter that uses a URIFilter
|
Modifier and Type | Field and Description |
---|---|
static String |
AUTH_SEPERATOR_PASSWORD
Separating the password from the username
|
static String |
AUTH_SEPERATOR_USERNAME
Separating the username from the rest of the URL/URI
|
private static File |
cachedir
Where are temporary files cached.
|
static String |
INDEX_FILE
For directory listings
|
private static org.slf4j.Logger |
LOGGER
The log stream
|
static String |
PROTOCOL_FILE
Constant for the file: protocol or scheme
|
static String |
PROTOCOL_FTP
Constant for the ftp: protocol or scheme
|
static String |
PROTOCOL_HTTP
Constant for the http: protocol or scheme
|
static String |
PROTOCOL_JAR
Constant for the jar: protocol or scheme
|
static String |
SEPARATOR
URL/URI separator
|
private static String |
TEMP_SUFFIX
The temporary suffix, used when a temporary file is needed in the
system's temporary directory.
|
Modifier | Constructor and Description |
---|---|
private |
NetUtil()
Basic constructor - ensure that we can't be instantiated
|
Modifier and Type | Method and Description |
---|---|
static boolean |
canRead(URI orig)
If there is a readable directory or file at the other end of this URI
return true.
|
static boolean |
canWrite(URI orig)
If there is a writable directory or file at the other end of this URI
return true.
|
private static void |
checkFileURI(URI uri)
Throw if the given URI does not use the 'file:' protocol
|
static URI |
copy(URI uri) |
static boolean |
delete(URI orig)
Delete a URI.
|
static File |
getAsFile(URI uri)
Return a File from the URI either by extracting from a file: URI or by
downloading to a temp dir first
|
static InputStream |
getInputStream(URI uri)
Attempt to obtain an InputStream from a URI.
|
static long |
getLastModified(URI uri)
When was the given URI last modified.
|
static long |
getLastModified(URI uri,
String proxyHost)
When was the given URI last modified.
|
static long |
getLastModified(URI uri,
String proxyHost,
Integer proxyPort)
When was the given URI last modified.
|
static OutputStream |
getOutputStream(URI uri)
Attempt to obtain an OutputStream from a URI.
|
static OutputStream |
getOutputStream(URI uri,
boolean append)
Attempt to obtain an OutputStream from a URI.
|
static int |
getSize(URI uri) |
static int |
getSize(URI uri,
String proxyHost) |
static int |
getSize(URI uri,
String proxyHost,
Integer proxyPort) |
static URI |
getTemporaryURI(String prefix,
String suffix)
A URI version of
File.createTempFile() |
static URI |
getURI(File file)
Get a URI version of the given file.
|
static File |
getURICacheDir()
Returns the cache directory.
|
static boolean |
isDirectory(URI orig)
If there is a directory at the other end of this URI return true.
|
static boolean |
isFile(URI uri)
If there is a file at the other end of this URI return true.
|
static boolean |
isNewer(URI left,
URI right)
Returns whether the left is newer than the right by comparing their last
modified dates.
|
static boolean |
isNewer(URI left,
URI right,
String proxyHost)
Returns whether the left is newer than the right by comparing their last
modified dates.
|
static boolean |
isNewer(URI left,
URI right,
String proxyHost,
Integer proxyPort)
Returns whether the left is newer than the right by comparing their last
modified dates.
|
private static boolean |
isSeparator(char c) |
static URI |
lengthenURI(URI orig,
String anExtra)
Utility to add a string to the end of a URI.
|
static String[] |
list(URI uri,
URIFilter filter)
List the items available assuming that this URI points to a directory.
|
static String[] |
listByFile(URI uri,
URIFilter filter)
List all the files specified by the index file passed in.
|
static String[] |
listByIndexFile(URI index)
List all the strings specified by the index file passed in.
|
static String[] |
listByIndexFile(URI index,
URIFilter filter)
List all the files specified by the index file passed in.
|
static PropertyMap |
loadProperties(URI uri)
Load up properties given by a URI.
|
static void |
makeDirectory(URI orig)
If the directory does not exist, create it.
|
static void |
makeFile(URI orig)
If the file does not exist, create it.
|
static boolean |
move(URI oldUri,
URI newUri)
Move a URI from one place to another.
|
static void |
setURICacheDir(File cachedir)
Sets the cache directory.
|
static URI |
shortenURI(URI orig,
String strip)
Utility to strip a string from the end of a URI.
|
static void |
storeProperties(PropertyMap properties,
URI uri,
String title)
Store the properties at the location given by the uri using the supplied
title.
|
static URI |
toURI(URL url)
Convert an URL to an URI.
|
static URL |
toURL(URI uri)
Convert an URI to an URL.
|
public static final String PROTOCOL_FILE
public static final String PROTOCOL_HTTP
public static final String PROTOCOL_FTP
public static final String PROTOCOL_JAR
public static final String INDEX_FILE
public static final String SEPARATOR
public static final String AUTH_SEPERATOR_USERNAME
public static final String AUTH_SEPERATOR_PASSWORD
private static final String TEMP_SUFFIX
private static File cachedir
private static final org.slf4j.Logger LOGGER
private NetUtil()
public static void makeDirectory(URI orig) throws MalformedURLException
orig
- The directory URI to createMalformedURLException
- If the URI is not validpublic static void makeFile(URI orig) throws MalformedURLException, IOException
orig
- The file URI to createMalformedURLException
- If the URI is not validIOException
- a problem with I/O happenedpublic static boolean isFile(URI uri)
uri
- The URI to checkpublic static boolean isDirectory(URI orig)
orig
- The URI to checkpublic static boolean canWrite(URI orig)
orig
- The URI to checkpublic static boolean canRead(URI orig)
orig
- The URI to checkpublic static boolean move(URI oldUri, URI newUri) throws IOException
oldUri
- The URI to movenewUri
- The destination URIIOException
- a problem with I/O happenedpublic static boolean delete(URI orig) throws IOException
orig
- The URI to deleteIOException
- a problem with I/O happenedpublic static File getAsFile(URI uri) throws IOException
uri
- The original URI to the file.IOException
- a problem with I/O happenedpublic static URI shortenURI(URI orig, String strip) throws MalformedURLException
orig
- The URI to stripstrip
- The text to strip from the end of the URIMalformedURLException
- If the URI does not end in the given textpublic static URI lengthenURI(URI orig, String anExtra)
orig
- The URI to lengthenanExtra
- The text to add to the end of the URIprivate static boolean isSeparator(char c)
public static InputStream getInputStream(URI uri) throws IOException
uri
- The URI to attempt to read fromIOException
- a problem with I/O happenedpublic static OutputStream getOutputStream(URI uri) throws IOException
uri
- The URI to attempt to write toIOException
- a problem with I/O happenedpublic static OutputStream getOutputStream(URI uri, boolean append) throws IOException
uri
- The URI to attempt to write toappend
- Do we write to the end of the file instead of the beginningIOException
- a problem with I/O happenedpublic static String[] list(URI uri, URIFilter filter) throws MalformedURLException, IOException
There are 2 methods of calculating the answer - if the URI is a file: URI then we can just use File.list(), otherwise we ask for a file inside the directory called index.txt and assume the directories contents to be listed one per line.
If the URI is a file: URI then we execute both methods and warn if there is a difference, but returning the values from the index.txt method.
uri
- the URI to listfilter
- the filter for the listingMalformedURLException
- If the URI is not validIOException
- a problem with I/O happenedpublic static String[] listByFile(URI uri, URIFilter filter) throws MalformedURLException
uri
- the URI to listfilter
- the filter for the listingMalformedURLException
- If the URI is not validpublic static String[] listByIndexFile(URI index) throws IOException
index
- the URI to listIOException
- a problem with I/O happenedpublic static String[] listByIndexFile(URI index, URIFilter filter) throws IOException
Each line is pre-processed:
index
- the URI to listfilter
- the filter for the listingIOException
- a problem with I/O happenedpublic static PropertyMap loadProperties(URI uri) throws IOException
uri
- the location of the propertiesIOException
- a problem with I/O happenedpublic static void storeProperties(PropertyMap properties, URI uri, String title) throws IOException
properties
- the properties to storeuri
- the location of the storetitle
- the label held in the properties fileIOException
- a problem with I/O happenedpublic static int getSize(URI uri)
uri
- the resource whose size is wantedpublic static long getLastModified(URI uri)
uri
- the URI to examinepublic static long getLastModified(URI uri, String proxyHost)
uri
- the URI to examineproxyHost
- the proxy hostpublic static long getLastModified(URI uri, String proxyHost, Integer proxyPort)
uri
- the URI to examineproxyHost
- the proxy hostproxyPort
- the proxy portpublic static boolean isNewer(URI left, URI right)
left
- one URI to compareright
- the other URI to comparepublic static boolean isNewer(URI left, URI right, String proxyHost)
left
- one URI to compareright
- the other URI to compareproxyHost
- the proxy hostpublic static boolean isNewer(URI left, URI right, String proxyHost, Integer proxyPort)
left
- one URI to compareright
- the other URI to compareproxyHost
- the proxy hostproxyPort
- the proxy portprivate static void checkFileURI(URI uri) throws MalformedURLException
uri
- The URI to checkMalformedURLException
- If the protocol is not file:public static File getURICacheDir()
public static void setURICacheDir(File cachedir)
cachedir
- The cache directory to setpublic static URI getURI(File file)
file
- The File to turn into a URIpublic static URI getTemporaryURI(String prefix, String suffix) throws IOException
File.createTempFile()
prefix
- the prefix of the temporary filesuffix
- the suffix of the temporary fileIOException
- If something goes wrong creating the temp URIpublic static URI toURI(URL url)
url
- to convert