Skip to content

Commit d9f0c46

Browse files
committed
BCodec and QCodec encode() methods throw UnsupportedCharsetException
instead of EncoderException
1 parent 9c68c83 commit d9f0c46

5 files changed

Lines changed: 14 additions & 24 deletions

File tree

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ The <action> type attribute can be add,update,fix,remove.
4848
<action type="add" dev="ggregory" due-to="Gary Gregory">Add override org.apache.commons.codec.language.bm.Rule.PhonemeExpr.size().</action>
4949
<!-- FIX -->
5050
<action type="fix" dev="ggregory" due-to="Gary Gregory">Optimize memory allocation in PhoneticEngine.</action>
51+
<action type="fix" dev="ggregory" due-to="Gary Gregory">BCodec and QCodec encode() methods throw UnsupportedCharsetException instead of EncoderException.</action>
5152
<!-- UPDATE -->
5253
<action dev="ggregory" type="update" due-to="Dependabot, Gary Gregory">Bump org.apache.commons:commons-parent from 66 to 69 #250, #261.</action>
5354
<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>

src/main/java/org/apache/commons/codec/net/BCodec.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.io.UnsupportedEncodingException;
2121
import java.nio.charset.Charset;
2222
import java.nio.charset.StandardCharsets;
23+
import java.nio.charset.UnsupportedCharsetException;
2324

2425
import org.apache.commons.codec.CodecPolicy;
2526
import org.apache.commons.codec.DecoderException;
@@ -149,11 +150,8 @@ public Object decode(final Object value) throws DecoderException {
149150
*/
150151
@Override
151152
public String decode(final String value) throws DecoderException {
152-
if (value == null) {
153-
return null;
154-
}
155153
try {
156-
return this.decodeText(value);
154+
return decodeText(value);
157155
} catch (final UnsupportedEncodingException | IllegalArgumentException e) {
158156
throw new DecoderException(e.getMessage(), e);
159157
}
@@ -250,7 +248,7 @@ public String encode(final String strSource, final String sourceCharset) throws
250248
}
251249
try {
252250
return this.encodeText(strSource, sourceCharset);
253-
} catch (final UnsupportedEncodingException e) {
251+
} catch (final UnsupportedCharsetException e) {
254252
throw new EncoderException(e.getMessage(), e);
255253
}
256254
}

src/main/java/org/apache/commons/codec/net/QCodec.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.io.UnsupportedEncodingException;
2121
import java.nio.charset.Charset;
2222
import java.nio.charset.StandardCharsets;
23+
import java.nio.charset.UnsupportedCharsetException;
2324
import java.util.BitSet;
2425

2526
import org.apache.commons.codec.DecoderException;
@@ -163,9 +164,7 @@ public Object decode(final Object obj) throws DecoderException {
163164
if (obj instanceof String) {
164165
return decode((String) obj);
165166
}
166-
throw new DecoderException("Objects of type " +
167-
obj.getClass().getName() +
168-
" cannot be decoded using Q codec");
167+
throw new DecoderException("Objects of type " + obj.getClass().getName() + " cannot be decoded using Q codec");
169168
}
170169

171170
/**
@@ -180,9 +179,6 @@ public Object decode(final Object obj) throws DecoderException {
180179
*/
181180
@Override
182181
public String decode(final String str) throws DecoderException {
183-
if (str == null) {
184-
return null;
185-
}
186182
try {
187183
return decodeText(str);
188184
} catch (final UnsupportedEncodingException e) {
@@ -308,7 +304,7 @@ public String encode(final String sourceStr, final String sourceCharset) throws
308304
}
309305
try {
310306
return encodeText(sourceStr, sourceCharset);
311-
} catch (final UnsupportedEncodingException e) {
307+
} catch (final UnsupportedCharsetException e) {
312308
throw new EncoderException(e.getMessage(), e);
313309
}
314310
}

src/main/java/org/apache/commons/codec/net/RFC1522Codec.java

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import java.io.UnsupportedEncodingException;
2121
import java.nio.charset.Charset;
22+
import java.nio.charset.UnsupportedCharsetException;
2223

2324
import org.apache.commons.codec.DecoderException;
2425
import org.apache.commons.codec.EncoderException;
@@ -37,7 +38,6 @@
3738
*
3839
* @see <a href="http://www.ietf.org/rfc/rfc1522.txt">MIME (Multipurpose Internet Mail Extensions) Part Two:
3940
* Message Header Extensions for Non-ASCII Text</a>
40-
*
4141
* @since 1.3
4242
*/
4343
abstract class RFC1522Codec {
@@ -66,8 +66,7 @@ abstract class RFC1522Codec {
6666
* @throws UnsupportedEncodingException
6767
* thrown if charset specified in the "encoded-word" header is not supported
6868
*/
69-
protected String decodeText(final String text)
70-
throws DecoderException, UnsupportedEncodingException {
69+
protected String decodeText(final String text) throws DecoderException, UnsupportedEncodingException {
7170
if (text == null) {
7271
return null;
7372
}
@@ -167,22 +166,18 @@ protected String encodeText(final String text, final Charset charset) throws Enc
167166
* @return RFC 1522 compliant "encoded-word"
168167
* @throws EncoderException
169168
* thrown if there is an error condition during the Encoding process.
170-
* @throws UnsupportedEncodingException
169+
* @throws UnsupportedCharsetException
171170
* if charset is not available
172171
* @see Charset
173172
*/
174-
protected String encodeText(final String text, final String charsetName)
175-
throws EncoderException, UnsupportedEncodingException {
176-
if (text == null) {
177-
return null;
178-
}
179-
return this.encodeText(text, Charset.forName(charsetName));
173+
protected String encodeText(final String text, final String charsetName) throws EncoderException {
174+
return encodeText(text, Charset.forName(charsetName));
180175
}
181176

182177
/**
183178
* Returns the codec name (referred to as encoding in the RFC 1522).
184179
*
185-
* @return name of the codec
180+
* @return name of the codec.
186181
*/
187182
protected abstract String getEncoding();
188183
}

src/test/java/org/apache/commons/codec/net/CustomRFC1522Codec.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ protected String encodeText(final String text, final Charset charset) throws Enc
4949
}
5050

5151
@Override
52-
protected String encodeText(final String text, final String charsetName) throws EncoderException, UnsupportedEncodingException {
52+
protected String encodeText(final String text, final String charsetName) throws EncoderException {
5353
return super.encodeText(text, charsetName);
5454
}
5555

0 commit comments

Comments
 (0)