From 75037576b6bb97fa970ace22d72ddc3d5d0d61bb Mon Sep 17 00:00:00 2001 From: Florian Weikert Date: Sat, 1 Aug 2015 20:03:46 +0000 Subject: TemplateExpansionAction now consistently uses UTF-8 instead of mixing UTF-8 with Latin-1 -- MOS_MIGRATED_REVID=99651466 --- .../build/lib/analysis/actions/TemplateExpansionAction.java | 10 ++++++---- .../com/google/devtools/build/lib/vfs/FileSystemUtils.java | 7 +++++++ 2 files changed, 13 insertions(+), 4 deletions(-) (limited to 'src/main/java/com/google') diff --git a/src/main/java/com/google/devtools/build/lib/analysis/actions/TemplateExpansionAction.java b/src/main/java/com/google/devtools/build/lib/analysis/actions/TemplateExpansionAction.java index 0a806825c5..30ff2fb453 100644 --- a/src/main/java/com/google/devtools/build/lib/analysis/actions/TemplateExpansionAction.java +++ b/src/main/java/com/google/devtools/build/lib/analysis/actions/TemplateExpansionAction.java @@ -14,8 +14,6 @@ package com.google.devtools.build.lib.analysis.actions; -import static java.nio.charset.StandardCharsets.UTF_8; - import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Joiner; import com.google.common.base.Objects; @@ -33,6 +31,8 @@ import com.google.devtools.build.lib.vfs.Path; import java.io.IOException; import java.io.OutputStream; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.util.Collection; import java.util.List; @@ -145,6 +145,8 @@ public class TemplateExpansionAction extends AbstractFileWriteAction { */ public abstract static class Template { + private static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8; + /** * We only allow subclasses in this file. */ @@ -218,7 +220,7 @@ public class TemplateExpansionAction extends AbstractFileWriteAction { protected String getContent() throws IOException { Path templatePath = templateArtifact.getPath(); try { - return new String(FileSystemUtils.readContentAsLatin1(templatePath)); + return FileSystemUtils.readContent(templatePath, DEFAULT_CHARSET); } catch (IOException e) { throw new IOException("failed to load template file '" + templatePath.getPathString() + "' due to I/O error: " + e.getMessage(), e); @@ -320,7 +322,7 @@ public class TemplateExpansionAction extends AbstractFileWriteAction { @Override public DeterministicWriter newDeterministicWriter(EventHandler eventHandler, Executor executor) throws IOException { - final byte[] bytes = getFileContents().getBytes(UTF_8); + final byte[] bytes = getFileContents().getBytes(Template.DEFAULT_CHARSET); return new DeterministicWriter() { @Override public void writeOutputFile(OutputStream out) throws IOException { diff --git a/src/main/java/com/google/devtools/build/lib/vfs/FileSystemUtils.java b/src/main/java/com/google/devtools/build/lib/vfs/FileSystemUtils.java index 6ce67aeac2..840433a11e 100644 --- a/src/main/java/com/google/devtools/build/lib/vfs/FileSystemUtils.java +++ b/src/main/java/com/google/devtools/build/lib/vfs/FileSystemUtils.java @@ -935,6 +935,13 @@ public class FileSystemUtils { return asByteSource(inputFile).read(); } + /** + * Reads the entire file using the given charset and returns the contents as a string + */ + public static String readContent(Path inputFile, Charset charset) throws IOException { + return asByteSource(inputFile).asCharSource(charset).read(); + } + /** * Reads at most {@code limit} bytes from {@code inputFile} and returns it as a byte array. * -- cgit v1.2.3