public class DiskCache extends Object
| Modifier and Type | Field and Description | 
|---|---|
static String | 
CACHE_DIRNAME  | 
static String | 
README_NAME  | 
| Constructor and Description | 
|---|
DiskCache(File dir,
         long limit)
Constructor. 
 | 
| Modifier and Type | Method and Description | 
|---|---|
void | 
fileAdded(File f)
Records that a file has been added to the cache. 
 | 
static String | 
formatByteSize(long nbyte)
Formats a string representing a number of bytes for human consumption. 
 | 
static long | 
getDefaultCacheLimit(File dir)
Returns a cacheLimit value that is suitable for general usage. 
 | 
File | 
getDir()
Returns the directory associated with this cache. 
 | 
static File | 
getSystemTmpDir()
Returns the default system scratch directory. 
 | 
String | 
getWriteSummary()
Returns a string summarising total cache files written to date. 
 | 
static String | 
hashText(String txt)
Returns a default-length hash string corresponding to a given string. 
 | 
static String | 
hashText(String txt,
        int nchar)
Returns a configurable-length hash string corresponding
 to a given string. 
 | 
void | 
log(String txt)
Logging utility function. 
 | 
static boolean | 
mkdirs(File dir,
      boolean ownerOnly)
Creates a scratch directory and any missing parents,
 assigning write privileges for all or owner-only. 
 | 
void | 
ready()
Ensures that this cache's directory is ready for use. 
 | 
void | 
tidy()
Removes files from this cache's directory if it contains more
 data than the configured cache limit. 
 | 
static File | 
toCacheDir(File baseDir,
          String label)
Constructs a cache directory path given a base directory and a
 purpose-specific label. 
 | 
void | 
touch(File file)
Updates the lastModified timestamp for a given file. 
 | 
static File | 
toWorkFilename(File file)
Produces a working filename to be used as temporary workspace
 when assembling a given destination filename. 
 | 
public static final String CACHE_DIRNAME
public static final String README_NAME
public DiskCache(File dir, long limit)
dir - directory into which cache files will be writtenlimit - cache size indicator;
                if positive, it's the maximum cache size in bytes;
                if negative, it's the amount of space on the
                disk that cache usage tries to keep free;
                if zero, it's something adaptivepublic File getDir()
public void fileAdded(File f)
f - file addedpublic void ready()
           throws IOException
IOExceptionpublic void log(String txt)
txt - message to logpublic void tidy()
public String getWriteSummary()
public void touch(File file)
file - file to touchpublic static String hashText(String txt)
txt - text to hashpublic static String hashText(String txt, int nchar)
txt - text to hashnchar - number of characters in output (currently up to 32)public static File toWorkFilename(File file)
We could just use one of the File.createTempFile methods,
 but attempt to come up with a name that is (a) likely to be unique and
 (b) bears some resemlance to the requested destination file,
 for ease of debugging etc.
file - destination filepublic static String formatByteSize(long nbyte)
nbyte - byte countpublic static File getSystemTmpDir()
java.io.tmpdir system property.public static File toCacheDir(File baseDir, String label)
baseDir - base directory; if null, java.io.tmpdir is usedlabel - purpose-specific label (used for subdirectory name)public static boolean mkdirs(File dir, boolean ownerOnly)
File.mkdirs(), but it allows the option
 of assigning global write privileges to any created directories.dir - target directoryownerOnly - true to make created directories writable by
                     ownwer only, false to make them writable by allpublic static long getDefaultCacheLimit(File dir)
dir - cache directoryCopyright © 2025 Central Laboratory of the Research Councils. All Rights Reserved.