Skip to content

Commit 0c838a7

Browse files
replace usage of deprecated Charsets constants with StandardCharsets constants
1 parent 9d43212 commit 0c838a7

8 files changed

Lines changed: 34 additions & 49 deletions

File tree

src/main/java/org/apache/commons/io/input/ReversedLinesFileReader.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.io.UnsupportedEncodingException;
2424
import java.nio.charset.Charset;
2525
import java.nio.charset.CharsetEncoder;
26+
import java.nio.charset.StandardCharsets;
2627

2728
import org.apache.commons.io.Charsets;
2829

@@ -91,7 +92,6 @@ public ReversedLinesFileReader(final File file, final Charset charset) throws IO
9192
* @throws IOException if an I/O error occurs
9293
* @since 2.3
9394
*/
94-
@SuppressWarnings("deprecation") // unavoidable until Java 7
9595
public ReversedLinesFileReader(final File file, final int blockSize, final Charset encoding) throws IOException {
9696
this.blockSize = blockSize;
9797
this.encoding = encoding;
@@ -103,7 +103,7 @@ public ReversedLinesFileReader(final File file, final int blockSize, final Chars
103103
if (maxBytesPerChar == 1f) {
104104
// all one byte encodings are no problem
105105
byteDecrement = 1;
106-
} else if (charset == Charsets.UTF_8) {
106+
} else if (charset == StandardCharsets.UTF_8) {
107107
// UTF-8 works fine out of the box, for multibyte sequences a second UTF-8 byte can never be a newline byte
108108
// http://en.wikipedia.org/wiki/UTF-8
109109
byteDecrement = 1;
@@ -114,11 +114,11 @@ public ReversedLinesFileReader(final File file, final int blockSize, final Chars
114114
charset == Charset.forName("gbk") || // Windows code page 936 (Simplified Chinese)
115115
charset == Charset.forName("x-windows-950")) { // Windows code page 950 (Traditional Chinese)
116116
byteDecrement = 1;
117-
} else if (charset == Charsets.UTF_16BE || charset == Charsets.UTF_16LE) {
117+
} else if (charset == StandardCharsets.UTF_16BE || charset == StandardCharsets.UTF_16LE) {
118118
// UTF-16 new line sequences are not allowed as second tuple of four byte sequences,
119119
// however byte order has to be specified
120120
byteDecrement = 2;
121-
} else if (charset == Charsets.UTF_16) {
121+
} else if (charset == StandardCharsets.UTF_16) {
122122
throw new UnsupportedEncodingException("For UTF-16, you need to specify the byte order (use UTF-16BE or " +
123123
"UTF-16LE)");
124124
} else {

src/test/java/org/apache/commons/io/CharsetsTestCase.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
*
2929
* @version $Id: CharEncodingTest.java 1298985 2012-03-09 19:12:49Z ggregory $
3030
*/
31+
@SuppressWarnings("deprecation") // testing deprecated code
3132
public class CharsetsTestCase {
3233

3334
@Test
@@ -44,7 +45,6 @@ public void testRequiredCharsets() {
4445
}
4546

4647
@Test
47-
@SuppressWarnings("deprecation") // unavoidable until Java 7
4848
public void testIso8859_1() {
4949
Assert.assertEquals("ISO-8859-1", Charsets.ISO_8859_1.name());
5050
}
@@ -58,31 +58,26 @@ public void testToCharset() {
5858
}
5959

6060
@Test
61-
@SuppressWarnings("deprecation") // unavoidable until Java 7
6261
public void testUsAscii() {
6362
Assert.assertEquals("US-ASCII", Charsets.US_ASCII.name());
6463
}
6564

6665
@Test
67-
@SuppressWarnings("deprecation") // unavoidable until Java 7
6866
public void testUtf16() {
6967
Assert.assertEquals("UTF-16", Charsets.UTF_16.name());
7068
}
7169

7270
@Test
73-
@SuppressWarnings("deprecation") // unavoidable until Java 7
7471
public void testUtf16Be() {
7572
Assert.assertEquals("UTF-16BE", Charsets.UTF_16BE.name());
7673
}
7774

7875
@Test
79-
@SuppressWarnings("deprecation") // unavoidable until Java 7
8076
public void testUtf16Le() {
8177
Assert.assertEquals("UTF-16LE", Charsets.UTF_16LE.name());
8278
}
8379

8480
@Test
85-
@SuppressWarnings("deprecation") // unavoidable until Java 7
8681
public void testUtf8() {
8782
Assert.assertEquals("UTF-8", Charsets.UTF_8.name());
8883
}

src/test/java/org/apache/commons/io/DemuxTestCase.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.io.ByteArrayInputStream;
2323
import java.io.InputStream;
2424
import java.io.OutputStream;
25+
import java.nio.charset.StandardCharsets;
2526
import java.util.HashMap;
2627
import java.util.Random;
2728

@@ -49,13 +50,12 @@ public class DemuxTestCase {
4950
private final HashMap<String, ByteArrayOutputStream> m_outputMap = new HashMap<>();
5051
private final HashMap<String, Thread> m_threadMap = new HashMap<>();
5152

52-
@SuppressWarnings("deprecation") // unavoidable until Java 7
5353
private String getOutput(final String threadName) {
5454
final ByteArrayOutputStream output =
5555
m_outputMap.get(threadName);
5656
assertNotNull("getOutput()", output);
5757

58-
return output.toString(Charsets.UTF_8);
58+
return output.toString(StandardCharsets.UTF_8);
5959
}
6060

6161
private String getInput(final String threadName) {

src/test/java/org/apache/commons/io/IOUtilsTestCase.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.nio.ByteBuffer;
2929
import java.nio.channels.FileChannel;
3030
import java.nio.channels.Selector;
31+
import java.nio.charset.StandardCharsets;
3132
import java.util.Arrays;
3233
import java.util.List;
3334

@@ -212,26 +213,25 @@ public synchronized void close() throws IOException {
212213
}
213214
}
214215

215-
@SuppressWarnings("deprecation") // unavoidable until Java 7
216216
@Test public void testContentEquals_InputStream_InputStream() throws Exception {
217217
{
218-
final ByteArrayInputStream input1 = new ByteArrayInputStream("".getBytes(Charsets.UTF_8));
218+
final ByteArrayInputStream input1 = new ByteArrayInputStream("".getBytes(StandardCharsets.UTF_8));
219219
assertTrue(IOUtils.contentEquals(input1, input1));
220220
}
221221
{
222-
final ByteArrayInputStream input1 = new ByteArrayInputStream("ABC".getBytes(Charsets.UTF_8));
222+
final ByteArrayInputStream input1 = new ByteArrayInputStream("ABC".getBytes(StandardCharsets.UTF_8));
223223
assertTrue(IOUtils.contentEquals(input1, input1));
224224
}
225225
assertTrue(IOUtils
226-
.contentEquals(new ByteArrayInputStream("".getBytes(Charsets.UTF_8)), new ByteArrayInputStream("".getBytes(Charsets.UTF_8))));
227-
assertTrue(IOUtils.contentEquals(new BufferedInputStream(new ByteArrayInputStream("".getBytes(Charsets.UTF_8))), new BufferedInputStream(
228-
new ByteArrayInputStream("".getBytes(Charsets.UTF_8)))));
229-
assertTrue(IOUtils.contentEquals(new ByteArrayInputStream("ABC".getBytes(Charsets.UTF_8)),
230-
new ByteArrayInputStream("ABC".getBytes(Charsets.UTF_8))));
231-
assertFalse(IOUtils.contentEquals(new ByteArrayInputStream("ABCD".getBytes(Charsets.UTF_8)),
232-
new ByteArrayInputStream("ABC".getBytes(Charsets.UTF_8))));
233-
assertFalse(IOUtils.contentEquals(new ByteArrayInputStream("ABC".getBytes(Charsets.UTF_8)),
234-
new ByteArrayInputStream("ABCD".getBytes(Charsets.UTF_8))));
226+
.contentEquals(new ByteArrayInputStream("".getBytes(StandardCharsets.UTF_8)), new ByteArrayInputStream("".getBytes(StandardCharsets.UTF_8))));
227+
assertTrue(IOUtils.contentEquals(new BufferedInputStream(new ByteArrayInputStream("".getBytes(StandardCharsets.UTF_8))), new BufferedInputStream(
228+
new ByteArrayInputStream("".getBytes(StandardCharsets.UTF_8)))));
229+
assertTrue(IOUtils.contentEquals(new ByteArrayInputStream("ABC".getBytes(StandardCharsets.UTF_8)),
230+
new ByteArrayInputStream("ABC".getBytes(StandardCharsets.UTF_8))));
231+
assertFalse(IOUtils.contentEquals(new ByteArrayInputStream("ABCD".getBytes(StandardCharsets.UTF_8)),
232+
new ByteArrayInputStream("ABC".getBytes(StandardCharsets.UTF_8))));
233+
assertFalse(IOUtils.contentEquals(new ByteArrayInputStream("ABC".getBytes(StandardCharsets.UTF_8)),
234+
new ByteArrayInputStream("ABCD".getBytes(StandardCharsets.UTF_8))));
235235
}
236236

237237
@Test public void testContentEquals_Reader_Reader() throws Exception {

src/test/java/org/apache/commons/io/filefilter/FileFilterTestCase.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
*/
1717
package org.apache.commons.io.filefilter;
1818

19-
import org.apache.commons.io.Charsets;
2019
import org.apache.commons.io.FileUtils;
2120
import org.apache.commons.io.IOCase;
2221
import org.apache.commons.io.IOUtils;
@@ -27,6 +26,7 @@
2726
import org.junit.Test;
2827

2928
import java.io.*;
29+
import java.nio.charset.StandardCharsets;
3030
import java.util.*;
3131

3232
import static org.junit.Assert.assertEquals;
@@ -1156,7 +1156,6 @@ public void testMakeFileOnly() throws Exception {
11561156

11571157
//-----------------------------------------------------------------------
11581158

1159-
@SuppressWarnings("deprecation") // unavoidable until Java 7
11601159
@Test
11611160
public void testMagicNumberFileFilterBytes() throws Exception {
11621161
final byte[] classFileMagicNumber =
@@ -1175,7 +1174,7 @@ public void testMagicNumberFileFilterBytes() throws Exception {
11751174
TestUtils.generateTestData(classFileAStream, (long) 32);
11761175
classFileAStream.close();
11771176

1178-
FileUtils.write(xmlFileB, xmlFileContent, Charsets.UTF_8);
1177+
FileUtils.write(xmlFileB, xmlFileContent, StandardCharsets.UTF_8);
11791178
FileUtils.touch(emptyFile);
11801179

11811180
IOFileFilter filter = new MagicNumberFileFilter(classFileMagicNumber);
@@ -1236,7 +1235,6 @@ public void testMagicNumberFileFilterBytesOffset() throws Exception {
12361235
assertFiltering(filter, dir, false);
12371236
}
12381237

1239-
@SuppressWarnings("deprecation") // unavoidable until Java 7
12401238
@Test
12411239
public void testMagicNumberFileFilterString() throws Exception {
12421240
final byte[] classFileMagicNumber =
@@ -1255,7 +1253,7 @@ public void testMagicNumberFileFilterString() throws Exception {
12551253
TestUtils.generateTestData(classFileAStream, (long) 32);
12561254
classFileAStream.close();
12571255

1258-
FileUtils.write(xmlFileB, xmlFileContent, Charsets.UTF_8);
1256+
FileUtils.write(xmlFileB, xmlFileContent, StandardCharsets.UTF_8);
12591257

12601258
IOFileFilter filter = new MagicNumberFileFilter(xmlMagicNumber);
12611259

@@ -1270,7 +1268,6 @@ public void testMagicNumberFileFilterString() throws Exception {
12701268
assertFiltering(filter, dir, false);
12711269
}
12721270

1273-
@SuppressWarnings("deprecation") // unavoidable until Java 7
12741271
@Test
12751272
public void testMagicNumberFileFilterStringOffset() throws Exception {
12761273
final String tarMagicNumber = "ustar";
@@ -1283,7 +1280,7 @@ public void testMagicNumberFileFilterStringOffset() throws Exception {
12831280

12841281
final OutputStream tarFileAStream = FileUtils.openOutputStream(tarFileA);
12851282
TestUtils.generateTestData(tarFileAStream, tarMagicNumberOffset);
1286-
IOUtils.write(tarMagicNumber, tarFileAStream, Charsets.UTF_8);
1283+
IOUtils.write(tarMagicNumber, tarFileAStream, StandardCharsets.UTF_8);
12871284
tarFileAStream.close();
12881285

12891286
if (!randomFileB.getParentFile().exists()) {

src/test/java/org/apache/commons/io/input/BOMInputStreamTest.java

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.io.InputStream;
2424
import java.io.Reader;
2525
import java.nio.charset.Charset;
26+
import java.nio.charset.StandardCharsets;
2627

2728
import org.apache.commons.io.ByteOrderMark;
2829
import org.apache.commons.io.Charsets;
@@ -440,8 +441,7 @@ public void testReadWithBOMInclude() throws Exception {
440441

441442
@Test
442443
public void testReadWithBOMUtf16Be() throws Exception {
443-
@SuppressWarnings("deprecation") // unavoidable until Java 7
444-
final byte[] data = "ABC".getBytes(Charsets.UTF_16BE);
444+
final byte[] data = "ABC".getBytes(StandardCharsets.UTF_16BE);
445445
final BOMInputStream in = new BOMInputStream(createUtf16BeDataStream(data, true), ByteOrderMark.UTF_16BE);
446446
assertEquals(0, in.read());
447447
assertEquals('A', in.read());
@@ -464,8 +464,7 @@ public void testReadWithBOMUtf16Be() throws Exception {
464464

465465
@Test
466466
public void testReadWithBOMUtf16Le() throws Exception {
467-
@SuppressWarnings("deprecation") // unavoidable until Java 7
468-
final byte[] data = "ABC".getBytes(Charsets.UTF_16LE);
467+
final byte[] data = "ABC".getBytes(StandardCharsets.UTF_16LE);
469468
final BOMInputStream in = new BOMInputStream(createUtf16LeDataStream(data, true), ByteOrderMark.UTF_16LE);
470469
assertEquals('A', in.read());
471470
assertEquals(0, in.read());
@@ -548,8 +547,7 @@ public void testReadWithBOMUtf32Le() throws Exception {
548547

549548
@Test
550549
public void testReadWithBOMUtf8() throws Exception {
551-
@SuppressWarnings("deprecation") // unavoidable until Java 7
552-
final byte[] data = "ABC".getBytes(Charsets.UTF_8);
550+
final byte[] data = "ABC".getBytes(StandardCharsets.UTF_8);
553551
final BOMInputStream in = new BOMInputStream(createUtf8DataStream(data, true), ByteOrderMark.UTF_8);
554552
assertEquals('A', in.read());
555553
assertEquals('B', in.read());
@@ -622,17 +620,15 @@ public void testReadXmlWithBOMUcs4() throws Exception {
622620
}
623621

624622
@Test
625-
@SuppressWarnings("deprecation") // unavoidable until Java 7
626623
public void testReadXmlWithBOMUtf16Be() throws Exception {
627-
final byte[] data = "<?xml version=\"1.0\" encoding=\"UTF-16BE\"?><X/>".getBytes(Charsets.UTF_16BE);
624+
final byte[] data = "<?xml version=\"1.0\" encoding=\"UTF-16BE\"?><X/>".getBytes(StandardCharsets.UTF_16BE);
628625
parseXml(new BOMInputStream(createUtf16BeDataStream(data, true), ByteOrderMark.UTF_16BE));
629626
parseXml(createUtf16BeDataStream(data, true));
630627
}
631628

632629
@Test
633-
@SuppressWarnings("deprecation") // unavoidable until Java 7
634630
public void testReadXmlWithBOMUtf16Le() throws Exception {
635-
final byte[] data = "<?xml version=\"1.0\" encoding=\"UTF-16LE\"?><X/>".getBytes(Charsets.UTF_16LE);
631+
final byte[] data = "<?xml version=\"1.0\" encoding=\"UTF-16LE\"?><X/>".getBytes(StandardCharsets.UTF_16LE);
636632
parseXml(new BOMInputStream(createUtf16LeDataStream(data, true), ByteOrderMark.UTF_16LE));
637633
parseXml(createUtf16LeDataStream(data, true));
638634
}
@@ -657,8 +653,7 @@ public void testReadXmlWithBOMUtf32Le() throws Exception {
657653

658654
@Test
659655
public void testReadXmlWithBOMUtf8() throws Exception {
660-
@SuppressWarnings("deprecation") // unavoidable until Java 7
661-
final byte[] data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><X/>".getBytes(Charsets.UTF_8);
656+
final byte[] data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><X/>".getBytes(StandardCharsets.UTF_8);
662657
parseXml(new BOMInputStream(createUtf8DataStream(data, true)));
663658
parseXml(createUtf8DataStream(data, true));
664659
}

src/test/java/org/apache/commons/io/input/TailerTest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@
1818

1919
import java.io.*;
2020
import java.nio.charset.Charset;
21+
import java.nio.charset.StandardCharsets;
2122
import java.util.ArrayList;
2223
import java.util.Collections;
2324
import java.util.List;
2425
import java.util.concurrent.Executor;
2526
import java.util.concurrent.ScheduledThreadPoolExecutor;
2627

27-
import org.apache.commons.io.Charsets;
2828
import org.apache.commons.io.FileUtils;
2929
import org.apache.commons.io.IOUtils;
3030
import org.apache.commons.io.testtools.FileBasedTestCase;
@@ -110,7 +110,6 @@ public void testBufferBreak() throws Exception {
110110
listener.clear();
111111
}
112112

113-
@SuppressWarnings("deprecation") // unavoidable until Java 7
114113
@Test
115114
public void testMultiByteBreak() throws Exception {
116115
System.out.println("testMultiByteBreak() Default charset: "+Charset.defaultCharset().displayName());
@@ -122,7 +121,7 @@ public void testMultiByteBreak() throws Exception {
122121
final String osname = System.getProperty("os.name");
123122
final boolean isWindows = osname.startsWith("Windows");
124123
// Need to use UTF-8 to read & write the file otherwise it can be corrupted (depending on the default charset)
125-
final Charset charsetUTF8 = Charsets.UTF_8;
124+
final Charset charsetUTF8 = StandardCharsets.UTF_8;
126125
tailer = new Tailer(file, charsetUTF8, listener, delay, false, isWindows, 4096);
127126
final Thread thread = new Thread(tailer);
128127
thread.start();

src/test/java/org/apache/commons/io/output/LockableFileWriterTest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
import java.io.File;
2020
import java.io.IOException;
2121
import java.io.Writer;
22+
import java.nio.charset.StandardCharsets;
2223
import java.nio.charset.UnsupportedCharsetException;
2324

24-
import org.apache.commons.io.Charsets;
2525
import org.apache.commons.io.FileUtils;
2626
import org.apache.commons.io.IOUtils;
2727
import org.apache.commons.io.testtools.FileBasedTestCase;
@@ -109,7 +109,6 @@ public void tearDown() throws IOException {
109109
}
110110

111111
//-----------------------------------------------------------------------
112-
@SuppressWarnings("deprecation") // unavoidable until Java 7
113112
@Test public void testAlternateLockDir() throws IOException {
114113
LockableFileWriter lfw1 = null;
115114
LockableFileWriter lfw2 = null;
@@ -121,7 +120,7 @@ public void tearDown() throws IOException {
121120

122121
// try to open a second writer
123122
try {
124-
lfw2 = new LockableFileWriter(file, Charsets.UTF_8, true, altLockDir.getAbsolutePath());
123+
lfw2 = new LockableFileWriter(file, StandardCharsets.UTF_8, true, altLockDir.getAbsolutePath());
125124
fail("Somehow able to open a locked file. ");
126125
} catch(final IOException ioe) {
127126
final String msg = ioe.getMessage();

0 commit comments

Comments
 (0)