Skip to content

Commit 8cb7c5c

Browse files
authored
Use consistently the encoding expected by Javadoc for reading and writing of data (#1278)
* Revert "Fix MalformedInputException on Windows with Java 8 due to charset mis… (#1274)" This reverts commit b453602. * For reading and writing of data use the encoding expected by Javadoc For reading and writing of data, Javadoc does expect a certain encoding dependending on which version of JDK we use. So we have no choice but to actually use that one. Writing only in UTF-8, will always fail in some corner cases.
1 parent b453602 commit 8cb7c5c

File tree

3 files changed

+26
-27
lines changed

3 files changed

+26
-27
lines changed

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

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4149,14 +4149,8 @@ private void addCommandLineOptions(Commandline cmd, List<String> arguments, File
41494149
StringBuilder options = new StringBuilder();
41504150
options.append(StringUtils.join(arguments.iterator(), SystemUtils.LINE_SEPARATOR));
41514151

4152-
Charset outputFileEncoding;
4153-
if (JAVA_VERSION.isAtLeast("9") && JAVA_VERSION.isBefore("12")) {
4154-
outputFileEncoding = StandardCharsets.UTF_8;
4155-
} else {
4156-
outputFileEncoding = Charset.defaultCharset();
4157-
}
41584152
try {
4159-
Files.write(optionsFile.toPath(), Collections.singleton(options), outputFileEncoding);
4153+
Files.write(optionsFile.toPath(), Collections.singleton(options), SystemUtils.getExpectedEncoding());
41604154
} catch (IOException e) {
41614155
throw new MavenReportException(
41624156
"Unable to write '" + optionsFile.getName() + "' temporary file for command execution", e);
@@ -4194,16 +4188,8 @@ private void addCommandLineArgFile(Commandline cmd, File javadocOutputDirectory,
41944188
quotedFiles.add(JavadocUtil.quotedPathArgument(file));
41954189
}
41964190

4197-
Charset cs;
4198-
if (JavaVersion.JAVA_SPECIFICATION_VERSION.isAtLeast("9")
4199-
&& JavaVersion.JAVA_SPECIFICATION_VERSION.isBefore("12")) {
4200-
cs = StandardCharsets.UTF_8;
4201-
} else {
4202-
cs = Charset.defaultCharset();
4203-
}
4204-
42054191
try {
4206-
Files.write(argfileFile.toPath(), quotedFiles, cs);
4192+
Files.write(argfileFile.toPath(), quotedFiles, SystemUtils.getExpectedEncoding());
42074193
} catch (IOException e) {
42084194
throw new MavenReportException(
42094195
"Unable to write '" + argfileFile.getName() + "' temporary file for command execution", e);
@@ -5005,7 +4991,8 @@ private boolean isUpToDate(Commandline cmd) throws MavenReportException {
50054991
Path cacheData = staleDataPath.toPath();
50064992
List<String> prvdata;
50074993
if (Files.isRegularFile(cacheData)) {
5008-
prvdata = Files.lines(cacheData, StandardCharsets.UTF_8).collect(Collectors.toList());
4994+
prvdata = Files.lines(cacheData, SystemUtils.getExpectedEncoding())
4995+
.collect(Collectors.toList());
50094996
} else {
50104997
prvdata = null;
50114998
}

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

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020

2121
import java.io.File;
2222
import java.io.IOException;
23-
import java.nio.charset.CharacterCodingException;
24-
import java.nio.charset.Charset;
2523
import java.nio.file.DirectoryStream;
2624
import java.nio.file.Files;
2725
import java.nio.file.Path;
@@ -33,13 +31,12 @@
3331
import org.apache.maven.reporting.MavenReportException;
3432
import org.codehaus.plexus.util.cli.Commandline;
3533

36-
import static java.nio.charset.StandardCharsets.UTF_8;
37-
3834
/**
3935
* Helper class to compute and write data used to detect a
4036
* stale javadoc.
4137
*/
4238
public class StaleHelper {
39+
4340
/**
4441
* Compute the data used to detect a stale javadoc
4542
*
@@ -58,12 +55,8 @@ public static List<String> getStaleData(Commandline cmd) throws MavenReportExcep
5855
for (String arg : args) {
5956
if (arg.startsWith("@")) {
6057
String name = arg.substring(1);
58+
options.addAll(Files.readAllLines(dir.resolve(name), SystemUtils.getExpectedEncoding()));
6159
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-
}
6760
}
6861
}
6962
List<String> state = new ArrayList<>(options);
@@ -113,7 +106,7 @@ public static void writeStaleData(Commandline cmd, Path path) throws MavenReport
113106
try {
114107
List<String> curdata = getStaleData(cmd);
115108
Files.createDirectories(path.getParent());
116-
Files.write(path, curdata, UTF_8);
109+
Files.write(path, curdata, SystemUtils.getExpectedEncoding());
117110
} catch (IOException e) {
118111
throw new MavenReportException("Error checking stale data", e);
119112
}

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
package org.apache.maven.plugins.javadoc;
2020

2121
import java.io.File;
22+
import java.nio.charset.Charset;
23+
import java.nio.charset.StandardCharsets;
24+
25+
import org.codehaus.plexus.languages.java.version.JavaVersion;
2226

2327
/**
2428
* Contains several OS-specific methods from Commons-Lang3's SystemUtils. We don't want to use that class because it
@@ -146,6 +150,21 @@ public static File getJavaHome() {
146150
return new File(System.getProperty(JAVA_HOME_KEY));
147151
}
148152

153+
/**
154+
* Compute the encoding that Javadoc expects for reading and writing of data
155+
*
156+
* @return the expected encoding
157+
* @since 3.12.1
158+
*/
159+
public static Charset getExpectedEncoding() {
160+
if (JavaVersion.JAVA_SPECIFICATION_VERSION.isAtLeast("9")
161+
&& JavaVersion.JAVA_SPECIFICATION_VERSION.isBefore("12")) {
162+
return StandardCharsets.UTF_8;
163+
} else {
164+
return Charset.defaultCharset();
165+
}
166+
}
167+
149168
/**
150169
* <p>
151170
* Gets a System property, defaulting to {@code null} if the property cannot be read.

0 commit comments

Comments
 (0)