You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2020/01/26 17:13:20 UTC

[commons-crypto] branch master updated: Use Objects.requireNonNull() instead of custom check.

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


The following commit(s) were added to refs/heads/master by this push:
     new 7d5c558  Use Objects.requireNonNull() instead of custom check.
7d5c558 is described below

commit 7d5c55819bc032b036041e779e8da7c80d8ff2ef
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sun Jan 26 12:13:15 2020 -0500

    Use Objects.requireNonNull() instead of custom check.
    
    Better param name.
---
 .../apache/commons/crypto/stream/CryptoInputStream.java    | 14 +++++++-------
 .../apache/commons/crypto/stream/CryptoOutputStream.java   | 14 +++++++-------
 src/main/java/org/apache/commons/crypto/utils/Utils.java   |  6 ++----
 .../crypto/stream/PositionedCryptoInputStreamTest.java     |  6 +++---
 4 files changed, 19 insertions(+), 21 deletions(-)

diff --git a/src/main/java/org/apache/commons/crypto/stream/CryptoInputStream.java b/src/main/java/org/apache/commons/crypto/stream/CryptoInputStream.java
index b346048..dba3903 100644
--- a/src/main/java/org/apache/commons/crypto/stream/CryptoInputStream.java
+++ b/src/main/java/org/apache/commons/crypto/stream/CryptoInputStream.java
@@ -26,6 +26,7 @@ import java.security.InvalidAlgorithmParameterException;
 import java.security.InvalidKeyException;
 import java.security.Key;
 import java.security.spec.AlgorithmParameterSpec;
+import java.util.Objects;
 import java.util.Properties;
 import javax.crypto.BadPaddingException;
 import javax.crypto.Cipher;
@@ -229,7 +230,7 @@ public class CryptoInputStream extends InputStream implements
      * read it out of this buffer. If there is no data in {@link #outBuffer},
      * then read more from the underlying stream and do the decryption.
      *
-     * @param b the buffer into which the decrypted data is read.
+     * @param array the buffer into which the decrypted data is read.
      * @param off the buffer offset.
      * @param len the maximum number of decrypted data bytes to read.
      * @return int the total number of decrypted data bytes read into the
@@ -237,11 +238,10 @@ public class CryptoInputStream extends InputStream implements
      * @throws IOException if an I/O error occurs.
      */
     @Override
-    public int read(byte[] b, int off, int len) throws IOException {
+    public int read(byte[] array, int off, int len) throws IOException {
         checkStream();
-        if (b == null) {
-            throw new NullPointerException();
-        } else if (off < 0 || len < 0 || len > b.length - off) {
+        Objects.requireNonNull(array, "array");
+        if (off < 0 || len < 0 || len > array.length - off) {
             throw new IndexOutOfBoundsException();
         } else if (len == 0) {
             return 0;
@@ -251,7 +251,7 @@ public class CryptoInputStream extends InputStream implements
         if (remaining > 0) {
             // Satisfy the read with the existing data
             int n = Math.min(len, remaining);
-            outBuffer.get(b, off, n);
+            outBuffer.get(array, off, n);
             return n;
         }
         // No data in the out buffer, try read new data and decrypt it
@@ -265,7 +265,7 @@ public class CryptoInputStream extends InputStream implements
         }
 
         int n = Math.min(len, outBuffer.remaining());
-        outBuffer.get(b, off, n);
+        outBuffer.get(array, off, n);
         return n;
     }
 
diff --git a/src/main/java/org/apache/commons/crypto/stream/CryptoOutputStream.java b/src/main/java/org/apache/commons/crypto/stream/CryptoOutputStream.java
index a7c7ae4..273637c 100644
--- a/src/main/java/org/apache/commons/crypto/stream/CryptoOutputStream.java
+++ b/src/main/java/org/apache/commons/crypto/stream/CryptoOutputStream.java
@@ -26,6 +26,7 @@ import java.security.InvalidAlgorithmParameterException;
 import java.security.InvalidKeyException;
 import java.security.Key;
 import java.security.spec.AlgorithmParameterSpec;
+import java.util.Objects;
 import java.util.Properties;
 import javax.crypto.BadPaddingException;
 import javax.crypto.Cipher;
@@ -213,27 +214,26 @@ public class CryptoOutputStream extends OutputStream implements
      * then write to this buffer. If {@link #inBuffer} is full, then do
      * encryption and write data to the underlying stream.
      *
-     * @param b the data.
+     * @param array the data.
      * @param off the start offset in the data.
      * @param len the number of bytes to write.
      * @throws IOException if an I/O error occurs.
      */
     @Override
-    public void write(byte[] b, int off, int len) throws IOException {
+    public void write(byte[] array, int off, int len) throws IOException {
         checkStream();
-        if (b == null) {
-            throw new NullPointerException();
-        } else if (off < 0 || len < 0 || off > b.length || len > b.length - off) {
+        Objects.requireNonNull(array, "array");
+        if (off < 0 || len < 0 || off > array.length || len > array.length - off) {
             throw new IndexOutOfBoundsException();
         }
 
         while (len > 0) {
             final int remaining = inBuffer.remaining();
             if (len < remaining) {
-                inBuffer.put(b, off, len);
+                inBuffer.put(array, off, len);
                 len = 0;
             } else {
-                inBuffer.put(b, off, remaining);
+                inBuffer.put(array, off, remaining);
                 off += remaining;
                 len -= remaining;
                 encrypt();
diff --git a/src/main/java/org/apache/commons/crypto/utils/Utils.java b/src/main/java/org/apache/commons/crypto/utils/Utils.java
index ae6324a..03d5f15 100644
--- a/src/main/java/org/apache/commons/crypto/utils/Utils.java
+++ b/src/main/java/org/apache/commons/crypto/utils/Utils.java
@@ -23,6 +23,7 @@ import java.security.GeneralSecurityException;
 import java.util.ArrayList;
 import java.util.Enumeration;
 import java.util.List;
+import java.util.Objects;
 import java.util.Properties;
 
 import org.apache.commons.crypto.Crypto;
@@ -169,10 +170,7 @@ public final class Utils {
      * @throws NullPointerException if reference is null.
      */
     public static <T> T checkNotNull(T reference) {
-        if (reference == null) {
-            throw new NullPointerException();
-        }
-        return reference;
+        return Objects.requireNonNull(reference, "reference");
     }
 
     /**
diff --git a/src/test/java/org/apache/commons/crypto/stream/PositionedCryptoInputStreamTest.java b/src/test/java/org/apache/commons/crypto/stream/PositionedCryptoInputStreamTest.java
index 465ce4a..c8d27ac 100644
--- a/src/test/java/org/apache/commons/crypto/stream/PositionedCryptoInputStreamTest.java
+++ b/src/test/java/org/apache/commons/crypto/stream/PositionedCryptoInputStreamTest.java
@@ -36,6 +36,7 @@ import java.io.OutputStream;
 import java.nio.ByteBuffer;
 import java.security.SecureRandom;
 import java.util.Arrays;
+import java.util.Objects;
 import java.util.Properties;
 import java.util.Random;
 
@@ -340,9 +341,8 @@ public class PositionedCryptoInputStreamTest {
         @Override
         public int read(long position, byte[] buffer, int offset, int length)
                 throws IOException {
-            if (buffer == null) {
-                throw new NullPointerException();
-            } else if (offset < 0 || length < 0
+            Objects.requireNonNull(buffer, "buffer");
+            if (offset < 0 || length < 0
                     || length > buffer.length - offset) {
                 throw new IndexOutOfBoundsException();
             }