public InputStream getInputStream ( File file , Attachment . Encoding encoding ) throws IOException { // First, open a stream to the raw bytes on disk. // Then, if we have a key assume the file is encrypted, so add a stream // to the chain which decrypts the data as we read from disk. // Finally, decode (unzip) the data if the attachment is encoded before // returning the data to the user. // // Read from disk [-> Decryption Stream] [-> Decoding Stream] -> user reads InputStream is = new FileInputStream ( file ) ; if ( key != null ) { try { is = new EncryptedAttachmentInputStream ( is , key ) ; } catch ( InvalidKeyException ex ) { // Replace with an IOException as we validate the key when opening // the databases and the key should be the same -- it's therefore // not worth forcing the developer to catch something they can't // fix during file read; generic IOException works better. throw new IOException ( "Bad key used to open file; check encryption key." , ex ) ; } } switch ( encoding ) { case Plain : break ; // nothing to do case Gzip : is = new GZIPInputStream ( is ) ; } return is ; }
Return a stream to be used to read from the file on disk .