1 package org.simantics.scl.compiler.internal.parsing.utils;
\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
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
16 UTF8_DECODER.onMalformedInput(CodingErrorAction.REPLACE);
\r
17 UTF8_DECODER.onUnmappableCharacter(CodingErrorAction.REPLACE);
\r
18 UTF8_DECODER.replaceWith("\ufffd");
\r
22 * Skips possible BOM (ef bb bf) in the beginning of the stream.
\r
24 private static BufferedInputStream skipBOM(BufferedInputStream stream) throws IOException {
\r
26 if(stream.read() == 0xef)
\r
27 if(stream.read() == 0xbb)
\r
28 if(stream.read() == 0xbf)
\r
34 public LaxUTF8Reader(BufferedInputStream stream) throws IOException {
\r
35 super(skipBOM(stream), UTF8_DECODER);
\r
38 public LaxUTF8Reader(String fileName) throws IOException {
\r
39 this(new BufferedInputStream(new FileInputStream(fileName)));
\r