Example usage for org.apache.commons.io CopyUtils copy

List of usage examples for org.apache.commons.io CopyUtils copy

Introduction

In this page you can find the example usage for org.apache.commons.io CopyUtils copy.

Prototype

public static void copy(InputStream input, Writer output, String encoding) throws IOException 

Source Link

Document

Copy and convert bytes from an InputStream to chars on a Writer, using the specified encoding.

Usage

From source file:org.roosster.input.processors.XmlProcessor.java

/**
 *
 *///ww w.jav a2s .  c  o  m
public Entry[] process(URL url, InputStream stream, String encoding) throws Exception {
    if (url == null || stream == null || encoding == null || "".equals(encoding))
        throw new IllegalArgumentException("No parameter is allowed to be null");

    StringWriter strWriter = new StringWriter();
    CopyUtils.copy(stream, strWriter, encoding);
    try {

        EntryList entryList = new EntryParser().parse(strWriter.toString(), encoding);

        return (Entry[]) entryList.toArray(new Entry[0]);

    } catch (ParseException ex1) {
        LOG.debug("Parsing Error while trying to parse EntryList: " + ex1.getMessage());

        try {
            // TODO Try to determine, if the stream is a feed, and parses it, 
            // if this is the case. If not, it just indexes it

            FeedParser parser = new FeedParser();
            return parser.parse(url, strWriter.toString());

        } catch (Exception ex) {
            throw new OperationException(ex);
        }
    }

}