Skip to content

Commit b453602

Browse files
authored
Fix MalformedInputException on Windows with Java 8 due to charset mis… (#1274)
* Fix MalformedInputException on Windows with Java 8 due to charset mismatch in stale data cache Changed StaleHelper to always save in UTF-8 instead of platform-dependent default charset. This ensures consistency with AbstractJavadocMojo.isUpToDate() which reads the stale data file using UTF-8. Previously on Windows with Java 8: - First run: file written with Cp1252 (default charset) - Second run: file read with UTF-8, causing MalformedInputException
1 parent b913338 commit b453602

File tree

1 file changed

+9
-19
lines changed

1 file changed

+9
-19
lines changed

src/main/java/org/apache/maven/plugins/javadoc/StaleHelper.java

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020

2121
import java.io.File;
2222
import java.io.IOException;
23+
import java.nio.charset.CharacterCodingException;
2324
import java.nio.charset.Charset;
24-
import java.nio.charset.StandardCharsets;
2525
import java.nio.file.DirectoryStream;
2626
import java.nio.file.Files;
2727
import java.nio.file.Path;
@@ -31,29 +31,15 @@
3131
import java.util.List;
3232

3333
import org.apache.maven.reporting.MavenReportException;
34-
import org.codehaus.plexus.languages.java.version.JavaVersion;
3534
import org.codehaus.plexus.util.cli.Commandline;
3635

36+
import static java.nio.charset.StandardCharsets.UTF_8;
37+
3738
/**
3839
* Helper class to compute and write data used to detect a
3940
* stale javadoc.
4041
*/
4142
public class StaleHelper {
42-
43-
/**
44-
* Compute the encoding of the stale javadoc
45-
*
46-
* @return the encoding of the stale data
47-
*/
48-
private static Charset getDataCharset() {
49-
if (JavaVersion.JAVA_SPECIFICATION_VERSION.isAtLeast("9")
50-
&& JavaVersion.JAVA_SPECIFICATION_VERSION.isBefore("12")) {
51-
return StandardCharsets.UTF_8;
52-
} else {
53-
return Charset.defaultCharset();
54-
}
55-
}
56-
5743
/**
5844
* Compute the data used to detect a stale javadoc
5945
*
@@ -72,8 +58,12 @@ public static List<String> getStaleData(Commandline cmd) throws MavenReportExcep
7258
for (String arg : args) {
7359
if (arg.startsWith("@")) {
7460
String name = arg.substring(1);
75-
options.addAll(Files.readAllLines(dir.resolve(name), getDataCharset()));
7661
ignored.add(name);
62+
try {
63+
options.addAll(Files.readAllLines(dir.resolve(name), UTF_8));
64+
} catch (CharacterCodingException e) {
65+
options.addAll(Files.readAllLines(dir.resolve(name), Charset.defaultCharset()));
66+
}
7767
}
7868
}
7969
List<String> state = new ArrayList<>(options);
@@ -123,7 +113,7 @@ public static void writeStaleData(Commandline cmd, Path path) throws MavenReport
123113
try {
124114
List<String> curdata = getStaleData(cmd);
125115
Files.createDirectories(path.getParent());
126-
Files.write(path, curdata, getDataCharset());
116+
Files.write(path, curdata, UTF_8);
127117
} catch (IOException e) {
128118
throw new MavenReportException("Error checking stale data", e);
129119
}

0 commit comments

Comments
 (0)