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-codec.git
commit d9f0c46ffc0ebb2871f4f791334216517f414e63 Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Sat Apr 13 08:10:26 2024 -0400 BCodec and QCodec encode() methods throw UnsupportedCharsetException instead of EncoderException --- src/changes/changes.xml | 1 + src/main/java/org/apache/commons/codec/net/BCodec.java | 8 +++----- src/main/java/org/apache/commons/codec/net/QCodec.java | 10 +++------- .../java/org/apache/commons/codec/net/RFC1522Codec.java | 17 ++++++----------- .../apache/commons/codec/net/CustomRFC1522Codec.java | 2 +- 5 files changed, 14 insertions(+), 24 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 07640701..fce3fe95 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -48,6 +48,7 @@ The <action> type attribute can be add,update,fix,remove. <action type="add" dev="ggregory" due-to="Gary Gregory">Add override org.apache.commons.codec.language.bm.Rule.PhonemeExpr.size().</action> <!-- FIX --> <action type="fix" dev="ggregory" due-to="Gary Gregory">Optimize memory allocation in PhoneticEngine.</action> + <action type="fix" dev="ggregory" due-to="Gary Gregory">BCodec and QCodec encode() methods throw UnsupportedCharsetException instead of EncoderException.</action> <!-- UPDATE --> <action dev="ggregory" type="update" due-to="Dependabot, Gary Gregory">Bump org.apache.commons:commons-parent from 66 to 69 #250, #261.</action> <action dev="ggregory" type="update" due-to="Dependabot, Gary Gregory">Bump commons-io:commons-io from 2.15.1 to 2.16.1 #258, #265.</action> 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 481cd1e0..3f66e557 100644 --- a/src/main/java/org/apache/commons/codec/net/BCodec.java +++ b/src/main/java/org/apache/commons/codec/net/BCodec.java @@ -20,6 +20,7 @@ package org.apache.commons.codec.net; import java.io.UnsupportedEncodingException; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; +import java.nio.charset.UnsupportedCharsetException; import org.apache.commons.codec.CodecPolicy; import org.apache.commons.codec.DecoderException; @@ -149,11 +150,8 @@ public class BCodec extends RFC1522Codec implements StringEncoder, StringDecoder */ @Override public String decode(final String value) throws DecoderException { - if (value == null) { - return null; - } try { - return this.decodeText(value); + return decodeText(value); } catch (final UnsupportedEncodingException | IllegalArgumentException e) { throw new DecoderException(e.getMessage(), e); } @@ -250,7 +248,7 @@ public class BCodec extends RFC1522Codec implements StringEncoder, StringDecoder } try { return this.encodeText(strSource, sourceCharset); - } catch (final UnsupportedEncodingException e) { + } catch (final UnsupportedCharsetException e) { throw new EncoderException(e.getMessage(), e); } } 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 4618a7e3..cbb4c941 100644 --- a/src/main/java/org/apache/commons/codec/net/QCodec.java +++ b/src/main/java/org/apache/commons/codec/net/QCodec.java @@ -20,6 +20,7 @@ package org.apache.commons.codec.net; import java.io.UnsupportedEncodingException; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; +import java.nio.charset.UnsupportedCharsetException; import java.util.BitSet; import org.apache.commons.codec.DecoderException; @@ -163,9 +164,7 @@ public class QCodec extends RFC1522Codec implements StringEncoder, StringDecoder if (obj instanceof String) { return decode((String) obj); } - throw new DecoderException("Objects of type " + - obj.getClass().getName() + - " cannot be decoded using Q codec"); + throw new DecoderException("Objects of type " + obj.getClass().getName() + " cannot be decoded using Q codec"); } /** @@ -180,9 +179,6 @@ public class QCodec extends RFC1522Codec implements StringEncoder, StringDecoder */ @Override public String decode(final String str) throws DecoderException { - if (str == null) { - return null; - } try { return decodeText(str); } catch (final UnsupportedEncodingException e) { @@ -308,7 +304,7 @@ public class QCodec extends RFC1522Codec implements StringEncoder, StringDecoder } try { return encodeText(sourceStr, sourceCharset); - } catch (final UnsupportedEncodingException e) { + } catch (final UnsupportedCharsetException e) { throw new EncoderException(e.getMessage(), e); } } 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 c71304a1..c0e63a9e 100644 --- a/src/main/java/org/apache/commons/codec/net/RFC1522Codec.java +++ b/src/main/java/org/apache/commons/codec/net/RFC1522Codec.java @@ -19,6 +19,7 @@ package org.apache.commons.codec.net; import java.io.UnsupportedEncodingException; import java.nio.charset.Charset; +import java.nio.charset.UnsupportedCharsetException; import org.apache.commons.codec.DecoderException; import org.apache.commons.codec.EncoderException; @@ -37,7 +38,6 @@ import org.apache.commons.codec.binary.StringUtils; * * @see <a href="http://www.ietf.org/rfc/rfc1522.txt">MIME (Multipurpose Internet Mail Extensions) Part Two: * Message Header Extensions for Non-ASCII Text</a> - * * @since 1.3 */ abstract class RFC1522Codec { @@ -66,8 +66,7 @@ abstract class RFC1522Codec { * @throws UnsupportedEncodingException * thrown if charset specified in the "encoded-word" header is not supported */ - protected String decodeText(final String text) - throws DecoderException, UnsupportedEncodingException { + protected String decodeText(final String text) throws DecoderException, UnsupportedEncodingException { if (text == null) { return null; } @@ -167,22 +166,18 @@ abstract class RFC1522Codec { * @return RFC 1522 compliant "encoded-word" * @throws EncoderException * thrown if there is an error condition during the Encoding process. - * @throws UnsupportedEncodingException + * @throws UnsupportedCharsetException * if charset is not available * @see Charset */ - protected String encodeText(final String text, final String charsetName) - throws EncoderException, UnsupportedEncodingException { - if (text == null) { - return null; - } - return this.encodeText(text, Charset.forName(charsetName)); + protected String encodeText(final String text, final String charsetName) throws EncoderException { + return encodeText(text, Charset.forName(charsetName)); } /** * Returns the codec name (referred to as encoding in the RFC 1522). * - * @return name of the codec + * @return name of the codec. */ protected abstract String getEncoding(); } diff --git a/src/test/java/org/apache/commons/codec/net/CustomRFC1522Codec.java b/src/test/java/org/apache/commons/codec/net/CustomRFC1522Codec.java index 90b2de31..1c99d4aa 100644 --- a/src/test/java/org/apache/commons/codec/net/CustomRFC1522Codec.java +++ b/src/test/java/org/apache/commons/codec/net/CustomRFC1522Codec.java @@ -49,7 +49,7 @@ public class CustomRFC1522Codec extends RFC1522Codec { } @Override - protected String encodeText(final String text, final String charsetName) throws EncoderException, UnsupportedEncodingException { + protected String encodeText(final String text, final String charsetName) throws EncoderException { return super.encodeText(text, charsetName); }