This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-crypto.git
commit 98608002dc4450155b98a528a9f4bee02a493e55 Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Sat Jun 6 19:25:51 2020 -0400 Use try-with-resources. --- .../crypto/stream/AbstractCipherStreamTest.java | 215 ++++++++++----------- 1 file changed, 107 insertions(+), 108 deletions(-) diff --git a/src/test/java/org/apache/commons/crypto/stream/AbstractCipherStreamTest.java b/src/test/java/org/apache/commons/crypto/stream/AbstractCipherStreamTest.java index 8d585a6..c9db8c4 100644 --- a/src/test/java/org/apache/commons/crypto/stream/AbstractCipherStreamTest.java +++ b/src/test/java/org/apache/commons/crypto/stream/AbstractCipherStreamTest.java @@ -163,152 +163,151 @@ public abstract class AbstractCipherStreamTest { } protected void doByteBufferRead(final String cipherClass, final boolean withChannel) - throws Exception { + throws Exception { if (AbstractCipherTest.OPENSSL_CIPHER_CLASSNAME.equals(cipherClass)) { if (!Crypto.isNativeCodeLoaded()) { return; // Skip this test if no JNI } } - // Default buffer size, initial buffer position is 0 - InputStream in = getCryptoInputStream( - new ByteArrayInputStream(encData), getCipher(cipherClass), - defaultBufferSize, iv, withChannel); ByteBuffer buf = ByteBuffer.allocate(dataLen + 100); - byteBufferReadCheck(in, buf, 0); - in.close(); + // Default buffer size, initial buffer position is 0 + try (InputStream in = getCryptoInputStream(new ByteArrayInputStream(encData), getCipher(cipherClass), + defaultBufferSize, iv, withChannel)) { + byteBufferReadCheck(in, buf, 0); + } // Default buffer size, initial buffer position is not 0 - in = getCryptoInputStream(new ByteArrayInputStream(encData), - getCipher(cipherClass), defaultBufferSize, iv, withChannel); - buf.clear(); - byteBufferReadCheck(in, buf, 11); - in.close(); + try (InputStream in = getCryptoInputStream(new ByteArrayInputStream(encData), getCipher(cipherClass), + defaultBufferSize, iv, withChannel)) { + buf.clear(); + byteBufferReadCheck(in, buf, 11); + } // Small buffer size, initial buffer position is 0 - in = getCryptoInputStream(new ByteArrayInputStream(encData), - getCipher(cipherClass), smallBufferSize, iv, withChannel); - buf.clear(); - byteBufferReadCheck(in, buf, 0); - in.close(); + try (InputStream in = getCryptoInputStream(new ByteArrayInputStream(encData), getCipher(cipherClass), + smallBufferSize, iv, withChannel)) { + buf.clear(); + byteBufferReadCheck(in, buf, 0); + } // Small buffer size, initial buffer position is not 0 - in = getCryptoInputStream(new ByteArrayInputStream(encData), - getCipher(cipherClass), smallBufferSize, iv, withChannel); - buf.clear(); - byteBufferReadCheck(in, buf, 11); - in.close(); + try (InputStream in = getCryptoInputStream(new ByteArrayInputStream(encData), getCipher(cipherClass), + smallBufferSize, iv, withChannel)) { + buf.clear(); + byteBufferReadCheck(in, buf, 11); + } // Direct buffer, default buffer size, initial buffer position is 0 - in = getCryptoInputStream(new ByteArrayInputStream(encData), - getCipher(cipherClass), defaultBufferSize, iv, withChannel); - buf = ByteBuffer.allocateDirect(dataLen + 100); - byteBufferReadCheck(in, buf, 0); - in.close(); + try (InputStream in = getCryptoInputStream(new ByteArrayInputStream(encData), getCipher(cipherClass), + defaultBufferSize, iv, withChannel)) { + buf = ByteBuffer.allocateDirect(dataLen + 100); + byteBufferReadCheck(in, buf, 0); + } // Direct buffer, default buffer size, initial buffer position is not 0 - in = getCryptoInputStream(new ByteArrayInputStream(encData), - getCipher(cipherClass), defaultBufferSize, iv, withChannel); - buf.clear(); - byteBufferReadCheck(in, buf, 11); - in.close(); + try (InputStream in = getCryptoInputStream(new ByteArrayInputStream(encData), getCipher(cipherClass), + defaultBufferSize, iv, withChannel)) { + buf.clear(); + byteBufferReadCheck(in, buf, 11); + } // Direct buffer, small buffer size, initial buffer position is 0 - in = getCryptoInputStream(new ByteArrayInputStream(encData), - getCipher(cipherClass), smallBufferSize, iv, withChannel); - buf.clear(); - byteBufferReadCheck(in, buf, 0); - in.close(); + try (InputStream in = getCryptoInputStream(new ByteArrayInputStream(encData), getCipher(cipherClass), + smallBufferSize, iv, withChannel)) { + buf.clear(); + byteBufferReadCheck(in, buf, 0); + } // Direct buffer, small buffer size, initial buffer position is not 0 - in = getCryptoInputStream(new ByteArrayInputStream(encData), - getCipher(cipherClass), smallBufferSize, iv, withChannel); - buf.clear(); - byteBufferReadCheck(in, buf, 11); - in.close(); - + try (InputStream in = getCryptoInputStream(new ByteArrayInputStream(encData), getCipher(cipherClass), + smallBufferSize, iv, withChannel)) { + buf.clear(); + byteBufferReadCheck(in, buf, 11); + } + // Direct buffer, small buffer size, initial buffer position is 0, final read - in = getCryptoInputStream(new ByteArrayInputStream(encData), - getCipher(cipherClass), smallBufferSize, iv, withChannel); - buf.clear(); - byteBufferFinalReadCheck(in, buf, 0); - in.close(); - + try (InputStream in = getCryptoInputStream(new ByteArrayInputStream(encData), getCipher(cipherClass), + smallBufferSize, iv, withChannel)) { + buf.clear(); + byteBufferFinalReadCheck(in, buf, 0); + } + // Default buffer size, initial buffer position is 0, insufficient dest buffer length - in = getCryptoInputStream(new ByteArrayInputStream(encData), - getCipher(cipherClass), defaultBufferSize, iv, withChannel); - buf = ByteBuffer.allocate(100); - byteBufferReadCheck(in, buf, 0); - in.close(); - + try (InputStream in = getCryptoInputStream(new ByteArrayInputStream(encData), getCipher(cipherClass), + defaultBufferSize, iv, withChannel)) { + buf = ByteBuffer.allocate(100); + byteBufferReadCheck(in, buf, 0); + } + // Default buffer size, initial buffer position is 0 - in = getCryptoInputStream(transformation, props, new ByteArrayInputStream(encData), key, - new IvParameterSpec(iv), withChannel); - buf = ByteBuffer.allocate(dataLen + 100); - byteBufferReadCheck(in, buf, 0); - in.close(); - + try (InputStream in = getCryptoInputStream(transformation, props, new ByteArrayInputStream(encData), key, + new IvParameterSpec(iv), withChannel)) { + buf = ByteBuffer.allocate(dataLen + 100); + byteBufferReadCheck(in, buf, 0); + } + // Default buffer size, initial buffer position is not 0 - in = getCryptoInputStream(transformation, props, new ByteArrayInputStream(encData), key, - new IvParameterSpec(iv), withChannel); - buf.clear(); - byteBufferReadCheck(in, buf, 11); - in.close(); + try (InputStream in = getCryptoInputStream(transformation, props, new ByteArrayInputStream(encData), key, + new IvParameterSpec(iv), withChannel)) { + buf.clear(); + byteBufferReadCheck(in, buf, 11); + } // Small buffer size, initial buffer position is 0 - in = getCryptoInputStream(transformation, props, new ByteArrayInputStream(encData), key, - new IvParameterSpec(iv), withChannel); - buf.clear(); - byteBufferReadCheck(in, buf, 0); - in.close(); + try (InputStream in = getCryptoInputStream(transformation, props, new ByteArrayInputStream(encData), key, + new IvParameterSpec(iv), withChannel)) { + buf.clear(); + byteBufferReadCheck(in, buf, 0); + } // Small buffer size, initial buffer position is not 0 - in = getCryptoInputStream(transformation, props, new ByteArrayInputStream(encData), key, - new IvParameterSpec(iv), withChannel); - buf.clear(); - byteBufferReadCheck(in, buf, 11); - in.close(); + try (InputStream in = getCryptoInputStream(transformation, props, new ByteArrayInputStream(encData), key, + new IvParameterSpec(iv), withChannel)) { + buf.clear(); + byteBufferReadCheck(in, buf, 11); + } // Direct buffer, default buffer size, initial buffer position is 0 - in = getCryptoInputStream(transformation, props, new ByteArrayInputStream(encData), key, - new IvParameterSpec(iv), withChannel); - buf = ByteBuffer.allocateDirect(dataLen + 100); - byteBufferReadCheck(in, buf, 0); - in.close(); + try (InputStream in = getCryptoInputStream(transformation, props, new ByteArrayInputStream(encData), key, + new IvParameterSpec(iv), withChannel)) { + buf = ByteBuffer.allocateDirect(dataLen + 100); + byteBufferReadCheck(in, buf, 0); + } // Direct buffer, default buffer size, initial buffer position is not 0 - in = getCryptoInputStream(transformation, props, new ByteArrayInputStream(encData), key, - new IvParameterSpec(iv), withChannel); - buf.clear(); - byteBufferReadCheck(in, buf, 11); - in.close(); + try (InputStream in = getCryptoInputStream(transformation, props, new ByteArrayInputStream(encData), key, + new IvParameterSpec(iv), withChannel)) { + buf.clear(); + byteBufferReadCheck(in, buf, 11); + } // Direct buffer, small buffer size, initial buffer position is 0 - in = getCryptoInputStream(transformation, props, new ByteArrayInputStream(encData), key, - new IvParameterSpec(iv), withChannel); - buf.clear(); - byteBufferReadCheck(in, buf, 0); - in.close(); + try (InputStream in = getCryptoInputStream(transformation, props, new ByteArrayInputStream(encData), key, + new IvParameterSpec(iv), withChannel)) { + buf.clear(); + byteBufferReadCheck(in, buf, 0); + } // Direct buffer, small buffer size, initial buffer position is not 0 - in = getCryptoInputStream(transformation, props, new ByteArrayInputStream(encData), key, - new IvParameterSpec(iv), withChannel); - buf.clear(); - byteBufferReadCheck(in, buf, 11); - in.close(); - + try (InputStream in = getCryptoInputStream(transformation, props, new ByteArrayInputStream(encData), key, + new IvParameterSpec(iv), withChannel)) { + buf.clear(); + byteBufferReadCheck(in, buf, 11); + } + // Direct buffer, default buffer size, initial buffer position is 0, final read - in = getCryptoInputStream(transformation, props, new ByteArrayInputStream(encData), key, - new IvParameterSpec(iv), withChannel); - buf.clear(); - byteBufferFinalReadCheck(in, buf, 0); - in.close(); - + try (InputStream in = getCryptoInputStream(transformation, props, new ByteArrayInputStream(encData), key, + new IvParameterSpec(iv), withChannel)) { + buf.clear(); + byteBufferFinalReadCheck(in, buf, 0); + } + // Default buffer size, initial buffer position is 0, insufficient dest buffer length - in = getCryptoInputStream(transformation, props, new ByteArrayInputStream(encData), key, - new IvParameterSpec(iv), withChannel); - buf = ByteBuffer.allocate(100); - byteBufferReadCheck(in, buf, 0); - in.close(); + try (InputStream in = getCryptoInputStream(transformation, props, new ByteArrayInputStream(encData), key, + new IvParameterSpec(iv), withChannel)) { + buf = ByteBuffer.allocate(100); + byteBufferReadCheck(in, buf, 0); + } } protected void doByteBufferWrite(final String cipherClass,