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