Skip to content

Commit 297f04d

Browse files
Validate byte and string. (#78)
1 parent 2b32ca0 commit 297f04d

5 files changed

Lines changed: 31 additions & 7 deletions

File tree

src/main/java/org/apache/commons/codec/binary/Base64.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ public static byte[] encodeBase64(final byte[] binaryData, final boolean isChunk
235235
*/
236236
public static byte[] encodeBase64(final byte[] binaryData, final boolean isChunked,
237237
final boolean urlSafe, final int maxResultSize) {
238-
if (binaryData == null || binaryData.length == 0) {
238+
if (BinaryCodec.isEmpty(binaryData)) {
239239
return binaryData;
240240
}
241241

src/main/java/org/apache/commons/codec/binary/BaseNCodec.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ protected boolean containsAlphabetOrPad(final byte[] arrayOctet) {
427427
*/
428428
@Override
429429
public byte[] decode(final byte[] pArray) {
430-
if (pArray == null || pArray.length == 0) {
430+
if (BinaryCodec.isEmpty(pArray)) {
431431
return pArray;
432432
}
433433
final Context context = new Context();
@@ -483,7 +483,7 @@ public byte[] decode(final String pArray) {
483483
*/
484484
@Override
485485
public byte[] encode(final byte[] pArray) {
486-
if (pArray == null || pArray.length == 0) {
486+
if (BinaryCodec.isEmpty(pArray)) {
487487
return pArray;
488488
}
489489
return encode(pArray, 0, pArray.length);
@@ -503,7 +503,7 @@ public byte[] encode(final byte[] pArray) {
503503
* @since 1.11
504504
*/
505505
public byte[] encode(final byte[] pArray, final int offset, final int length) {
506-
if (pArray == null || pArray.length == 0) {
506+
if (BinaryCodec.isEmpty(pArray)) {
507507
return pArray;
508508
}
509509
final Context context = new Context();

src/main/java/org/apache/commons/codec/binary/BinaryCodec.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,10 @@ public static byte[] fromAscii(final char[] ascii) {
136136
* @param array
137137
* the source array
138138
* @return {@code true} if the given array is {@code null} or empty (size 0.)
139+
*
140+
* @since 1.16 change visibility to public
139141
*/
140-
private static boolean isEmpty(final byte[] array) {
142+
static boolean isEmpty(final byte[] array) {
141143
return array == null || array.length == 0;
142144
}
143145

src/main/java/org/apache/commons/codec/language/Caverphone2.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
package org.apache.commons.codec.language;
1919

20+
import org.apache.commons.codec.binary.StringUtils;
21+
2022
/**
2123
* Encodes a string into a Caverphone 2.0 value.
2224
*
@@ -43,7 +45,7 @@ public class Caverphone2 extends AbstractCaverphone {
4345
@Override
4446
public String encode(final String source) {
4547
String txt = source;
46-
if (txt == null || txt.isEmpty()) {
48+
if (SoundexUtils.isEmpty(txt)) {
4749
return TEN_1;
4850
}
4951

src/main/java/org/apache/commons/codec/language/SoundexUtils.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import org.apache.commons.codec.EncoderException;
2121
import org.apache.commons.codec.StringEncoder;
22+
import org.apache.commons.codec.binary.StringUtils;
2223

2324
/**
2425
* Utility methods for {@link Soundex} and {@link RefinedSoundex} classes.
@@ -38,7 +39,7 @@ final class SoundexUtils {
3839
* @return A clean String.
3940
*/
4041
static String clean(final String str) {
41-
if (str == null || str.isEmpty()) {
42+
if (isEmpty(str)) {
4243
return str;
4344
}
4445
final int len = str.length();
@@ -120,4 +121,23 @@ static int differenceEncoded(final String es1, final String es2) {
120121
return diff;
121122
}
122123

124+
/**
125+
* <p>Checks if a CharSequence is empty ("") or null.</p>
126+
*
127+
* <pre>
128+
* StringUtils.isEmpty(null) = true
129+
* StringUtils.isEmpty("") = true
130+
* StringUtils.isEmpty(" ") = false
131+
* StringUtils.isEmpty("bob") = false
132+
* StringUtils.isEmpty(" bob ") = false
133+
* </pre>
134+
*
135+
* @param cs the CharSequence to check, may be null
136+
* @return {@code true} if the CharSequence is empty or null
137+
* @since 1.16
138+
*/
139+
static boolean isEmpty(final CharSequence cs) {
140+
return cs == null || cs.length() == 0;
141+
}
142+
123143
}

0 commit comments

Comments
 (0)