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

/**
 * Extract the full stacktrace from {@code t} into a String for logging
 * @param t an exception//from w  w w.j  a  va2s.  com
 * @return a string representation of the full stack trace
 */
public static String sprintFullStack(final Throwable t) {
    if (t == null) {
        return "";
    }

    StringWriter writer = new StringWriter();
    t.printStackTrace(new PrintWriter(writer));
    return writer.toString();
}

From source file:Main.java

public static String printDocument(Node doc) throws IOException, TransformerException {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
    StringWriter sw = new StringWriter();
    transformer.transform(new DOMSource(doc), new StreamResult(sw));
    return sw.toString();
}

From source file:AIR.Common.Json.JsonHelper.java

public static <T> String serialize(T obj) throws JsonGenerationException, JsonMappingException, IOException {
    if (obj == null)
        return null;

    ObjectMapper mapper = new ObjectMapper();

    String json = null;/*from  w  w w.j  av a  2  s . c  om*/

    StringWriter sw = new StringWriter();
    mapper.writeValue(sw, obj);

    json = sw.toString();
    sw.close();

    return json;
}

From source file:Main.java

/**
 * Returns the content of the given node as string
 *//*  w w w  .ja v  a2s  . c om*/
public static String toString(final Node node) {
    final StringWriter out = new StringWriter();
    write(node, new StreamResult(out));
    return out.toString();
}

From source file:Main.java

public static String marshal(JAXBContext context, Object object, Map<String, Object> properties)
        throws JAXBException {
    StringWriter writer = new StringWriter();
    marshal(context, object, writer, properties);
    return writer.toString();
}

From source file:Main.java

public static String printDocument(Node doc) throws TransformerException {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
    StringWriter sw = new StringWriter();
    transformer.transform(new DOMSource(doc), new StreamResult(sw));
    return sw.toString();
}

From source file:Main.java

public static void print(Node dom) {
    try {//ww w  .j  av  a2s.  c  o m
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer t = factory.newTransformer();
        StringWriter sw = new StringWriter();
        t.transform(new DOMSource(dom), new StreamResult(sw));
        System.out.println(sw.toString());
    } catch (Exception ex) {
        System.out.println("Could not print XML document");
    }
}

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

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

From source file:lohbihler.process.ProcessRunner.java

private static String toString(final InputStream in) throws IOException {
    final StringWriter output = new StringWriter();
    IOUtils.copy(in, output, ASCII);/*from  w w w . j  a v  a2 s  .  co  m*/
    return output.toString();
}

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();/* w w w  . jav a  2  s .  co  m*/
    return writer.toString();
}