Refactor null-safe length query code into new IOUtils methods.
MODIFY
src/main/java/org/apache/commons/io/IOUtils.java
src/main/java/org/apache/commons/io/IOUtils.java
@@ -3290,4 +3290,48 @@ public IOUtils() {
super();
}
+ /**
+ * Returns the length of the given array in a null-safe manner.
+ *
+ * @param array an array or null
+ * @return the array length or null if the given array is null.
+ * @since 2.7
+ */
+ public static int length(final byte[] array) {
+ return array == null ? 0 : array.length;
+ }
+
+ /**
+ * Returns the length of the given array in a null-safe manner.
+ *
+ * @param array an array or null
+ * @return the array length or null if the given array is null.
+ * @since 2.7
+ */
+ public static int length(final char[] array) {
+ return array == null ? 0 : array.length;
+ }
+
+ /**
+ * Returns the length of the given CharSequence in a null-safe manner.
+ *
+ * @param csq a CharSequence or null
+ * @return the CharSequence length or null if the given CharSequence is null.
+ * @since 2.7
+ */
+ public static int length(final CharSequence csq) {
+ return csq == null ? 0 : csq.length();
+ }
+
+ /**
+ * Returns the length of the given array in a null-safe manner.
+ *
+ * @param array an array or null
+ * @return the array length or null if the given array is null.
+ * @since 2.7
+ */
+ public static int length(final Object[] array) {
+ return array == null ? 0 : array.length;
+ }
+
}

MODIFY
src/main/java/org/apache/commons/io/input/BOMInputStream.java
src/main/java/org/apache/commons/io/input/BOMInputStream.java
import java.util.List;
import org.apache.commons.io.ByteOrderMark;
+import org.apache.commons.io.IOUtils;
/**
* This class is used to wrap a stream that includes an encoded {@link ByteOrderMark} as its first bytes.
@@ -164,7 +165,7 @@ public int compare(final ByteOrderMark bom1, final ByteOrderMark bom2) {
*/
public BOMInputStream(final InputStream delegate, final boolean include, final ByteOrderMark... boms) {
super(delegate);
- if (boms == null || boms.length == 0) {
+ if (IOUtils.length(boms) == 0) {
throw new IllegalArgumentException("No BOMs specified");
}
this.include = include;

MODIFY
src/main/java/org/apache/commons/io/input/ProxyInputStream.java
src/main/java/org/apache/commons/io/input/ProxyInputStream.java
import java.io.IOException;
import java.io.InputStream;
+import org.apache.commons.io.IOUtils;
+
/**
* A Proxy stream which acts as expected, that is it passes the method
* calls on to the proxied stream and doesn't change which methods are
@@ -74,7 +76,7 @@ public int read() throws IOException {
@Override
public int read(final byte[] bts) throws IOException {
try {
- beforeRead(bts != null ? bts.length : 0);
+ beforeRead(IOUtils.length(bts));
final int n = in.read(bts);
afterRead(n);
return n;

MODIFY
src/main/java/org/apache/commons/io/input/ProxyReader.java
src/main/java/org/apache/commons/io/input/ProxyReader.java
import java.io.Reader;
import java.nio.CharBuffer;
+import org.apache.commons.io.IOUtils;
+
/**
* A Proxy stream which acts as expected, that is it passes the method
* calls on to the proxied stream and doesn't change which methods are
@@ -72,7 +74,7 @@ public int read() throws IOException {
@Override
public int read(final char[] chr) throws IOException {
try {
- beforeRead(chr != null ? chr.length : 0);
+ beforeRead(IOUtils.length(chr));
final int n = in.read(chr);
afterRead(n);
return n;
@@ -113,7 +115,7 @@ public int read(final char[] chr, final int st, final int len) throws IOExceptio
@Override
public int read(final CharBuffer target) throws IOException {
try {
- beforeRead(target != null ? target.length() : 0);
+ beforeRead(IOUtils.length(target));
final int n = in.read(target);
afterRead(n);
return n;

MODIFY
src/main/java/org/apache/commons/io/output/ProxyOutputStream.java
src/main/java/org/apache/commons/io/output/ProxyOutputStream.java
import java.io.IOException;
import java.io.OutputStream;
+import org.apache.commons.io.IOUtils;
+
/**
* A Proxy stream which acts as expected, that is it passes the method
* calls on to the proxied stream and doesn't change which methods are
@@ -66,7 +68,7 @@ public void write(final int idx) throws IOException {
@Override
public void write(final byte[] bts) throws IOException {
try {
- final int len = bts != null ? bts.length : 0;
+ final int len = IOUtils.length(bts);
beforeWrite(len);
out.write(bts);
afterWrite(len);

MODIFY
src/main/java/org/apache/commons/io/output/ProxyWriter.java
src/main/java/org/apache/commons/io/output/ProxyWriter.java
import java.io.IOException;
import java.io.Writer;
+import org.apache.commons.io.IOUtils;
+
/**
* A Proxy stream which acts as expected, that is it passes the method
* calls on to the proxied stream and doesn't change which methods are
@@ -90,11 +92,7 @@ public Writer append(final CharSequence csq, final int start, final int end) thr
@Override
public Writer append(final CharSequence csq) throws IOException {
try {
- int len = 0;
- if (csq != null) {
- len = csq.length();
- }
-
+ int len = IOUtils.length(csq);
beforeWrite(len);
out.append(csq);
afterWrite(len);
@@ -128,11 +126,7 @@ public void write(final int c) throws IOException {
@Override
public void write(final char[] cbuf) throws IOException {
try {
- int len = 0;
- if (cbuf != null) {
- len = cbuf.length;
- }
-
+ int len = IOUtils.length(cbuf);
beforeWrite(len);
out.write(cbuf);
afterWrite(len);
@@ -167,11 +161,7 @@ public void write(final char[] cbuf, final int off, final int len) throws IOExce
@Override
public void write(final String str) throws IOException {
try {
- int len = 0;
- if (str != null) {
- len = str.length();
- }
-
+ int len = IOUtils.length(str);
beforeWrite(len);
out.write(str);
afterWrite(len);

Refactor null-safe length query code into new IOUtils methods.