commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ggreg...@apache.org
Subject commons-crypto git commit: Use try-with-resources.
Date Tue, 21 Jun 2016 23:27:28 GMT
Repository: commons-crypto
Updated Branches:
  refs/heads/master 1f6e1c3e2 -> 5c14932be


Use try-with-resources.

Project: http://git-wip-us.apache.org/repos/asf/commons-crypto/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-crypto/commit/5c14932b
Tree: http://git-wip-us.apache.org/repos/asf/commons-crypto/tree/5c14932b
Diff: http://git-wip-us.apache.org/repos/asf/commons-crypto/diff/5c14932b

Branch: refs/heads/master
Commit: 5c14932be14cb1739a7405a409d90c923bb2a6c4
Parents: 1f6e1c3
Author: ggregory <ggregory@apache.org>
Authored: Tue Jun 21 16:27:26 2016 -0700
Committer: ggregory <ggregory@apache.org>
Committed: Tue Jun 21 16:27:26 2016 -0700

----------------------------------------------------------------------
 .../examples/CipherByteBufferExample.java       | 52 ++++++++++----------
 .../commons/crypto/examples/StreamExample.java  | 25 +++++-----
 2 files changed, 39 insertions(+), 38 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/5c14932b/src/main/java/org/apache/commons/crypto/examples/CipherByteBufferExample.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/crypto/examples/CipherByteBufferExample.java
b/src/main/java/org/apache/commons/crypto/examples/CipherByteBufferExample.java
index 367cad4..484c5e3 100644
--- a/src/main/java/org/apache/commons/crypto/examples/CipherByteBufferExample.java
+++ b/src/main/java/org/apache/commons/crypto/examples/CipherByteBufferExample.java
@@ -32,28 +32,30 @@ public class CipherByteBufferExample {
         Properties properties = new Properties();
         //Creates a CryptoCipher instance with the transformation and properties.
         final CipherTransformation transform = CipherTransformation.AES_CBC_PKCS5PADDING;
-        CryptoCipher encipher = Utils.getCipherInstance(transform, properties);
-
+        final ByteBuffer outBuffer;
         final int bufferSize = 1024;
+        final int updateBytes;
+        final int finalBytes;
+        try (CryptoCipher encipher = Utils.getCipherInstance(transform, properties)) {
 
-        ByteBuffer inBuffer = ByteBuffer.allocateDirect(bufferSize);
-        ByteBuffer outBuffer = ByteBuffer.allocateDirect(bufferSize);
-        inBuffer.put(getUTF8Bytes("hello world!"));
+            ByteBuffer inBuffer = ByteBuffer.allocateDirect(bufferSize);
+            outBuffer = ByteBuffer.allocateDirect(bufferSize);
+            inBuffer.put(getUTF8Bytes("hello world!"));
 
-        inBuffer.flip(); // ready for the cipher to read it
-        // Show the data is there
-        System.out.println("inBuffer="+asString(inBuffer));
+            inBuffer.flip(); // ready for the cipher to read it
+            // Show the data is there
+            System.out.println("inBuffer=" + asString(inBuffer));
 
-        // Initializes the cipher with ENCRYPT_MODE,key and iv.
-        encipher.init(Cipher.ENCRYPT_MODE, key, iv);
-        // Continues a multiple-part encryption/decryption operation for byte buffer.
-        final int updateBytes = encipher.update(inBuffer, outBuffer);
-        System.out.println(updateBytes);
+            // Initializes the cipher with ENCRYPT_MODE,key and iv.
+            encipher.init(Cipher.ENCRYPT_MODE, key, iv);
+            // Continues a multiple-part encryption/decryption operation for byte buffer.
+            updateBytes = encipher.update(inBuffer, outBuffer);
+            System.out.println(updateBytes);
 
-        // We should call do final at the end of encryption/decryption.
-        final int finalBytes = encipher.doFinal(inBuffer, outBuffer);
-        System.out.println(finalBytes);
-        encipher.close();
+            // We should call do final at the end of encryption/decryption.
+            finalBytes = encipher.doFinal(inBuffer, outBuffer);
+            System.out.println(finalBytes);
+        }
 
         outBuffer.flip(); // ready for use as decrypt
         byte [] encoded = new byte[updateBytes + finalBytes];
@@ -61,14 +63,14 @@ public class CipherByteBufferExample {
         System.out.println(Arrays.toString(encoded));
 
         // Now reverse the process
-        CryptoCipher decipher = Utils.getCipherInstance(transform, properties);
-        decipher.init(Cipher.DECRYPT_MODE, key, iv);
-        ByteBuffer decoded = ByteBuffer.allocateDirect(bufferSize);
-        decipher.update(outBuffer, decoded);
-        decipher.doFinal(outBuffer, decoded);
-        decipher.close();
-        decoded.flip(); // ready for use
-        System.out.println("decoded="+asString(decoded));
+        try (CryptoCipher decipher = Utils.getCipherInstance(transform, properties)) {
+            decipher.init(Cipher.DECRYPT_MODE, key, iv);
+            ByteBuffer decoded = ByteBuffer.allocateDirect(bufferSize);
+            decipher.update(outBuffer, decoded);
+            decipher.doFinal(outBuffer, decoded);
+            decoded.flip(); // ready for use
+            System.out.println("decoded="+asString(decoded));
+        }
     }
 
 }

http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/5c14932b/src/main/java/org/apache/commons/crypto/examples/StreamExample.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/crypto/examples/StreamExample.java b/src/main/java/org/apache/commons/crypto/examples/StreamExample.java
index 3cc874b..895843d 100644
--- a/src/main/java/org/apache/commons/crypto/examples/StreamExample.java
+++ b/src/main/java/org/apache/commons/crypto/examples/StreamExample.java
@@ -35,10 +35,10 @@ public class StreamExample {
 
         ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
 
-        CryptoOutputStream cos = new CryptoOutputStream(transform, properties, outputStream,
key, iv);
-        cos.write(getUTF8Bytes(input));
-        cos.flush();
-        cos.close();
+        try (CryptoOutputStream cos = new CryptoOutputStream(transform, properties, outputStream,
key, iv)) {
+            cos.write(getUTF8Bytes(input));
+            cos.flush();
+        }
 
         // The encrypted data:
         System.out.println("Encrypted: "+Arrays.toString(outputStream.toByteArray()));
@@ -46,15 +46,14 @@ public class StreamExample {
         // Decryption with CryptoInputStream.
         InputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
 
-        CryptoInputStream cis = new CryptoInputStream(transform, properties, inputStream,
key, iv);
-      
-        byte[] decryptedData  = new byte[1024];
-        int decryptedLen = 0;
-        int i;
-        while((i = cis.read(decryptedData, decryptedLen, decryptedData.length - decryptedLen))
> -1 ) {
-            decryptedLen += i;
+        try (CryptoInputStream cis = new CryptoInputStream(transform, properties, inputStream,
key, iv)) {
+            byte[] decryptedData = new byte[1024];
+            int decryptedLen = 0;
+            int i;
+            while ((i = cis.read(decryptedData, decryptedLen, decryptedData.length - decryptedLen))
> -1) {
+                decryptedLen += i;
+            }
+            System.out.println("Decrypted: "+new String(decryptedData, 0, decryptedLen, StandardCharsets.UTF_8));
         }
-        cis.close();
-        System.out.println("Decrypted: "+new String(decryptedData, 0, decryptedLen, StandardCharsets.UTF_8));
     }
 }


Mime
View raw message