Example usage for java.io ByteArrayOutputStream toString

List of usage examples for java.io ByteArrayOutputStream toString

Introduction

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

Prototype

public synchronized String toString() 

Source Link

Document

Converts the buffer's contents into a string decoding bytes using the platform's default character set.

Usage

From source file:Main.java

/**
 * This method takes an XML Document, and converts it to a String for you.
 * @param doc The XML Document to be converted to a String.
 * @return The XML String that represents the Document.
 * @throws TransformerConfigurationException
 * @throws TransformerException//from  w  w  w  .j a  v a 2 s. co m
 *
 * @author <a href='mailto:intere@gmail.com'>Eric Internicola</a>
 */
public static String toString(Document doc) throws TransformerConfigurationException, TransformerException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    writeDocument(doc, out);

    return out.toString();
}

From source file:Main.java

public static String toString(Node node, boolean formatted)
        throws ClassCastException, ClassNotFoundException, InstantiationException, IllegalAccessException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    writeNode(node, baos, formatted);/* w w  w  .jav a 2 s  .c o m*/
    return (baos.toString());
}

From source file:Main.java

public static String toXml(Object o) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    XMLEncoder e = new XMLEncoder(baos);
    e.writeObject(o);/*from ww  w  . ja  v a 2  s.  c  om*/
    e.close();
    return baos.toString();
}

From source file:Main.java

public static <T> String serialize(T object) throws JAXBException {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    serialize(object, outputStream);/* w  w  w .  j  a va  2  s  .  c  o  m*/
    return outputStream.toString();
}

From source file:Main.java

/** Converts a stack trace to a string */
public static String stackTraceToString(Exception e) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PrintStream ps = new PrintStream(baos);
    e.printStackTrace(ps);//w w w.  j  av a  2s  .c  om
    return baos.toString();
}

From source file:Main.java

public static String nodeAsString(Node node) {
    String nodeStr = "";
    TransformerFactory tff = TransformerFactory.newInstance();
    try {//from  www .jav a 2  s  . com
        Transformer tf = tff.newTransformer();
        tf.setOutputProperty("encoding", "UTF-8");

        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        tf.transform(new DOMSource(node), new StreamResult(bos));
        return bos.toString();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return nodeStr;
}

From source file:Main.java

public static String readStream(InputStream in) throws IOException {

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    int i;/*from  w w  w  .  j ava  2 s .  c om*/
    while ((i = in.read()) != -1) {
        baos.write(i);
    }
    return baos.toString();
}

From source file:Main.java

/**
 * Writes the given DOM to a string./*from   ww w  .j a  va 2  s  .c  o m*/
 * 
 * @return The string to which the DOM was written.
 */
public static String writeXML(final Document doc) {
    final ByteArrayOutputStream os = new ByteArrayOutputStream();
    writeXML(os, doc);
    return os.toString();
}

From source file:Main.java

public static String inputStream2String(InputStream is) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    int i = -1;//from   w ww . j av a  2 s .c  om
    while ((i = is.read()) != -1) {
        baos.write(i);
    }
    return baos.toString();
}

From source file:Main.java

/**
 * Returns the output of printStackTrace as a String.
 *
 * @param e A Throwable./*from  w  w  w .  ja v a2s .  c  o  m*/
 * @return A String.
 */
public static final String stackTrace(Throwable e) {
    String foo = null;
    try {
        // And show the Error Screen.
        ByteArrayOutputStream ostr = new ByteArrayOutputStream();
        e.printStackTrace(new PrintWriter(ostr, true));
        foo = ostr.toString();
    } catch (Exception f) {
        // Do nothing.
    }
    return foo;
}