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

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

Introduction

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

Prototype

public String getBOMCharsetName() throws IOException 

Source Link

Document

Return the BOM charset Name - ByteOrderMark#getCharsetName() .

Usage

From source file:com.github.anba.test262.environment.RhinoEnv.java

/**
 * Returns a new {@link Reader} for the {@code stream} parameter
 *///from  ww w  . j  a  va2s.  c o  m
private static Reader newReader(InputStream stream, String defaultCharset) throws IOException {
    BOMInputStream bomstream = new BOMInputStream(stream, ByteOrderMark.UTF_8, ByteOrderMark.UTF_16LE,
            ByteOrderMark.UTF_16BE);
    String charset = defaultIfNull(bomstream.getBOMCharsetName(), defaultCharset);
    return new InputStreamReader(bomstream, charset);
}

From source file:com.github.anba.test262.util.Test262Info.java

private static Reader newReader(InputStream stream, String defaultCharset) throws IOException {
    BOMInputStream bomstream = new BOMInputStream(stream, ByteOrderMark.UTF_8, ByteOrderMark.UTF_16LE,
            ByteOrderMark.UTF_16BE);// w  w w.jav  a 2  s  .co m
    String charset = defaultIfNull(bomstream.getBOMCharsetName(), defaultCharset);
    return new InputStreamReader(bomstream, charset);
}

From source file:cn.dreampie.resource.LessSource.java

private String loadResource(Resource resource, Charset charset) throws IOException {
    BOMInputStream inputStream = new BOMInputStream(resource.getInputStream());
    try {/* ww w  . j  a  v a  2 s  .com*/
        if (inputStream.hasBOM()) {
            logger.debug("BOM found %s", inputStream.getBOMCharsetName());
            return IOUtils.toString(inputStream, inputStream.getBOMCharsetName());
        } else {
            logger.debug("Using charset " + charset.name());
            return IOUtils.toString(inputStream, charset.name());
        }
    } finally {
        inputStream.close();
    }
}

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

private void insert() throws IOException {
    int ret;/* ww w .ja v a2  s.  c  om*/
    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;/*ww  w .j  av  a  2 s .  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.users.dragomerlin.vcs2icsCalendarConverter.ConvertSingleFile.java

private static BufferedReader detectEncodingAndOpenFile(File inFile) throws IOException {
    String encodingType = null;//from   w  ww .  j  a v a2s  . co  m
    BufferedReader input = null;
    BOMInputStream bomIn = null;

    // Detect file encoding
    encodingType = TestDetector.main(inFile.getAbsolutePath().toString());

    // Entire file reading. FileReader always assumes default encoding is
    // OK!
    // We must check for BOM in UTF files and remove them with
    // org.apache.commons.io.input.BOMInputStream because
    // java doesn't do that automatically. See Oracle bug 4508058.
    if (encodingType == null) {
        // ASCII expected
        input = new BufferedReader(new InputStreamReader(new FileInputStream(inFile)));
    } else if (encodingType.startsWith("UTF-8")) {
        // UTF-8 requires an exclusive call to BOMInputStream
        bomIn = new BOMInputStream(new FileInputStream(inFile));
        input = new BufferedReader(new InputStreamReader(bomIn, encodingType));
        if (bomIn.hasBOM())
            System.out.println("This file has UTF-8 BOM, removing it");
        else
            System.out.println("This file has UTF-8 without BOM");
    } else if (encodingType.startsWith("UTF-")) {
        // The other UTF cases except UTF-8
        bomIn = new BOMInputStream(new FileInputStream(inFile), ByteOrderMark.UTF_16LE, ByteOrderMark.UTF_16BE,
                ByteOrderMark.UTF_32LE, ByteOrderMark.UTF_32BE);
        input = new BufferedReader(new InputStreamReader(bomIn, encodingType));
        System.out.println("This file has " + bomIn.getBOMCharsetName() + " BOM, removing it");
    } else {
        // Any other encoding
        input = new BufferedReader(new InputStreamReader(new FileInputStream(inFile), encodingType));
    }
    return input;
}

From source file:org.apache.flex.compiler.internal.embedding.transcoders.XMLTranscoder.java

private String getXMLString(Collection<ICompilerProblem> problems) {
    InputStream strm = getDataStream(problems);
    if (strm == null)
        return "";

    Reader reader = null;/*from w  ww  .  j  a  v  a2 s . c o  m*/
    BOMInputStream bomStream = null;
    StringBuilder str = new StringBuilder();
    try {
        bomStream = new BOMInputStream(strm);
        String bomCharsetName = bomStream.getBOMCharsetName();
        if (bomCharsetName == null) {
            if (encoding == null || encoding.length() == 0) {
                bomCharsetName = System.getProperty("file.encoding");
            } else {
                bomCharsetName = encoding;
            }
        }

        reader = new InputStreamReader(bomStream, bomCharsetName);
        char[] line = new char[2048];
        int count = 0;
        while ((count = reader.read(line, 0, line.length)) >= 0) {
            str.append(line, 0, count);
        }
    } catch (IOException e) {
        problems.add(new EmbedSourceAttributeCouldNotBeReadProblem(source));
    } finally {
        if (bomStream != null) {
            try {
                bomStream.close();
            } catch (IOException e) {
            }
        }

        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
            }
        }
    }

    return str.toString();
}

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

From source file:org.sakaiproject.citation.tool.CitationHelperAction.java

/**
 *
 * @param data//from   www . j a v a  2  s . co  m
 */
public void doImport(RunData data) {
    logger.debug("doImport called.");

    // get the state object
    SessionState state = ((JetspeedRunData) data).getPortletSessionState(((JetspeedRunData) data).getJs_peid());
    ParameterParser params = data.getParameters();

    int requestStateId = params.getInt("requestStateId", 0);
    restoreRequestState(state,
            new String[] { CitationHelper.RESOURCES_REQUEST_PREFIX, CitationHelper.CITATION_PREFIX },
            requestStateId);

    Iterator iter = params.getNames();

    String param = null;

    while (iter.hasNext()) {
        param = (String) iter.next();
        if (logger.isDebugEnabled()) {
            logger.debug("param = " + param);
            logger.debug(param + " value = " + params.get(param));
        }
    }

    String citationCollectionId = params.getString("citationCollectionId");

    if (citationCollectionId == null) {
        citationCollectionId = (String) state.getAttribute(STATE_CITATION_COLLECTION_ID);
    }

    CitationCollection collection = null;
    collection = getCitationService().getUnnestedCitationCollection(citationCollectionId);

    String ristext = params.get("ristext");

    // We're going to read the RIS file from the submitted form's textarea first. If that's
    // empty we'll read it from the uploaded file.  We'll crate a BufferedReader in either
    // circumstance so that the parsing code need not know where the ris text came from.
    java.io.BufferedReader bread = null;

    if (ristext.trim().length() > 0) // form has text in the risimport textarea
    {
        java.io.StringReader risStringReader = new java.io.StringReader(ristext);
        bread = new java.io.BufferedReader(risStringReader);
        logger.debug("String buffered reader ready");

    } // end RIS text is in the textarea
    else // textarea empty, set the read of the import from the file
    {
        String upload = params.get("risupload");
        if (logger.isDebugEnabled()) {
            logger.debug("Upload String = " + upload);
        }

        FileItem risImport = params.getFileItem("risupload");

        if (risImport == null) {
            logger.debug("risImport is null.");
            return;
        }

        if (logger.isDebugEnabled()) {
            logger.debug("Filename = " + risImport.getFileName());
        }

        InputStream risImportStream = risImport.getInputStream();

        // Attempt to detect the encoding of the file.
        BOMInputStream irs = new BOMInputStream(risImportStream);

        // below is needed if UTF-8 above is commented out
        Reader isr = null;
        String bomCharsetName = null;
        try {
            bomCharsetName = irs.getBOMCharsetName();
            if (bomCharsetName != null) {
                isr = new InputStreamReader(risImportStream, bomCharsetName);
            }
        } catch (UnsupportedEncodingException uee) {
            // Something strange as the JRE should support all the formats.
            if (logger.isInfoEnabled()) {
                logger.info("Problem using character set when importing RIS: " + bomCharsetName);
            }
        } catch (IOException ioe) {
            // Probably won't get any further, but may as well try.
            if (logger.isDebugEnabled()) {
                logger.debug("Problem reading the character set from RIS import: " + ioe.getMessage());
            }
        }
        // Fallback to platform default
        if (isr == null) {
            isr = new InputStreamReader(irs);
        }

        bread = new java.io.BufferedReader(isr);
    } // end set the read of the import from the uploaded file.

    // The below code is a major work in progress.
    // This code is for demonstration purposes only. No gambling or production use!

    StringBuilder fileString = new StringBuilder();
    String importLine = null;
    java.util.List importList = new java.util.ArrayList();

    // Read the BufferedReader and populate the importList. Each entry in the list
    // is a line in the RIS import "file".
    try {
        while ((importLine = bread.readLine()) != null) {
            importLine = importLine.trim();

            if (importLine != null && importLine.length() > 2) {
                importList.add(importLine);
                if (logger.isDebugEnabled()) {
                    fileString.append("\n");
                    fileString.append(importLine);
                }
            }

        } // end while
    } // end try
    catch (Exception e) {
        logger.debug("ISR error = " + e);
    } // end catch
    finally {
        if (bread != null) {
            try {
                bread.close();
            } catch (IOException e) {
                // tried
            }
        }
    }

    if (logger.isDebugEnabled()) {
        logger.debug("fileString = \n" + fileString.toString());
    }

    // tempList holds the entries read in to make a citation up to and
    // including the ER entry from importList
    List tempList = new java.util.ArrayList();

    Citation importCitation = getCitationService().getTemporaryCitation();
    CitationCollection importCollection = getCitationService().getTemporaryCollection();

    int sucessfullyReadCitations = 0;
    int totalNumberCitations = 0;

    // Read each entry in the RIS List and build a citation
    for (int i = 0; i < importList.size(); i++) {
        String importEntryString = (String) importList.get(i);
        //         logger.debug("Import line (#1) = " + importEntryString);
        //         logger.debug("Substring is = " + importEntryString.substring(0, 2));
        tempList.add(importEntryString);

        // make sure importEntryString can be tested for "ER" existence. It could
        // be a dinky invalid line less than 2 characters.
        if (importEntryString != null && importEntryString.length() > 1
                && importEntryString.substring(0, 2).equalsIgnoreCase("ER")) {
            // end of citation (signaled by ER).

            totalNumberCitations++;
            if (logger.isDebugEnabled()) {
                logger.debug("------> Trying to add citation " + totalNumberCitations);
            }
            if (importCitation.importFromRisList(tempList)) // import went well
            {
                importCollection.add(importCitation);
                sucessfullyReadCitations++;
            }
            tempList.clear();
            importCitation = getCitationService().getTemporaryCitation();
        }
    } // end for

    if (logger.isDebugEnabled()) {
        logger.debug(
                "Done reading in " + sucessfullyReadCitations + " / " + totalNumberCitations + " citations.");
    }

    collection.addAll(importCollection);
    getCitationService().save(collection);

    // remove collection from state
    state.removeAttribute(STATE_CITATION_COLLECTION);

    //setMode(state, Mode.LIST);
    setMode(state, Mode.NEW_RESOURCE);
}