Skip to content

Commit

Permalink
Add SourceFile.fromPath which uses the modern java.nio.files.Path ins…
Browse files Browse the repository at this point in the history
…tead of SourceFile.fromFile which uses the old java.io.File which does not support alternate file systems.

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

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

@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")
public static SourceFile fromFile(File file, Charset c) {
return builder().withCharset(c).buildFromFile(file);
}

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

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

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

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

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

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

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

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

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

Expand Down

0 comments on commit 152a148

Please sign in to comment.