Search in sources :

Example 1 with BOMInputStream

use of org.apache.commons.io.input.BOMInputStream in project languagetool by languagetool-org.

the class Main method getInputStreamReader.

private InputStreamReader getInputStreamReader(String filename, String encoding) throws IOException {
    String charsetName = encoding != null ? encoding : Charset.defaultCharset().name();
    InputStream is = System.in;
    if (!isStdIn(filename)) {
        is = new FileInputStream(new File(filename));
        BOMInputStream bomIn = new BOMInputStream(is, true, ByteOrderMark.UTF_8, ByteOrderMark.UTF_16BE, ByteOrderMark.UTF_16LE, ByteOrderMark.UTF_32BE, ByteOrderMark.UTF_32LE);
        if (bomIn.hasBOM() && encoding == null) {
            charsetName = bomIn.getBOMCharsetName();
        }
        is = bomIn;
    }
    return new InputStreamReader(new BufferedInputStream(is), charsetName);
}
Also used : BOMInputStream(org.apache.commons.io.input.BOMInputStream) InputStreamReader(java.io.InputStreamReader) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) BOMInputStream(org.apache.commons.io.input.BOMInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) StringTools.readerToString(org.languagetool.tools.StringTools.readerToString) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 2 with BOMInputStream

use of org.apache.commons.io.input.BOMInputStream in project languagetool by languagetool-org.

the class Main method loadFile.

private void loadFile(File file) {
    try (FileInputStream inputStream = new FileInputStream(file)) {
        BOMInputStream bomIn = new BOMInputStream(inputStream, false, ByteOrderMark.UTF_8, ByteOrderMark.UTF_16BE, ByteOrderMark.UTF_16LE, ByteOrderMark.UTF_32BE, ByteOrderMark.UTF_32LE);
        String charsetName;
        if (bomIn.hasBOM() == false) {
            // No BOM found
            bom = null;
            charsetName = null;
        } else {
            bom = bomIn.getBOM();
            charsetName = bom.getCharsetName();
        }
        String fileContents = StringTools.readStream(bomIn, charsetName);
        textArea.setText(fileContents);
        currentFile = file;
        updateTitle();
        if (recentFiles.contains(file.getAbsolutePath())) {
            recentFiles.remove(file.getAbsolutePath());
        }
        recentFiles.add(file.getAbsolutePath());
        localStorage.saveProperty("recentFiles", recentFiles);
        updateRecentFilesMenu();
    } catch (IOException e) {
        Tools.showError(e);
    }
}
Also used : BOMInputStream(org.apache.commons.io.input.BOMInputStream)

Example 3 with BOMInputStream

use of org.apache.commons.io.input.BOMInputStream in project es6draft by anba.

the class ChakraTest method bomReader.

private static BufferedReader bomReader(InputStream is) throws IOException {
    BOMInputStream bis = new BOMInputStream(is);
    Charset cs = charsetFor(bis, StandardCharsets.UTF_8);
    return new BufferedReader(new InputStreamReader(bis, cs));
}
Also used : BOMInputStream(org.apache.commons.io.input.BOMInputStream) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) Charset(java.nio.charset.Charset)

Example 4 with BOMInputStream

use of org.apache.commons.io.input.BOMInputStream in project es6draft by anba.

the class Resources method readExcludeXML.

/**
     * Load the exclusion xml-list for invalid test cases from {@link InputStream}
     */
private static Set<String> readExcludeXML(InputStream is) throws IOException {
    Set<String> exclude = new HashSet<>();
    Reader reader = new InputStreamReader(new BOMInputStream(is), StandardCharsets.UTF_8);
    NodeList ns = xml(reader).getDocumentElement().getElementsByTagName("test");
    for (int i = 0, len = ns.getLength(); i < len; ++i) {
        exclude.add(((Element) ns.item(i)).getAttribute("id"));
    }
    return exclude;
}
Also used : BOMInputStream(org.apache.commons.io.input.BOMInputStream) InputStreamReader(java.io.InputStreamReader) NodeList(org.w3c.dom.NodeList) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader)

Example 5 with BOMInputStream

use of org.apache.commons.io.input.BOMInputStream in project disconf by knightliao.

the class MyStringUtils method multipartFileToString.

public static String multipartFileToString(MultipartFile file) throws IOException {
    BOMInputStream bomInputStream = new BOMInputStream(file.getInputStream());
    ByteOrderMark bom = bomInputStream.getBOM();
    String charsetName = bom == null ? DEFAULT_ENCODING : bom.getCharsetName();
    return inputStreamToString(bomInputStream, charsetName);
}
Also used : BOMInputStream(org.apache.commons.io.input.BOMInputStream) ByteOrderMark(org.apache.commons.io.ByteOrderMark)

Aggregations

BOMInputStream (org.apache.commons.io.input.BOMInputStream)6 InputStreamReader (java.io.InputStreamReader)4 BufferedInputStream (java.io.BufferedInputStream)2 BufferedReader (java.io.BufferedReader)2 FileInputStream (java.io.FileInputStream)2 ByteOrderMark (org.apache.commons.io.ByteOrderMark)2 File (java.io.File)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Reader (java.io.Reader)1 Charset (java.nio.charset.Charset)1 StringTools.readerToString (org.languagetool.tools.StringTools.readerToString)1 NodeList (org.w3c.dom.NodeList)1