org.openstreetmap.josm.io
Class CacheFiles

java.lang.Object
  extended by org.openstreetmap.josm.io.CacheFiles

public class CacheFiles
extends java.lang.Object

Use this class if you want to cache a lot of files that shouldn't be kept in memory. You can specify how much data should be stored and after which date the files should be expired. This works on a last-access basis, so files get deleted after they haven't been used for x days. You can turn this off by calling setUpdateModTime(false). Files get deleted on a first-in-first-out basis.


Field Summary
static int CLEAN_ALL
           
static int CLEAN_BY_DATE
           
static int CLEAN_SMALL_FILES
           
private static int CLEANUP_INTERVAL
           
private static int CLEANUP_TRESHOLD
           
private  java.io.File dir
           
private  boolean enabled
           
private  long expire
           
static int EXPIRE_DAILY
           
static int EXPIRE_MONTHLY
           
static int EXPIRE_NEVER
          Common expirey dates
static int EXPIRE_WEEKLY
           
private  java.lang.String ident
           
private  long maxsize
           
private  boolean updateModTime
           
private  int writes
           
 
Constructor Summary
CacheFiles(java.lang.String ident)
          Creates a new cache class.
CacheFiles(java.lang.String ident, boolean isPlugin)
           
 
Method Summary
 void checkCleanUp()
          Checks if a clean up is needed and will do so if necessary
 void cleanUp()
          Performs a default clean up with the set values (deletes oldest files first)
 void customCleanUp(int type, int size)
          Performs a non-default, specified clean up
 byte[] getData(java.lang.String ident)
          Loads the data for the given ident as an byte array.
private  long getDirSize()
          Calculates the size of the directory
 java.awt.image.BufferedImage getImg(java.lang.String ident)
          Loads the data for the given ident as an image.
private  java.io.File getPath(java.lang.String ident)
          Gets file path for ident
private  java.io.File getPath(java.lang.String ident, java.lang.String ending)
          Gets file path for ident with customizable file-ending
private static java.lang.String getUniqueFilename(java.lang.String ident)
          Returns a short and unique file name for a given long identifier
private  boolean isExpired(java.io.File file)
          Checks whether a given file is expired
 void saveData(java.lang.String ident, byte[] data)
          Writes an byte-array to disk
 void saveImg(java.lang.String ident, java.awt.image.BufferedImage image)
          Saves a given image and ident to the cache
 void setExpire(int amount, boolean force)
          Sets the amount of time data is stored before it gets expired
 void setMaxSize(int amount, boolean force)
          Sets the amount of data stored in the cache
 void setUpdateModTime(boolean to)
          Call this with true to update the last modification time when a file it is read.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

EXPIRE_NEVER

public static final int EXPIRE_NEVER
Common expirey dates

See Also:
Constant Field Values

EXPIRE_DAILY

public static final int EXPIRE_DAILY
See Also:
Constant Field Values

EXPIRE_WEEKLY

public static final int EXPIRE_WEEKLY
See Also:
Constant Field Values

EXPIRE_MONTHLY

public static final int EXPIRE_MONTHLY
See Also:
Constant Field Values

dir

private final java.io.File dir

ident

private final java.lang.String ident

enabled

private final boolean enabled

expire

private long expire

maxsize

private long maxsize

updateModTime

private boolean updateModTime

CLEANUP_TRESHOLD

private static final int CLEANUP_TRESHOLD
See Also:
Constant Field Values

CLEANUP_INTERVAL

private static final int CLEANUP_INTERVAL
See Also:
Constant Field Values

writes

private int writes

CLEAN_ALL

public static final int CLEAN_ALL
See Also:
Constant Field Values

CLEAN_SMALL_FILES

public static final int CLEAN_SMALL_FILES
See Also:
Constant Field Values

CLEAN_BY_DATE

public static final int CLEAN_BY_DATE
See Also:
Constant Field Values
Constructor Detail

CacheFiles

public CacheFiles(java.lang.String ident)
Creates a new cache class. The ident will be used to store the files on disk and to save expire/space settings.

Parameters:
ident -

CacheFiles

public CacheFiles(java.lang.String ident,
                  boolean isPlugin)
Method Detail

getData

public byte[] getData(java.lang.String ident)
Loads the data for the given ident as an byte array. Returns null if data not available.

Parameters:
ident -
Returns:

saveData

public void saveData(java.lang.String ident,
                     byte[] data)
Writes an byte-array to disk

Parameters:
ident -
data -

getImg

public java.awt.image.BufferedImage getImg(java.lang.String ident)
Loads the data for the given ident as an image. If no image is found, null is returned

Parameters:
ident - Identifier
Returns:
BufferedImage or null

saveImg

public void saveImg(java.lang.String ident,
                    java.awt.image.BufferedImage image)
Saves a given image and ident to the cache

Parameters:
ident -
image -

setExpire

public void setExpire(int amount,
                      boolean force)
Sets the amount of time data is stored before it gets expired

Parameters:
amount - of time in seconds
force - will also write it to the preferences

setMaxSize

public void setMaxSize(int amount,
                       boolean force)
Sets the amount of data stored in the cache

Parameters:
amount - in Megabytes
force - will also write it to the preferences

setUpdateModTime

public void setUpdateModTime(boolean to)
Call this with true to update the last modification time when a file it is read. Call this with false to not update the last modification time when a file is read.

Parameters:
to -

checkCleanUp

public void checkCleanUp()
Checks if a clean up is needed and will do so if necessary


cleanUp

public void cleanUp()
Performs a default clean up with the set values (deletes oldest files first)


customCleanUp

public void customCleanUp(int type,
                          int size)
Performs a non-default, specified clean up

Parameters:
type - any of the CLEAN_XX constants.
size - for CLEAN_SMALL_FILES: deletes all files smaller than (size) bytes

getDirSize

private long getDirSize()
Calculates the size of the directory

Returns:
long Size of directory in bytes

getUniqueFilename

private static java.lang.String getUniqueFilename(java.lang.String ident)
Returns a short and unique file name for a given long identifier

Returns:
String short filename

getPath

private java.io.File getPath(java.lang.String ident,
                             java.lang.String ending)
Gets file path for ident with customizable file-ending

Parameters:
ident -
ending -
Returns:
File

getPath

private java.io.File getPath(java.lang.String ident)
Gets file path for ident

Parameters:
ident -
ending -
Returns:
File

isExpired

private boolean isExpired(java.io.File file)
Checks whether a given file is expired

Parameters:
file -
Returns:
expired?


JOSM