Example usage for org.apache.commons.io ByteOrderMark UTF_8

List of usage examples for org.apache.commons.io ByteOrderMark UTF_8

Introduction

In this page you can find the example usage for org.apache.commons.io ByteOrderMark UTF_8.

Prototype

ByteOrderMark UTF_8

To view the source code for org.apache.commons.io ByteOrderMark UTF_8.

Click Source Link

Document

UTF-8 BOM

Usage

From source file:at.jku.ce.adaptivetesting.vaadin.ui.topic.accounting.AccountingQuestionManager.java

public int loadQuestions(File containingFolder) throws JAXBException, IOException {
    assert containingFolder.exists() && containingFolder.isDirectory();
    JAXBContext accountingJAXB = JAXBContext.newInstance(XmlAccountingQuestion.class,
            AccountingDataStorage.class);
    JAXBContext profitJAXB = JAXBContext.newInstance(XmlProfitQuestion.class, ProfitDataStorage.class);

    Unmarshaller accountingUnmarshaller = accountingJAXB.createUnmarshaller();
    Unmarshaller profitUnmarshaller = profitJAXB.createUnmarshaller();

    final List<AccountingQuestion> accountingList = new ArrayList<>();
    final List<ProfitQuestion> profitList = new ArrayList<>();

    String accountingRootElement = XmlAccountingQuestion.class.getAnnotation(XmlRootElement.class).name();
    String profitRootElement = XmlProfitQuestion.class.getAnnotation(XmlRootElement.class).name();

    File[] questions = containingFolder
            .listFiles(f -> f.isFile() && (f.canRead() || f.setReadable(true)) && f.getName().endsWith(".xml"));

    // read all questions
    LogHelper.logInfo("Found " + questions.length + " potential questions");
    int successfullyLoaded = 0;
    for (File f : questions) {
        LogHelper.logInfo("Loading question with filename: " + f.getName());
        BufferedReader reader = null;
        StringBuilder sb = new StringBuilder();
        try {/* w  w w  .  jav  a 2 s .  c o  m*/
            reader = new BufferedReader(new InputStreamReader(
                    new BOMInputStream(new FileInputStream(f), ByteOrderMark.UTF_8), "UTF8"));

            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
        } finally {
            if (reader != null) {
                reader.close();
            }
        }
        String fileAsString = sb.toString().replaceAll("& ", "&amp; ");
        if (fileAsString.contains(profitRootElement)) {
            LogHelper.logInfo("Question detected as " + ProfitQuestion.class.getName());
            // Profit Question
            XmlProfitQuestion question = (XmlProfitQuestion) profitUnmarshaller
                    .unmarshal(new StringReader(fileAsString));
            profitList.add(AccountingXmlHelper.fromXml(question, f.getName()));
            successfullyLoaded++;
        } else if (fileAsString.contains(accountingRootElement)) {
            LogHelper.logInfo("Question detected as " + AccountingQuestion.class.getName());
            // Accounting Question
            XmlAccountingQuestion question = (XmlAccountingQuestion) accountingUnmarshaller
                    .unmarshal(new StringReader(fileAsString));
            accountingList.add(AccountingXmlHelper.fromXml(question, f.getName()));
            successfullyLoaded++;
        } else {
            LogHelper.logInfo(
                    "QuestionManager: item type not supported for " + f.getName() + ", ignoring file.");
            //            throw new IllegalArgumentException(
            //                  "Question type not supported. File: " + f);
            continue;
        }
        LogHelper.logInfo("Loaded question with filename:" + f.getName());
    }
    // Add question to the question manager
    accountingList.forEach(q -> addQuestion(q));
    profitList.forEach(q -> addQuestion(q));
    LogHelper.logInfo("Successfully loaded " + successfullyLoaded + " questions.");
    return questions.length;
}

From source file:com.gargoylesoftware.htmlunit.xml.XmlPageTest.java

/**
 * @throws Exception if the test fails/*  w ww.  ja  va2 s  .co m*/
 */
@Test
public void bom() throws Exception {
    asText(new String(ByteOrderMark.UTF_8.getBytes()) + "<msg>abc</msg>", "abc");
}

From source file:net.sourceforge.subsonic.controller.CaptionsController.java

private void send(File captionsFile, HttpServletResponse response, String format) throws IOException {
    if (CAPTION_FORMAT_VTT.equals(format)) {
        Files.copy(captionsFile, response.getOutputStream());
    } else {// w  ww .  j  a  v a 2 s. co  m

        BOMInputStream bomInputStream = null;
        Reader reader = null;
        try {
            bomInputStream = new BOMInputStream(new FileInputStream(captionsFile));
            String encoding = ByteOrderMark.UTF_8.equals(bomInputStream.getBOM()) ? StringUtil.ENCODING_UTF8
                    : StringUtil.ENCODING_LATIN;

            reader = new InputStreamReader(bomInputStream, encoding);
            IOUtils.copy(reader, response.getOutputStream(), StringUtil.ENCODING_UTF8);
        } finally {
            IOUtils.closeQuietly(bomInputStream);
            IOUtils.closeQuietly(reader);
        }
    }
}

From source file:net.sourceforge.subsonic.controller.CaptionsController.java

private void convertAndSend(File captionsFile, HttpServletResponse response) throws IOException {
    BOMInputStream bomInputStream = null;
    Reader reader = null;//from   w  w w  . jav a  2  s .  co  m
    try {
        bomInputStream = new BOMInputStream(new FileInputStream(captionsFile));
        String encoding = ByteOrderMark.UTF_8.equals(bomInputStream.getBOM()) ? StringUtil.ENCODING_UTF8
                : StringUtil.ENCODING_LATIN;

        reader = new InputStreamReader(bomInputStream, encoding);
        Writer writer = new OutputStreamWriter(response.getOutputStream(), StringUtil.ENCODING_UTF8);
        SrtToVtt.convert(reader, writer);
    } finally {
        IOUtils.closeQuietly(bomInputStream);
        IOUtils.closeQuietly(reader);
    }
}

From source file:org.apache.any23.util.StreamUtils.java

public static Document inputStreamToDocument(InputStream is) throws MalformedByteSequenceException {
    DocumentBuilderFactory factory = null;
    DocumentBuilder builder = null;
    Document doc = null;//w w  w .ja  v  a2  s  . c  om

    try {
        factory = DocumentBuilderFactory.newInstance();
        builder = factory.newDocumentBuilder();
    } catch (ParserConfigurationException e) {
        logger.error("Error converting InputStream to Document: {}", e);
    }

    try {
        BOMInputStream bomIn = new BOMInputStream(is, ByteOrderMark.UTF_8, ByteOrderMark.UTF_16BE,
                ByteOrderMark.UTF_16LE, ByteOrderMark.UTF_32BE, ByteOrderMark.UTF_32LE);
        if (bomIn.hasBOM()) {
            @SuppressWarnings("unused")
            int firstNonBOMByte = bomIn.read(); // Skips BOM
        }
        doc = builder.parse(bomIn);
    } catch (SAXException | IOException e) {
        logger.error("Error converting InputStream to Document: {}", e);
    }
    return doc;
}

From source file:org.hillview.storage.TextFileLoader.java

Reader getFileReader() {
    try {/*  ww w  .j av  a2 s . co m*/
        HillviewLogger.instance.info("Reading file", "{0}", this.filename);
        this.inputStream = new FileInputStream(this.filename);
        this.bufferedInputStream = new BufferedInputStream(inputStream);
        // The buffered input stream is needed by the CompressorStream
        // to detect the compression method at runtime.
        InputStream fis = this.bufferedInputStream;

        if (Utilities.isCompressed(this.filename)) {
            this.compressedStream = new CompressorStreamFactory().createCompressorInputStream(fis);
            fis = this.compressedStream;
        }
        this.bomStream = new BOMInputStream(fis, ByteOrderMark.UTF_8, ByteOrderMark.UTF_16LE,
                ByteOrderMark.UTF_16BE, ByteOrderMark.UTF_32LE, ByteOrderMark.UTF_32BE);
        ByteOrderMark bom = this.bomStream.getBOM();
        String charsetName = bom == null ? "UTF-8" : bom.getCharsetName();
        return new InputStreamReader(this.bomStream, charsetName);
    } catch (IOException | CompressorException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.languagetool.commandline.Main.java

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();
        }//from   w  ww  . j a va2 s  .c  o  m
        is = bomIn;
    }
    return new InputStreamReader(new BufferedInputStream(is), charsetName);
}

From source file:org.languagetool.gui.Main.java

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;/*from w  ww  . j a  va 2  s  . c o  m*/
        if (bomIn.hasBOM()) {
            bom = bomIn.getBOM();
            charsetName = bom.getCharsetName();
        } else {
            // No BOM found
            bom = null;
            charsetName = null;
        }
        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);
    }
}

From source file:org.omegat.util.TMXReaderTest.java

public void testCharset() throws Exception {
    File xml = new File("build/testdata/test.xml");
    xml.getParentFile().mkdirs();/*from w w w  .  j  ava 2  s  . c  o m*/

    testXml(xml, ByteOrderMark.UTF_8, "<?xml version=\"1.0\"?>", "UTF-8");
    testXml(xml, ByteOrderMark.UTF_16LE, "<?xml version=\"1.0\"?>", "UTF-16LE");
    testXml(xml, ByteOrderMark.UTF_16BE, "<?xml version=\"1.0\"?>", "UTF-16BE");
    testXml(xml, ByteOrderMark.UTF_32LE, "<?xml version=\"1.0\"?>", "UTF-32LE");
    testXml(xml, ByteOrderMark.UTF_32BE, "<?xml version=\"1.0\"?>", "UTF-32BE");
    testXml(xml, null, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>", "UTF-8");
    testXml(xml, null, "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>", "ISO-8859-1");
}

From source file:org.openlmis.fulfillment.Resource2Db.java

Pair<List<String>, List<Object[]>> resourceCsvToBatchedPair(final Resource resource) throws IOException {
    XLOGGER.entry(resource.getDescription());

    // parse CSV//  ww w .j  a  v a2s.  co  m
    try (InputStreamReader isReader = new InputStreamReader(
            new BOMInputStream(resource.getInputStream(), ByteOrderMark.UTF_8))) {
        CSVParser parser = CSVFormat.DEFAULT.withHeader().withNullString("").parse(isReader);

        // read header row
        MutablePair<List<String>, List<Object[]>> readData = new MutablePair<>();
        readData.setLeft(new ArrayList<>(parser.getHeaderMap().keySet()));
        XLOGGER.info("Read header: " + readData.getLeft());

        // read data rows
        List<Object[]> rows = new ArrayList<>();
        for (CSVRecord record : parser.getRecords()) {
            if (!record.isConsistent()) {
                throw new IllegalArgumentException("CSV record inconsistent: " + record);
            }

            List theRow = IteratorUtils.toList(record.iterator());
            rows.add(theRow.toArray());
        }
        readData.setRight(rows);

        XLOGGER.exit("Records read: " + readData.getRight().size());
        return readData;
    }
}