Example usage for java.io CharArrayWriter toCharArray

List of usage examples for java.io CharArrayWriter toCharArray

Introduction

In this page you can find the example usage for java.io CharArrayWriter toCharArray.

Prototype

public char[] toCharArray() 

Source Link

Document

Returns a copy of the input data.

Usage

From source file:IOUtils.java

/**
 * Get the contents of an <code>InputStream</code> as a character array
 * using the specified character encoding.
 * <p>/*from w  w w.j a va  2 s . c om*/
 * Character encoding names can be found at
 * <a href="http://www.iana.org/assignments/character-sets">IANA</a>.
 * <p>
 * This method buffers the input internally, so there is no need to use a
 * <code>BufferedInputStream</code>.
 * 
 * @param is  the <code>InputStream</code> to read from
 * @param encoding  the encoding to use, null means platform default
 * @return the requested character array
 * @throws NullPointerException if the input is null
 * @throws IOException if an I/O error occurs
 * @since Commons IO 1.1
 */
public static char[] toCharArray(InputStream is, String encoding) throws IOException {
    CharArrayWriter output = new CharArrayWriter();
    copy(is, output, encoding);
    return output.toCharArray();
}

From source file:org.sakaiproject.search.component.adapter.contenthosting.XLContentDigester.java

public String getContent(ContentResource contentResource) {
    CharArrayWriter writer = new CharArrayWriter();
    loadContent(writer, contentResource);
    return new String(writer.toCharArray());
}

From source file:org.sakaiproject.search.component.adapter.contenthosting.XLContentDigester.java

public Reader getContentReader(ContentResource contentResource) {
    CharArrayWriter writer = new CharArrayWriter();
    loadContent(writer, contentResource);
    return new CharArrayReader(writer.toCharArray());
}

From source file:org.sakaiproject.search.component.adapter.contenthosting.PDFContentDigester.java

public String getContent(ContentResource contentResource) {
    if (contentResource == null) {
        throw new RuntimeException("Null contentResource passed to getContent");
    }//from w w  w.j a  v  a 2 s .  c  o m

    InputStream contentStream = null;
    PDFParser parser = null;
    PDDocument pddoc = null;
    try {
        contentStream = contentResource.streamContent();
        parser = new PDFParser(new BufferedInputStream(contentStream));
        parser.parse();
        pddoc = parser.getPDDocument();
        if (pddoc != null) {
            PDFTextStripper stripper = new PDFTextStripper();
            stripper.setLineSeparator("\n");
            CharArrayWriter cw = new CharArrayWriter();
            stripper.writeText(pddoc, cw);
            return SearchUtils.appendCleanString(cw.toCharArray(), null).toString();
        }
    } catch (ServerOverloadException e) {
        String eMessage = e.getMessage();
        if (eMessage == null) {
            eMessage = e.toString();
        }
        throw new RuntimeException(
                "Failed to get content for indexing: cause: ServerOverloadException: " + eMessage, e);
    } catch (IOException e) {
        String eMessage = e.getMessage();
        if (eMessage == null) {
            eMessage = e.toString();
        }
        throw new RuntimeException("Failed to get content for indexing: cause: IOException:  " + eMessage, e);
    } finally {
        if (pddoc != null) {
            try {
                pddoc.close();
            } catch (IOException e) {
                log.debug(e);
            }
        }

        if (contentStream != null) {
            try {
                contentStream.close();
            } catch (IOException e) {
                log.debug(e);
            }
        }
    }
    return null;
}

From source file:net.ontopia.topicmaps.nav2.servlets.DataIntegrationServlet.java

public TopicMapIF transformRequest(String transformId, InputStream xmlstream, LocatorIF base) throws Exception {

    InputStream xsltstream = StreamUtils.getInputStream("classpath:" + transformId + ".xsl");
    if (xsltstream == null)
        throw new ServletException("Could not find style sheet '" + transformId + ".xsl'");

    // set up source and target streams
    // Source xmlSource = new StreamSource(xmlstream);
    Source xmlSource = new StreamSource(xmlstream);
    Source xsltSource = new StreamSource(xsltstream);

    // the factory pattern supports different XSLT processors
    TransformerFactory transFact = TransformerFactory.newInstance();
    Transformer trans = transFact.newTransformer(xsltSource);

    CharArrayWriter cw = new CharArrayWriter();
    trans.transform(xmlSource, new StreamResult(cw));
    CharArrayReader cr = new CharArrayReader(cw.toCharArray());

    TopicMapStoreIF store = new InMemoryTopicMapStore();
    TopicMapIF topicmap = store.getTopicMap();
    store.setBaseAddress(base);//  w  w w. j a v  a2 s  . c o  m
    XTMTopicMapReader xr = new XTMTopicMapReader(cr, base);
    xr.setValidation(false);
    xr.importInto(topicmap);

    return topicmap;
}

From source file:org.apereo.portal.portlets.sitemap.SitemapTest.java

@Test
public void testStylesheetCompilation() throws IOException {
    final MockHttpServletRequest request = new MockHttpServletRequest();
    Resource resource = new ClassPathResource(STYLESHEET_LOCATION);
    Source source = new StreamSource(resource.getInputStream(), resource.getURI().toASCIIString());
    try {//from  w w w .j av a  2 s  . co  m
        Transformer transformer = TransformerFactory.newInstance().newTransformer(source);
        transformer.setParameter(SitemapPortletController.USE_TAB_GROUPS, useTabGroups);
        transformer.setParameter(SitemapPortletController.USER_LANG, "en_US");
        transformer.setParameter(XsltPortalUrlProvider.CURRENT_REQUEST, request);
        transformer.setParameter(XsltPortalUrlProvider.XSLT_PORTAL_URL_PROVIDER, this.xsltPortalUrlProvider);

        Source xmlSource = new StreamSource(new ClassPathResource(XML_LOCATION).getFile());
        CharArrayWriter buffer = new CharArrayWriter();
        TransformerUtils.enableIndenting(transformer);
        transformer.transform(xmlSource, new StreamResult(buffer));
        if (logger.isTraceEnabled()) {
            logger.trace("XML: " + new String(buffer.toCharArray()));
        }
    } catch (TransformerConfigurationException e) {
        logger.error(e.getMessage(), e);
        throw new RuntimeException(e);
    } catch (TransformerException e) {
        logger.error(e.getMessage(), e);
        throw new RuntimeException(e);
    }
}

From source file:com.nominanuda.solr.DataImportHandlerWs.java

private String toXml(DataObject o) {
    CharArrayWriter w = new CharArrayWriter();
    pipe.build(new SAXSource(new JsonXmlReader(), new InputSource(new StringReader(o.toString()))),
            new StreamResult(w)).run();
    return new String(w.toCharArray());
}

From source file:org.bsc.maven.plugin.confluence.PegdownTest.java

private char[] loadResource(String name) throws IOException {

    final ClassLoader cl = PegdownTest.class.getClassLoader();

    final java.io.InputStream is = cl.getResourceAsStream(name);
    try {/*  w  w  w. j av a2s.c  om*/

        java.io.CharArrayWriter caw = new java.io.CharArrayWriter();

        for (int c = is.read(); c != -1; c = is.read()) {
            caw.write(c);
        }

        return caw.toCharArray();

    } finally {
        IOUtils.closeQuietly(is);
    }

}

From source file:edu.duke.cabig.c3pr.domain.scheduler.runtime.job.CCTSNotificationMessageJob.java

private String serialize(Notification notification) {
    try {//from   w  w w .  jav a 2 s. c  om
        JAXBContext context = JAXBContext.newInstance(Notification.class);
        Marshaller m = context.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        JAXBElement<Notification> jaxbElement = new JAXBElement<Notification>(
                new QName(CCTS_NOTIFICATIONS_NS, "notification"), Notification.class, notification);
        CharArrayWriter writer = new CharArrayWriter();
        m.marshal(jaxbElement, writer);
        return String.valueOf(writer.toCharArray());
    } catch (JAXBException e) {
        throw new RuntimeException(e);
    }

}

From source file:org.bsc.maven.plugin.confluence.PegdownParse.java

protected char[] loadResource(String name) throws IOException {

    final ClassLoader cl = PegdownParse.class.getClassLoader();

    final java.io.InputStream is = cl.getResourceAsStream(name);
    try {/*from   ww  w.  j  a v a2s  .c  om*/

        java.io.CharArrayWriter caw = new java.io.CharArrayWriter();

        for (int c = is.read(); c != -1; c = is.read()) {
            caw.write(c);
        }

        return caw.toCharArray();

    } finally {
        IOUtils.closeQuietly(is);
    }

}