Source:
<code>public void attachmentsDataEncrypted() throws IOException, DocumentException { MutableDocumentRevision rev = new MutableDocumentRevision(); rev.body = DocumentBodyFactory.create(new HashMap<String, String>()); File expectedPlainText = TestUtils.loadFixture("fixture/EncryptedAttachmentTest_plainText"); assertNotNull(expectedPlainText); UnsavedFileAttachment attachment = new UnsavedFileAttachment( expectedPlainText, "text/plain"); rev.attachments.put("EncryptedAttachmentTest_plainText", attachment); datastore.createDocumentFromRevision(rev); File attachmentsFolder = new File(datastore_manager_dir + File.separator + "EndToEndEncryptionTest" + File.separator + "extensions" + File.separator + "com.cloudant.attachments"); File[] contents = attachmentsFolder.listFiles(); assertNotNull("Didn't find expected attachments folder", contents); assertThat("Didn't find expected file in attachments", contents.length, IsEqual.equalTo(1)); InputStream in = new FileInputStream(contents[0]); if(dataShouldBeEncrypted) { byte[] actualContent = new byte[expectedFirstAttachmentByte.length]; <START> int readLength = in.read(actualContent); <END> assertEquals("Didn't read full buffer", actualContent.length, readLength); assertArrayEquals("First byte not version byte", actualContent, expectedFirstAttachmentByte); } else { assertTrue(IOUtils.contentEquals(new FileInputStream(expectedPlainText), in)); } }</code><technical_language>If want byte do in.read() (the version for reading byte a time) - is assert else in encrypted attachment</technical_language>

Target:
public void attachmentsDataEncrypted() throws IOException, DocumentException, InvalidKeyException { MutableDocumentRevision rev = new MutableDocumentRevision(); rev.body = DocumentBodyFactory.create(new HashMap<String, String>()); File expectedPlainText = TestUtils.loadFixture("fixture/EncryptedAttachmentTest_plainText"); assertNotNull(expectedPlainText); UnsavedFileAttachment attachment = new UnsavedFileAttachment( expectedPlainText, "text/plain"); rev.attachments.put("EncryptedAttachmentTest_plainText", attachment); datastore.createDocumentFromRevision(rev); File attachmentsFolder = new File(datastoreManagerDir + File.separator + "EndToEndEncryptionTest" + File.separator + "extensions" + File.separator + "com.cloudant.attachments"); File[] contents = attachmentsFolder.listFiles(); assertNotNull("Didn't find expected attachments folder", contents); assertThat("Didn't find expected file in attachments", contents.length, IsEqual.equalTo(1)); InputStream in = new FileInputStream(contents[0]); if(dataShouldBeEncrypted) { byte[] actualContent = new byte[expectedFirstAttachmentByte.length]; int readLength = in.read(actualContent); assertEquals("Didn't read full buffer", actualContent.length, readLength); assertArrayEquals("First byte not version byte", expectedFirstAttachmentByte, actualContent); assertTrue("Encrypted attachment did not decrypt correctly", IOUtils.contentEquals( new EncryptedAttachmentInputStream(new FileInputStream(contents[0]), KEY), new FileInputStream(expectedPlainText))); } else { assertTrue("Unencrypted attachment did not read correctly", IOUtils.contentEquals(new FileInputStream(expectedPlainText), in)); } }