net.sf.jomic.tools
Class ArchiveCache

java.lang.Object
  extended by net.sf.jomic.tools.ArchiveCache
All Implemented Interfaces:
CacheInfo

public class ArchiveCache
extends java.lang.Object
implements CacheInfo

Cache for extracted archives. The cache maps the file path a a comic book archive to a directory where the extracted images are restored. While there is a size limit, it can temporarily be exeeded, for example because a single comic is bigger than the limit, or no comics can be removed because all of them are locked by a client right now.

Author:
Thomas Aglassinger
See Also:
ArchiveCacheEntry

Constructor Summary
ArchiveCache(java.io.File newBaseDir, long newMaxSize)
          Create ArchiveCache storing extracted files and index in the directory newBaseDir .
 
Method Summary
 void attemptToReadEntries()
          Attempt to read entries from cacheMap.xml.
 void clear()
          Clear entries and remove all files in the base directory.
 ArchiveCacheEntry createCacheEntry(java.io.File newArchiveFile)
          Create a new cache entry for newArchiveFile using the next free index.
 ArchiveCacheEntry get(java.io.File archiveFile)
           
 java.io.File getBaseDir()
           
 java.io.File getCacheEntryDir(ArchiveCacheEntry cacheEntry)
          Get directory where cached files extracted from the archive are stored.
 int getEntryCount()
          Number of cache entries.
 long getMaxSize()
          Number of bytes the cache should use at the maximum.
 long getUsedSize()
          Number of bytes currently in use by cache.
 void put(ArchiveCacheEntry entry)
           
 void readEntries(java.io.Reader reader)
           
 void remove(java.io.File archiveFile)
           
 void setCacheEntrySize(ArchiveCacheEntry entry)
          Set the number of bytes used by entry by computing the number of bytes used by all files stored in its archive cache directory.
 void setMaxSize(long newMaxSize)
          Set the maximum number of bytes the cache should use.
 void writeEntries()
           
 void writeEntries(java.io.Writer writer)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ArchiveCache

public ArchiveCache(java.io.File newBaseDir,
                    long newMaxSize)
             throws java.io.FileNotFoundException
Create ArchiveCache storing extracted files and index in the directory newBaseDir . This directory must not contain any other data because clearing the cache means deleting this directory.

Parameters:
newBaseDir - the directory where extracted files will be stored
newMaxSize - intended maximum size in bytes extracted archives should use before older files are removed from the cache
Throws:
java.io.FileNotFoundException - if newBaseDir cannot be created
Method Detail

setCacheEntrySize

public void setCacheEntrySize(ArchiveCacheEntry entry)
Set the number of bytes used by entry by computing the number of bytes used by all files stored in its archive cache directory.


setMaxSize

public void setMaxSize(long newMaxSize)
Set the maximum number of bytes the cache should use. If the cache currently is bigger than that, attempt to shrink it to fit.


get

public ArchiveCacheEntry get(java.io.File archiveFile)

getBaseDir

public java.io.File getBaseDir()

getCacheEntryDir

public java.io.File getCacheEntryDir(ArchiveCacheEntry cacheEntry)
Get directory where cached files extracted from the archive are stored.


getEntryCount

public int getEntryCount()
Description copied from interface: CacheInfo
Number of cache entries.

Specified by:
getEntryCount in interface CacheInfo

getMaxSize

public long getMaxSize()
Description copied from interface: CacheInfo
Number of bytes the cache should use at the maximum.

Specified by:
getMaxSize in interface CacheInfo

getUsedSize

public long getUsedSize()
Description copied from interface: CacheInfo
Number of bytes currently in use by cache. This can be more than getMaxSize() in case there is only one entry in the cache bigger than this, or (in case entry size can not be estimated before adding them) temporarely when adding an entry before removing an old one.

Specified by:
getUsedSize in interface CacheInfo

attemptToReadEntries

public void attemptToReadEntries()
Attempt to read entries from cacheMap.xml. If the map cannot be found or is broken, start with an empty cache.


clear

public void clear()
Clear entries and remove all files in the base directory.


createCacheEntry

public ArchiveCacheEntry createCacheEntry(java.io.File newArchiveFile)
Create a new cache entry for newArchiveFile using the next free index.


put

public void put(ArchiveCacheEntry entry)

readEntries

public void readEntries(java.io.Reader reader)
                 throws java.io.IOException,
                        org.xml.sax.SAXException
Throws:
java.io.IOException
org.xml.sax.SAXException

remove

public void remove(java.io.File archiveFile)

writeEntries

public void writeEntries()
                  throws java.io.IOException
Throws:
java.io.IOException

writeEntries

public void writeEntries(java.io.Writer writer)
                  throws java.io.IOException
Throws:
java.io.IOException