Skip to content

Commit

Permalink
Use Objects.requireNonNull() instead of custom check.
Browse files Browse the repository at this point in the history
Better param name.
  • Loading branch information
garydgregory committed Jan 26, 2020
1 parent 836bee5 commit 7d5c558
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
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;
Expand Down Expand Up @@ -229,19 +230,18 @@ public int read() throws IOException {
* 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
* buffer.
* @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;
Expand All @@ -251,7 +251,7 @@ public int read(byte[] b, int off, int len) throws IOException {
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
Expand All @@ -265,7 +265,7 @@ public int read(byte[] b, int off, int len) throws IOException {
}

int n = Math.min(len, outBuffer.remaining());
outBuffer.get(b, off, n);
outBuffer.get(array, off, n);
return n;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
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;
Expand Down Expand Up @@ -213,27 +214,26 @@ public void write(int b) throws IOException {
* 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();
Expand Down
6 changes: 2 additions & 4 deletions src/main/java/org/apache/commons/crypto/utils/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
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;
Expand Down Expand Up @@ -169,10 +170,7 @@ public static void checkArgument(boolean expression, Object errorMessage) {
* @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");
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.nio.ByteBuffer;
import java.security.SecureRandom;
import java.util.Arrays;
import java.util.Objects;
import java.util.Properties;
import java.util.Random;

Expand Down Expand Up @@ -340,9 +341,8 @@ public long skip(long n) throws IOException {
@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();
}
Expand Down

0 comments on commit 7d5c558

Please sign in to comment.