public static StoredFile createLocalStoredFile ( InputStream is , String sourceUrl , String localFilePath , String mimeType ) { if ( is == null ) { return null ; } // Copy the file contents over. CountingOutputStream cos = null ; BufferedOutputStream bos = null ; FileOutputStream fos = null ; try { File localFile = new File ( localFilePath ) ; /* Local cache file name may not be unique, and can be reused, in which case, the previously created * cache file need to be deleted before it is being copied over. */ if ( localFile . exists ( ) ) { localFile . delete ( ) ; } fos = new FileOutputStream ( localFile ) ; bos = new BufferedOutputStream ( fos ) ; cos = new CountingOutputStream ( bos ) ; byte [ ] buf = new byte [ 2048 ] ; int count ; while ( ( count = is . read ( buf , 0 , 2048 ) ) != - 1 ) { cos . write ( buf , 0 , count ) ; } ApptentiveLog . v ( UTIL , "File saved, size = " + ( cos . getBytesWritten ( ) / 1024 ) + "k" ) ; } catch ( IOException e ) { ApptentiveLog . e ( UTIL , "Error creating local copy of file attachment." ) ; logException ( e ) ; return null ; } finally { Util . ensureClosed ( cos ) ; Util . ensureClosed ( bos ) ; Util . ensureClosed ( fos ) ; } // Create a StoredFile database entry for this locally saved file. StoredFile storedFile = new StoredFile ( ) ; storedFile . setSourceUriOrPath ( sourceUrl ) ; storedFile . setLocalFilePath ( localFilePath ) ; storedFile . setMimeType ( mimeType ) ; return storedFile ; }
This method creates a cached file copy from the source input stream .