Chromium Code Reviews| 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); |
| + } |
| + } |
| } |