Skip to content

Commit 1a30728

Browse files
committed
[CODEC-278] Deprecate Charset constants in
org.apache.commons.codec.Charsets in favor of java.nio.charset.StandardCharsets.
1 parent 84efbac commit 1a30728

6 files changed

Lines changed: 35 additions & 34 deletions

File tree

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ The <action> type attribute can be add,update,fix,remove.
5757
<action issue="CODEC-275" dev="ggregory" type="add" due-to="Claude Warren">Add missing note in javadoc when sign extension error is present #34.</action>
5858
<action issue="CODEC-276" dev="ggregory" type="fix" due-to="Gary Gregory">Reliance on default encoding in MurmurHash2 and MurmurHash3.</action>
5959
<action issue="CODEC-277" dev="ggregory" type="update" due-to="Gary Gregory">Don't reload standard Charsets in org.apache.commons.codec.Charsets.</action>
60+
<action issue="CODEC-278" dev="ggregory" type="update" due-to="Gary Gregory">Deprecate Charset constants in org.apache.commons.codec.Charsets in favor of java.nio.charset.StandardCharsets.</action>
6061
</release>
6162

6263
<release version="1.13" date="2019-07-20" description="Feature and fix release.">

src/main/java/org/apache/commons/codec/Charsets.java

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -92,25 +92,23 @@ public static Charset toCharset(final String charset) {
9292
* <p>
9393
* Every implementation of the Java platform is required to support this character encoding.
9494
* </p>
95-
* <p>
96-
* On Java 7 or later, use {@link java.nio.charset.StandardCharsets#ISO_8859_1} instead.
97-
* </p>
9895
*
96+
* @deprecated Use {@link java.nio.charset.StandardCharsets#ISO_8859_1} instead.
9997
* @see <a href="http://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
10098
*/
99+
@Deprecated
101100
public static final Charset ISO_8859_1 = StandardCharsets.ISO_8859_1;
102101

103102
/**
104103
* Seven-bit ASCII, also known as ISO646-US, also known as the Basic Latin block of the Unicode character set.
105104
* <p>
106105
* Every implementation of the Java platform is required to support this character encoding.
107106
* </p>
108-
* <p>
109-
* On Java 7 or later, use {@link java.nio.charset.StandardCharsets#ISO_8859_1} instead.
110-
* </p>
111107
*
108+
* @deprecated Use {@link java.nio.charset.StandardCharsets#US_ASCII} instead.
112109
* @see <a href="http://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
113110
*/
111+
@Deprecated
114112
public static final Charset US_ASCII = StandardCharsets.US_ASCII;
115113

116114
/**
@@ -119,50 +117,46 @@ public static Charset toCharset(final String charset) {
119117
* <p>
120118
* Every implementation of the Java platform is required to support this character encoding.
121119
* </p>
122-
* <p>
123-
* On Java 7 or later, use {@link java.nio.charset.StandardCharsets#ISO_8859_1} instead.
124-
* </p>
125120
*
121+
* @deprecated Use {@link java.nio.charset.StandardCharsets#UTF_16} instead.
126122
* @see <a href="http://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
127123
*/
124+
@Deprecated
128125
public static final Charset UTF_16 = StandardCharsets.UTF_16;
129126

130127
/**
131128
* Sixteen-bit Unicode Transformation Format, big-endian byte order.
132129
* <p>
133130
* Every implementation of the Java platform is required to support this character encoding.
134131
* </p>
135-
* <p>
136-
* On Java 7 or later, use {@link java.nio.charset.StandardCharsets#ISO_8859_1} instead.
137-
* </p>
138132
*
133+
* @deprecated Use {@link java.nio.charset.StandardCharsets#UTF_16BE} instead.
139134
* @see <a href="http://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
140135
*/
136+
@Deprecated
141137
public static final Charset UTF_16BE = StandardCharsets.UTF_16BE;
142138

143139
/**
144140
* Sixteen-bit Unicode Transformation Format, little-endian byte order.
145141
* <p>
146142
* Every implementation of the Java platform is required to support this character encoding.
147143
* </p>
148-
* <p>
149-
* On Java 7 or later, use {@link java.nio.charset.StandardCharsets#ISO_8859_1} instead.
150-
* </p>
151144
*
145+
* @deprecated Use {@link java.nio.charset.StandardCharsets#UTF_16LE} instead.
152146
* @see <a href="http://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
153147
*/
148+
@Deprecated
154149
public static final Charset UTF_16LE = StandardCharsets.UTF_16LE;
155150

156151
/**
157152
* Eight-bit Unicode Transformation Format.
158153
* <p>
159154
* Every implementation of the Java platform is required to support this character encoding.
160155
* </p>
161-
* <p>
162-
* On Java 7 or later, use {@link java.nio.charset.StandardCharsets#ISO_8859_1} instead.
163-
* </p>
164156
*
157+
* @deprecated Use {@link java.nio.charset.StandardCharsets#UTF_8} instead.
165158
* @see <a href="http://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
166159
*/
160+
@Deprecated
167161
public static final Charset UTF_8 = StandardCharsets.UTF_8;
168162
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
import org.apache.commons.codec.EncoderException;
3030

3131
/**
32-
* Converts hexadecimal Strings. The charset used for certain operation can be set, the default is set in
32+
* Converts hexadecimal Strings. The Charset used for certain operation can be set, the default is set in
3333
* {@link #DEFAULT_CHARSET_NAME}
3434
*
3535
* This class is thread-safe.
@@ -39,7 +39,7 @@
3939
public class Hex implements BinaryEncoder, BinaryDecoder {
4040

4141
/**
42-
* Default charset is {@link Charsets#UTF_8}
42+
* Default charset is {@link StandardCharsets#UTF_8}
4343
*
4444
* @since 1.7
4545
*/

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ private static ByteBuffer getByteBuffer(final String string, final Charset chars
121121
* the String to encode, may be {@code null}
122122
* @return encoded bytes, or {@code null} if the input string was {@code null}
123123
* @throws NullPointerException
124-
* Thrown if {@link Charsets#UTF_8} is not initialized, which should never happen since it is
124+
* Thrown if {@link StandardCharsets#UTF_8} is not initialized, which should never happen since it is
125125
* required by the Java platform specification.
126126
* @see <a href="http://download.oracle.com/javase/7/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
127127
* @see #getBytesUnchecked(String, String)
@@ -139,7 +139,7 @@ public static ByteBuffer getByteBufferUtf8(final String string) {
139139
* the String to encode, may be {@code null}
140140
* @return encoded bytes, or {@code null} if the input string was {@code null}
141141
* @throws NullPointerException
142-
* Thrown if {@link Charsets#ISO_8859_1} is not initialized, which should never happen since it is
142+
* Thrown if {@link StandardCharsets#ISO_8859_1} is not initialized, which should never happen since it is
143143
* required by the Java platform specification.
144144
* @since As of 1.7, throws {@link NullPointerException} instead of UnsupportedEncodingException
145145
* @see <a href="http://download.oracle.com/javase/7/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
@@ -188,7 +188,7 @@ public static byte[] getBytesUnchecked(final String string, final String charset
188188
* the String to encode, may be {@code null}
189189
* @return encoded bytes, or {@code null} if the input string was {@code null}
190190
* @throws NullPointerException
191-
* Thrown if {@link Charsets#US_ASCII} is not initialized, which should never happen since it is
191+
* Thrown if {@link StandardCharsets#US_ASCII} is not initialized, which should never happen since it is
192192
* required by the Java platform specification.
193193
* @since As of 1.7, throws {@link NullPointerException} instead of UnsupportedEncodingException
194194
* @see <a href="http://download.oracle.com/javase/7/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
@@ -206,7 +206,7 @@ public static byte[] getBytesUsAscii(final String string) {
206206
* the String to encode, may be {@code null}
207207
* @return encoded bytes, or {@code null} if the input string was {@code null}
208208
* @throws NullPointerException
209-
* Thrown if {@link Charsets#UTF_16} is not initialized, which should never happen since it is
209+
* Thrown if {@link StandardCharsets#UTF_16} is not initialized, which should never happen since it is
210210
* required by the Java platform specification.
211211
* @since As of 1.7, throws {@link NullPointerException} instead of UnsupportedEncodingException
212212
* @see <a href="http://download.oracle.com/javase/7/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
@@ -224,7 +224,7 @@ public static byte[] getBytesUtf16(final String string) {
224224
* the String to encode, may be {@code null}
225225
* @return encoded bytes, or {@code null} if the input string was {@code null}
226226
* @throws NullPointerException
227-
* Thrown if {@link Charsets#UTF_16BE} is not initialized, which should never happen since it is
227+
* Thrown if {@link StandardCharsets#UTF_16BE} is not initialized, which should never happen since it is
228228
* required by the Java platform specification.
229229
* @since As of 1.7, throws {@link NullPointerException} instead of UnsupportedEncodingException
230230
* @see <a href="http://download.oracle.com/javase/7/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
@@ -242,7 +242,7 @@ public static byte[] getBytesUtf16Be(final String string) {
242242
* the String to encode, may be {@code null}
243243
* @return encoded bytes, or {@code null} if the input string was {@code null}
244244
* @throws NullPointerException
245-
* Thrown if {@link Charsets#UTF_16LE} is not initialized, which should never happen since it is
245+
* Thrown if {@link StandardCharsets#UTF_16LE} is not initialized, which should never happen since it is
246246
* required by the Java platform specification.
247247
* @since As of 1.7, throws {@link NullPointerException} instead of UnsupportedEncodingException
248248
* @see <a href="http://download.oracle.com/javase/7/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
@@ -260,7 +260,7 @@ public static byte[] getBytesUtf16Le(final String string) {
260260
* the String to encode, may be {@code null}
261261
* @return encoded bytes, or {@code null} if the input string was {@code null}
262262
* @throws NullPointerException
263-
* Thrown if {@link Charsets#UTF_8} is not initialized, which should never happen since it is
263+
* Thrown if {@link StandardCharsets#UTF_8} is not initialized, which should never happen since it is
264264
* required by the Java platform specification.
265265
* @since As of 1.7, throws {@link NullPointerException} instead of UnsupportedEncodingException
266266
* @see <a href="http://download.oracle.com/javase/7/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
@@ -329,7 +329,7 @@ public static String newString(final byte[] bytes, final String charsetName) {
329329
* @return A new {@code String} decoded from the specified array of bytes using the ISO-8859-1 charset, or
330330
* {@code null} if the input byte array was {@code null}.
331331
* @throws NullPointerException
332-
* Thrown if {@link Charsets#ISO_8859_1} is not initialized, which should never happen since it is
332+
* Thrown if {@link StandardCharsets#ISO_8859_1} is not initialized, which should never happen since it is
333333
* required by the Java platform specification.
334334
* @since As of 1.7, throws {@link NullPointerException} instead of UnsupportedEncodingException
335335
*/
@@ -345,7 +345,7 @@ public static String newStringIso8859_1(final byte[] bytes) {
345345
* @return A new {@code String} decoded from the specified array of bytes using the US-ASCII charset,
346346
* or {@code null} if the input byte array was {@code null}.
347347
* @throws NullPointerException
348-
* Thrown if {@link Charsets#US_ASCII} is not initialized, which should never happen since it is
348+
* Thrown if {@link StandardCharsets#US_ASCII} is not initialized, which should never happen since it is
349349
* required by the Java platform specification.
350350
* @since As of 1.7, throws {@link NullPointerException} instead of UnsupportedEncodingException
351351
*/
@@ -361,7 +361,7 @@ public static String newStringUsAscii(final byte[] bytes) {
361361
* @return A new {@code String} decoded from the specified array of bytes using the UTF-16 charset
362362
* or {@code null} if the input byte array was {@code null}.
363363
* @throws NullPointerException
364-
* Thrown if {@link Charsets#UTF_16} is not initialized, which should never happen since it is
364+
* Thrown if {@link StandardCharsets#UTF_16} is not initialized, which should never happen since it is
365365
* required by the Java platform specification.
366366
* @since As of 1.7, throws {@link NullPointerException} instead of UnsupportedEncodingException
367367
*/
@@ -377,7 +377,7 @@ public static String newStringUtf16(final byte[] bytes) {
377377
* @return A new {@code String} decoded from the specified array of bytes using the UTF-16BE charset,
378378
* or {@code null} if the input byte array was {@code null}.
379379
* @throws NullPointerException
380-
* Thrown if {@link Charsets#UTF_16BE} is not initialized, which should never happen since it is
380+
* Thrown if {@link StandardCharsets#UTF_16BE} is not initialized, which should never happen since it is
381381
* required by the Java platform specification.
382382
* @since As of 1.7, throws {@link NullPointerException} instead of UnsupportedEncodingException
383383
*/
@@ -393,7 +393,7 @@ public static String newStringUtf16Be(final byte[] bytes) {
393393
* @return A new {@code String} decoded from the specified array of bytes using the UTF-16LE charset,
394394
* or {@code null} if the input byte array was {@code null}.
395395
* @throws NullPointerException
396-
* Thrown if {@link Charsets#UTF_16LE} is not initialized, which should never happen since it is
396+
* Thrown if {@link StandardCharsets#UTF_16LE} is not initialized, which should never happen since it is
397397
* required by the Java platform specification.
398398
* @since As of 1.7, throws {@link NullPointerException} instead of UnsupportedEncodingException
399399
*/
@@ -409,7 +409,7 @@ public static String newStringUtf16Le(final byte[] bytes) {
409409
* @return A new {@code String} decoded from the specified array of bytes using the UTF-8 charset,
410410
* or {@code null} if the input byte array was {@code null}.
411411
* @throws NullPointerException
412-
* Thrown if {@link Charsets#UTF_8} is not initialized, which should never happen since it is
412+
* Thrown if {@link StandardCharsets#UTF_8} is not initialized, which should never happen since it is
413413
* required by the Java platform specification.
414414
* @since As of 1.7, throws {@link NullPointerException} instead of UnsupportedEncodingException
415415
*/

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public class QuotedPrintableCodec implements BinaryEncoder, BinaryDecoder, Strin
108108
}
109109

110110
/**
111-
* Default constructor, assumes default Charset of {@link Charsets#UTF_8}
111+
* Default constructor, assumes default Charset of {@link StandardCharsets#UTF_8}
112112
*/
113113
public QuotedPrintableCodec() {
114114
this(StandardCharsets.UTF_8, false);

src/test/java/org/apache/commons/codec/CharsetsTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,31 +37,37 @@ public void testToCharset() {
3737
Assert.assertEquals(Charset.forName("UTF-8"), Charsets.toCharset(Charset.forName("UTF-8")));
3838
}
3939

40+
@SuppressWarnings("deprecation")
4041
@Test
4142
public void testIso8859_1() {
4243
Assert.assertEquals("ISO-8859-1", Charsets.ISO_8859_1.name());
4344
}
4445

46+
@SuppressWarnings("deprecation")
4547
@Test
4648
public void testUsAscii() {
4749
Assert.assertEquals("US-ASCII", Charsets.US_ASCII.name());
4850
}
4951

52+
@SuppressWarnings("deprecation")
5053
@Test
5154
public void testUtf16() {
5255
Assert.assertEquals("UTF-16", Charsets.UTF_16.name());
5356
}
5457

58+
@SuppressWarnings("deprecation")
5559
@Test
5660
public void testUtf16Be() {
5761
Assert.assertEquals("UTF-16BE", Charsets.UTF_16BE.name());
5862
}
5963

64+
@SuppressWarnings("deprecation")
6065
@Test
6166
public void testUtf16Le() {
6267
Assert.assertEquals("UTF-16LE", Charsets.UTF_16LE.name());
6368
}
6469

70+
@SuppressWarnings("deprecation")
6571
@Test
6672
public void testUtf8() {
6773
Assert.assertEquals("UTF-8", Charsets.UTF_8.name());

0 commit comments

Comments
 (0)