Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(868)

Unified Diff: editor/tools/plugins/com.google.dart.engine/src/com/google/dart/engine/source/FileBasedSource.java

Issue 270573007: Issue 16314. Skip leading UTF-8 BOM. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | tests/language/language_analyzer.status » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: editor/tools/plugins/com.google.dart.engine/src/com/google/dart/engine/source/FileBasedSource.java
diff --git a/editor/tools/plugins/com.google.dart.engine/src/com/google/dart/engine/source/FileBasedSource.java b/editor/tools/plugins/com.google.dart.engine/src/com/google/dart/engine/source/FileBasedSource.java
index 1e1af482743980b8b80ec2ebe3ca738839260b62..92d271ccc39ef7f5a5951c2a6fbf05a4b8a8e36f 100644
--- a/editor/tools/plugins/com.google.dart.engine/src/com/google/dart/engine/source/FileBasedSource.java
+++ b/editor/tools/plugins/com.google.dart.engine/src/com/google/dart/engine/source/FileBasedSource.java
@@ -223,6 +223,7 @@ public class FileBasedSource implements Source {
}
if (byteBuffer != null) {
byteBuffer.rewind();
+ skipOptionalBOM(byteBuffer);
return new TimestampedData<CharSequence>(modificationTime, UTF_8_CHARSET.decode(byteBuffer));
}
} catch (IOException exception) {
@@ -235,7 +236,7 @@ public class FileBasedSource implements Source {
InputStreamReader reader = null;
String contents;
try {
- reader = new InputStreamReader(new FileInputStream(file), "UTF-8");
+ reader = new InputStreamReader(getFileInputStreamWithoutBOM(), "UTF-8");
contents = FileUtilities.getContents(reader);
} finally {
if (reader != null) {
@@ -286,6 +287,7 @@ public class FileBasedSource implements Source {
}
if (byteBuffer != null) {
byteBuffer.rewind();
+ skipOptionalBOM(byteBuffer);
receiver.accept(UTF_8_CHARSET.decode(byteBuffer), modificationTime);
return;
}
@@ -299,7 +301,7 @@ public class FileBasedSource implements Source {
InputStreamReader reader = null;
String contents;
try {
- reader = new InputStreamReader(new FileInputStream(file), "UTF-8");
+ reader = new InputStreamReader(getFileInputStreamWithoutBOM(), "UTF-8");
contents = FileUtilities.getContents(reader);
} finally {
if (reader != null) {
@@ -314,6 +316,21 @@ public class FileBasedSource implements Source {
}
/**
+ * Returns a {@link FileInputStream} for the {@link #file} with skipped optional leading UTF-8
+ * BOM.
+ */
+ private FileInputStream getFileInputStreamWithoutBOM() throws Exception {
+ FileInputStream in = new FileInputStream(file);
+ // check if there is an UTF-8 BOM
+ if (in.read() == (byte) 0xEF && in.read() == (byte) 0xBB && in.read() == (byte) 0xBF) {
+ return in;
+ }
+ // re-open scream
Brian Wilkerson 2014/05/07 20:26:57 "scream" --> "stream"
+ in.close();
+ return new FileInputStream(file);
+ }
+
+ /**
* Record the time the IO took if it was slow
*/
private void reportIfSlowIO(long nanos) {
@@ -328,4 +345,14 @@ public class FileBasedSource implements Source {
}
}
}
+
+ /**
+ * Skips an optional UTF-8 BOM.
+ */
+ private void skipOptionalBOM(ByteBuffer byteBuffer) {
+ if (byteBuffer.remaining() >= 3 && byteBuffer.get(0) == (byte) 0xEF
+ && byteBuffer.get(1) == (byte) 0xBB && byteBuffer.get(2) == (byte) 0xBF) {
+ byteBuffer.position(3);
+ }
+ }
}
« no previous file with comments | « no previous file | tests/language/language_analyzer.status » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698