net.sourceforge.wohenchan
Class Cache

java.lang.Object
  |
  +--net.sourceforge.wohenchan.Cache

public class Cache
extends java.lang.Object

This class is the Facade class for all of the WoHenChan caching functionality. Given a directory, this class can cache various resources in that directory by name. Example usage:

 Cache c = new Cache(System.getProperty("user.home"))
 if (c.contains("Unihan.txt"))
 {
    InputStream unihan = c.get("Unihan.txt");
 }
 else
 {
    InputStream unihanStream;
    // open a socket to download unihan.txt

    try
    {
       c.put ("Unihan.txt", unihanStream);
    }
    catch (AlreadyCachedException e)
    {
    }
 }
 
it will be important to decide whether or not this cache should try to deal with the Multiple JVM case (multiple JVMs munging on the same Cache object). Not only that, is it a good idea to control construction of Cache objects so that, for any given directory, there is at most one cache object? The current usage example doesn't even go this far. What if two threads get access to the same Cache object and both get output streams for the same cache object? As of right now, this object does not deal with these issues.

Version:
$Name: $ $Date: 2003/06/22 17:44:44 $
Author:
$Author: wtanaka $

Field Summary
private  java.io.File m_directoryName
           
private static Cache s_singleton
          Singleton cache object which uses the .wohenchan subdirectory of the user's homedirectory.
private static java.lang.String SUBDIR
          Represents the directory in the users home directory to use for the singleton convenience cache.
 
Constructor Summary
Cache(java.io.File directoryName)
           
Cache(java.lang.String directoryName)
           
 
Method Summary
 boolean contains(java.lang.String resourceName)
           
private  java.io.File dataFileForResource(java.lang.String resourceName)
           
 java.io.InputStream get(java.lang.String resourceName)
          Gets a stream corresponding to the resource named by the parameter.
static Cache getInstance()
          Singleton cache object which uses the .wohenchan subdirectory of the user's homedirectory.
 long length(java.lang.String resourceName)
           
 void put(java.lang.String resourceName, java.io.InputStream source)
          associates the given resource name with the data that will appear on source.
 void put(java.lang.String resourceName, java.io.InputStream source, int expectedSize)
          associates the given resource name with the data that will appear on source.
private  java.io.File sizeFileForResource(java.lang.String resourceName)
           
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

m_directoryName

private java.io.File m_directoryName

SUBDIR

private static final java.lang.String SUBDIR
Represents the directory in the users home directory to use for the singleton convenience cache.

See Also:
Constant Field Values

s_singleton

private static Cache s_singleton
Singleton cache object which uses the .wohenchan subdirectory of the user's homedirectory. Although this is convenient to use, we should allow the user to specify the directory for the cache of large things (like dictionaries) in the UI, eventually.

Constructor Detail

Cache

public Cache(java.lang.String directoryName)

Cache

public Cache(java.io.File directoryName)
Method Detail

getInstance

public static Cache getInstance()
Singleton cache object which uses the .wohenchan subdirectory of the user's homedirectory. Although this is convenient to use, we should allow the user to specify the directory for the cache of large things (like dictionaries) in the UI, eventually.


sizeFileForResource

private java.io.File sizeFileForResource(java.lang.String resourceName)

dataFileForResource

private java.io.File dataFileForResource(java.lang.String resourceName)

get

public java.io.InputStream get(java.lang.String resourceName)
                        throws java.io.IOException
Gets a stream corresponding to the resource named by the parameter.

java.io.IOException

put

public void put(java.lang.String resourceName,
                java.io.InputStream source)
         throws java.io.IOException
associates the given resource name with the data that will appear on source.

Parameters:
resourceName - the name to associate with the resource
source - the input stream from which to grab the cached data
java.io.IOException

put

public void put(java.lang.String resourceName,
                java.io.InputStream source,
                int expectedSize)
         throws java.io.IOException
associates the given resource name with the data that will appear on source.

Parameters:
resourceName - the name to associate with the resource
source - the input stream from which to grab the cached data
expectedSize - the expected size of the data. The authoritative size is taken from however many bytes are read from source. This is used for making a guess as to what progress has been made.
java.io.IOException

contains

public boolean contains(java.lang.String resourceName)

length

public long length(java.lang.String resourceName)
            throws java.io.IOException
java.io.IOException

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object