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

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

Introduction

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

Prototype

public BOMInputStream(InputStream delegate, ByteOrderMark... boms) 

Source Link

Document

Constructs a new BOM InputStream that excludes the specified BOMs.

Usage

From source file:adept.io.Reader.java

/**
 * File to string./*  w w  w .java 2  s  .c  o m*/
 *
 * @param filename the filename
 * @return the list
 */
private String fileToString(String filename) {
    String lines = "";
    String line = "";
    try {
        BufferedReader in = new BufferedReader(new InputStreamReader(
                new BOMInputStream(new FileInputStream(new File(filename)), false), "UTF-8"));
        while ((line = in.readLine()) != null) {
            lines = lines + line + "\n";
        }
        in.close();
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    }
    return lines;
}

From source file:org.apromore.canoniser.bpmn.bpmn.JarLSResourceResolver.java

/** {@inheritDoc} */
@Override/*from w w w . j ava 2  s . co m*/
public LSInput resolveResource(final String type, final String namespaceURI, final String publicId,
        final String systemId, final String baseURI) {
    return new LSInput() {

        /** @return a byte stream from the <code>xsd/</code> directory within the classpath */
        @Override
        public InputStream getByteStream() {
            return getClass().getClassLoader().getResourceAsStream("xsd/" + systemId);
        }

        /** @return a character stream in the default platform encoding, with any byte-order-mark stripped. */
        @Override
        public Reader getCharacterStream() {
            return new InputStreamReader(new BOMInputStream(getByteStream(), false));
        }

        // Cursory implementations of the remainder of the LSInput interface

        @Override
        public String getBaseURI() {
            return null;
        }

        @Override
        public boolean getCertifiedText() {
            return false;
        }

        @Override
        public String getEncoding() {
            return null;
        }

        @Override
        public String getPublicId() {
            return null;
        }

        @Override
        public String getStringData() {
            return null;
            /* throw new UnsupportedOperationException(systemId); */ }

        @Override
        public String getSystemId() {
            return null;
            /* return systemId; */ }

        @Override
        public void setBaseURI(final String baseURI) {
            throw new UnsupportedOperationException(baseURI);
        }

        @Override
        public void setByteStream(final InputStream in) {
            throw new UnsupportedOperationException();
        }

        @Override
        public void setCertifiedText(final boolean certifiedText) {
            throw new UnsupportedOperationException();
        }

        @Override
        public void setCharacterStream(final Reader reader) {
            throw new UnsupportedOperationException();
        }

        @Override
        public void setEncoding(final String encoding) {
            throw new UnsupportedOperationException(encoding);
        }

        @Override
        public void setPublicId(final String publicId) {
            throw new UnsupportedOperationException(publicId);
        }

        @Override
        public void setStringData(final String stringData) {
            throw new UnsupportedOperationException(stringData);
        }

        @Override
        public void setSystemId(final String systemId) {
            throw new UnsupportedOperationException(systemId);
        }
    };
}

From source file:org.eclipse.rdf4j.rio.nquads.NQuadsParser.java

@Override
public synchronized void parse(final InputStream inputStream, final String baseURI)
        throws IOException, RDFParseException, RDFHandlerException {
    if (inputStream == null) {
        throw new IllegalArgumentException("Input stream can not be 'null'");
    }/*  w w  w  .  ja  va 2  s .  c om*/
    // Note: baseURI will be checked in parse(Reader, String)

    try {
        parse(new InputStreamReader(new BOMInputStream(inputStream, false), Charset.forName("UTF-8")), baseURI);
    } catch (UnsupportedEncodingException e) {
        // Every platform should support the UTF-8 encoding...
        throw new RuntimeException(e);
    }
}

From source file:org.eclipse.rdf4j.rio.ntriples.NTriplesParser.java

/**
 * Implementation of the <tt>parse(InputStream, String)</tt> method defined in the RDFParser interface.
 * /*from   w w  w .  j  a  v a 2s  .  co m*/
 * @param in
 *        The InputStream from which to read the data, must not be <tt>null</tt>. The InputStream is
 *        supposed to contain 7-bit US-ASCII characters, as per the N-Triples specification.
 * @param baseURI
 *        The URI associated with the data in the InputStream, must not be <tt>null</tt>.
 * @throws IOException
 *         If an I/O error occurred while data was read from the InputStream.
 * @throws RDFParseException
 *         If the parser has found an unrecoverable parse error.
 * @throws RDFHandlerException
 *         If the configured statement handler encountered an unrecoverable error.
 * @throws IllegalArgumentException
 *         If the supplied input stream or base URI is <tt>null</tt>.
 */
@Override
public synchronized void parse(InputStream in, String baseURI)
        throws IOException, RDFParseException, RDFHandlerException {
    if (in == null) {
        throw new IllegalArgumentException("Input stream can not be 'null'");
    }
    // Note: baseURI will be checked in parse(Reader, String)

    try {
        parse(new InputStreamReader(new BOMInputStream(in, false), Charset.forName("UTF-8")), baseURI);
    } catch (UnsupportedEncodingException e) {
        // Every platform should support the UTF-8 encoding...
        throw new RuntimeException(e);
    }
}

From source file:org.eclipse.rdf4j.rio.rdfjson.RDFJSONParser.java

@Override
public void parse(final InputStream inputStream, final String baseUri)
        throws IOException, RDFParseException, RDFHandlerException {
    JsonParser jp = null;/*from ww  w.j  a v a 2  s . c om*/

    clear();

    try {
        if (this.rdfHandler != null) {
            this.rdfHandler.startRDF();
        }

        jp = RDFJSONUtility.JSON_FACTORY.createParser(new BOMInputStream(inputStream, false));
        rdfJsonToHandlerInternal(this.rdfHandler, this.valueFactory, jp);
    } catch (final IOException e) {
        if (jp != null) {
            reportFatalError("Found IOException during parsing", e, jp.getCurrentLocation());
        } else {
            reportFatalError(e);
        }
    } finally {
        clear();
        if (jp != null) {
            try {
                jp.close();
            } catch (final IOException e) {
                reportFatalError("Found exception while closing JSON parser", e, jp.getCurrentLocation());
            }
        }
    }
    if (this.rdfHandler != null) {
        this.rdfHandler.endRDF();
    }
}

From source file:org.eclipse.rdf4j.rio.rdfxml.RDFXMLParser.java

/**
 * Parses the data from the supplied InputStream, using the supplied baseURI to resolve any relative URI
 * references.//from w ww . j  av  a 2 s  .c  om
 * 
 * @param in
 *        The InputStream from which to read the data, must not be <tt>null</tt>.
 * @param baseURI
 *        The URI associated with the data in the InputStream, must not be <tt>null</tt>.
 * @throws IOException
 *         If an I/O error occurred while data was read from the InputStream.
 * @throws RDFParseException
 *         If the parser has found an unrecoverable parse error.
 * @throws RDFHandlerException
 *         If the configured statement handler encountered an unrecoverable error.
 * @throws IllegalArgumentException
 *         If the supplied input stream or base URI is <tt>null</tt>.
 */
@Override
public synchronized void parse(InputStream in, String baseURI)
        throws IOException, RDFParseException, RDFHandlerException {
    if (in == null) {
        throw new IllegalArgumentException("Input stream cannot be 'null'");
    }
    if (baseURI == null) {
        throw new IllegalArgumentException("Base URI cannot be 'null'");
    }

    InputSource inputSource = new InputSource(new BOMInputStream(in, false));
    inputSource.setSystemId(baseURI);

    parse(inputSource);
}

From source file:org.eclipse.rdf4j.rio.trix.TriXParser.java

/**
 * Parses the data from the supplied InputStream, using the supplied baseURI to resolve any relative URI
 * references.//w w  w  .j  av  a2s  .c om
 * 
 * @param in
 *        The InputStream from which to read the data, must not be <tt>null</tt>.
 * @param baseURI
 *        The URI associated with the data in the InputStream, must not be <tt>null</tt>.
 * @throws IOException
 *         If an I/O error occurred while data was read from the InputStream.
 * @throws RDFParseException
 *         If the parser has found an unrecoverable parse error.
 * @throws RDFHandlerException
 *         If the configured statement handler encountered an unrecoverable error.
 * @throws IllegalArgumentException
 *         If the supplied input stream or base URI is <tt>null</tt>.
 */
@Override
public void parse(InputStream in, String baseURI) throws IOException, RDFParseException, RDFHandlerException {
    if (in == null) {
        throw new IllegalArgumentException("Input stream cannot be 'null'");
    }
    if (baseURI == null) {
        throw new IllegalArgumentException("Base URI cannot be 'null'");
    }

    InputSource inputSource = new InputSource(new BOMInputStream(in, false));
    inputSource.setSystemId(baseURI);

    parse(inputSource);
}

From source file:org.eclipse.rdf4j.rio.turtle.TurtleParser.java

/**
 * Implementation of the <tt>parse(InputStream, String)</tt> method defined
 * in the RDFParser interface./*from  w w w. java2  s  . c o m*/
 *
 * @param in
 *            The InputStream from which to read the data, must not be
 *            <tt>null</tt>. The InputStream is supposed to contain UTF-8
 *            encoded Unicode characters, as per the Turtle specification.
 * @param baseURI
 *            The URI associated with the data in the InputStream, must not
 *            be <tt>null</tt>.
 * @throws IOException
 *             If an I/O error occurred while data was read from the
 *             InputStream.
 * @throws RDFParseException
 *             If the parser has found an unrecoverable parse error.
 * @throws RDFHandlerException
 *             If the configured statement handler encountered an
 *             unrecoverable error.
 * @throws IllegalArgumentException
 *             If the supplied input stream or base URI is <tt>null</tt>.
 */
public synchronized void parse(InputStream in, String baseURI)
        throws IOException, RDFParseException, RDFHandlerException {
    if (in == null) {
        throw new IllegalArgumentException("Input stream must not be 'null'");
    }
    // Note: baseURI will be checked in parse(Reader, String)

    try {
        parse(new InputStreamReader(new BOMInputStream(in, false), StandardCharsets.UTF_8), baseURI);
    } catch (UnsupportedEncodingException e) {
        // Every platform should support the UTF-8 encoding...
        throw new RuntimeException(e);
    }
}

From source file:org.lockss.plugin.metapress.MetapressTextAndRisFilterFactory.java

@Override
public InputStream createFilteredInputStream(ArchivalUnit au, InputStream in, String encoding)
        throws PluginException {
    return new BOMInputStream(in, false);
}

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  a 2  s .  com*/
    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;
    }
}