Skip to content

Commit b3ce147

Browse files
author
Gary Gregory
committed
Refactor null-safe length query code into new IOUtils methods.
1 parent 48f6d3f commit b3ce147

6 files changed

Lines changed: 61 additions & 20 deletions

File tree

src/main/java/org/apache/commons/io/IOUtils.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3290,4 +3290,48 @@ public IOUtils() {
32903290
super();
32913291
}
32923292

3293+
/**
3294+
* Returns the length of the given array in a null-safe manner.
3295+
*
3296+
* @param array an array or null
3297+
* @return the array length or null if the given array is null.
3298+
* @since 2.7
3299+
*/
3300+
public static int length(final byte[] array) {
3301+
return array == null ? 0 : array.length;
3302+
}
3303+
3304+
/**
3305+
* Returns the length of the given array in a null-safe manner.
3306+
*
3307+
* @param array an array or null
3308+
* @return the array length or null if the given array is null.
3309+
* @since 2.7
3310+
*/
3311+
public static int length(final char[] array) {
3312+
return array == null ? 0 : array.length;
3313+
}
3314+
3315+
/**
3316+
* Returns the length of the given CharSequence in a null-safe manner.
3317+
*
3318+
* @param csq a CharSequence or null
3319+
* @return the CharSequence length or null if the given CharSequence is null.
3320+
* @since 2.7
3321+
*/
3322+
public static int length(final CharSequence csq) {
3323+
return csq == null ? 0 : csq.length();
3324+
}
3325+
3326+
/**
3327+
* Returns the length of the given array in a null-safe manner.
3328+
*
3329+
* @param array an array or null
3330+
* @return the array length or null if the given array is null.
3331+
* @since 2.7
3332+
*/
3333+
public static int length(final Object[] array) {
3334+
return array == null ? 0 : array.length;
3335+
}
3336+
32933337
}

src/main/java/org/apache/commons/io/input/BOMInputStream.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.util.List;
2727

2828
import org.apache.commons.io.ByteOrderMark;
29+
import org.apache.commons.io.IOUtils;
2930

3031
/**
3132
* 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) {
164165
*/
165166
public BOMInputStream(final InputStream delegate, final boolean include, final ByteOrderMark... boms) {
166167
super(delegate);
167-
if (boms == null || boms.length == 0) {
168+
if (IOUtils.length(boms) == 0) {
168169
throw new IllegalArgumentException("No BOMs specified");
169170
}
170171
this.include = include;

src/main/java/org/apache/commons/io/input/ProxyInputStream.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import java.io.IOException;
2323
import java.io.InputStream;
2424

25+
import org.apache.commons.io.IOUtils;
26+
2527
/**
2628
* A Proxy stream which acts as expected, that is it passes the method
2729
* calls on to the proxied stream and doesn't change which methods are
@@ -74,7 +76,7 @@ public int read() throws IOException {
7476
@Override
7577
public int read(final byte[] bts) throws IOException {
7678
try {
77-
beforeRead(bts != null ? bts.length : 0);
79+
beforeRead(IOUtils.length(bts));
7880
final int n = in.read(bts);
7981
afterRead(n);
8082
return n;

src/main/java/org/apache/commons/io/input/ProxyReader.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import java.io.Reader;
2424
import java.nio.CharBuffer;
2525

26+
import org.apache.commons.io.IOUtils;
27+
2628
/**
2729
* A Proxy stream which acts as expected, that is it passes the method
2830
* calls on to the proxied stream and doesn't change which methods are
@@ -72,7 +74,7 @@ public int read() throws IOException {
7274
@Override
7375
public int read(final char[] chr) throws IOException {
7476
try {
75-
beforeRead(chr != null ? chr.length : 0);
77+
beforeRead(IOUtils.length(chr));
7678
final int n = in.read(chr);
7779
afterRead(n);
7880
return n;
@@ -113,7 +115,7 @@ public int read(final char[] chr, final int st, final int len) throws IOExceptio
113115
@Override
114116
public int read(final CharBuffer target) throws IOException {
115117
try {
116-
beforeRead(target != null ? target.length() : 0);
118+
beforeRead(IOUtils.length(target));
117119
final int n = in.read(target);
118120
afterRead(n);
119121
return n;

src/main/java/org/apache/commons/io/output/ProxyOutputStream.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import java.io.IOException;
2121
import java.io.OutputStream;
2222

23+
import org.apache.commons.io.IOUtils;
24+
2325
/**
2426
* A Proxy stream which acts as expected, that is it passes the method
2527
* 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 {
6668
@Override
6769
public void write(final byte[] bts) throws IOException {
6870
try {
69-
final int len = bts != null ? bts.length : 0;
71+
final int len = IOUtils.length(bts);
7072
beforeWrite(len);
7173
out.write(bts);
7274
afterWrite(len);

src/main/java/org/apache/commons/io/output/ProxyWriter.java

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import java.io.IOException;
2121
import java.io.Writer;
2222

23+
import org.apache.commons.io.IOUtils;
24+
2325
/**
2426
* A Proxy stream which acts as expected, that is it passes the method
2527
* 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
9092
@Override
9193
public Writer append(final CharSequence csq) throws IOException {
9294
try {
93-
int len = 0;
94-
if (csq != null) {
95-
len = csq.length();
96-
}
97-
95+
int len = IOUtils.length(csq);
9896
beforeWrite(len);
9997
out.append(csq);
10098
afterWrite(len);
@@ -128,11 +126,7 @@ public void write(final int c) throws IOException {
128126
@Override
129127
public void write(final char[] cbuf) throws IOException {
130128
try {
131-
int len = 0;
132-
if (cbuf != null) {
133-
len = cbuf.length;
134-
}
135-
129+
int len = IOUtils.length(cbuf);
136130
beforeWrite(len);
137131
out.write(cbuf);
138132
afterWrite(len);
@@ -167,11 +161,7 @@ public void write(final char[] cbuf, final int off, final int len) throws IOExce
167161
@Override
168162
public void write(final String str) throws IOException {
169163
try {
170-
int len = 0;
171-
if (str != null) {
172-
len = str.length();
173-
}
174-
164+
int len = IOUtils.length(str);
175165
beforeWrite(len);
176166
out.write(str);
177167
afterWrite(len);

0 commit comments

Comments
 (0)