You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by se...@apache.org on 2016/06/21 23:14:59 UTC

commons-crypto git commit: Simplify

Repository: commons-crypto
Updated Branches:
  refs/heads/master d0fd391d1 -> e1021d82b


Simplify

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

Branch: refs/heads/master
Commit: e1021d82baed3c81d65a433d35f0d21589e10677
Parents: d0fd391
Author: Sebb <se...@apache.org>
Authored: Wed Jun 22 00:14:55 2016 +0100
Committer: Sebb <se...@apache.org>
Committed: Wed Jun 22 00:14:55 2016 +0100

----------------------------------------------------------------------
 .../crypto/examples/CipherByteBufferExample.java       | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/e1021d82/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 5b2c1fb..367cad4 100644
--- a/src/main/java/org/apache/commons/crypto/examples/CipherByteBufferExample.java
+++ b/src/main/java/org/apache/commons/crypto/examples/CipherByteBufferExample.java
@@ -19,11 +19,8 @@ public class CipherByteBufferExample {
         return input.getBytes(StandardCharsets.UTF_8);
     }
 
-    private static String asString(ByteBuffer buffer, boolean flip) {
+    private static String asString(ByteBuffer buffer) {
         final ByteBuffer copy = buffer.duplicate();
-        if (flip) {
-            copy.flip();
-        }
         final byte[] bytes = new byte[Math.min(copy.remaining(),50)];
         copy.get(bytes);
         return new String(bytes, StandardCharsets.UTF_8);
@@ -42,10 +39,11 @@ public class CipherByteBufferExample {
         ByteBuffer inBuffer = ByteBuffer.allocateDirect(bufferSize);
         ByteBuffer outBuffer = ByteBuffer.allocateDirect(bufferSize);
         inBuffer.put(getUTF8Bytes("hello world!"));
-        // Show the data is there
-        System.out.println("inBuffer="+asString(inBuffer, true));
 
         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.
@@ -69,7 +67,8 @@ public class CipherByteBufferExample {
         decipher.update(outBuffer, decoded);
         decipher.doFinal(outBuffer, decoded);
         decipher.close();
-        System.out.println("decoded="+asString(decoded, true));
+        decoded.flip(); // ready for use
+        System.out.println("decoded="+asString(decoded));
     }
 
 }