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

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

Introduction

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

Prototype

@Override
public int read() throws IOException 

Source Link

Document

Invokes the delegate's read() method, detecting and optionally skipping BOM.

Usage

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;/*  ww  w  .ja v a 2 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;
}