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

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

Introduction

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

Prototype

public static void copy(Reader input, OutputStream output, String encoding) throws IOException 

Source Link

Document

Copy chars from a Reader to bytes on an OutputStream using the specified character encoding, and calling flush.

Usage

From source file:email.mandrill.SendMail.java

public static MessageResponses sendMail(Message message) {
    try {/*from w ww .  j  ava2s.c  om*/
        logger.log(Level.INFO, "Message:" + message.toJSONString());

        HttpClient httpclient = HttpClients.createDefault();
        HttpPost httppost = new HttpPost("https://mandrillapp.com/api/1.0/messages/send.json");

        String request = message.toJSONString();
        HttpEntity entity = new ByteArrayEntity(request.getBytes("UTF-8"));
        httppost.setEntity(entity);
        //Execute and get the response.
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity responseEntity = response.getEntity();

        if (responseEntity != null) {
            InputStream instream = responseEntity.getContent();
            try {
                StringWriter writer = new StringWriter();
                IOUtils.copy(instream, writer, "UTF-8");
                String theString = writer.toString();

                MessageResponses messageResponses = new MessageResponses(theString);
                //Do whateveer is needed with the response.
                logger.log(Level.INFO, theString);
                return messageResponses;
            } finally {
                instream.close();
            }
        }
    } catch (Exception e) {
        logger.log(Level.SEVERE, util.Utility.logMessage(e, "Exception while updating org name:", null));
    }
    return null;
}

From source file:com.opendoorlogistics.studio.dialogs.AboutBoxDialog.java

private static String info(boolean showLicenses) {

    // Use own class loader to prevent problems when jar loaded by reflection
    InputStream is = AboutBoxDialog.class
            .getResourceAsStream(showLicenses ? "/resources/Licences.html" : "/resources/About.html");
    StringWriter writer = new StringWriter();
    try {/*www.j  av a 2s. c  o m*/
        IOUtils.copy(is, writer, Charsets.UTF_8);
        is.close();
    } catch (Throwable e) {
    }

    String s = writer.toString();

    s = replaceVersionNumberTags(s);
    return s;
}

From source file:jlotoprint.model.Template.java

public static Model load(File templateFile, boolean showFeedback) {
    Model modelRef = null;//from   www.  j  a va 2  s  .  c  o m
    try {
        StringWriter writer = new StringWriter();
        IOUtils.copy(new FileInputStream(templateFile), writer, "UTF-8");
        Gson g = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
        modelRef = g.fromJson(writer.toString(), Model.class);
    } catch (Exception ex) {
        //Logger.getLogger(Template.class.getName()).log(Level.SEVERE, null, ex);
        if (showFeedback) {
            Alert dialog = new Alert(Alert.AlertType.ERROR, "The template you are trying to load is invalid.",
                    ButtonType.OK);
            dialog.initModality(Modality.APPLICATION_MODAL);
            dialog.showAndWait();
        }
    }
    return modelRef;
}

From source file:edu.harvard.hms.dbmi.scidb.examples.Apply.java

private static String inputStreamToString(InputStream inputStream) throws IOException {
    StringWriter writer = new StringWriter();
    IOUtils.copy(inputStream, writer, Charset.defaultCharset());
    inputStream.close();//from  w ww  .ja v  a  2  s  . co  m
    return writer.toString();
}

From source file:com.enonic.cms.core.search.ConfigFileMappingProvider.java

public String getMapping(final String indexName, final String indexType) {
    InputStream stream = ConfigFileMappingProvider.class
            .getResourceAsStream(createMappingFileName(indexName, indexType));

    if (stream == null) {
        throw new ElasticSearchException(
                "Mapping-file not found: " + createMappingFileName(indexName, indexType));
    }/* w  ww. ja v a 2 s  .co m*/

    StringWriter writer = new StringWriter();
    try {
        IOUtils.copy(stream, writer, "UTF-8");
        return writer.toString();
    } catch (IOException e) {
        throw new ElasticSearchException("Failed to get mapping-file as stream", e);
    }
}

From source file:it.cnr.ilc.ilcioutils.IlcInputToString.java

/**
 * Convert an inputstream into a string//  ww w .  j  ava2s. c  o m
 *
 * @param theUrl the url to get the context from
 * @return the string from the input
 */
public static String convertInputStreamFromUrlToString(String theUrl) {
    StringWriter writer = new StringWriter();
    InputStream is = null;
    String encoding = "UTF-8";
    String message = "";
    String theString = "";
    try {
        is = new URL(theUrl).openStream();
        IOUtils.copy(is, writer, encoding);
        theString = writer.toString();
    } catch (Exception e) {
        message = "IOException in coverting the stream into a string " + e.getMessage();
        Logger.getLogger(CLASS_NAME).log(Level.SEVERE, message);
    }

    //System.err.println("DDDD " + theString);
    IOUtils.closeQuietly(is);
    IOUtils.closeQuietly(writer);
    return theString;
}

From source file:com.streamreduce.util.MessageUtils.java

public static String readJSONFromClasspath(String resource) {
    InputStream inputStream = MessageUtils.class.getResourceAsStream(resource);
    StringWriter writer = new StringWriter();
    try {/*from ww w . j  a v  a  2s  .c  o m*/
        IOUtils.copy(inputStream, writer, "UTF-8");
    } catch (IOException e) {
        LOGGER.error(e.getMessage(), e);
    }
    return writer.toString();
}

From source file:com.vigglet.servlet.PostJsonServlet.java

@Override
protected void processRequest(HttpServletRequest req, HttpServletResponse resp, User user) throws Exception {

    StringWriter writer = new StringWriter();
    IOUtils.copy(req.getInputStream(), writer, "UTF-8");
    String theString = writer.toString();

    T model = JsonUtil.read(theString, getModelClass());
    if (model != null) {
        model = setCompany(user, model);
        JsonUtil.write(resp.getOutputStream(), update(req, resp, user, model));
    } else {//  w ww .j a v  a  2 s  .  c o  m
        Logger.getLogger(PostJsonServlet.class.getName()).log(Level.WARNING, "Could not read json!", theString);
    }
}

From source file:de.micromata.genome.gwiki.page.attachments.TxtTextExtractor.java

public String extractText(String fileName, InputStream data) {
    try {/*from   ww w  . ja va2  s . c o  m*/
        StringWriter sout = new StringWriter();
        IOUtils.copy(data, sout, "ISO-8859-1");
        return sout.toString();
    } catch (IOException ex) {
        throw new RuntimeIOException(ex);
    }
}

From source file:com.scorpio4.util.io.StreamCopy.java

public static String toString(InputStream inputStream, String encoding) throws IOException {
    StringWriter writer = new StringWriter();
    IOUtils.copy(inputStream, writer, encoding);
    return writer.toString();
}