Skip to content

Commit

Permalink
Rollback SourceFile changes.
Browse files Browse the repository at this point in the history
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=154601962
  • Loading branch information
tbreisacher authored and brad4d committed May 1, 2017
1 parent 778833e commit 9b1d41a
Showing 1 changed file with 15 additions and 29 deletions.
44 changes: 15 additions & 29 deletions src/com/google/javascript/jscomp/SourceFile.java
Expand Up @@ -22,6 +22,7 @@
import com.google.common.annotations.GwtIncompatible; import com.google.common.annotations.GwtIncompatible;
import com.google.common.annotations.VisibleForTesting; import com.google.common.annotations.VisibleForTesting;
import com.google.common.io.CharStreams; import com.google.common.io.CharStreams;
import com.google.common.io.Files;
import com.google.common.io.Resources; import com.google.common.io.Resources;
import com.google.javascript.rhino.StaticSourceFile; import com.google.javascript.rhino.StaticSourceFile;
import java.io.File; import java.io.File;
Expand All @@ -36,8 +37,6 @@
import java.net.URLConnection; import java.net.URLConnection;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Enumeration; import java.util.Enumeration;
Expand Down Expand Up @@ -395,23 +394,14 @@ public static SourceFile fromFile(String fileName, Charset charset) {


@GwtIncompatible("java.io.File") @GwtIncompatible("java.io.File")
public static SourceFile fromFile(String fileName) { public static SourceFile fromFile(String fileName) {
return fromFile(fileName, UTF_8); return builder().buildFromFile(fileName);
} }


@GwtIncompatible("java.io.File")
public static SourceFile fromPath(Path path, Charset c) {
return builder().withCharset(c).buildFromPath(path);
}

/** @deprecated Use {@link SourceFile#fromPath(Path, Charset)} */
@Deprecated
@GwtIncompatible("java.io.File") @GwtIncompatible("java.io.File")
public static SourceFile fromFile(File file, Charset c) { public static SourceFile fromFile(File file, Charset c) {
return builder().withCharset(c).buildFromFile(file); return builder().withCharset(c).buildFromFile(file);
} }


/** @deprecated Use {@link #fromPath(Path, Charset)} */
@Deprecated
@GwtIncompatible("java.io.File") @GwtIncompatible("java.io.File")
public static SourceFile fromFile(File file) { public static SourceFile fromFile(File file) {
return builder().buildFromFile(file); return builder().buildFromFile(file);
Expand Down Expand Up @@ -483,12 +473,7 @@ public SourceFile buildFromFile(String fileName) {


@GwtIncompatible("java.io.File") @GwtIncompatible("java.io.File")
public SourceFile buildFromFile(File file) { public SourceFile buildFromFile(File file) {
return new OnDisk(file.toPath(), originalPath, charset); return new OnDisk(file, originalPath, charset);
}

@GwtIncompatible("java.io.File")
public SourceFile buildFromPath(Path path) {
return new OnDisk(path, originalPath, charset);
} }


@GwtIncompatible("java.net.URL") @GwtIncompatible("java.net.URL")
Expand Down Expand Up @@ -577,17 +562,17 @@ public void clearCachedSource() {
@GwtIncompatible("java.io.File") @GwtIncompatible("java.io.File")
static class OnDisk extends SourceFile { static class OnDisk extends SourceFile {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
private final Path path; private final File file;


// This is stored as a String, but passed in and out as a Charset so that // This is stored as a String, but passed in and out as a Charset so that
// we can serialize the class. // we can serialize the class.
// Default input file format for the compiler has always been UTF_8. // Default input file format for the compiler has always been UTF_8.
private Charset inputCharset = UTF_8; private String inputCharset = UTF_8.name();


OnDisk(Path path, String originalPath, Charset c) { OnDisk(File file, String originalPath, Charset c) {
super(path.toString()); super(file.getPath());
this.path = path; this.file = file;
setOriginalPath(originalPath); super.setOriginalPath(originalPath);
if (c != null) { if (c != null) {
this.setCharset(c); this.setCharset(c);
} }
Expand All @@ -598,8 +583,8 @@ public synchronized String getCode() throws IOException {
String cachedCode = super.getCode(); String cachedCode = super.getCode();


if (cachedCode == null) { if (cachedCode == null) {
cachedCode = CharStreams.toString(getCodeReader()); cachedCode = Files.toString(file, this.getCharset());
super.setCode(cachedCode, Objects.equals(this.getCharset(), inputCharset)); super.setCode(cachedCode, Objects.equals(this.getCharset(), StandardCharsets.UTF_8));
// Byte Order Mark can be removed by setCode // Byte Order Mark can be removed by setCode
cachedCode = super.getCode(); cachedCode = super.getCode();
} }
Expand All @@ -610,12 +595,13 @@ public synchronized String getCode() throws IOException {
* Gets a reader for the code in this source file. * Gets a reader for the code in this source file.
*/ */
@Override @Override
@GwtIncompatible("java.io.Reader")
public Reader getCodeReader() throws IOException { public Reader getCodeReader() throws IOException {
if (hasSourceInMemory()) { if (hasSourceInMemory()) {
return super.getCodeReader(); return super.getCodeReader();
} else { } else {
// If we haven't pulled the code into memory yet, don't. // If we haven't pulled the code into memory yet, don't.
return Files.newBufferedReader(path, inputCharset); return Files.newReader(file, StandardCharsets.UTF_8);
} }
} }


Expand All @@ -633,7 +619,7 @@ public void clearCachedSource() {
* @param c charset to use when reading the input. * @param c charset to use when reading the input.
*/ */
public void setCharset(Charset c) { public void setCharset(Charset c) {
inputCharset = c; inputCharset = c.name();
} }


/** /**
Expand All @@ -643,7 +629,7 @@ public void setCharset(Charset c) {
* @return Charset object representing charset to use. * @return Charset object representing charset to use.
*/ */
public Charset getCharset() { public Charset getCharset() {
return inputCharset; return Charset.forName(inputCharset);
} }
} }


Expand Down

0 comments on commit 9b1d41a

Please sign in to comment.