Skip to content

Commit 54c37df

Browse files
mihnitamarkusicu
authored andcommitted
ICU-23247 Fix Errorprone DefaultCharset error
1 parent 721daf5 commit 54c37df

15 files changed

Lines changed: 223 additions & 225 deletions

File tree

icu4j/main/common_tests/src/test/java/com/ibm/icu/dev/test/format/NumberFormatDataDrivenTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@
2020
import java.math.RoundingMode;
2121
import java.text.ParsePosition;
2222
import org.junit.Test;
23+
import org.junit.runner.RunWith;
24+
import org.junit.runners.JUnit4;
2325

26+
@RunWith(JUnit4.class)
2427
public class NumberFormatDataDrivenTest {
2528

2629
private static ULocale EN = new ULocale("en");

icu4j/main/common_tests/src/test/java/com/ibm/icu/dev/test/util/ICUResourceBundleTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.net.URL;
2828
import java.net.URLConnection;
2929
import java.nio.ByteBuffer;
30+
import java.nio.charset.StandardCharsets;
3031
import java.util.Enumeration;
3132
import java.util.HashMap;
3233
import java.util.HashSet;
@@ -65,17 +66,16 @@ public void TestGetResources() {
6566
JarEntry je = jc.getJarEntry();
6667
logln("jar entry: " + je.toString());
6768
} else {
68-
BufferedReader br =
69-
new BufferedReader(new InputStreamReader(c.getInputStream()));
70-
logln("input stream:");
71-
try {
69+
try (BufferedReader br =
70+
new BufferedReader(
71+
new InputStreamReader(
72+
c.getInputStream(), StandardCharsets.UTF_8))) {
73+
logln("input stream:");
7274
String line = null;
7375
int n = 0;
7476
while ((line = br.readLine()) != null) {
7577
logln(" " + ++n + ": " + line);
7678
}
77-
} finally {
78-
br.close();
7979
}
8080
}
8181
}

icu4j/main/core/src/main/java/com/ibm/icu/impl/URLHandler.java

Lines changed: 41 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@
1111

1212
import java.io.BufferedReader;
1313
import java.io.File;
14-
import java.io.IOException;
1514
import java.io.InputStream;
1615
import java.io.InputStreamReader;
1716
import java.lang.reflect.InvocationTargetException;
1817
import java.lang.reflect.Method;
1918
import java.net.JarURLConnection;
2019
import java.net.URISyntaxException;
2120
import java.net.URL;
21+
import java.nio.charset.StandardCharsets;
2222
import java.util.Enumeration;
2323
import java.util.HashMap;
2424
import java.util.Map;
@@ -33,63 +33,38 @@ public abstract class URLHandler {
3333
private static final boolean DEBUG = ICUDebug.enabled("URLHandler");
3434

3535
static {
36-
Map<String, Method> h = null;
37-
38-
BufferedReader br = null;
39-
try {
40-
@SuppressWarnings("resource") // Closed by BufferedReader.
41-
ClassLoader loader = ClassLoaderUtil.getClassLoader(URLHandler.class);
42-
InputStream is = loader.getResourceAsStream(PROPNAME);
43-
44-
if (is != null) {
45-
Class<?>[] params = {URL.class};
46-
br = new BufferedReader(new InputStreamReader(is));
47-
48-
for (String line = br.readLine(); line != null; line = br.readLine()) {
49-
line = line.trim();
50-
51-
if (line.length() == 0 || line.charAt(0) == '#') {
52-
continue;
53-
}
54-
55-
int ix = line.indexOf('=');
56-
57-
if (ix == -1) {
58-
if (DEBUG) System.err.println("bad urlhandler line: '" + line + "'");
59-
break;
60-
}
61-
62-
String key = line.substring(0, ix).trim();
63-
String value = line.substring(ix + 1).trim();
64-
65-
try {
66-
Class<?> cl = Class.forName(value);
67-
Method m = cl.getDeclaredMethod("get", params);
68-
69-
if (h == null) {
70-
h = new HashMap<String, Method>();
71-
}
72-
73-
h.put(key, m);
74-
} catch (ClassNotFoundException e) {
75-
if (DEBUG) System.err.println(e);
76-
} catch (NoSuchMethodException e) {
77-
if (DEBUG) System.err.println(e);
78-
} catch (SecurityException e) {
79-
if (DEBUG) System.err.println(e);
80-
}
81-
}
82-
br.close();
83-
}
36+
final Map<String, Method> h = new HashMap<>();
37+
38+
Class<?>[] params = {URL.class};
39+
ClassLoader loader = ClassLoaderUtil.getClassLoader(URLHandler.class);
40+
try (InputStream is = loader.getResourceAsStream(PROPNAME);
41+
InputStreamReader isr = new InputStreamReader(is, StandardCharsets.UTF_8);
42+
BufferedReader br = new BufferedReader(isr)) {
43+
br.lines()
44+
.map(String::trim)
45+
.filter(line -> !line.isEmpty() && !line.startsWith("#"))
46+
.forEach(
47+
line -> {
48+
int ix = line.indexOf('=');
49+
if (ix == -1) {
50+
if (DEBUG)
51+
System.err.println("bad urlhandler line: '" + line + "'");
52+
return;
53+
}
54+
String key = line.substring(0, ix).trim();
55+
String value = line.substring(ix + 1).trim();
56+
try {
57+
Class<?> cl = Class.forName(value);
58+
Method m = cl.getDeclaredMethod("get", params);
59+
h.put(key, m);
60+
} catch (ClassNotFoundException
61+
| NoSuchMethodException
62+
| SecurityException e) {
63+
if (DEBUG) System.err.println(e);
64+
}
65+
});
8466
} catch (Throwable t) {
8567
if (DEBUG) System.err.println(t);
86-
} finally {
87-
if (br != null) {
88-
try {
89-
br.close();
90-
} catch (IOException ignored) {
91-
}
92-
}
9368
}
9469

9570
handlers = h;
@@ -102,23 +77,19 @@ public static URLHandler get(URL url) {
10277

10378
String protocol = url.getProtocol();
10479

105-
if (handlers != null) {
106-
Method m = handlers.get(protocol);
80+
Method m = handlers.get(protocol);
10781

108-
if (m != null) {
109-
try {
110-
URLHandler handler = (URLHandler) m.invoke(null, new Object[] {url});
82+
if (m != null) {
83+
try {
84+
URLHandler handler = (URLHandler) m.invoke(null, new Object[] {url});
11185

112-
if (handler != null) {
113-
return handler;
114-
}
115-
} catch (IllegalAccessException e) {
116-
if (DEBUG) System.err.println(e);
117-
} catch (IllegalArgumentException e) {
118-
if (DEBUG) System.err.println(e);
119-
} catch (InvocationTargetException e) {
120-
if (DEBUG) System.err.println(e);
86+
if (handler != null) {
87+
return handler;
12188
}
89+
} catch (IllegalAccessException
90+
| IllegalArgumentException
91+
| InvocationTargetException e) {
92+
if (DEBUG) System.err.println(e);
12293
}
12394
}
12495

icu4j/main/core/src/main/java/com/ibm/icu/impl/data/ResourceReader.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import java.io.InputStream;
1717
import java.io.InputStreamReader;
1818
import java.io.UnsupportedEncodingException;
19+
import java.nio.charset.StandardCharsets;
1920

2021
/**
2122
* A reader for text resource data in the current package or the package of a given class object.
@@ -95,7 +96,7 @@ public ResourceReader(InputStream is, String resourceName, String encoding) {
9596
try {
9697
InputStreamReader isr =
9798
(encoding == null)
98-
? new InputStreamReader(is)
99+
? new InputStreamReader(is, StandardCharsets.UTF_8)
99100
: new InputStreamReader(is, encoding);
100101

101102
this.reader = new BufferedReader(isr);
@@ -233,7 +234,7 @@ private void _reset() throws UnsupportedEncodingException {
233234

234235
InputStreamReader isr =
235236
(encoding == null)
236-
? new InputStreamReader(is)
237+
? new InputStreamReader(is, StandardCharsets.UTF_8)
237238
: new InputStreamReader(is, encoding);
238239
reader = new BufferedReader(isr);
239240
lineNo = 0;

icu4j/main/core/src/test/java/com/ibm/icu/dev/test/format/DataDrivenNumberFormatTestUtility.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import java.io.ByteArrayOutputStream;
1717
import java.io.IOException;
1818
import java.io.PrintStream;
19+
import java.nio.charset.StandardCharsets;
1920
import java.util.ArrayList;
2021
import java.util.List;
2122

@@ -249,9 +250,9 @@ private void run(String fileName, RunMode runMode) {
249250
} else if (!shouldFail && errorMessage != null) {
250251
if (err != null) {
251252
ByteArrayOutputStream os = new ByteArrayOutputStream();
252-
PrintStream ps = new PrintStream(os);
253+
PrintStream ps = new PrintStream(os, true, StandardCharsets.UTF_8);
253254
err.printStackTrace(ps);
254-
String stackTrace = os.toString();
255+
String stackTrace = os.toString(StandardCharsets.UTF_8);
255256
showError(
256257
errorMessage
257258
+ " Stack trace: "
@@ -266,9 +267,9 @@ private void run(String fileName, RunMode runMode) {
266267
}
267268
} catch (Exception e) {
268269
ByteArrayOutputStream os = new ByteArrayOutputStream();
269-
PrintStream ps = new PrintStream(os);
270+
PrintStream ps = new PrintStream(os, true, StandardCharsets.UTF_8);
270271
e.printStackTrace(ps);
271-
String stackTrace = os.toString();
272+
String stackTrace = os.toString(StandardCharsets.UTF_8);
272273
showError(
273274
"MAJOR ERROR: "
274275
+ e.toString()

icu4j/main/core/src/test/java/com/ibm/icu/dev/test/format/PersonNameConsistencyTest.java

Lines changed: 52 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -50,20 +50,24 @@ private static Map<String, String> makeKnownIssuesList() {
5050

5151
static List<String> readTestCases() throws Exception {
5252
List<String> tests = new ArrayList<>();
53-
InputStream catalogFileStream =
54-
TestUtil.class.getResourceAsStream(DATA_PATH + "catalog.txt");
55-
LineNumberReader catalogFile =
56-
new LineNumberReader(new InputStreamReader(catalogFileStream));
57-
String filename = null;
58-
while ((filename = catalogFile.readLine()) != null) {
59-
if (filename.startsWith("#")) { // comment line, skip without logging
60-
continue;
61-
}
62-
if (!filename.endsWith(".txt")) {
63-
logln("Skipping " + filename + "...");
64-
continue;
65-
}
66-
tests.add(filename);
53+
try (InputStream catalogFileStream =
54+
TestUtil.class.getResourceAsStream(DATA_PATH + "catalog.txt");
55+
LineNumberReader catalogFile =
56+
new LineNumberReader(
57+
new InputStreamReader(catalogFileStream, StandardCharsets.UTF_8))) {
58+
catalogFile
59+
.lines()
60+
.filter(
61+
filename ->
62+
!filename.startsWith("#")) // comment line, skip without logging
63+
.forEach(
64+
filename -> {
65+
if (!filename.endsWith(".txt")) {
66+
logln("Skipping " + filename + "...");
67+
return;
68+
}
69+
tests.add(filename);
70+
});
6771
}
6872
return tests;
6973
}
@@ -72,41 +76,45 @@ static List<String> readTestCases() throws Exception {
7276
@Parameters(method = "readTestCases")
7377
public void TestPersonNames(String filename) throws IOException {
7478
String knownIssue = KNOWN_ISSUES.get(filename);
75-
LineNumberReader in =
79+
try (LineNumberReader in =
7680
new LineNumberReader(
7781
new InputStreamReader(
7882
TestUtil.class.getResourceAsStream(DATA_PATH + filename),
79-
StandardCharsets.UTF_8));
80-
String line = null;
81-
PersonNameTester tester = new PersonNameTester(filename);
82-
83-
int errors = 0;
84-
try {
85-
while ((line = in.readLine()) != null) {
86-
tester.processLine(line, filename, in.getLineNumber());
87-
}
88-
errors = tester.getErrorCount();
89-
} catch (Exception e) {
90-
if (knownIssue != null) {
91-
logKnownIssue(knownIssue, "Exception thrown on " + filename + ": " + e.toString());
92-
} else if (RUN_ALL_TESTS) {
93-
logln("Exception thrown on " + filename + ": " + e.toString());
94-
} else {
95-
throw e;
83+
StandardCharsets.UTF_8))) {
84+
String line = null;
85+
PersonNameTester tester = new PersonNameTester(filename);
86+
87+
int errors = 0;
88+
try {
89+
while ((line = in.readLine()) != null) {
90+
tester.processLine(line, filename, in.getLineNumber());
91+
}
92+
errors = tester.getErrorCount();
93+
} catch (Exception e) {
94+
if (knownIssue != null) {
95+
logKnownIssue(
96+
knownIssue, "Exception thrown on " + filename + ": " + e.toString());
97+
} else if (RUN_ALL_TESTS) {
98+
logln("Exception thrown on " + filename + ": " + e.toString());
99+
} else {
100+
throw e;
101+
}
96102
}
97-
}
98103

99-
if (errors != 0) {
100-
if (knownIssue != null) {
101-
logKnownIssue(
102-
knownIssue, "Failure in " + filename + ": Found " + errors + " errors.");
103-
} else if (RUN_ALL_TESTS) {
104-
logln("Failure in " + filename + ": Found " + errors + " errors.");
105-
} else {
106-
errln("Failure in " + filename + ": Found " + errors + " errors.");
107-
}
108-
if (!isVerbose()) {
109-
System.out.println("Note: Use verbose ( -DICU.logging=3 ) to get verbose output");
104+
if (errors != 0) {
105+
if (knownIssue != null) {
106+
logKnownIssue(
107+
knownIssue,
108+
"Failure in " + filename + ": Found " + errors + " errors.");
109+
} else if (RUN_ALL_TESTS) {
110+
logln("Failure in " + filename + ": Found " + errors + " errors.");
111+
} else {
112+
errln("Failure in " + filename + ": Found " + errors + " errors.");
113+
}
114+
if (!isVerbose()) {
115+
System.out.println(
116+
"Note: Use verbose ( -DICU.logging=3 ) to get verbose output");
117+
}
110118
}
111119
}
112120
}

0 commit comments

Comments
 (0)