Example usage for java.io OutputStream toString

List of usage examples for java.io OutputStream toString

Introduction

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

Prototype

public String toString() 

Source Link

Document

Returns a string representation of the object.

Usage

From source file:Main.java

public static String parseBeanToXmlStringByJAXB(Object bean, Class clase) throws Exception {
    JAXBContext jc = JAXBContext.newInstance(clase);
    Marshaller marshaller = jc.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, false);
    JAXBElement<Object> rootElement = new JAXBElement<Object>(new QName(clase.getSimpleName()), clase, bean);

    OutputStream output = new OutputStream() {

        private StringBuilder string = new StringBuilder();

        public void write(int b) throws IOException {
            this.string.append((char) b);
        }// www  .  j a  v  a  2  s .  c  o  m

        //Netbeans IDE automatically overrides this toString()
        public String toString() {
            return this.string.toString();
        }
    };

    marshaller.marshal(rootElement, output);

    return output.toString();

}

From source file:eu.europa.ec.markt.dss.validation102853.ValidationResourceManager.java

/**
 * This method saves the data in the output stream to a file.
 *
 * @param diagnosticDataFileName/*from w w  w.  j  ava  2 s.co  m*/
 * @param outputStream
 * @throws IOException
 */
protected static void saveToFile(final String diagnosticDataFileName, final OutputStream outputStream)
        throws IOException {

    FileWriter file = null;
    try {

        file = new FileWriter(diagnosticDataFileName);
        file.write(outputStream.toString());
    } finally {

        if (file != null) {

            file.close();
        }
    }
}

From source file:org.xsystem.utils.Auxilary.java

public static String throwableToString(Throwable t) {
    OutputStream out = new ByteArrayOutputStream();
    try {//from   w ww  . j a v  a  2 s . c om
        PrintStream strm = new PrintStream(out);
        try {
            t.printStackTrace(strm);
            String rez = out.toString();//strm.toString();
            return rez;
        } finally {
            try {
                strm.close();
            } catch (Exception ex) {
            }
            ;
        }
    } finally {
        try {
            out.close();
        } catch (Exception ex) {
        }
        ;
    }
}

From source file:org.jahia.utils.PomUtils.java

/**
 * Serializes Maven project model into a specified file.
 * /*from w  ww  .  j  av  a  2 s  .co m*/
 * @param model
 *            the Maven project model to serialize
 * @param targetPomXmlFile
 *            the target file to write the provided model into
 * @throws IOException
 *             in case of a serialization error
 */
public static void write(Model model, File targetPomXmlFile) throws IOException {
    String copyright = null;
    try {
        DocumentBuilder docBuilder = JahiaDocumentBuilderFactory.newInstance().newDocumentBuilder();
        Document doc = docBuilder.parse(targetPomXmlFile);
        Node firstChild = doc.getFirstChild();
        if (firstChild.getNodeType() == Node.COMMENT_NODE) {
            copyright = firstChild.getTextContent();
        }
    } catch (Exception e) {
        logger.warn("Failed to read pom.xml copyright", e);
    }

    MavenXpp3Writer xpp3Writer = new MavenXpp3Writer();
    OutputStream os = null;
    String pomContent;
    try {
        os = new ByteArrayOutputStream();
        xpp3Writer.write(os, model);
        pomContent = os.toString();
    } finally {
        IOUtils.closeQuietly(os);
    }

    if (copyright != null) {
        int i = pomContent.indexOf("<project");
        pomContent = pomContent.substring(0, i) + "<!--" + copyright + "-->\n" + pomContent.substring(i);
    }

    org.apache.commons.io.FileUtils.write(targetPomXmlFile, pomContent);
}

From source file:pt.webdetails.cpf.InterPluginComms.java

public static String callPlugin(IPentahoSession userSession, IContentGenerator contentGenerator,
        OutputStream outputStream, Map<String, IParameterProvider> paramProvider) {
    IOutputHandler outputHandler = new SimpleOutputHandler(outputStream, false);
    try {/*from ww w  . j a  v  a 2  s . co  m*/
        contentGenerator.setSession(userSession);
        contentGenerator.setOutputHandler(outputHandler);
        contentGenerator.setParameterProviders(paramProvider);
        contentGenerator.createContent();
        return outputStream.toString();
    } catch (Exception e) {
        logger.error("Failed to execute call to plugin: " + e.toString());
        return null;
    }
}

From source file:org.giavacms.base.common.util.HtmlUtils.java

public static String normalizeHtml(String code) {
    if (code == null) {
        code = "";
    }/*from w  w  w . j a  v  a2s .c  o  m*/
    Tidy tidy = new Tidy();
    tidy.setXHTML(true);
    tidy.setTidyMark(false);
    tidy.setDocType("omit");
    tidy.setPrintBodyOnly(true);
    tidy.setInputEncoding("UTF-8");
    tidy.setShowErrors(0);
    tidy.setShowWarnings(false);
    tidy.setIndentContent(true);
    InputStream is;
    String content = "";
    OutputStream arg1 = new ByteArrayOutputStream();
    try {
        is = new ByteArrayInputStream(code.getBytes("UTF-8"));

        tidy.parse(is, arg1);
        // logger.info("*****************PRIMA: ");
        // logger.info(arg1.toString());
        // logger.info("*******************DOPO: ");
        // StringEscapeUtils.unescapeHtml(arg0)
        content = StringEscapeUtils.unescapeHtml(arg1.toString());
        content = handleAmpersand(content);
        // logger.info(content);

    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return content;
}

From source file:Main.java

public static String getResponseInFile(String address, File file) throws IOException {
    String dataString = "";

    InputStream inputStream = null;
    OutputStream outputStream = null;
    try {//from   w  ww  .j av a  2  s  .c  om
        URL url = new URL(address);
        URLConnection connection = url.openConnection();

        inputStream = connection.getInputStream();

        byte[] data;
        if (inputStream.available() > BUFFER_SIZE) {
            data = new byte[BUFFER_SIZE];
        } else {
            data = new byte[inputStream.available()];
        }

        int dataCount;
        while ((dataCount = inputStream.read(data)) > 0) {
            outputStream.write(data, 0, dataCount);
        }

        OutputStream output = new OutputStream() {
            private StringBuilder string = new StringBuilder();

            @Override
            public void write(int b) throws IOException {
                this.string.append((char) b);
            }

            //Netbeans IDE automatically overrides this toString()
            public String toString() {
                return this.string.toString();
            }
        };
        output.write(data);

        dataString = output.toString();

    } finally {
        try {
            inputStream.close();
        } catch (Exception e) {
        }
        try {
            outputStream.close();
        } catch (Exception e) {
        }
    }

    return dataString;
}

From source file:org.tizzit.util.XercesHelper.java

public static Node html2node(String html) {
    //Import HTML and convert it to XHTML
    Tidy myTidy = new Tidy();
    myTidy.setQuoteAmpersand(true);/*from  w  ww  . j  ava  2 s  . c om*/
    myTidy.setQuoteNbsp(true);
    //myTidy.setQuoteMarks(true);
    myTidy.setXmlOut(true);
    //myTidy.setXmlTags(true);
    myTidy.setCharEncoding(Configuration.ISO2022);
    myTidy.setShowWarnings(false);
    myTidy.setRawOut(false);
    myTidy.setQuiet(true);
    myTidy.setNumEntities(false);
    InputStream in = new ByteArrayInputStream(html.getBytes());
    OutputStream outStream = new ByteArrayOutputStream();
    myTidy.parseDOM(in, outStream);
    String strOut = outStream.toString();

    Node nde = null;
    try {
        Document htmlDoc = XercesHelper.string2Dom(strOut);
        nde = XercesHelper.findNode(htmlDoc, "//body");
    } catch (Exception exe) {
    }
    return nde;
}

From source file:com.valco.utility.FacturasUtility.java

private static byte[] getBytesCadenaFirmada(java.security.PrivateKey pk, OutputStream output)
        throws NoSuchAlgorithmException, InvalidKeyException, SignatureException, UnsupportedEncodingException {
    Signature firma = Signature.getInstance("SHA1withRSA");
    firma.initSign(pk);/*  w w  w.  ja  v  a2 s.com*/
    firma.update(output.toString().getBytes("UTF-8"));
    byte[] cadenaFirmada = firma.sign();
    return cadenaFirmada;
}

From source file:com.stimulus.archiva.extraction.MessageExtraction.java

private static String getTextContent(Part p) throws IOException, MessagingException {
    try {/*from w w w.  j a va2 s.  c  om*/
        return (String) p.getContent();
    } catch (UnsupportedEncodingException e) {
        OutputStream os = new ByteArrayOutputStream();
        p.writeTo(os);
        String raw = os.toString();
        os.close();

        //cp932 -> Windows-31J
        raw = raw.replaceAll("cp932", "Windows-31J");

        InputStream is = new ByteArrayInputStream(raw.getBytes());
        Part newPart = new MimeBodyPart(is);
        is.close();

        return (String) newPart.getContent();
    }
}