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 2019/08/08 14:43:23 UTC
[commons-io] branch master updated: Refactor null-safe length query
code into new IOUtils methods.
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-io.git
The following commit(s) were added to refs/heads/master by this push:
new b3ce147 Refactor null-safe length query code into new IOUtils methods.
b3ce147 is described below
commit b3ce147431dbad1bbd2e91d527560d7d4a1b9def
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Thu Aug 8 10:43:20 2019 -0400
Refactor null-safe length query code into new IOUtils methods.
---
src/main/java/org/apache/commons/io/IOUtils.java | 44 ++++++++++++++++++++++
.../apache/commons/io/input/BOMInputStream.java | 3 +-
.../apache/commons/io/input/ProxyInputStream.java | 4 +-
.../org/apache/commons/io/input/ProxyReader.java | 6 ++-
.../commons/io/output/ProxyOutputStream.java | 4 +-
.../org/apache/commons/io/output/ProxyWriter.java | 20 +++-------
6 files changed, 61 insertions(+), 20 deletions(-)
diff --git a/src/main/java/org/apache/commons/io/IOUtils.java b/src/main/java/org/apache/commons/io/IOUtils.java
index a75578a..12a44dc 100644
--- a/src/main/java/org/apache/commons/io/IOUtils.java
+++ b/src/main/java/org/apache/commons/io/IOUtils.java
@@ -3290,4 +3290,48 @@ public class 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;
+ }
+
}
diff --git a/src/main/java/org/apache/commons/io/input/BOMInputStream.java b/src/main/java/org/apache/commons/io/input/BOMInputStream.java
index 81b7844..9ce7fa8 100644
--- a/src/main/java/org/apache/commons/io/input/BOMInputStream.java
+++ b/src/main/java/org/apache/commons/io/input/BOMInputStream.java
@@ -26,6 +26,7 @@ import java.util.Comparator;
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 class BOMInputStream extends ProxyInputStream {
*/
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;
diff --git a/src/main/java/org/apache/commons/io/input/ProxyInputStream.java b/src/main/java/org/apache/commons/io/input/ProxyInputStream.java
index d449cec..0616d13 100644
--- a/src/main/java/org/apache/commons/io/input/ProxyInputStream.java
+++ b/src/main/java/org/apache/commons/io/input/ProxyInputStream.java
@@ -22,6 +22,8 @@ import java.io.FilterInputStream;
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 abstract class ProxyInputStream extends FilterInputStream {
@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;
diff --git a/src/main/java/org/apache/commons/io/input/ProxyReader.java b/src/main/java/org/apache/commons/io/input/ProxyReader.java
index 75fadee..5c27159 100644
--- a/src/main/java/org/apache/commons/io/input/ProxyReader.java
+++ b/src/main/java/org/apache/commons/io/input/ProxyReader.java
@@ -23,6 +23,8 @@ import java.io.IOException;
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 abstract class ProxyReader extends FilterReader {
@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 abstract class ProxyReader extends FilterReader {
@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;
diff --git a/src/main/java/org/apache/commons/io/output/ProxyOutputStream.java b/src/main/java/org/apache/commons/io/output/ProxyOutputStream.java
index 4f8f73f..34d7044 100644
--- a/src/main/java/org/apache/commons/io/output/ProxyOutputStream.java
+++ b/src/main/java/org/apache/commons/io/output/ProxyOutputStream.java
@@ -20,6 +20,8 @@ import java.io.FilterOutputStream;
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 class ProxyOutputStream extends FilterOutputStream {
@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);
diff --git a/src/main/java/org/apache/commons/io/output/ProxyWriter.java b/src/main/java/org/apache/commons/io/output/ProxyWriter.java
index c234079..79dec7e 100644
--- a/src/main/java/org/apache/commons/io/output/ProxyWriter.java
+++ b/src/main/java/org/apache/commons/io/output/ProxyWriter.java
@@ -20,6 +20,8 @@ import java.io.FilterWriter;
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 class ProxyWriter extends FilterWriter {
@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 class ProxyWriter extends FilterWriter {
@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 class ProxyWriter extends FilterWriter {
@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);