### Question:
AttachmentStreamFactory { public OutputStream getOutputStream(File file, Attachment.Encoding encoding) throws IOException { OutputStream os = FileUtils.openOutputStream(file); if (key != null) { try { byte[] iv = new byte[16]; new SecureRandom().nextBytes(iv); os = new EncryptedAttachmentOutputStream(os, key, iv); } catch (InvalidKeyException ex) { throw new IOException("Bad key used to write file; check encryption key.", ex); } catch (InvalidAlgorithmParameterException ex) { throw new IOException("Bad key used to write file; check encryption key.", ex); } } switch (encoding) { case Plain: break; case Gzip: os = new GZIPOutputStream(os); } return os; } AttachmentStreamFactory(KeyProvider keyProvider); InputStream getInputStream(File file, Attachment.Encoding encoding); OutputStream getOutputStream(File file, Attachment.Encoding encoding);  }### Answer:
@Test public void testPreparedAttachmentWritesUnencryptedEncodedStream() throws AttachmentException, IOException { AttachmentStreamFactory asf = new AttachmentStreamFactory(new NullKeyProvider()); File plainText = f("fixture/EncryptedAttachmentTest_plainText"); File zippedPlainText = f("fixture/EncryptedAttachmentTest_plainText.gz"); File actualOutput = new File(datastore_manager_dir, "temp" + UUID.randomUUID()); OutputStream out = asf.getOutputStream(actualOutput, Attachment.Encoding.Gzip); IOUtils.copy(new FileInputStream(plainText), out); out.close(); Assert.assertTrue("Writing to unencrypted, encoded stream didn't give correct output", IOUtils.contentEquals( new FileInputStream(actualOutput), new FileInputStream(zippedPlainText))); }
@Test public void testPreparedAttachmentWritesEncryptedEncodedStream() throws AttachmentException, IOException, InvalidKeyException { AttachmentStreamFactory asf = new AttachmentStreamFactory( EncryptionTestConstants.keyProvider16Byte ); File plainText = f("fixture/EncryptedAttachmentTest_plainText"); File actualOutput = new File(datastore_manager_dir, "temp" + UUID.randomUUID()); OutputStream out = asf.getOutputStream(actualOutput, Attachment.Encoding.Gzip); IOUtils.copy(new FileInputStream(plainText), out); out.close(); Assert.assertTrue("Writing to encrypted, encoded stream didn't give correct output", IOUtils.contentEquals( new GZIPInputStream( new EncryptedAttachmentInputStream( new FileInputStream(actualOutput), EncryptionTestConstants.key16Byte)), new FileInputStream(plainText))); }