Example usage for org.apache.commons.io.input BOMInputStream getBOM

List of usage examples for org.apache.commons.io.input BOMInputStream getBOM

Introduction

In this page you can find the example usage for org.apache.commons.io.input BOMInputStream getBOM.

Prototype

public ByteOrderMark getBOM() throws IOException 

Source Link

Document

Return the BOM (Byte Order Mark).

Usage

From source file:com.github.anba.es6draft.chakra.ChakraTest.java

private static Charset charsetFor(BOMInputStream bis, Charset defaultCharset) throws IOException {
    ByteOrderMark bom = bis.getBOM();
    if (ByteOrderMark.UTF_8.equals(bom)) {
        return StandardCharsets.UTF_8;
    }/*from  w w w  .j  a v  a 2  s  . c o m*/
    if (ByteOrderMark.UTF_16LE.equals(bom)) {
        return StandardCharsets.UTF_16LE;
    }
    if (ByteOrderMark.UTF_16BE.equals(bom)) {
        return StandardCharsets.UTF_16BE;
    }
    return defaultCharset;
}

From source file:com.ibm.wala.cast.ipa.callgraph.CAstCallGraphUtil.java

public static SourceFileModule makeSourceModule(URL script, String scriptName) {
    String hackedName = script.getFile().replaceAll("%5c", "/").replaceAll("%20", " ");

    File scriptFile = new File(hackedName);

    assert hackedName.endsWith(scriptName) : scriptName + " does not match file " + script.getFile();

    return new SourceFileModule(scriptFile, scriptName, null) {
        @Override//from  w  w  w. jav  a  2 s. co  m
        public InputStream getInputStream() {
            BOMInputStream bs = new BOMInputStream(super.getInputStream(), false, ByteOrderMark.UTF_8,
                    ByteOrderMark.UTF_16LE, ByteOrderMark.UTF_16BE, ByteOrderMark.UTF_32LE,
                    ByteOrderMark.UTF_32BE);
            try {
                if (bs.hasBOM()) {
                    System.err.println("removing BOM " + bs.getBOM());
                }
                return bs;
            } catch (IOException e) {
                return super.getInputStream();
            }
        }
    };
}

From source file:ca.nines.ise.dom.DOMStream.java

/**
 * Construct a DOMStream from an input stream and record the source of the
 * input data./*w  ww  . jav  a 2 s. c  om*/
 *
 * @param in
 * @param source
 * @throws java.io.IOException
 */
public DOMStream(InputStream in, String source) throws IOException {
    lines = new ArrayList<>();
    boolean warnedSmartQuotes = false;

    BOMInputStream bomStream = new BOMInputStream(in, ByteOrderMark.UTF_8, ByteOrderMark.UTF_32LE,
            ByteOrderMark.UTF_32BE, ByteOrderMark.UTF_16LE, ByteOrderMark.UTF_16BE);
    bom = bomStream.getBOM();
    if (bom != null) {
        Message m = Message.builder("builder.bom").setSource(source)
                .addNote("The byte order mark was " + bom.getCharsetName()).build();
        Log.addMessage(m);
        encoding = bom.getCharsetName();
    } else {
        encoding = "UTF-8";
    }

    if (!encoding.equals("UTF-8")) {
        Message m = Message.builder("builder.notutf8").setSource(source)
                .addNote("The incorrect encoding is " + encoding).build();
        Log.addMessage(m);
    }

    BufferedReader buffer = new BufferedReader(new InputStreamReader(bomStream, encoding));
    String line;
    StringBuilder sb = new StringBuilder();

    Pattern p = Pattern.compile("\u201C|\u201D");

    while ((line = buffer.readLine()) != null) {
        line = Normalizer.normalize(line, Form.NFKC);
        Matcher m = p.matcher(line);
        if (m.find()) {
            line = m.replaceAll("\"");
            if (!warnedSmartQuotes) {
                warnedSmartQuotes = true;
                Message msg = Message.builder("builder.smartquotes").setSource(source)
                        .addNote("The first occurence of smart quotes was at line " + lines.size()).build();
                Log.addMessage(msg);
            }
        }
        lines.add(line);
        sb.append(line).append("\n");
    }

    content = sb.toString().trim();
}

From source file:de.iteratec.iteraplan.businesslogic.exchange.nettoExport.NettoCSVTransformerTest.java

@Test
public void testCSVTransformerForOverviewPage() {

    NettoTransformer csvTransformer = NettoCSVTransformer.newInstance(createSimpleOverviewPageTableStructure());

    assertNotNull("Can't create netto transformer for overview page table structure for csv", csvTransformer);

    //Create output stream
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    csvTransformer.transform(sourceList, out, TypeOfBuildingBlock.INFORMATIONSYSTEMRELEASE);

    //assert that output stream
    InputStream in = new ByteArrayInputStream(out.toByteArray());
    CSVReader reader;/*w w w .j av a2  s.  com*/
    try {

        //BOMInputStream is necessary, because of leading BOM in Stream
        //Otherwise assertion would fail
        BOMInputStream bOMInputStream = new BOMInputStream(in);
        ByteOrderMark bom = bOMInputStream.getBOM();
        String charsetName = bom == null ? "UTF-8" : bom.getCharsetName();

        reader = new CSVReader(new InputStreamReader(new BufferedInputStream(bOMInputStream), charsetName),
                ';');

        List<String[]> allLines = reader.readAll();

        int index = 0;
        SimpleDateFormat df = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.US);

        for (String[] line : allLines) {

            String name = line[0];
            String desc = line[1];
            String start = line[2];
            String end = line[3];
            String status = line[4];

            if (index == 0) {
                //assert the headers of CSV file
                assertEquals("Name und Version", name.trim());
                assertEquals("Beschreibung", desc);
                assertEquals("von", start);
                assertEquals("bis", end);
                assertEquals("Status", status);
            } else {
                assertEquals(isArray[index - 1].getName(), name);
                assertEquals(isArray[index - 1].getDescription(), desc);
                assertEquals(df.format(isArray[index - 1].getRuntimePeriod().getStart()), start);
                assertEquals(df.format(isArray[index - 1].getRuntimePeriod().getEnd()), end);
                assertEquals(isArray[index - 1].getTypeOfStatus().toString(), status);
            }

            index++;

        }

        reader.close();

    } catch (IOException e) {
        e.printStackTrace();
        fail("Fail due to IO Exception");
    }

}

From source file:de.iteratec.iteraplan.businesslogic.exchange.nettoExport.NettoCSVTransformerTest.java

@Test
/**/*from w ww .j ava2  s. c  o  m*/
 * Testcase creates 4 Informationsystemreleases with Text, Date and Numeric Attributes, which are transformed in CSV
 * The CSV output stream will be parsed again as CSV and the values are asserted
 */
public void testCSVTransformerForSpreadsheetReport() {

    NettoTransformer csvTransformer = NettoCSVTransformer
            .newInstance(createSimpleSpreadsheetReportTableStructure());

    assertNotNull("Can't create netto transformer for overview page table structure for csv", csvTransformer);

    //Create output stream
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    csvTransformer.transform(sourceList, out, TypeOfBuildingBlock.INFORMATIONSYSTEMRELEASE);

    //assert that output stream
    InputStream in = new ByteArrayInputStream(out.toByteArray());
    CSVReader reader;
    try {

        //BOMInputStream is necessary, because of leading BOM in Stream
        //Otherwise assertion would fail
        BOMInputStream bOMInputStream = new BOMInputStream(in);
        ByteOrderMark bom = bOMInputStream.getBOM();
        String charsetName = bom == null ? "UTF-8" : bom.getCharsetName();

        reader = new CSVReader(new InputStreamReader(new BufferedInputStream(bOMInputStream), charsetName),
                ';');

        List<String[]> allLines = reader.readAll();

        int index = 0;
        SimpleDateFormat df = new SimpleDateFormat("dd.MM.yyyy", Locale.GERMAN);

        for (String[] line : allLines) {

            String name = line[0];
            String desc = line[1];
            String start = line[2];
            String end = line[3];
            String statusString = line[4];
            String numString = line[5];

            //get the status enum first
            //we onlny need planned and current in this test
            TypeOfStatus status = null;

            if ("Plan".equals(statusString)) {
                status = TypeOfStatus.PLANNED;
            } else if ("Ist".equals(statusString)) {
                status = TypeOfStatus.CURRENT;
            }

            if (index == 0) {
                //assert the headers of CSV file
                assertEquals("Name und Version", name.trim());
                assertEquals("Beschreibung", desc);
                assertEquals("von", start);
                assertEquals("bis", end);
                assertEquals("Status", statusString);
                assertEquals("Complexity", numString);
            } else {

                //format the number
                NumberFormat nf = new DecimalFormat("0.00");
                String number = nf.format(Double.valueOf(numString));

                assertEquals(isArray[index - 1].getName(), name);
                assertEquals(isArray[index - 1].getDescription(), desc);
                assertEquals(df.format(isArray[index - 1].getRuntimePeriod().getStart()), start);
                assertEquals(df.format(isArray[index - 1].getRuntimePeriod().getEnd()), end);
                assertEquals(isArray[index - 1].getTypeOfStatus(), status);
                assertEquals(isArray[index - 1].getAttributeValue(numberAT.getName(), Locale.GERMAN), number);
            }

            index++;

        }

        reader.close();

    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:com.hangum.tadpole.commons.dialogs.fileupload.SingleFileuploadDialog.java

private boolean insert() throws Exception {
    BOMInputStream bomInputStream;

    File[] arryFiles = receiver.getTargetFiles();
    if (arryFiles.length == 0) {
        throw new Exception(Messages.get().SingleFileuploadDialog_5);
    }//from   w  ww  .ja v  a2s  . c o m

    File userUploadFile = arryFiles[arryFiles.length - 1];
    try {
        // bom?  charset? ? ?.
        bomInputStream = new BOMInputStream(FileUtils.openInputStream(FileUtils.getFile(userUploadFile)));
        ByteOrderMark bom = bomInputStream.getBOM();
        String charsetName = bom == null ? "CP949" : bom.getCharsetName(); //$NON-NLS-1$

        strTxtFile = FileUtils.readFileToString(userUploadFile, charsetName);
        if (bom != null) {
            // ByteOrderMark.UTF_8.equals(strTxtFile.getBytes()[0]);
            //??? ??  BOM? .
            strTxtFile = strTxtFile.substring(1);
        }

    } catch (Exception e) {
        logger.error("file read error", e); //$NON-NLS-1$
        throw new Exception(Messages.get().SingleFileuploadDialog_7 + e.getMessage());
    }

    return true;
}

From source file:com.hangum.tadpole.importdb.core.dialog.importdb.sql.SQLToDBImportDialog.java

private void insert() throws IOException {
    int ret;//from  w  w  w. ja va2  s.  c o  m
    BOMInputStream bomInputStream = null;

    File[] arryFiles = receiver.getTargetFiles();
    if (arryFiles.length == 0) {
        MessageDialog.openError(null, Messages.CsvToRDBImportDialog_4, Messages.CsvToRDBImportDialog_21);
        return;
    }

    if (!MessageDialog.openConfirm(null, Messages.CsvToRDBImportDialog_4,
            Messages.SQLToDBImportDialog_UploadQuestion))
        return;
    bufferBatchResult = new StringBuffer();

    try {
        batchSize = Integer.valueOf(textBatchSize.getText());
    } catch (Exception e) {
        batchSize = 1000;
    }

    File userUploadFile = arryFiles[arryFiles.length - 1];
    try {
        // bom?  charset? ? ?.
        bomInputStream = new BOMInputStream(FileUtils.openInputStream(FileUtils.getFile(userUploadFile)));//`, false, ByteOrderMark.UTF_8, ByteOrderMark.UTF_16LE, ByteOrderMark.UTF_16BE, ByteOrderMark.UTF_32LE, ByteOrderMark.UTF_32BE);

        String charsetName = "utf-8";
        String strSQLData = "";
        if (bomInputStream.getBOM() == null) {
            strSQLData = FileUtils.readFileToString(userUploadFile, charsetName);
        } else {
            charsetName = bomInputStream.getBOMCharsetName();
            strSQLData = FileUtils.readFileToString(userUploadFile, charsetName).substring(1);
        }

        String[] strArrySQL = StringUtils.split(strSQLData, textSeprator.getText());
        ret = runSQLExecuteBatch(Arrays.asList(strArrySQL));

        if (ret == 0)
            MessageDialog.openInformation(null, "Confirm", Messages.SQLToDBImportDialog_StoreData); //$NON-NLS-1$
    } catch (IOException e) {
        logger.error(Messages.SQLToDBImportDialog_ReadError, e);
        MessageDialog.openError(null, Messages.CsvToRDBImportDialog_4,
                Messages.SQLToDBImportDialog_LoadException + e.getMessage());

    } catch (Exception e) {
        logger.error(Messages.SQLToDBImportDialog_ImportException, e);
        MessageDialog.openError(null, Messages.CsvToRDBImportDialog_4,
                Messages.SQLToDBImportDialog_LoadException + e.getMessage());
    } finally {
        if (bomInputStream != null)
            bomInputStream.close();
    }
}

From source file:com.hangum.tadpole.importexport.core.dialogs.SQLToDBImportDialog.java

private void insert() throws IOException {
    int ret;//from w w w.j  ava  2s.  c o  m
    BOMInputStream bomInputStream = null;

    File[] arryFiles = receiver.getTargetFiles();
    if (arryFiles.length == 0) {
        MessageDialog.openWarning(null, Messages.get().Warning, Messages.get().CsvToRDBImportDialog_21);
        return;
    }

    if (!MessageDialog.openConfirm(null, Messages.get().Confirm,
            Messages.get().SQLToDBImportDialog_UploadQuestion))
        return;
    bufferBatchResult = new StringBuffer();

    try {
        batchSize = Integer.valueOf(textBatchSize.getText());
    } catch (Exception e) {
        batchSize = 1000;
    }

    File userUploadFile = arryFiles[arryFiles.length - 1];
    try {
        // bom?  charset? ? ?.
        bomInputStream = new BOMInputStream(FileUtils.openInputStream(FileUtils.getFile(userUploadFile)));//`, false, ByteOrderMark.UTF_8, ByteOrderMark.UTF_16LE, ByteOrderMark.UTF_16BE, ByteOrderMark.UTF_32LE, ByteOrderMark.UTF_32BE);

        String charsetName = "utf-8"; //$NON-NLS-1$
        String strSQLData = ""; //$NON-NLS-1$
        if (bomInputStream.getBOM() == null) {
            strSQLData = FileUtils.readFileToString(userUploadFile, charsetName);
        } else {
            charsetName = bomInputStream.getBOMCharsetName();
            strSQLData = FileUtils.readFileToString(userUploadFile, charsetName).substring(1);
        }

        String[] strArrySQL = StringUtils.split(strSQLData, textSeprator.getText());
        ret = runSQLExecuteBatch(Arrays.asList(strArrySQL));

        if (ret == 0)
            MessageDialog.openInformation(null, Messages.get().Confirm,
                    Messages.get().SQLToDBImportDialog_StoreData); //$NON-NLS-1$
    } catch (IOException e) {
        logger.error(Messages.get().SQLToDBImportDialog_ReadError, e);
        MessageDialog.openError(null, Messages.get().Confirm,
                Messages.get().SQLToDBImportDialog_LoadException + e.getMessage());

    } catch (Exception e) {
        logger.error(Messages.get().SQLToDBImportDialog_ImportException, e);
        MessageDialog.openError(null, Messages.get().Confirm,
                Messages.get().SQLToDBImportDialog_LoadException + e.getMessage());
    } finally {
        if (bomInputStream != null)
            bomInputStream.close();
    }
}

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 {/*from w w w  .  j av  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 .  j a  v  a2  s .c om
    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);
    }
}