Example usage for java.io StringWriter toString

List of usage examples for java.io StringWriter toString

Introduction

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

Prototype

public String toString() 

Source Link

Document

Return the buffer's current value as a string.

Usage

From source file:Main.java

public static String getStackTrace(Throwable exception) {
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    exception.printStackTrace(pw);//from   www.  j ava2s. co  m
    return sw.toString();

}

From source file:Main.java

public static String getStackTraceString(Throwable t) {
    String value;// w w w. j  av  a 2  s.  c o  m
    if (t == null) {
        value = "";
    } else {
        final StringWriter stackTrace = new StringWriter();
        t.printStackTrace(new PrintWriter(stackTrace));
        value = stackTrace.toString();
    }
    return value;
}

From source file:Main.java

/**
 * Serializes any Object to XML/*from   www .  jav a  2s  . c  o  m*/
 *
 * @param object Object to serialize
 * @return XML serialization of the supplied object
 */
public static String toXmlJaxb(Object object) {
    String result = "";
    try {
        JAXBContext jaxbContext = JAXBContext.newInstance(object.getClass());
        Marshaller marshaller = jaxbContext.createMarshaller();
        StringWriter writer = new StringWriter();
        marshaller.marshal(object, writer);
        result = writer.toString();
    } catch (JAXBException e) {
        throw new RuntimeException(e);
    }
    return result;
}

From source file:net.fizzl.redditengine.data.FlairListing.java

public static FlairListing fromInputStream(InputStream is) throws IOException {
    StringWriter writer = new StringWriter();
    IOUtils.copy(is, writer, "UTF-8");
    return fromString(writer.toString());
}

From source file:net.fizzl.redditengine.data.ModlogListing.java

public static ModlogListing fromInputStream(InputStream is) throws IOException {
    StringWriter writer = new StringWriter();
    IOUtils.copy(is, writer, "UTF-8");
    return fromString(writer.toString());
}

From source file:Main.java

public static String XMLToString(Document doc) throws TransformerException {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    StringWriter writer = new StringWriter();
    transformer.transform(new DOMSource(doc), new StreamResult(writer));
    return writer.toString();
}

From source file:Main.java

/**
 * Returns a string representation of {@code node}. This method should only
 * be used for debugging purposes, at it creates a new transformer factory
 * and transformer upon each call./* w  ww  .ja v  a  2  s.c o m*/
 * 
 * @param n
 *            the node
 * @return a string representation of {@code node}
 */
public static String toString(Node n) {
    try {
        Transformer t = TransformerFactory.newInstance().newTransformer();
        StringWriter sw = new StringWriter();
        t.transform(new DOMSource(n), new StreamResult(sw));
        return sw.toString();
    } catch (TransformerException e) {
        return null;
    }
}

From source file:controllers.MyController.java

protected static String json(Object obj) throws Exception {
    StringWriter w = new StringWriter();
    mapper.writeValue(w, obj);/*from  w w w .  ja  v a2s .c o  m*/
    String result = w.toString();
    return result;
}

From source file:net.fizzl.redditengine.data.MessageListing.java

public static MessageListing fromInputStream(InputStream is) throws IOException {
    StringWriter writer = new StringWriter();
    IOUtils.copy(is, writer, "UTF-8");
    return fromString(writer.toString());
}

From source file:Main.java

/** 
 * Serialized a node to a string. XML declarations will be omitted. 
 * // w  w  w  .  ja va  2 s . com
 * @param node The node to be serialized.
 * 
 * @return The serialized node.
 * 
 * @throws TransformerFactoryConfigurationError
 * @throws TransformerException
 */
public static String nodeToString(Node node) throws TransformerFactoryConfigurationError, TransformerException {
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty("omit-xml-declaration", "yes");
    StringWriter sw = new StringWriter();
    transformer.transform(new DOMSource(node), new StreamResult(sw));
    return sw.toString();
}