Skip to content

Commit d2f2709

Browse files
committed
CODEC-229 StringUtils.newStringxxx(null) should return null, not NPE
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/codec/trunk@1788755 13f79535-47bb-0310-9956-ffa450edef68
1 parent 1a4d9cc commit d2f2709

3 files changed

Lines changed: 17 additions & 6 deletions

File tree

src/changes/changes.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,10 @@ The <action> type attribute can be add,update,fix,remove.
4242
<author>Apache Commons Developers</author>
4343
</properties>
4444
<body>
45-
<release version="1.11" date="2016-MM-DD" description="Feature and fix release.">
45+
<release version="1.11" date="2017-MM-DD" description="Feature and fix release.">
4646
<!-- The first attribute below should be the issue id; makes it easier to navigate in the IDE outline -->
4747

48+
<action issue="CODEC-229" dev="sebb" type="fix">StringUtils.newStringxxx(null) should return null, not NPE</action>
4849
<action issue="CODEC-220" dev="sebb" type="add">Fluent interface for DigestUtils</action>
4950
<action issue="CODEC-222" dev="sebb" type="add">Fluent interface for HmacUtils</action>
5051
<action issue="CODEC-225" dev="jochen" type="fix" due-to="Svetlin Zarev">Fix minor resource leaks</action>

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ public static String newString(final byte[] bytes, final String charsetName) {
336336
* @since As of 1.7, throws {@link NullPointerException} instead of UnsupportedEncodingException
337337
*/
338338
public static String newStringIso8859_1(final byte[] bytes) {
339-
return new String(bytes, Charsets.ISO_8859_1);
339+
return newString(bytes, Charsets.ISO_8859_1);
340340
}
341341

342342
/**
@@ -352,7 +352,7 @@ public static String newStringIso8859_1(final byte[] bytes) {
352352
* @since As of 1.7, throws {@link NullPointerException} instead of UnsupportedEncodingException
353353
*/
354354
public static String newStringUsAscii(final byte[] bytes) {
355-
return new String(bytes, Charsets.US_ASCII);
355+
return newString(bytes, Charsets.US_ASCII);
356356
}
357357

358358
/**
@@ -368,7 +368,7 @@ public static String newStringUsAscii(final byte[] bytes) {
368368
* @since As of 1.7, throws {@link NullPointerException} instead of UnsupportedEncodingException
369369
*/
370370
public static String newStringUtf16(final byte[] bytes) {
371-
return new String(bytes, Charsets.UTF_16);
371+
return newString(bytes, Charsets.UTF_16);
372372
}
373373

374374
/**
@@ -384,7 +384,7 @@ public static String newStringUtf16(final byte[] bytes) {
384384
* @since As of 1.7, throws {@link NullPointerException} instead of UnsupportedEncodingException
385385
*/
386386
public static String newStringUtf16Be(final byte[] bytes) {
387-
return new String(bytes, Charsets.UTF_16BE);
387+
return newString(bytes, Charsets.UTF_16BE);
388388
}
389389

390390
/**
@@ -400,7 +400,7 @@ public static String newStringUtf16Be(final byte[] bytes) {
400400
* @since As of 1.7, throws {@link NullPointerException} instead of UnsupportedEncodingException
401401
*/
402402
public static String newStringUtf16Le(final byte[] bytes) {
403-
return new String(bytes, Charsets.UTF_16LE);
403+
return newString(bytes, Charsets.UTF_16LE);
404404
}
405405

406406
/**

src/test/java/org/apache/commons/codec/binary/StringUtilsTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,16 @@ public void testNewStringNullInput() {
145145
Assert.assertNull(StringUtils.newString(null, "UNKNOWN"));
146146
}
147147

148+
@Test
149+
public void testNewStringNullInput_CODEC229() {
150+
Assert.assertNull(StringUtils.newStringUtf8(null));
151+
Assert.assertNull(StringUtils.newStringIso8859_1(null));
152+
Assert.assertNull(StringUtils.newStringUsAscii(null));
153+
Assert.assertNull(StringUtils.newStringUtf16(null));
154+
Assert.assertNull(StringUtils.newStringUtf16Be(null));
155+
Assert.assertNull(StringUtils.newStringUtf16Le(null));
156+
}
157+
148158
@Test
149159
public void testNewStringIso8859_1() throws UnsupportedEncodingException {
150160
final String charsetName = "ISO-8859-1";

0 commit comments

Comments
 (0)