private static void testEncryptDecryptWithStream(
      StreamingAead encryptionStreamingAead,
      StreamingAead decryptionStreamingAead,
      int firstSegmentOffset,
      int plaintextSize,
      int chunkSize)
      throws Exception {
    byte[] associatedData = Hex.decode("aabbccddeeff");
    byte[] plaintext = generatePlaintext(plaintextSize);
    byte[] ciphertext =
        encryptWithStream(encryptionStreamingAead, plaintext, associatedData, firstSegmentOffset);

    // Construct an InputStream from the ciphertext where the first
    // firstSegmentOffset bytes have already been read.
    InputStream ctStream = new ByteArrayInputStream(ciphertext);
    ctStream.read(new byte[firstSegmentOffset]);

    // Construct an InputStream that returns the plaintext.
    InputStream ptStream = decryptionStreamingAead.newDecryptingStream(ctStream, associatedData);
    int decryptedSize = 0;
    while (true) {
      byte[] chunk = new byte[chunkSize];
      int read = ptStream.read(chunk);
      if (read == -1) {
        break;
      }
      byte[] expected = Arrays.copyOfRange(plaintext, decryptedSize, decryptedSize + read);
      TestUtil.assertByteArrayEquals(expected, Arrays.copyOf(chunk, read));
      decryptedSize += read;
      if (read < chunkSize && decryptedSize < plaintextSize) {
        // read should block until either all requested bytes are read, the end of the stream has
        // been reached or an error occurred.
        fail("read did not return enough bytes");
      }
    }
    assertEquals("Size of decryption does not match plaintext", plaintextSize, decryptedSize);
  }
Encrypts and decrypts some plaintext in a stream and checks that the expected plaintext is
returned.

@param encryptionStreamingAead the StreamingAead test object that encrypts.
@param decryptionStreamingAead the StreamingAead test object that decrypts (can be the same
    object as {@code encryptionStreamingAead}).
@param firstSegmentOffset number of bytes prepended to the ciphertext stream.
@param plaintextSize the size of the plaintext
@param chunkSize decryption read chunks of this size.
java_src/src/main/java/com/google/crypto/tink/testing/StreamingTestUtil.java