]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/parsing/utils/LaxUTF8Reader.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / internal / parsing / utils / LaxUTF8Reader.java
1 package org.simantics.scl.compiler.internal.parsing.utils;\r
2 \r
3 import java.io.BufferedInputStream;\r
4 import java.io.FileInputStream;\r
5 import java.io.IOException;\r
6 import java.io.InputStreamReader;\r
7 import java.nio.charset.Charset;\r
8 import java.nio.charset.CharsetDecoder;\r
9 import java.nio.charset.CodingErrorAction;\r
10 \r
11 public class LaxUTF8Reader extends InputStreamReader {\r
12     private static final Charset UTF8 = Charset.forName("UTF-8");\r
13     private static final CharsetDecoder UTF8_DECODER = UTF8.newDecoder();\r
14     \r
15     static {\r
16         UTF8_DECODER.onMalformedInput(CodingErrorAction.REPLACE);\r
17         UTF8_DECODER.onUnmappableCharacter(CodingErrorAction.REPLACE);\r
18         UTF8_DECODER.replaceWith("\ufffd");\r
19     }\r
20     \r
21     /**\r
22      * Skips possible BOM (ef bb bf) in the beginning of the stream.\r
23      */\r
24     private static BufferedInputStream skipBOM(BufferedInputStream stream) throws IOException {\r
25         stream.mark(4);\r
26         if(stream.read() == 0xef)\r
27             if(stream.read() == 0xbb)\r
28                 if(stream.read() == 0xbf)\r
29                     return stream;\r
30         stream.reset();\r
31         return stream;\r
32     }\r
33 \r
34     public LaxUTF8Reader(BufferedInputStream stream) throws IOException {\r
35         super(skipBOM(stream), UTF8_DECODER);\r
36     }\r
37     \r
38     public LaxUTF8Reader(String fileName) throws IOException {\r
39         this(new BufferedInputStream(new FileInputStream(fileName)));\r
40     }\r
41 }\r