com.calitha.io
Class URLContentCache

java.lang.Object
  extended by com.calitha.io.URLContentCache

public class URLContentCache
extends Object

An URLContentCache object represents a cache for files and other resources.

The cache can work with both local files and with other resources which are referenced by a URL that contains a supported protocol such as http and ftp.

Each URLContentCache object has a context which by default is set to the application startup directory. The context is used to create an URL if the resource has a relative URI.

The URLContentCache can be set to auto mode, which is on by default, to automatically add resources when they are requested.

Author:
Robert van Loenhout

Constructor Summary
URLContentCache()
          Creates a new URIFileCacher.
 
Method Summary
 void add(File file)
          Adds a file in the cache by using a File object.
 void add(String location)
          Adds a resource to the cache by using a string location.
 void add(URL url)
          Adds a resource to the cache by using an URL.
 void clear()
          Clears the cache.
 boolean contains(String location)
          Determines if this cache already has this location cached.
 boolean contains(URL url)
          Determines if this cache already has this location cached.
 byte[] get(File file)
          Gets a resource from the cache by specifying a file object.
 byte[] get(String location)
          Gets a resource and possibly adds it to the cache.
 byte[] get(URL url)
          Gets a resource and possibly adds it to the cache.
 boolean getAutoMode()
          Gets current auto mode state.
 ByteArrayInputStream getBIStream(File file)
          Gets the file from the cache as a byte input stream.
 ByteArrayInputStream getBIStream(String location)
          Gets the resource from the cache as a byte input stream.
 ByteArrayInputStream getBIStream(URL url)
          Gets the resource from the cache as a byte input stream.
 URL getContext()
          Gets the context of the cacher.
 void setAutoMode(boolean value)
          Sets auto mode on or off.
 void setContext(URL context)
          Sets the context of the cacher.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

URLContentCache

public URLContentCache()
Creates a new URIFileCacher.

By default the auto mode is enabled and the context is the application startup directory.

Method Detail

getContext

public URL getContext()
Gets the context of the cacher.

This is used when a relative URL is being used to get or add a resource.

Returns:
context

setContext

public void setContext(URL context)
Sets the context of the cacher.

This is used when a relative URL is being used to get or add a resource.

Parameters:
context - context
Throws:
IllegalArgumentException - if an argument is null

getAutoMode

public boolean getAutoMode()
Gets current auto mode state.

If auto mode is true the resource is automatically cached when it is first requested (and has not been added yet).

Returns:
true if auto mode is on; otherwise false

setAutoMode

public void setAutoMode(boolean value)
Sets auto mode on or off.

If auto mode is true the resource is automatically cached when it is first requested (and has not been added yet).

Parameters:
value - true switches auto mode on; false switches it off

contains

public boolean contains(URL url)
Determines if this cache already has this location cached.

Parameters:
url - absolute resource location
Returns:
true if the cache contains the url, otherwise false
Throws:
IllegalArgumentException - if an argument is null

contains

public boolean contains(String location)
                 throws MalformedURLException
Determines if this cache already has this location cached.

The location is transformed into an absolute URL (using the context) if the location is relative. This ensures that a resource is only cached once even though the resource can be represented as a relative or an absolute URL.

Parameters:
location - absolute or relative URL location as a string
Returns:
true if the cache contains this url, otherwise false
Throws:
MalformedURLException - when the location cannot be transformed into an URL
IllegalArgumentException - if an argument is null

add

public void add(String location)
         throws IOException
Adds a resource to the cache by using a string location.

The location can be absolute that contains a supported protocol. Or it can be relative in which case the context is being used to transform it into an absolute URL.

Parameters:
location - absolute or relative URL
Throws:
IOException - thrown when the location cannot be transformed in an URL, or if the resource cannot be read into the cache
IllegalArgumentException - if an argument is null

add

public void add(URL url)
         throws IOException
Adds a resource to the cache by using an URL.

The URL must contain a supported protocol.

Parameters:
url - absolute URL
Throws:
IOException - thrown if the file cannot be read into the cache
IllegalArgumentException - if an argument is null

add

public void add(File file)
         throws IOException
Adds a file in the cache by using a File object.

The file object file will be transformed into an URL first which functions as the key to store the resource in the cache.

Parameters:
file - file being added
Throws:
IOException - thrown when the file cannot be read into the cache
IllegalArgumentException - if an argument is null

get

public byte[] get(URL url)
           throws IOException
Gets a resource and possibly adds it to the cache.

Parameters:
url - url of the requested resource
Returns:
bytes of the file, or null if the cache does not contain this resource and auto mode is off
Throws:
IOException - thrown if the file cannot be added (in auto mode)
IllegalArgumentException - if an argument is null

get

public byte[] get(String location)
           throws IOException
Gets a resource and possibly adds it to the cache.

If auto mode is on then the file is added to the cache if it was not in there already.

Parameters:
location - absolute or relative URL location
Returns:
bytes of the file, or null if the cache does not contain this file and auto mode is off
Throws:
IOException - thrown if the location cannot be transformed into an URL or the file cannot be added to the cache (in auto mode)
IllegalArgumentException - if an argument is null

get

public byte[] get(File file)
           throws IOException
Gets a resource from the cache by specifying a file object.

Parameters:
file - requested file
Returns:
bytes of the file or null if the file has not been cached and auto mode is off
Throws:
IOException - thrown if the file cannot be added (in auto mode)
IllegalArgumentException - if an argument is null

getBIStream

public ByteArrayInputStream getBIStream(String location)
                                 throws IOException
Gets the resource from the cache as a byte input stream.

If auto mode is on then the file is also added if it was not added yet.

Parameters:
location - absolute or relative URL location
Returns:
byte array input stream
Throws:
IOException - thrown if the uri cannot be transformed into an URL or the resource cannot be added (in auto mode)
IllegalArgumentException - if an argument is null

getBIStream

public ByteArrayInputStream getBIStream(URL url)
                                 throws IOException
Gets the resource from the cache as a byte input stream.

Parameters:
url - url of the requested file
Returns:
byte array input stream
Throws:
IOException - thrown if the resource cannot be added (in auto mode)
IllegalArgumentException - if an argument is null

getBIStream

public ByteArrayInputStream getBIStream(File file)
                                 throws IOException
Gets the file from the cache as a byte input stream.

Parameters:
file - requested file
Returns:
byte array input stream
Throws:
IOException - thrown if the file cannot be transformed into an URL or the file cannot be loaded
IllegalArgumentException - if an argument is null

clear

public void clear()
Clears the cache.