Example usage for java.io Writer toString

List of usage examples for java.io Writer toString

Introduction

In this page you can find the example usage for java.io Writer toString.

Prototype

public String toString() 

Source Link

Document

Returns a string representation of the object.

Usage

From source file:ome.formats.importer.util.ErrorHandler.java

/**
 * Return stack trace from throwable/*w w w .  j a va 2s. c o m*/
 * @param throwable
 * @return stack trace
 */
public static String getStackTrace(Throwable throwable) {
    final Writer writer = new StringWriter();
    final PrintWriter printWriter = new PrintWriter(writer);
    throwable.printStackTrace(printWriter);
    return writer.toString();
}

From source file:com.google.visualization.datasource.render.HtmlRenderer.java

/**
 * Transforms a document to a valid html string.
 *
 * @param document The document to transform
 *
 * @return A string representation of a valid html.
 *///from ww  w  .  ja v a 2  s. c  o m
private static String transformDocumentToHtmlString(Document document) {
    // Generate a CharSequence from the xml document.
    Transformer transformer = null;
    try {
        transformer = TransformerFactory.newInstance().newTransformer();
    } catch (TransformerConfigurationException e) {
        log.error("Couldn't create a transformer", e);
        throw new RuntimeException("Couldn't create a transformer. This should never happen.", e);
    }
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, "-//W3C//DTD HTML 4.01//EN");
    transformer.setOutputProperty(OutputKeys.METHOD, "html");
    transformer.setOutputProperty(OutputKeys.VERSION, "4.01");

    DOMSource source = new DOMSource(document);
    Writer writer = new StringWriter();
    StreamResult result = new StreamResult(writer);
    try {
        transformer.transform(source, result);
    } catch (TransformerException e) {
        log.error("Couldn't transform", e);
        throw new RuntimeException("Couldn't transform. This should never happen.", e);
    }

    return writer.toString();
}

From source file:mx.itesm.mexadl.metrics.util.Util.java

/**
 * Load the contents of a configuration file into a String.
 * /*from  w w  w.j  a  v  a  2  s . co m*/
 * @param path
 * @return
 * @throws Exception
 */
public static String loadFileContent(final String path) throws Exception {
    String returnValue;
    InputStream inputStream;

    inputStream = Util.class.getClassLoader().getResourceAsStream(path);

    returnValue = null;
    if (inputStream != null) {
        Writer writer = new StringWriter();

        char[] buffer = new char[1024];
        try {
            Reader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
            int n;
            while ((n = reader.read(buffer)) != -1) {
                writer.write(buffer, 0, n);
            }
        } finally {
            inputStream.close();
        }
        returnValue = writer.toString();
    }

    return returnValue;
}

From source file:com.cisco.ca.cstg.pdi.utils.Util.java

/**
 * This method returns a json string converted from myMap
 * //from  ww  w.ja va  2  s  . c om
 * @author padmkris
 * 
 * @param myMap
 *            contains the entry of key value pair for json object
 */
public static String convertMapToJson(Map<String, Object> myMap) {
    Writer strWriter = new StringWriter();
    ObjectMapper mapper = new ObjectMapper();
    String pmDataJSON = "";
    try {
        mapper.writeValue(strWriter, myMap);
        pmDataJSON = strWriter.toString();

    } catch (JsonGenerationException e) {
        LOGGER.error(e.getMessage(), e);
    } catch (JsonMappingException e) {
        LOGGER.error(e.getMessage(), e);
    } catch (IOException e) {
        LOGGER.error(e.getMessage(), e);
    }

    return pmDataJSON;
}

From source file:niclients.main.getni.java

/**
* To convert the InputStream to String we use the
* Reader.read(char[] buffer) method. We iterate until the
* Reader return -1 which means there's no more data to
* read. We use the StringWriter class to produce the string.
* 
* @param is inputStream from where content to be converted is read
*
* @exception throws IOException//  ww  w  . ja  va  2  s  .  co  m
*
* @return converted stream string (null if stream is empty)
*/
public static String convertStreamToString(InputStream is) throws IOException {

    if (is != null) {
        Writer writer = new StringWriter();

        char[] buffer = new char[1024];
        try {
            Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            int n;
            while ((n = reader.read(buffer)) != -1) {
                writer.write(buffer, 0, n);
            }
        } finally {
            is.close();
        }
        return writer.toString();
    } else {
        return "";
    }
}

From source file:cop.raml.processor.RestProcessor.java

private static void saveRamlToTest(@NotNull RestApi api) throws IOException, TemplateException {
    DevUtils.setRestApi(api);// w w w  .  java 2  s .co  m

    Writer out = new StringWriter();
    String template = Config.get().ramlVersion().getTemplate();

    assemble(out, api, template);

    DevUtils.setRaml(out.toString().trim());
}

From source file:com.sample.ecommerce.util.ElasticSearchUtil.java

public static String render(String templateName, Object model) {
    Writer writer = new StringWriter();
    MustacheFactory mf = new DefaultMustacheFactory();
    Mustache mustache = mf.compile(/*  ww w  . j  a  v a  2s .  c  o  m*/
            new StringReader(getContentFromClasspath("/search/templates/" + templateName + ".mustache")),
            "example");
    mustache.execute(writer, model);
    try {
        writer.flush();
    } catch (IOException ex) {
        Logger.getLogger(ElasticSearchUtil.class.getName()).log(Level.SEVERE, null, ex);
    }
    return writer.toString();
}

From source file:de.feanor.yeoldemensa.data.MensaFactory.java

private static String convertStreamToString(InputStream is) throws IOException {
    /*//from  w  w w. java2s  .c o m
     * To convert the InputStream to String we use the Reader.read(char[]
     * buffer) method. We iterate until the Reader return -1 which means
     * there's no more data to read. We use the StringWriter class to
     * produce the string.
     */
    if (is != null) {
        Writer writer = new StringWriter();

        char[] buffer = new char[1024];
        try {
            Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            int n;
            while ((n = reader.read(buffer)) != -1) {
                writer.write(buffer, 0, n);
            }
        } finally {
            is.close();
        }
        return writer.toString();
    }
    return "";
}

From source file:com.tri_voltage.md.io.YoutubeVideoParser.java

private static String getStringFromInputStream(String encoding, InputStream instream)
        throws UnsupportedEncodingException, IOException {
    Writer writer = new StringWriter();

    char[] buffer = new char[1024];
    try {/*from  w  ww  . j av a2  s  .  c  o m*/
        Reader reader = new BufferedReader(new InputStreamReader(instream, encoding));
        int n;
        while ((n = reader.read(buffer)) != -1) {
            writer.write(buffer, 0, n);
        }
    } finally {
        instream.close();
    }
    String result = writer.toString();
    return result;
}

From source file:com.ebay.cloud.cms.metadata.dataloader.MetadataDataLoader.java

private static String convertStreamToString(InputStream is) throws IOException {
    if (is != null) {
        Writer writer = new StringWriter();
        char[] buffer = new char[1024];
        try {//  w w  w .  j ava  2  s .  com
            Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            int n;
            while ((n = reader.read(buffer)) != -1) {
                writer.write(buffer, 0, n);
            }
        } finally {
            is.close();
        }
        return writer.toString();
    } else {
        return "";
    }
}