Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[JENKINS-49112] Revert use of Files.newBufferedReader() in Util.loadF…
…ile() (#3259)

* [JENKINS-49112] Revert use of Files.newBufferedReader() in Util.loadFile()

This partially reverts 6707683.

* Restore the use of StringBuilder and reading into buf
  • Loading branch information
dtrebbien authored and oleg-nenashev committed Jan 26, 2018
1 parent f9b3b4d commit e798f64
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion core/src/main/java/hudson/Util.java
Expand Up @@ -193,11 +193,26 @@ public static String loadFile(@Nonnull File logfile, @Nonnull Charset charset) t

StringBuilder str = new StringBuilder((int)logfile.length());

try (BufferedReader r = Files.newBufferedReader(fileToPath(logfile), charset)) {
// We're not using Files.newBufferedReader() here because there is a
// difference in how an InputStreamReader constructed from a Charset and
// the reader returned by Files.newBufferedReader() handle malformed and
// unmappable byte sequences for the specified encoding; the latter is
// more picky and will throw a CharacterCodingException. See:
// https://issues.jenkins-ci.org/browse/JENKINS-49060?focusedCommentId=325989&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-325989
//
// As reported at https://issues.jenkins-ci.org/browse/JENKINS-49112
// Run.getLog() calls loadFile() to fully read the generated log file.
// Until charset handling is resolved (e.g. by implementing
// https://issues.jenkins-ci.org/browse/JENKINS-48923 ), malformed
// bytes will need to be tolerated.
try (InputStream rawIn = Files.newInputStream(fileToPath(logfile));
Reader r = new BufferedReader(new InputStreamReader(rawIn, charset))) {
char[] buf = new char[1024];
int len;
while ((len = r.read(buf, 0, buf.length)) > 0)
str.append(buf, 0, len);
} catch (Exception e) {
throw new IOException("Failed to fully read " + logfile + " using charset " + charset.name(), e);
}

return str.toString();
Expand Down

0 comments on commit e798f64

Please sign in to comment.