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