org.crosswire.common.util
Class NetUtil

java.lang.Object
  extended by org.crosswire.common.util.NetUtil

public final class NetUtil
extends Object

The NetUtil class looks after general utility stuff around the java.net package.

Author:
Joe Walker [joe at eireneh dot com], Mark Goodwin [goodwinster at gmail dot com], DM Smith [dmsmith555 at yahoo dot com]
See Also:
for license details.
The copyright to this program is held by it's authors.

Nested Class Summary
static class NetUtil.IsDirectoryURLFilter
          Check that the directories in the version directory really represent versions.
static class NetUtil.URLFilterFilenameFilter
          Quick implementation of FilenameFilter that uses a URLFilter
 
Field Summary
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
private static File cachedir
          Where are temporary files cached.
static String INDEX_FILE
          For directory listings
private static Logger log
          The log stream
static String PROTOCOL_FILE
          Constant for the file: protocol
static String PROTOCOL_FTP
          Constant for the file: protocol
static String PROTOCOL_HTTP
          Constant for the file: protocol
static String PROTOCOL_JAR
          Constant for the jar: protocol
static String SEPARATOR
          URL separator
private static String TEMP_SUFFIX
          The temporary suffix, used when a temporary file is needed in the system's temporary directory.
 
Constructor Summary
private NetUtil()
          Basic constructor - ensure that we can't be instansiated
 
Method Summary
private static void checkFileURL(URL url)
          Throw if the given URL does not use the 'file:' protocol
static boolean delete(URL orig)
          Delete a URL.
static File getAsFile(URL url)
          Return a File from the URL either by extracting from a file: URL or by downloading to a temp dir first
static long getLastModified(URL url)
          When was the given URL last modified.
static long getLastModified(URL url, String proxyHost)
           
static long getLastModified(URL url, String proxyHost, Integer proxyPort)
           
static OutputStream getOutputStream(URL url)
          Attempt to obtain an OutputStream from a URL.
static OutputStream getOutputStream(URL url, boolean append)
          Attempt to obtain an OutputStream from a URL.
static int getSize(URL url)
           
static int getSize(URL url, String proxyHost)
           
static int getSize(URL url, String proxyHost, Integer proxyPort)
           
static URL getTemporaryURL(String prefix, String suffix)
          A URL version of File.createTempFile()
static URL getURL(File file)
          Get a URL version of the given file.
static File getURLCacheDir()
          Returns the URLCacheDir.
static boolean isDirectory(URL orig)
          If there is a directory at the other end of this URL return true.
static boolean isFile(URL url)
          If there is a file at the other end of this URL return true.
static boolean isNewer(URL left, URL right)
          Returns whether the left is newer than the right by comparing their last modified dates.
static boolean isNewer(URL left, URL right, String proxyHost)
           
static boolean isNewer(URL left, URL right, String proxyHost, Integer proxyPort)
           
private static boolean isSeparator(char c)
           
static URL lengthenURL(URL orig, String anExtra)
          Utility to add a string to the end of a URL.
static String[] list(URL url, URLFilter filter)
          List the items available assuming that this URL points to a directory.
static String[] listByFile(URL url, URLFilter filter)
          List all the files specified by the index file passed in.
static String[] listByIndexFile(URL index, URLFilter filter)
          List all the files specified by the index file passed in.
static void makeDirectory(URL orig)
          If the directory does not exist, create it.
static void makeFile(URL orig)
          If the file does not exist, create it.
static boolean move(URL oldUrl, URL newUrl)
          Move a URL from one place to another.
static void setURLCacheDir(File cachedir)
          Sets the cache directory.
static URL shortenURL(URL orig, String strip)
          Utility to strip a string from the end of a URL.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PROTOCOL_FILE

public static final String PROTOCOL_FILE
Constant for the file: protocol

See Also:
Constant Field Values

PROTOCOL_HTTP

public static final String PROTOCOL_HTTP
Constant for the file: protocol

See Also:
Constant Field Values

PROTOCOL_FTP

public static final String PROTOCOL_FTP
Constant for the file: protocol

See Also:
Constant Field Values

PROTOCOL_JAR

public static final String PROTOCOL_JAR
Constant for the jar: protocol

See Also:
Constant Field Values

INDEX_FILE

public static final String INDEX_FILE
For directory listings

See Also:
Constant Field Values

SEPARATOR

public static final String SEPARATOR
URL separator

See Also:
Constant Field Values

AUTH_SEPERATOR_USERNAME

public static final String AUTH_SEPERATOR_USERNAME
Separating the username from the rest of the URL

See Also:
Constant Field Values

AUTH_SEPERATOR_PASSWORD

public static final String AUTH_SEPERATOR_PASSWORD
Separating the password from the username

See Also:
Constant Field Values

TEMP_SUFFIX

private static final String TEMP_SUFFIX
The temporary suffix, used when a temporary file is needed in the system's temporary directory.

See Also:
Constant Field Values

cachedir

private static File cachedir
Where are temporary files cached.


log

private static final Logger log
The log stream

Constructor Detail

NetUtil

private NetUtil()
Basic constructor - ensure that we can't be instansiated

Method Detail

makeDirectory

public static void makeDirectory(URL orig)
                          throws MalformedURLException
If the directory does not exist, create it. Note this currently only works with file: type URLs

Parameters:
orig - The URL to check
Throws:
MalformedURLException

makeFile

public static void makeFile(URL orig)
                     throws MalformedURLException,
                            IOException
If the file does not exist, create it. Note this currently only works with file: type URLs

Parameters:
orig - The URL to check
Throws:
MalformedURLException
IOException

isFile

public static boolean isFile(URL url)
If there is a file at the other end of this URL return true. Note this currently only works with file: type URLs

Parameters:
url - The URL to check
Returns:
true if the URL points at a file

isDirectory

public static boolean isDirectory(URL orig)
If there is a directory at the other end of this URL return true. Note non file: type URLs will always return false

Parameters:
orig - The URL to check
Returns:
true if the URL points at a file: directory

move

public static boolean move(URL oldUrl,
                           URL newUrl)
                    throws IOException
Move a URL from one place to another. Currently this only works for file: URLs, however the interface should not need to change to handle more complex URLs

Parameters:
oldUrl - The URL to move
newUrl - The desitination URL
Throws:
IOException

delete

public static boolean delete(URL orig)
                      throws IOException
Delete a URL. Currently this only works for file: URLs, however the interface should not need to change to handle more complex URLs

Parameters:
orig - The URL to delete
Throws:
IOException

getAsFile

public static File getAsFile(URL url)
                      throws IOException
Return a File from the URL either by extracting from a file: URL or by downloading to a temp dir first

Parameters:
url - The original URL to the file.
Returns:
The URL as a file
Throws:
IOException

shortenURL

public static URL shortenURL(URL orig,
                             String strip)
                      throws MalformedURLException
Utility to strip a string from the end of a URL.

Parameters:
orig - The URL to strip
strip - The text to strip from the end of the URL
Returns:
The stripped URL
Throws:
MalformedURLException - If the URL does not end in the given text

lengthenURL

public static URL lengthenURL(URL orig,
                              String anExtra)
Utility to add a string to the end of a URL.

Parameters:
orig - The URL to strip
anExtra - The text to add to the end of the URL
Returns:
The stripped URL

isSeparator

private static boolean isSeparator(char c)

getOutputStream

public static OutputStream getOutputStream(URL url)
                                    throws IOException
Attempt to obtain an OutputStream from a URL. The simple case will just call url.openConnection().getOutputStream(), however in some JVMs (MS at least this fails where new FileOutputStream(url) works. So if openConnection().getOutputStream() fails and the protocol is file, then the alternate version is used.

Parameters:
url - The URL to attempt to write to
Returns:
An OutputStream connection
Throws:
IOException

getOutputStream

public static OutputStream getOutputStream(URL url,
                                           boolean append)
                                    throws IOException
Attempt to obtain an OutputStream from a URL. The simple case will just call url.openConnection().getOutputStream(), however in some JVMs (MS at least this fails where new FileOutputStream(url) works. So if openConnection().getOutputStream() fails and the protocol is file, then the alternate version is used.

Parameters:
url - The URL to attempt to write to
append - Do we write to the end of the file instead of the beginning
Returns:
An OutputStream connection
Throws:
IOException

list

public static String[] list(URL url,
                            URLFilter filter)
                     throws MalformedURLException,
                            IOException
List the items available assuming that this URL points to a directory.

There are 2 methods of calculating the answer - if the URL is a file: URL 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 URL is a file: URL then we execute both methods and warn if there is a difference, but returning the values from the index.txt method.

Throws:
MalformedURLException
IOException

listByFile

public static String[] listByFile(URL url,
                                  URLFilter filter)
                           throws MalformedURLException
List all the files specified by the index file passed in.

Returns:
String[] Matching results.
Throws:
MalformedURLException

listByIndexFile

public static String[] listByIndexFile(URL index,
                                       URLFilter filter)
                                throws IOException
List all the files specified by the index file passed in.

Returns:
String[] Matching results.
Throws:
IOException

getSize

public static int getSize(URL url)
Parameters:
url - the resource whose size is wanted
Returns:
the size of that resource

getSize

public static int getSize(URL url,
                          String proxyHost)

getSize

public static int getSize(URL url,
                          String proxyHost,
                          Integer proxyPort)

getLastModified

public static long getLastModified(URL url)
When was the given URL last modified. If no modification time is available then this method return the current time.


getLastModified

public static long getLastModified(URL url,
                                   String proxyHost)

getLastModified

public static long getLastModified(URL url,
                                   String proxyHost,
                                   Integer proxyPort)

isNewer

public static boolean isNewer(URL left,
                              URL right)
Returns whether the left is newer than the right by comparing their last modified dates.

Parameters:
left -
right -
Returns:
true if the left is newer

isNewer

public static boolean isNewer(URL left,
                              URL right,
                              String proxyHost)

isNewer

public static boolean isNewer(URL left,
                              URL right,
                              String proxyHost,
                              Integer proxyPort)

checkFileURL

private static void checkFileURL(URL url)
                          throws MalformedURLException
Throw if the given URL does not use the 'file:' protocol

Parameters:
url - The URL to check
Throws:
MalformedURLException - If the protocol is not file:

getURLCacheDir

public static File getURLCacheDir()
Returns the URLCacheDir.

Returns:
File

setURLCacheDir

public static void setURLCacheDir(File cachedir)
Sets the cache directory.

Parameters:
cachedir - The URLCacheDir to set

getURL

public static URL getURL(File file)
Get a URL version of the given file.

Parameters:
file - The File to turn into a URL
Returns:
a URL for the given file

getTemporaryURL

public static URL getTemporaryURL(String prefix,
                                  String suffix)
                           throws IOException
A URL version of File.createTempFile()

Returns:
A new temporary URL
Throws:
IOException - If something goes wrong creating the temp URL

Copyright ? 2003-2006