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

@SuppressWarnings("unchecked")
public static <T> String marshal(T object) throws IOException, JAXBException {
    Class<T> clzz = (Class<T>) object.getClass();
    JAXBContext context;//from   w  w  w  . j  av a 2 s. co m
    context = JAXBContext.newInstance(clzz);
    Marshaller m = context.createMarshaller();
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    m.marshal(object, os);
    return os.toString();
}

From source file:Main.java

public static <T> String toXmlString(T obj, Class<T> type) {
    try {/*from w  w w.  ja v  a  2  s .  c  o  m*/
        JAXBContext jaxbContext = JAXBContext.newInstance(type);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        jaxbContext.createMarshaller().marshal(obj, byteArrayOutputStream);
        return byteArrayOutputStream.toString();
    } catch (JAXBException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static String parse(Document document) {
    try {/*w  w  w.  j  a v  a  2 s  .c  o m*/
        TransformerFactory dbf = TransformerFactory.newInstance();
        Transformer t = dbf.newTransformer();
        t.setOutputProperty("encoding", "utf-8");
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        t.transform(new DOMSource(document), new StreamResult(bos));
        return bos.toString();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

private static String marshal(Object objJAXB) throws Exception {
    ByteArrayOutputStream result = new ByteArrayOutputStream();
    newMarshaller(objJAXB.getClass()).marshal(objJAXB, newWriter(result));
    return result.toString();
}

From source file:Main.java

/**
 * Marshal a jaxb object to obtain the xml format as a string.
 * //from w w  w.  java2  s . c o m
 * @param jabxbObject the jaxb object
 * @param jaxbContextName the contect name where to look to find jaxb classes
 * @param cl : {@link ClassLoader} of the ObjectFactory
 * @return a string representing the jaxb object
 * @throws JAXBException
 */
public static String marshal(Object jabxbObject, String jaxbContextName, ClassLoader cl) throws JAXBException {
    Marshaller marshaller = createMarshaller(jaxbContextName, cl);

    final ByteArrayOutputStream xmlStream = new ByteArrayOutputStream();
    marshaller.marshal(jabxbObject, xmlStream);

    return xmlStream.toString();
}

From source file:Main.java

public static String drain(InputStream inputStream) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    int buffer;/*ww  w.j  a v a2  s  . c om*/
    while ((buffer = inputStream.read()) >= 0) {
        baos.write(buffer);
    }
    return baos.toString();
}

From source file:com.indoqa.maven.wadldoc.transformation.Wadl2HtmlPipelineTest.java

private static Diff createDiff(URL expected, ByteArrayOutputStream actual) throws Exception {
    String string1 = IOUtils.toString(expected.openStream());
    String string2 = actual.toString();

    return new Diff(string1, string2);
}

From source file:Main.java

public static String convertStreamToString(InputStream is) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    int i = is.read();
    while (i != -1) {
        baos.write(i);//from  w w w . j a va2  s  . c  o  m
        i = is.read();
    }
    return baos.toString();
}

From source file:Main.java

public static String soapMessageToString(SOAPMessage message) {
    String result = null;/*  w w  w. j  av  a2s.c  o  m*/
    if (message != null) {
        ByteArrayOutputStream baos = null;
        try {
            baos = new ByteArrayOutputStream();
            message.writeTo(baos);
            result = baos.toString();
        } catch (Exception e) {
        } finally {
            if (baos != null) {
                try {
                    baos.close();
                } catch (IOException ioe) {
                }
            }
        }
    }
    return result;
}

From source file:Main.java

/**
 * Generic method to Validate XML file while marshalling against their schema.
 * /*from  w  w w.ja v  a2  s  . com*/
 * @param context
 * @param schemaFile
 * @param object
 * @return
 * @throws SAXException
 * @throws JAXBException
 */
public static String validateAndMarshallXML(JAXBContext context, String schemaFile, Object object)
        throws SAXException, JAXBException {
    String xmlFormOfBean = null;

    if (context != null && (schemaFile != null && schemaFile.trim().length() > 0)) {
        Marshaller marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

        SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); // thread- safe 
        marshaller.setSchema(sf.newSchema(new File(schemaFile))); // validate jaxb context against schema 
        ByteArrayOutputStream sos = new ByteArrayOutputStream(); // for XML output into string

        marshaller.marshal(object, sos);
        xmlFormOfBean = sos.toString();

    }
    return xmlFormOfBean;
}