This is an automated email from the ASF dual-hosted git repository.

garydgregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-codec.git

commit 1cc420e7f5f5decab1af8cb3a609b25648fee02a
Author: Gary Gregory <[email protected]>
AuthorDate: Sat Nov 14 13:49:25 2026 -0500

    Handle null charsets consistently across codecs
    
    Use the JVM default charset for null charset arguments in Hex,
    BCodec, QCodec, QuotedPrintableCodec, URLCodec, and StringUtils.
    Keep RFC 1522 charset labels consistent with the encoded bytes
    and preserve existing invalid charset-name exception behavior.
    
    Update Javadoc and release notes, and add regression tests.
---
 src/changes/changes.xml                            |  7 +++++
 .../java/org/apache/commons/codec/Charsets.java    |  5 +++-
 .../java/org/apache/commons/codec/binary/Hex.java  |  9 ++++---
 .../apache/commons/codec/binary/StringUtils.java   |  8 +++---
 .../java/org/apache/commons/codec/net/BCodec.java  | 11 ++++----
 .../java/org/apache/commons/codec/net/QCodec.java  |  9 ++++---
 .../commons/codec/net/QuotedPrintableCodec.java    | 29 ++++++++++----------
 .../org/apache/commons/codec/net/RFC1522Codec.java | 14 +++++-----
 .../org/apache/commons/codec/net/URLCodec.java     | 13 ++++-----
 .../org/apache/commons/codec/binary/HexTest.java   | 11 ++++++++
 .../commons/codec/binary/StringUtilsTest.java      | 18 +++++++++++++
 .../org/apache/commons/codec/net/BCodecTest.java   | 11 ++++++++
 .../org/apache/commons/codec/net/QCodecTest.java   | 10 +++++++
 .../codec/net/QuotedPrintableCodecTest.java        | 31 ++++++++++++++++++++++
 .../org/apache/commons/codec/net/URLCodecTest.java | 25 +++++++++++++++++
 15 files changed, 167 insertions(+), 44 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 45497677..bb2de828 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -53,6 +53,13 @@ The <action> type attribute can be add,update,fix,remove.
       <action type="fix" dev="ggregory" due-to="Gary 
Gregory">QuotedPrintableCodec(String) now uses the JVM default charset when the 
charset name is null.</action>
       <action type="fix" dev="ggregory" due-to="Gary 
Gregory">RFC1522Codec.encodeText(String, String) now uses the JVM default 
charset when the charset name is null.</action>
       <action type="fix" dev="ggregory" due-to="Atharva Dudhe, Gary 
Gregory">QuotedPrintableCodec strict mode returns null for inputs shorter than 
3 bytes (#455⁠).</action>
+      <action type="fix" dev="ggregory" due-to="Gary Gregory">Hex now uses the 
JVM default charset when its Charset constructor argument is null.</action>
+      <action type="fix" dev="ggregory" due-to="Gary Gregory">BCodec now uses 
the JVM default charset for null Charset arguments in constructors and 
encoding.</action>
+      <action type="fix" dev="ggregory" due-to="Gary Gregory">QCodec now uses 
the JVM default charset for null Charset arguments in its constructor and 
encoding.</action>
+      <action type="fix" dev="ggregory" due-to="Gary Gregory">RFC1522Codec now 
uses the JVM default charset for null Charset arguments in its constructor and 
encoding, keeping the encoded-word charset label consistent with the encoded 
bytes.</action>
+      <action type="fix" dev="ggregory" due-to="Gary 
Gregory">QuotedPrintableCodec now uses the JVM default charset for null Charset 
constructor arguments and null charset arguments in encoding and 
decoding.</action>
+      <action type="fix" dev="ggregory" due-to="Gary Gregory">URLCodec now 
uses the JVM default charset for null charset names in its constructor, 
encoding, and decoding.</action>
+      <action type="fix" dev="ggregory" due-to="Gary Gregory">StringUtils now 
uses the JVM default charset for null charset names in getBytesUnchecked and 
newString.</action>
       <!-- ADD -->
       <!-- UPDATE -->
       <action type="update" dev="ggregory" due-to="Gary Gregory">Bump 
org.apache.commons:commons-parent from 105 to 107.</action>
diff --git a/src/main/java/org/apache/commons/codec/Charsets.java 
b/src/main/java/org/apache/commons/codec/Charsets.java
index da32bba8..d73421d4 100644
--- a/src/main/java/org/apache/commons/codec/Charsets.java
+++ b/src/main/java/org/apache/commons/codec/Charsets.java
@@ -17,7 +17,9 @@
 package org.apache.commons.codec;
 
 import java.nio.charset.Charset;
+import java.nio.charset.IllegalCharsetNameException;
 import java.nio.charset.StandardCharsets;
+import java.nio.charset.UnsupportedCharsetException;
 
 /**
  * Charsets required of every implementation of the Java platform.
@@ -170,7 +172,8 @@ public class Charsets {
      *
      * @param charset The name of the requested charset, may be null.
      * @return A Charset for the named charset.
-     * @throws java.nio.charset.UnsupportedCharsetException Thrown if the 
named charset is unavailable.
+     * @throws IllegalCharsetNameException Thrown if the given charset name is 
illegal.
+     * @throws UnsupportedCharsetException Thrown if no support for the named 
charset is available in this instance of the Java virtual machine.
      */
     public static Charset toCharset(final String charset) {
         return charset == null ? Charset.defaultCharset() : 
Charset.forName(charset);
diff --git a/src/main/java/org/apache/commons/codec/binary/Hex.java 
b/src/main/java/org/apache/commons/codec/binary/Hex.java
index 3943c519..6c712600 100644
--- a/src/main/java/org/apache/commons/codec/binary/Hex.java
+++ b/src/main/java/org/apache/commons/codec/binary/Hex.java
@@ -19,7 +19,9 @@ package org.apache.commons.codec.binary;
 
 import java.nio.ByteBuffer;
 import java.nio.charset.Charset;
+import java.nio.charset.IllegalCharsetNameException;
 import java.nio.charset.StandardCharsets;
+import java.nio.charset.UnsupportedCharsetException;
 
 import org.apache.commons.codec.BinaryDecoder;
 import org.apache.commons.codec.BinaryEncoder;
@@ -416,18 +418,19 @@ public class Hex implements BinaryEncoder, BinaryDecoder {
     /**
      * Creates a new codec with the given Charset.
      *
-     * @param charset The charset.
+     * @param charset The charset, or {@code null} to use {@link 
Charset#defaultCharset()}.
      * @since 1.7
      */
     public Hex(final Charset charset) {
-        this.charset = charset;
+        this.charset = Charsets.toCharset(charset);
     }
 
     /**
      * Creates a new codec with the given charset name.
      *
      * @param charsetName The charset name, or {@code null} to use {@link 
Charset#defaultCharset()}.
-     * @throws java.nio.charset.UnsupportedCharsetException Thrown if the 
named charset is unavailable.
+     * @throws IllegalCharsetNameException Thrown if the given charset name is 
illegal.
+     * @throws UnsupportedCharsetException Thrown if no support for the named 
charset is available in this instance of the Java virtual machine.
      * @since 1.4
      * @since 1.7 throws UnsupportedCharsetException if the named charset is 
unavailable
      */
diff --git a/src/main/java/org/apache/commons/codec/binary/StringUtils.java 
b/src/main/java/org/apache/commons/codec/binary/StringUtils.java
index f5560488..babc8079 100644
--- a/src/main/java/org/apache/commons/codec/binary/StringUtils.java
+++ b/src/main/java/org/apache/commons/codec/binary/StringUtils.java
@@ -158,7 +158,7 @@ public class StringUtils {
      * @param string
      *            the String to encode, may be {@code null}.
      * @param charsetName
-     *            The name of a required {@link Charset}.
+     *            The name of a required {@link Charset}, or {@code null} to 
use {@link java.nio.charset.Charset#defaultCharset()}.
      * @return encoded bytes, or {@code null} if the input string was {@code 
null}.
      * @throws IllegalStateException
      *             Thrown when a {@link UnsupportedEncodingException} is 
caught, which should never happen for a
@@ -171,7 +171,7 @@ public class StringUtils {
             return null;
         }
         try {
-            return string.getBytes(charsetName);
+            return string.getBytes(charsetName == null ? 
Charset.defaultCharset().name() : charsetName);
         } catch (final UnsupportedEncodingException e) {
             throw newIllegalStateException(charsetName, e);
         }
@@ -297,7 +297,7 @@ public class StringUtils {
      * @param bytes
      *            The bytes to be decoded into characters, may be {@code null}.
      * @param charsetName
-     *            The name of a required {@link Charset}.
+     *            The name of a required {@link Charset}, or {@code null} to 
use {@link java.nio.charset.Charset#defaultCharset()}.
      * @return A new {@code String} decoded from the specified array of bytes 
using the given charset,
      *         or {@code null} if the input byte array was {@code null}.
      * @throws IllegalStateException
@@ -311,7 +311,7 @@ public class StringUtils {
             return null;
         }
         try {
-            return new String(bytes, charsetName);
+            return new String(bytes, charsetName == null ? 
Charset.defaultCharset().name() : charsetName);
         } catch (final UnsupportedEncodingException e) {
             throw newIllegalStateException(charsetName, e);
         }
diff --git a/src/main/java/org/apache/commons/codec/net/BCodec.java 
b/src/main/java/org/apache/commons/codec/net/BCodec.java
index 34948d76..ebc785a6 100644
--- a/src/main/java/org/apache/commons/codec/net/BCodec.java
+++ b/src/main/java/org/apache/commons/codec/net/BCodec.java
@@ -19,6 +19,7 @@ package org.apache.commons.codec.net;
 
 import java.io.UnsupportedEncodingException;
 import java.nio.charset.Charset;
+import java.nio.charset.IllegalCharsetNameException;
 import java.nio.charset.StandardCharsets;
 import java.nio.charset.UnsupportedCharsetException;
 
@@ -87,7 +88,7 @@ public class BCodec extends RFC1522Codec implements 
StringEncoder, StringDecoder
      * Constructs a new instance for the selection of a default Charset.
      *
      * @param charset
-     *            the default string Charset to use.
+     *            the default string Charset to use, or {@code null} to use 
{@link Charset#defaultCharset()}.
      *
      * @see Charset
      * @since 1.7
@@ -105,7 +106,7 @@ public class BCodec extends RFC1522Codec implements 
StringEncoder, StringDecoder
      * </p>
      *
      * @param charset
-     *            the default string Charset to use.
+     *            the default string Charset to use, or {@code null} to use 
{@link Charset#defaultCharset()}.
      * @param decodingPolicy The decoding policy.
      * @see Charset
      * @since 1.15
@@ -119,8 +120,8 @@ public class BCodec extends RFC1522Codec implements 
StringEncoder, StringDecoder
      * Constructs a new instance for the selection of a default Charset.
      *
      * @param charsetName The charset name, or {@code null} to use {@link 
Charset#defaultCharset()}.
-     * @throws java.nio.charset.UnsupportedCharsetException
-     *             Thrown if the named Charset is unavailable.
+     * @throws IllegalCharsetNameException Thrown if the given charset name is 
illegal.
+     * @throws UnsupportedCharsetException Thrown if no support for the named 
charset is available in this instance of the Java virtual machine.
      * @since 1.7 throws UnsupportedCharsetException if the named Charset is 
unavailable
      * @see Charset
      */
@@ -252,7 +253,7 @@ public class BCodec extends RFC1522Codec implements 
StringEncoder, StringDecoder
      * @param strSource
      *            string to convert to Base64 form.
      * @param sourceCharset
-     *            the Charset for {@code value}.
+     *            the Charset for {@code value}, or {@code null} to use {@link 
Charset#defaultCharset()}.
      * @return Base64 string.
      * @throws EncoderException
      *             Thrown if a failure condition is encountered during the 
encoding process.
diff --git a/src/main/java/org/apache/commons/codec/net/QCodec.java 
b/src/main/java/org/apache/commons/codec/net/QCodec.java
index db6c90b4..51fc9ffe 100644
--- a/src/main/java/org/apache/commons/codec/net/QCodec.java
+++ b/src/main/java/org/apache/commons/codec/net/QCodec.java
@@ -19,6 +19,7 @@ package org.apache.commons.codec.net;
 
 import java.io.UnsupportedEncodingException;
 import java.nio.charset.Charset;
+import java.nio.charset.IllegalCharsetNameException;
 import java.nio.charset.StandardCharsets;
 import java.nio.charset.UnsupportedCharsetException;
 import java.util.BitSet;
@@ -118,7 +119,7 @@ public class QCodec extends RFC1522Codec implements 
StringEncoder, StringDecoder
      * Constructs a new instance for the selection of a default Charset.
      *
      * @param charset
-     *            the default string Charset to use.
+     *            the default string Charset to use, or {@code null} to use 
{@link Charset#defaultCharset()}.
      *
      * @see Charset
      * @since 1.7
@@ -131,8 +132,8 @@ public class QCodec extends RFC1522Codec implements 
StringEncoder, StringDecoder
      * Constructs a new instance for the selection of a default Charset.
      *
      * @param charsetName The charset name, or {@code null} to use {@link 
Charset#defaultCharset()}.
-     * @throws java.nio.charset.UnsupportedCharsetException
-     *             Thrown if the named Charset is unavailable.
+     * @throws IllegalCharsetNameException Thrown if the given charset name is 
illegal.
+     * @throws UnsupportedCharsetException Thrown if no support for the named 
charset is available in this instance of the Java virtual machine.
      * @since 1.7 throws UnsupportedCharsetException if the named Charset is 
unavailable
      * @see Charset
      */
@@ -270,7 +271,7 @@ public class QCodec extends RFC1522Codec implements 
StringEncoder, StringDecoder
      * @param sourceStr
      *            string to convert to quoted-printable form.
      * @param sourceCharset
-     *            the Charset for sourceStr.
+     *            the Charset for sourceStr, or {@code null} to use {@link 
Charset#defaultCharset()}.
      * @return quoted-printable string.
      * @throws EncoderException
      *             Thrown if a failure condition is encountered during the 
encoding process.
diff --git 
a/src/main/java/org/apache/commons/codec/net/QuotedPrintableCodec.java 
b/src/main/java/org/apache/commons/codec/net/QuotedPrintableCodec.java
index e3f36870..aa9abd02 100644
--- a/src/main/java/org/apache/commons/codec/net/QuotedPrintableCodec.java
+++ b/src/main/java/org/apache/commons/codec/net/QuotedPrintableCodec.java
@@ -343,7 +343,7 @@ public class QuotedPrintableCodec implements BinaryEncoder, 
BinaryDecoder, Strin
     /**
      * Constructs a new instance for the selection of a default Charset.
      *
-     * @param charset The default string Charset to use.
+     * @param charset The default string Charset to use, or {@code null} to 
use {@link Charset#defaultCharset()}.
      * @since 1.7
      */
     public QuotedPrintableCodec(final Charset charset) {
@@ -353,12 +353,12 @@ public class QuotedPrintableCodec implements 
BinaryEncoder, BinaryDecoder, Strin
     /**
      * Constructs a new instance for the selection of a default Charset and 
strict mode.
      *
-     * @param charset The default string Charset to use.
+     * @param charset The default string Charset to use, or {@code null} to 
use {@link Charset#defaultCharset()}.
      * @param strict  if {@code true}, trailing whitespace is escaped and soft 
line breaks are inserted as needed.
      * @since 1.10
      */
     public QuotedPrintableCodec(final Charset charset, final boolean strict) {
-        this.charset = charset;
+        this.charset = Charsets.toCharset(charset);
         this.strict = strict;
     }
 
@@ -366,12 +366,11 @@ public class QuotedPrintableCodec implements 
BinaryEncoder, BinaryDecoder, Strin
      * Constructs a new instance for the selection of a default Charset.
      *
      * @param charsetName The charset name, or {@code null} to use {@link 
Charset#defaultCharset()}.
-     * @throws UnsupportedCharsetException Thrown if no support for the named 
Charset is available in this instance of the Java virtual machine.
-     * @throws IllegalCharsetNameException Thrown if the given Charset name is 
illegal.
-     *
+     * @throws IllegalCharsetNameException Thrown if the given charset name is 
illegal.
+     * @throws UnsupportedCharsetException Thrown if no support for the named 
charset is available in this instance of the Java virtual machine.
      * @since 1.7 throws UnsupportedCharsetException if the named Charset is 
unavailable
      */
-    public QuotedPrintableCodec(final String charsetName) throws 
IllegalCharsetNameException, IllegalArgumentException, 
UnsupportedCharsetException {
+    public QuotedPrintableCodec(final String charsetName) throws 
IllegalCharsetNameException, UnsupportedCharsetException {
         this(Charsets.toCharset(charsetName), false);
     }
 
@@ -431,7 +430,7 @@ public class QuotedPrintableCodec implements BinaryEncoder, 
BinaryDecoder, Strin
      * representation.
      *
      * @param sourceStr     quoted-printable string to convert into its 
original form.
-     * @param sourceCharset The original string Charset.
+     * @param sourceCharset The original string Charset, or {@code null} to 
use {@link Charset#defaultCharset()}.
      * @return original string.
      * @throws DecoderException Thrown if quoted-printable decoding is 
unsuccessful.
      * @since 1.7
@@ -440,7 +439,7 @@ public class QuotedPrintableCodec implements BinaryEncoder, 
BinaryDecoder, Strin
         if (sourceStr == null) {
             return null;
         }
-        return new String(this.decode(StringUtils.getBytesUsAscii(sourceStr)), 
sourceCharset);
+        return new String(this.decode(StringUtils.getBytesUsAscii(sourceStr)), 
Charsets.toCharset(sourceCharset));
     }
 
     /**
@@ -448,7 +447,7 @@ public class QuotedPrintableCodec implements BinaryEncoder, 
BinaryDecoder, Strin
      * representation.
      *
      * @param sourceStr     quoted-printable string to convert into its 
original form.
-     * @param sourceCharset The original string Charset.
+     * @param sourceCharset The original string Charset, or {@code null} to 
use {@link Charset#defaultCharset()}.
      * @return original string.
      * @throws DecoderException             Thrown if quoted-printable 
decoding is unsuccessful.
      * @throws UnsupportedEncodingException Thrown if Charset is not supported.
@@ -457,7 +456,7 @@ public class QuotedPrintableCodec implements BinaryEncoder, 
BinaryDecoder, Strin
         if (sourceStr == null) {
             return null;
         }
-        return new String(decode(StringUtils.getBytesUsAscii(sourceStr)), 
sourceCharset);
+        return new String(decode(StringUtils.getBytesUsAscii(sourceStr)), 
sourceCharset == null ? Charset.defaultCharset().name() : sourceCharset);
     }
 
     /**
@@ -522,7 +521,7 @@ public class QuotedPrintableCodec implements BinaryEncoder, 
BinaryDecoder, Strin
      * </p>
      *
      * @param sourceStr     string to convert to quoted-printable form.
-     * @param sourceCharset The Charset for sourceStr.
+     * @param sourceCharset The Charset for sourceStr, or {@code null} to use 
{@link Charset#defaultCharset()}.
      * @return quoted-printable string.
      * @since 1.7
      */
@@ -530,7 +529,7 @@ public class QuotedPrintableCodec implements BinaryEncoder, 
BinaryDecoder, Strin
         if (sourceStr == null) {
             return null;
         }
-        return 
StringUtils.newStringUsAscii(this.encode(sourceStr.getBytes(sourceCharset)));
+        return 
StringUtils.newStringUsAscii(this.encode(sourceStr.getBytes(Charsets.toCharset(sourceCharset))));
     }
 
     /**
@@ -541,7 +540,7 @@ public class QuotedPrintableCodec implements BinaryEncoder, 
BinaryDecoder, Strin
      * </p>
      *
      * @param sourceStr     string to convert to quoted-printable form.
-     * @param sourceCharset The Charset for sourceStr.
+     * @param sourceCharset The Charset for sourceStr, or {@code null} to use 
{@link Charset#defaultCharset()}.
      * @return quoted-printable string.
      * @throws UnsupportedEncodingException Thrown if the Charset is not 
supported.
      */
@@ -549,7 +548,7 @@ public class QuotedPrintableCodec implements BinaryEncoder, 
BinaryDecoder, Strin
         if (sourceStr == null) {
             return null;
         }
-        return 
StringUtils.newStringUsAscii(encode(sourceStr.getBytes(sourceCharset)));
+        return 
StringUtils.newStringUsAscii(encode(sourceStr.getBytes(sourceCharset == null ? 
Charset.defaultCharset().name() : sourceCharset)));
     }
 
     /**
diff --git a/src/main/java/org/apache/commons/codec/net/RFC1522Codec.java 
b/src/main/java/org/apache/commons/codec/net/RFC1522Codec.java
index a62344e7..68eb4b77 100644
--- a/src/main/java/org/apache/commons/codec/net/RFC1522Codec.java
+++ b/src/main/java/org/apache/commons/codec/net/RFC1522Codec.java
@@ -19,8 +19,8 @@ package org.apache.commons.codec.net;
 
 import java.io.UnsupportedEncodingException;
 import java.nio.charset.Charset;
+import java.nio.charset.IllegalCharsetNameException;
 import java.nio.charset.UnsupportedCharsetException;
-import java.util.Objects;
 
 import org.apache.commons.codec.Charsets;
 import org.apache.commons.codec.DecoderException;
@@ -57,7 +57,7 @@ abstract class RFC1522Codec {
     protected final Charset charset;
 
     RFC1522Codec(final Charset charset) {
-        this.charset = Objects.requireNonNull(charset, "charset");
+        this.charset = Charsets.toCharset(charset);
     }
 
     /**
@@ -134,7 +134,7 @@ abstract class RFC1522Codec {
      * </p>
      *
      * @param text    A string to encode.
-     * @param charset A charset to be used.
+     * @param charset A charset to be used, or {@code null} to use {@link 
Charset#defaultCharset()}.
      * @return RFC 1522 compliant "encoded-word".
      * @throws EncoderException Thrown if there is an error condition during 
the encoding process.
      * @see Charset
@@ -143,13 +143,14 @@ abstract class RFC1522Codec {
         if (text == null) {
             return null;
         }
+        final Charset actualCharset = Charsets.toCharset(charset);
         final StringBuilder buffer = new StringBuilder();
         buffer.append(PREFIX);
-        buffer.append(charset);
+        buffer.append(actualCharset);
         buffer.append(SEP);
         buffer.append(getEncoding());
         buffer.append(SEP);
-        
buffer.append(StringUtils.newStringUsAscii(doEncoding(text.getBytes(charset))));
+        
buffer.append(StringUtils.newStringUsAscii(doEncoding(text.getBytes(actualCharset))));
         buffer.append(POSTFIX);
         return buffer.toString();
     }
@@ -165,7 +166,8 @@ abstract class RFC1522Codec {
      * @param charsetName The charset name, or {@code null} to use {@link 
Charset#defaultCharset()}.
      * @return RFC 1522 compliant "encoded-word".
      * @throws EncoderException            Thrown if there is an error 
condition during the encoding process.
-     * @throws UnsupportedCharsetException Thrown if charset is not available.
+     * @throws IllegalCharsetNameException Thrown if the given charset name is 
illegal.
+     * @throws UnsupportedCharsetException Thrown if no support for the named 
charset is available in this instance of the Java virtual machine.
      * @see Charset
      */
     protected String encodeText(final String text, final String charsetName) 
throws EncoderException {
diff --git a/src/main/java/org/apache/commons/codec/net/URLCodec.java 
b/src/main/java/org/apache/commons/codec/net/URLCodec.java
index 68fbdc02..fc34d3dd 100644
--- a/src/main/java/org/apache/commons/codec/net/URLCodec.java
+++ b/src/main/java/org/apache/commons/codec/net/URLCodec.java
@@ -21,6 +21,7 @@ import java.io.ByteArrayOutputStream;
 import java.io.UnsupportedEncodingException;
 import java.net.URLDecoder;
 import java.net.URLEncoder;
+import java.nio.charset.Charset;
 import java.util.BitSet;
 
 import org.apache.commons.codec.BinaryDecoder;
@@ -225,10 +226,10 @@ public class URLCodec implements BinaryEncoder, 
BinaryDecoder, StringEncoder, St
     /**
      * Constructs a new instance for the selection of a default charset.
      *
-     * @param charset The default string charset to use.
+     * @param charset The default string charset to use, or {@code null} to 
use {@link java.nio.charset.Charset#defaultCharset()}.
      */
     public URLCodec(final String charset) {
-        this.charset = charset;
+        this.charset = charset == null ? Charset.defaultCharset().name() : 
charset;
     }
 
     /**
@@ -325,7 +326,7 @@ public class URLCodec implements BinaryEncoder, 
BinaryDecoder, StringEncoder, St
      * @param str
      *            URL safe string to convert into its original form.
      * @param charsetName
-     *            the original string charset.
+     *            the original string charset, or {@code null} to use {@link 
java.nio.charset.Charset#defaultCharset()}.
      * @return original string.
      * @throws DecoderException
      *             Thrown if URL decoding is unsuccessful.
@@ -337,7 +338,7 @@ public class URLCodec implements BinaryEncoder, 
BinaryDecoder, StringEncoder, St
         if (str == null) {
             return null;
         }
-        return new String(decode(StringUtils.getBytesUsAscii(str)), 
charsetName);
+        return new String(decode(StringUtils.getBytesUsAscii(str)), 
charsetName == null ? Charset.defaultCharset().name() : charsetName);
     }
 
     /**
@@ -403,7 +404,7 @@ public class URLCodec implements BinaryEncoder, 
BinaryDecoder, StringEncoder, St
      * @param str
      *            string to convert to a URL safe form.
      * @param charsetName
-     *            the charset for str.
+     *            the charset for str, or {@code null} to use {@link 
java.nio.charset.Charset#defaultCharset()}.
      * @return URL safe string.
      * @throws UnsupportedEncodingException
      *             Thrown if charset is not supported.
@@ -412,7 +413,7 @@ public class URLCodec implements BinaryEncoder, 
BinaryDecoder, StringEncoder, St
         if (str == null) {
             return null;
         }
-        return StringUtils.newStringUsAscii(encode(str.getBytes(charsetName)));
+        return StringUtils.newStringUsAscii(encode(str.getBytes(charsetName == 
null ? Charset.defaultCharset().name() : charsetName)));
     }
 
     /**
diff --git a/src/test/java/org/apache/commons/codec/binary/HexTest.java 
b/src/test/java/org/apache/commons/codec/binary/HexTest.java
index 37d9b37f..142beec5 100644
--- a/src/test/java/org/apache/commons/codec/binary/HexTest.java
+++ b/src/test/java/org/apache/commons/codec/binary/HexTest.java
@@ -687,6 +687,17 @@ class HexTest {
         assertEquals(StandardCharsets.UTF_8.name(), new 
Hex(StandardCharsets.UTF_8).getCharsetName());
     }
 
+    @Test
+    void testNullCharset() throws Exception {
+        final Hex codec = new Hex((Charset) null);
+        final Hex expected = new Hex(Charset.defaultCharset());
+        final byte[] bytes = {65, 66};
+        assertEquals(Charset.defaultCharset(), codec.getCharset());
+        assertEquals(Charset.defaultCharset().name(), codec.getCharsetName());
+        assertArrayEquals(expected.encode(bytes), codec.encode(bytes));
+        assertArrayEquals(bytes, codec.decode(expected.encode(bytes)));
+    }
+
     @ParameterizedTest
     
@MethodSource("org.apache.commons.codec.CharsetsTest#getRequiredCharsets()")
     void testRequiredCharset(final Charset charset) throws 
UnsupportedEncodingException, DecoderException {
diff --git a/src/test/java/org/apache/commons/codec/binary/StringUtilsTest.java 
b/src/test/java/org/apache/commons/codec/binary/StringUtilsTest.java
index 3d73f4a7..8acbf7cf 100644
--- a/src/test/java/org/apache/commons/codec/binary/StringUtilsTest.java
+++ b/src/test/java/org/apache/commons/codec/binary/StringUtilsTest.java
@@ -175,6 +175,14 @@ class StringUtilsTest {
         assertNull(StringUtils.getBytesUnchecked(null, "UNKNOWN"));
     }
 
+    @Test
+    void testInvalidCharsetNamesRemainWrapped() {
+        for (final String name : new String[] {"NONSENSE", "!invalid!"}) {
+            assertThrows(IllegalStateException.class, () -> 
StringUtils.getBytesUnchecked("x", name));
+            assertThrows(IllegalStateException.class, () -> 
StringUtils.newString(new byte[] {65}, name));
+        }
+    }
+
     @ParameterizedTest(name = "{0}: {1}")
     @MethodSource("newStringCases")
     void testNewString(final Charset charset, final Function<byte[], String> 
decoder, final byte[] input) throws UnsupportedEncodingException {
@@ -198,4 +206,14 @@ class StringUtilsTest {
     void testNewStringNullInput_CODEC229(final Function<byte[], String> 
decoder) {
         assertNull(decoder.apply(null));
     }
+
+    @Test
+    void testNullCharsetNames() {
+        final String text = "caf\u00e9";
+        final byte[] bytes = text.getBytes(Charset.defaultCharset());
+        assertArrayEquals(bytes, StringUtils.getBytesUnchecked(text, null));
+        assertEquals(new String(bytes, Charset.defaultCharset()), 
StringUtils.newString(bytes, null));
+        assertNull(StringUtils.getBytesUnchecked(null, null));
+        assertNull(StringUtils.newString(null, null));
+    }
 }
diff --git a/src/test/java/org/apache/commons/codec/net/BCodecTest.java 
b/src/test/java/org/apache/commons/codec/net/BCodecTest.java
index f953d4b5..e5dae316 100644
--- a/src/test/java/org/apache/commons/codec/net/BCodecTest.java
+++ b/src/test/java/org/apache/commons/codec/net/BCodecTest.java
@@ -199,6 +199,17 @@ class BCodecTest {
         assertThrows(UnsupportedCharsetException.class, () -> new 
BCodec("NONSENSE"));
     }
 
+    @Test
+    void testNullCharset() throws Exception {
+        final BCodec codec = new BCodec((Charset) null);
+        assertEquals(Charset.defaultCharset(), codec.getCharset());
+        final String text = "caf\u00e9";
+        final BCodec other = new BCodec("UTF-16BE");
+        assertEquals(codec.encode(text), other.encode(text, (Charset) null));
+        assertNull(other.encode(null, (Charset) null));
+        assertEquals(Charset.defaultCharset(), new BCodec(null, 
CodecPolicy.STRICT).getCharset());
+    }
+
     @Test
     void testNullInput() throws Exception {
         final BCodec bcodec = new BCodec();
diff --git a/src/test/java/org/apache/commons/codec/net/QCodecTest.java 
b/src/test/java/org/apache/commons/codec/net/QCodecTest.java
index b482beb3..8a9b3315 100644
--- a/src/test/java/org/apache/commons/codec/net/QCodecTest.java
+++ b/src/test/java/org/apache/commons/codec/net/QCodecTest.java
@@ -214,6 +214,16 @@ class QCodecTest {
         assertFalse(qcodec.isEncodeBlanks());
     }
 
+    @Test
+    void testNullCharset() throws Exception {
+        final QCodec codec = new QCodec((Charset) null);
+        assertEquals(Charset.defaultCharset(), codec.getCharset());
+        final String text = "caf\u00e9";
+        final QCodec other = new QCodec("UTF-16BE");
+        assertEquals(codec.encode(text), other.encode(text, (Charset) null));
+        assertNull(other.encode(null, (Charset) null));
+    }
+
     @Test
     void testNullInput() throws Exception {
         final QCodec qcodec = new QCodec();
diff --git 
a/src/test/java/org/apache/commons/codec/net/QuotedPrintableCodecTest.java 
b/src/test/java/org/apache/commons/codec/net/QuotedPrintableCodecTest.java
index aed1a63c..3a70b964 100644
--- a/src/test/java/org/apache/commons/codec/net/QuotedPrintableCodecTest.java
+++ b/src/test/java/org/apache/commons/codec/net/QuotedPrintableCodecTest.java
@@ -224,6 +224,15 @@ class QuotedPrintableCodecTest {
         }
     }
 
+    @Test
+    void testInvalidCharsetNamesRemainChecked() {
+        final QuotedPrintableCodec codec = new QuotedPrintableCodec();
+        for (final String name : new String[] {"NONSENSE", "!invalid!"}) {
+            assertThrows(java.io.UnsupportedEncodingException.class, () -> 
codec.encode("x", name));
+            assertThrows(java.io.UnsupportedEncodingException.class, () -> 
codec.decode("x", name));
+        }
+    }
+
     @Test
     void testInvalidEncoding() {
         assertThrows(UnsupportedCharsetException.class, () -> new 
QuotedPrintableCodec("NONSENSE"));
@@ -238,6 +247,28 @@ class QuotedPrintableCodecTest {
         }
     }
 
+    @Test
+    void testNullCharsets() throws Exception {
+        final QuotedPrintableCodec codec = new QuotedPrintableCodec((Charset) 
null);
+        final QuotedPrintableCodec expected = new 
QuotedPrintableCodec(Charset.defaultCharset());
+        final String text = "caf\u00e9";
+        final String encoded = expected.encode(text);
+        assertEquals(Charset.defaultCharset(), codec.getCharset());
+        assertEquals(Charset.defaultCharset().name(), 
codec.getDefaultCharset());
+        assertEquals(encoded, codec.encode(text));
+        assertEquals(expected.decode(encoded), codec.decode(encoded));
+        assertEquals(encoded, codec.encode(text, (Charset) null));
+        assertEquals(encoded, codec.encode(text, (String) null));
+        assertEquals(expected.decode(encoded), codec.decode(encoded, (Charset) 
null));
+        assertEquals(expected.decode(encoded), codec.decode(encoded, (String) 
null));
+        assertEquals(new QuotedPrintableCodec(Charset.defaultCharset(), 
true).encode(text),
+                new QuotedPrintableCodec(null, true).encode(text));
+        assertNull(codec.encode(null, (Charset) null));
+        assertNull(codec.encode(null, (String) null));
+        assertNull(codec.decode(null, (Charset) null));
+        assertNull(codec.decode(null, (String) null));
+    }
+
     @Test
     void testPreserveNotEncodedCRLF() throws Exception {
         final String qpdata = "CRLF in an\n encoded text should 
be=20=\r\n\rpreserved in the\r decoding.";
diff --git a/src/test/java/org/apache/commons/codec/net/URLCodecTest.java 
b/src/test/java/org/apache/commons/codec/net/URLCodecTest.java
index c792422d..e9678cf6 100644
--- a/src/test/java/org/apache/commons/codec/net/URLCodecTest.java
+++ b/src/test/java/org/apache/commons/codec/net/URLCodecTest.java
@@ -22,6 +22,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertNull;
 import static org.junit.jupiter.api.Assertions.assertThrows;
 
+import java.nio.charset.Charset;
 import java.nio.charset.StandardCharsets;
 import java.util.BitSet;
 
@@ -275,6 +276,15 @@ class URLCodecTest {
         assertEquals(" ", new String(decoded, StandardCharsets.US_ASCII), 
"Default decoding interprets a literal plus as a space");
     }
 
+    @Test
+    void testInvalidCharsetNamesRemainChecked() {
+        final URLCodec codec = new URLCodec();
+        for (final String name : new String[] {"NONSENSE", "!invalid!"}) {
+            assertThrows(java.io.UnsupportedEncodingException.class, () -> 
codec.encode("x", name));
+            assertThrows(java.io.UnsupportedEncodingException.class, () -> 
codec.decode("x", name));
+        }
+    }
+
     @Test
     void testInvalidEncoding() {
         final URLCodec urlCodec = new URLCodec("NONSENSE");
@@ -284,6 +294,21 @@ class URLCodecTest {
         validateState(urlCodec);
     }
 
+    @Test
+    void testNullCharset() throws Exception {
+        final URLCodec codec = new URLCodec(null);
+        final URLCodec expected = new 
URLCodec(Charset.defaultCharset().name());
+        final String text = "caf\u00e9";
+        final String encoded = expected.encode(text);
+        assertEquals(Charset.defaultCharset().name(), 
codec.getDefaultCharset());
+        assertEquals(encoded, codec.encode(text));
+        assertEquals(encoded, codec.encode(text, null));
+        assertEquals(expected.decode(encoded), codec.decode(encoded));
+        assertEquals(expected.decode(encoded), codec.decode(encoded, null));
+        assertNull(codec.encode(null, null));
+        assertNull(codec.decode(null, null));
+    }
+
     @Test
     void testSafeCharEncodeDecode() throws Exception {
         final URLCodec urlCodec = new URLCodec();

Reply via email to