@ Override public synchronized void initialize ( ) { if ( ! mRootDirectory . exists ( ) ) { if ( ! mRootDirectory . mkdirs ( ) ) { //silently ignoring no cache is not good so we throw and let the user decide // what // to do throw new IllegalStateException ( "Unable to create cache dir: " + mRootDirectory . getAbsolutePath ( ) ) ; } //we have an empty cache so we just return return ; } File [ ] files = mRootDirectory . listFiles ( ) ; if ( files == null ) { return ; } for ( File file : files ) { BufferedInputStream fis = null ; try { fis = new BufferedInputStream ( new FileInputStream ( file ) ) ; CacheHeader entry = CacheHeader . readHeader ( fis ) ; entry . size = file . length ( ) ; putEntry ( entry . key , entry ) ; } catch ( IOException e ) { if ( file != null ) { file . delete ( ) ; } } finally { try { if ( fis != null ) { fis . close ( ) ; } } catch ( IOException ignored ) { } } } }
Initializes the DiskBasedCache by scanning for all files currently in the specified root directory . Creates the root directory if necessary .