private void cacheStream ( ) { try { File fi = getTemproralCacheFile ( ) ; if ( fi . exists ( ) ) { if ( ! fi . delete ( ) ) { throw new IllegalStateException ( "Cannot delete file " + fi . getAbsolutePath ( ) + "!" ) ; } } FileOutputStream fout = new FileOutputStream ( fi ) ; InputStream in = grabStream ( ) ; byte [ ] buffer = new byte [ DEFAULT_BUFFER_SIZE ] ; int n ; while ( - 1 != ( n = in . read ( buffer ) ) ) { fout . write ( buffer , 0 , n ) ; } fout . flush ( ) ; fout . close ( ) ; in . close ( ) ; File cacheFile = getCacheFile ( ) ; if ( ! fi . renameTo ( cacheFile ) ) { throw new IllegalStateException ( "Cannot rename file \"" + fi . getAbsolutePath ( ) + "\" to \"" + cacheFile . getAbsolutePath ( ) + "\"!" ) ; } } catch ( Exception e ) { e . printStackTrace ( ) ; } }
Save a copy in the local cache - in case remote source is not available in future .