Skip to content

Commit 2ce0a11

Browse files
author
Gary Gregory
committed
Reuse StandardCharsets
1 parent 9a25718 commit 2ce0a11

22 files changed

Lines changed: 135 additions & 103 deletions

src/main/java/org/apache/commons/io/FileUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2134,7 +2134,7 @@ public static LineIterator lineIterator(final File file) throws IOException {
21342134
* The recommended usage pattern is:
21352135
* </p>
21362136
* <pre>
2137-
* LineIterator it = FileUtils.lineIterator(file, "UTF-8");
2137+
* LineIterator it = FileUtils.lineIterator(file, StandardCharsets.UTF_8.name());
21382138
* try {
21392139
* while (it.hasNext()) {
21402140
* String line = it.nextLine();

src/main/java/org/apache/commons/io/IOUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1726,7 +1726,7 @@ public static LineIterator lineIterator(final InputStream input, final Charset c
17261726
* </p>
17271727
* <pre>
17281728
* try {
1729-
* LineIterator it = IOUtils.lineIterator(stream, "UTF-8");
1729+
* LineIterator it = IOUtils.lineIterator(stream, StandardCharsets.UTF_8.name());
17301730
* while (it.hasNext()) {
17311731
* String line = it.nextLine();
17321732
* /// do something with line

src/main/java/org/apache/commons/io/LineIterator.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
* <p>
3636
* The recommended usage pattern is:
3737
* <pre>
38-
* LineIterator it = FileUtils.lineIterator(file, "UTF-8");
38+
* LineIterator it = FileUtils.lineIterator(file, StandardCharsets.UTF_8.name());
3939
* try {
4040
* while (it.hasNext()) {
4141
* String line = it.nextLine();
@@ -64,8 +64,10 @@ public class LineIterator implements Iterator<String>, Closeable {
6464
public static void closeQuietly(final LineIterator iterator) {
6565
IOUtils.closeQuietly(iterator);
6666
}
67+
6768
/** The reader that is being read. */
6869
private final BufferedReader bufferedReader;
70+
6971
/** The current line. */
7072
private String cachedLine;
7173

src/main/java/org/apache/commons/io/output/WriterOutputStream.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.nio.charset.CharsetDecoder;
2626
import java.nio.charset.CoderResult;
2727
import java.nio.charset.CodingErrorAction;
28+
import java.nio.charset.StandardCharsets;
2829

2930
import org.apache.commons.io.Charsets;
3031
import org.apache.commons.io.charset.CharsetDecoders;
@@ -83,7 +84,7 @@ public class WriterOutputStream extends OutputStream {
8384
* @param charset the charset to check the support for
8485
*/
8586
private static void checkIbmJdkWithBrokenUTF16(final Charset charset){
86-
if (!"UTF-16".equals(charset.name())) {
87+
if (!StandardCharsets.UTF_16.name().equals(charset.name())) {
8788
return;
8889
}
8990
final String TEST_STRING_2 = "v\u00e9s";

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public void copy_inputStreamToWriter() throws Exception {
9999
@Test
100100
public void copy_inputStreamToWriterWithEncoding() throws Exception {
101101
final String inDataStr = "data";
102-
final String charsetName = "UTF-8";
102+
final String charsetName = StandardCharsets.UTF_8.name();
103103
final StringWriter writer = new StringWriter();
104104
CopyUtils.copy(new StringInputStream(inDataStr, charsetName), writer, charsetName);
105105
assertEquals(inDataStr, writer.toString());
@@ -160,7 +160,7 @@ public void copy_stringToWriter() throws Exception {
160160
@Test
161161
public void testCopy_byteArrayToWriterWithEncoding() throws Exception {
162162
final String inDataStr = "data";
163-
final String charsetName = "UTF-8";
163+
final String charsetName = StandardCharsets.UTF_8.name();
164164
final StringWriter writer = new StringWriter();
165165
CopyUtils.copy(inDataStr.getBytes(charsetName), writer, charsetName);
166166
assertEquals(inDataStr, writer.toString());

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@
8989
@SuppressWarnings({"deprecation", "ResultOfMethodCallIgnored"}) // unit tests include tests of many deprecated methods
9090
public class FileUtilsTest extends AbstractTempDirTest {
9191

92+
private static final String UTF_8 = StandardCharsets.UTF_8.name();
93+
9294
/**
9395
* DirectoryWalker implementation that recursively lists all files and directories.
9496
*/
@@ -1426,16 +1428,16 @@ public void testFileUtils() throws Exception {
14261428

14271429
final File file2 = new File(tempDirFile, "test2.txt");
14281430

1429-
FileUtils.writeStringToFile(file2, filename, "UTF-8");
1431+
FileUtils.writeStringToFile(file2, filename, UTF_8);
14301432
assertTrue(file2.exists());
14311433
assertTrue(file2.length() > 0);
14321434

1433-
final String file2contents = FileUtils.readFileToString(file2, "UTF-8");
1435+
final String file2contents = FileUtils.readFileToString(file2, UTF_8);
14341436
assertEquals(filename, file2contents, "Second file's contents correct");
14351437

14361438
assertTrue(file2.delete());
14371439

1438-
final String contents = FileUtils.readFileToString(new File(filename), "UTF-8");
1440+
final String contents = FileUtils.readFileToString(new File(filename), UTF_8);
14391441
assertEquals("This is a test", contents, "FileUtils.fileRead()");
14401442

14411443
}
@@ -2480,7 +2482,7 @@ public void testReadLines() throws Exception {
24802482
final String[] data = {"hello", "/u1234", "", "this is", "some text"};
24812483
TestUtils.createLineBasedFile(file, data);
24822484

2483-
final List<String> lines = FileUtils.readLines(file, "UTF-8");
2485+
final List<String> lines = FileUtils.readLines(file, UTF_8);
24842486
assertEquals(Arrays.asList(data), lines);
24852487
} finally {
24862488
TestUtils.deleteFile(file);

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@
9292
@SuppressWarnings("deprecation") // deliberately testing deprecated code
9393
public class IOUtilsTest {
9494

95+
private static final String UTF_8 = StandardCharsets.UTF_8.name();
96+
9597
private static final int FILE_SIZE = 1024 * 4 + 1;
9698

9799
/** Determine if this is windows. */
@@ -1028,7 +1030,7 @@ public void testReadLines_InputStream_String() throws Exception {
10281030
TestUtils.createLineBasedFile(file, data);
10291031

10301032
in = Files.newInputStream(file.toPath());
1031-
final List<String> lines = IOUtils.readLines(in, "UTF-8");
1033+
final List<String> lines = IOUtils.readLines(in, UTF_8);
10321034
assertEquals(Arrays.asList(data), lines);
10331035
assertEquals(-1, in.read());
10341036
} finally {
@@ -1449,7 +1451,7 @@ public void testToByteArray_InputStream_SizeZero() throws Exception {
14491451

14501452
@Test
14511453
public void testToByteArray_Reader() throws IOException {
1452-
final String charsetName = "UTF-8";
1454+
final String charsetName = UTF_8;
14531455
final byte[] expecteds = charsetName.getBytes(charsetName);
14541456
byte[] actuals = IOUtils.toByteArray(new InputStreamReader(new ByteArrayInputStream(expecteds)));
14551457
assertArrayEquals(expecteds, actuals);
@@ -1505,7 +1507,7 @@ public void testToCharArray_InputStream() throws Exception {
15051507
@Test
15061508
public void testToCharArray_InputStream_CharsetName() throws Exception {
15071509
try (InputStream fin = Files.newInputStream(testFilePath)) {
1508-
final char[] out = IOUtils.toCharArray(fin, "UTF-8");
1510+
final char[] out = IOUtils.toCharArray(fin, UTF_8);
15091511
assertNotNull(out);
15101512
assertEquals(0, fin.available(), "Not all chars were read");
15111513
assertEquals(FILE_SIZE, out.length, "Wrong output size");
@@ -1539,7 +1541,7 @@ public void testToInputStream_CharSequence() throws Exception {
15391541
inStream = IOUtils.toInputStream(csq, (String) null);
15401542
bytes = IOUtils.toByteArray(inStream);
15411543
assertEqualContent(csq.toString().getBytes(), bytes);
1542-
inStream = IOUtils.toInputStream(csq, "UTF-8");
1544+
inStream = IOUtils.toInputStream(csq, UTF_8);
15431545
bytes = IOUtils.toByteArray(inStream);
15441546
assertEqualContent(csq.toString().getBytes(StandardCharsets.UTF_8), bytes);
15451547
}
@@ -1560,7 +1562,7 @@ public void testToInputStream_String() throws Exception {
15601562
inStream = IOUtils.toInputStream(str, (String) null);
15611563
bytes = IOUtils.toByteArray(inStream);
15621564
assertEqualContent(str.getBytes(), bytes);
1563-
inStream = IOUtils.toInputStream(str, "UTF-8");
1565+
inStream = IOUtils.toInputStream(str, UTF_8);
15641566
bytes = IOUtils.toByteArray(inStream);
15651567
assertEqualContent(str.getBytes(StandardCharsets.UTF_8), bytes);
15661568
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -543,13 +543,13 @@ public void testWriteLines_OutputStream_Encoding() throws Exception {
543543
final ByteArrayOutputStream baout = new ByteArrayOutputStream();
544544
final ThrowOnFlushAndCloseOutputStream out = new ThrowOnFlushAndCloseOutputStream(baout, false, true);
545545

546-
IOUtils.writeLines(list, "*", out, "UTF-8");
546+
IOUtils.writeLines(list, "*", out, StandardCharsets.UTF_8.name());
547547

548548
out.off();
549549
out.flush();
550550

551551
final String expected = "hello\u8364*world**this is**some text*";
552-
final String actual = baout.toString("UTF-8");
552+
final String actual = baout.toString(StandardCharsets.UTF_8.name());
553553
assertEquals(expected, actual);
554554
}
555555

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

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.io.IOException;
2828
import java.io.Reader;
2929
import java.io.StringReader;
30+
import java.nio.charset.StandardCharsets;
3031
import java.nio.charset.UnsupportedCharsetException;
3132
import java.nio.file.Files;
3233
import java.nio.file.NoSuchFileException;
@@ -42,6 +43,8 @@
4243
*/
4344
public class LineIteratorTest {
4445

46+
private static final String UTF_8 = StandardCharsets.UTF_8.name();
47+
4548
@TempDir
4649
public File temporaryFolder;
4750

@@ -108,7 +111,7 @@ private List<String> createStringLines(final int lineCount) {
108111
* @throws IOException If an I/O error occurs while creating the file
109112
*/
110113
private void doTestFileWithSpecifiedLines(final int lineCount) throws IOException {
111-
final String encoding = "UTF-8";
114+
final String encoding = UTF_8;
112115

113116
final String fileName = "LineIterator-" + lineCount + "-test.txt";
114117
final File testFile = new File(temporaryFolder, fileName);
@@ -134,7 +137,7 @@ private void doTestFileWithSpecifiedLines(final int lineCount) throws IOExceptio
134137

135138
@Test
136139
public void testCloseEarly() throws Exception {
137-
final String encoding = "UTF-8";
140+
final String encoding = UTF_8;
138141

139142
final File testFile = new File(temporaryFolder, "LineIterator-closeEarly.txt");
140143
createLinesFile(testFile, encoding, 3);
@@ -195,7 +198,7 @@ protected boolean isValidLine(final String line) {
195198

196199
@Test
197200
public void testFilteringBufferedReader() throws Exception {
198-
final String encoding = "UTF-8";
201+
final String encoding = UTF_8;
199202

200203
final String fileName = "LineIterator-Filter-test.txt";
201204
final File testFile = new File(temporaryFolder, fileName);
@@ -207,7 +210,7 @@ public void testFilteringBufferedReader() throws Exception {
207210

208211
@Test
209212
public void testFilteringFileReader() throws Exception {
210-
final String encoding = "UTF-8";
213+
final String encoding = UTF_8;
211214

212215
final String fileName = "LineIterator-Filter-test.txt";
213216
final File testFile = new File(temporaryFolder, fileName);
@@ -222,15 +225,15 @@ public void testInvalidEncoding() throws Exception {
222225
final String encoding = "XXXXXXXX";
223226

224227
final File testFile = new File(temporaryFolder, "LineIterator-invalidEncoding.txt");
225-
createLinesFile(testFile, "UTF-8", 3);
228+
createLinesFile(testFile, UTF_8, 3);
226229

227230
assertThrows(UnsupportedCharsetException.class, () -> FileUtils.lineIterator(testFile, encoding));
228231
}
229232

230233
@Test
231234
public void testMissingFile() throws Exception {
232235
final File testFile = new File(temporaryFolder, "dummy-missing-file.txt");
233-
assertThrows(NoSuchFileException.class, () -> FileUtils.lineIterator(testFile, "UTF-8"));
236+
assertThrows(NoSuchFileException.class, () -> FileUtils.lineIterator(testFile, UTF_8));
234237
}
235238

236239
@Test
@@ -255,7 +258,7 @@ public void testNextLineOnlyNullEncoding() throws Exception {
255258

256259
@Test
257260
public void testNextLineOnlyUtf8Encoding() throws Exception {
258-
final String encoding = "UTF-8";
261+
final String encoding = UTF_8;
259262

260263
final File testFile = new File(temporaryFolder, "LineIterator-nextOnly.txt");
261264
final List<String> lines = createLinesFile(testFile, encoding, 3);
@@ -310,7 +313,7 @@ public void testTwoLines() throws Exception {
310313

311314
@Test
312315
public void testValidEncoding() throws Exception {
313-
final String encoding = "UTF-8";
316+
final String encoding = UTF_8;
314317

315318
final File testFile = new File(temporaryFolder, "LineIterator-validEncoding.txt");
316319
createLinesFile(testFile, encoding, 3);

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

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636

3737
public class CharSequenceInputStreamTest {
3838

39+
private static final String UTF_16 = StandardCharsets.UTF_16.name();
40+
private static final String UTF_8 = StandardCharsets.UTF_8.name();
3941
private static final String ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
4042
private static final String LARGE_TEST_STRING;
4143

@@ -172,7 +174,7 @@ public void testBufferedRead_RequiredCharset() throws IOException {
172174

173175
@Test
174176
public void testBufferedRead_UTF8() throws IOException {
175-
testBufferedRead(TEST_STRING, "UTF-8");
177+
testBufferedRead(TEST_STRING, UTF_8);
176178
}
177179

178180
private void testCharsetMismatchInfiniteLoop(final String csName) throws IOException {
@@ -222,37 +224,37 @@ private void testIO_356(final int bufferSize, final int dataSize, final int read
222224

223225
@Test
224226
public void testIO_356_B10_D10_S0_UTF16() throws Exception {
225-
testIO_356(10, 10, 0, "UTF-16");
227+
testIO_356(10, 10, 0, UTF_16);
226228
}
227229

228230
@Test
229231
public void testIO_356_B10_D10_S0_UTF8() throws Exception {
230-
testIO_356(10, 10, 0, "UTF-8");
232+
testIO_356(10, 10, 0, UTF_8);
231233
}
232234

233235
@Test
234236
public void testIO_356_B10_D10_S1_UTF8() throws Exception {
235-
testIO_356(10, 10, 1, "UTF-8");
237+
testIO_356(10, 10, 1, UTF_8);
236238
}
237239

238240
@Test
239241
public void testIO_356_B10_D10_S2_UTF8() throws Exception {
240-
testIO_356(10, 10, 2, "UTF-8");
242+
testIO_356(10, 10, 2, UTF_8);
241243
}
242244

243245
@Test
244246
public void testIO_356_B10_D13_S0_UTF8() throws Exception {
245-
testIO_356(10, 13, 0, "UTF-8");
247+
testIO_356(10, 13, 0, UTF_8);
246248
}
247249

248250
@Test
249251
public void testIO_356_B10_D13_S1_UTF8() throws Exception {
250-
testIO_356(10, 13, 1, "UTF-8");
252+
testIO_356(10, 13, 1, UTF_8);
251253
}
252254

253255
@Test
254256
public void testIO_356_B10_D20_S0_UTF8() throws Exception {
255-
testIO_356(10, 20, 0, "UTF-8");
257+
testIO_356(10, 20, 0, UTF_8);
256258
}
257259

258260
private void testIO_356_Loop(final String csName, final int maxBytesPerChar) throws Exception {
@@ -284,7 +286,7 @@ public void testLargeBufferedRead_RequiredCharsets() throws IOException {
284286

285287
@Test
286288
public void testLargeBufferedRead_UTF8() throws IOException {
287-
testBufferedRead(LARGE_TEST_STRING, "UTF-8");
289+
testBufferedRead(LARGE_TEST_STRING, UTF_8);
288290
}
289291

290292
@Test
@@ -296,7 +298,7 @@ public void testLargeSingleByteRead_RequiredCharsets() throws IOException {
296298

297299
@Test
298300
public void testLargeSingleByteRead_UTF8() throws IOException {
299-
testSingleByteRead(LARGE_TEST_STRING, "UTF-8");
301+
testSingleByteRead(LARGE_TEST_STRING, UTF_8);
300302
}
301303

302304
// This test is broken for charsets that don't create a single byte for each char
@@ -331,12 +333,12 @@ public void testMarkReset_USASCII() throws Exception {
331333

332334
@Test
333335
public void testMarkReset_UTF8() throws Exception {
334-
testMarkReset("UTF-8");
336+
testMarkReset(UTF_8);
335337
}
336338

337339
@Test
338340
public void testMarkSupported() throws Exception {
339-
try (InputStream r = new CharSequenceInputStream("test", "UTF-8")) {
341+
try (InputStream r = new CharSequenceInputStream("test", UTF_8)) {
340342
assertTrue(r.markSupported());
341343
}
342344
}
@@ -366,7 +368,7 @@ private void testReadZero(final String csName) throws Exception {
366368

367369
@Test
368370
public void testReadZero_EmptyString() throws Exception {
369-
try (InputStream r = new CharSequenceInputStream("", "UTF-8")) {
371+
try (InputStream r = new CharSequenceInputStream("", UTF_8)) {
370372
final byte[] bytes = new byte[30];
371373
assertEquals(0, r.read(bytes, 0, 0));
372374
}
@@ -401,12 +403,12 @@ public void testSingleByteRead_RequiredCharsets() throws IOException {
401403

402404
@Test
403405
public void testSingleByteRead_UTF16() throws IOException {
404-
testSingleByteRead(TEST_STRING, "UTF-16");
406+
testSingleByteRead(TEST_STRING, UTF_16);
405407
}
406408

407409
@Test
408410
public void testSingleByteRead_UTF8() throws IOException {
409-
testSingleByteRead(TEST_STRING, "UTF-8");
411+
testSingleByteRead(TEST_STRING, UTF_8);
410412
}
411413

412414
// This is broken for charsets that don't map each char to a byte
@@ -435,6 +437,6 @@ public void testSkip_USASCII() throws Exception {
435437

436438
@Test
437439
public void testSkip_UTF8() throws Exception {
438-
testSkip("UTF-8");
440+
testSkip(UTF_8);
439441
}
440442
}

0 commit comments

Comments
 (0)