Example usage for java.io StringWriter close

List of usage examples for java.io StringWriter close

Introduction

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

Prototype

public void close() throws IOException 

Source Link

Document

Closing a StringWriter has no effect.

Usage

From source file:org.apache.hadoop.hbase.util.JSONMetricUtil.java

/**
 * Returns a subset of mbeans defined by qry.
 * Modeled after {@link JSONBean#dumpRegionServerMetrics()}
 * Example: String qry= "java.lang:type=Memory"
 * @throws MalformedObjectNameException if json have bad format
 * @throws IOException //* w  w  w.ja v  a2  s .  c om*/
 * @return String representation of json array.
 */
public static String dumpBeanToString(String qry) throws MalformedObjectNameException, IOException {
    StringWriter sw = new StringWriter(1024 * 100); // Guess this size
    try (PrintWriter writer = new PrintWriter(sw)) {
        JSONBean dumper = new JSONBean();
        try (JSONBean.Writer jsonBeanWriter = dumper.open(writer)) {
            MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer();
            jsonBeanWriter.write(mbeanServer, new ObjectName(qry), null, false);
        }
    }
    sw.close();
    return sw.toString();
}

From source file:frameworkcontentspeed.Utils.SendEmailAtachament.java

public static void SendEmailSephoraPassedJava(String from, String grupSephora, String subject, String filename)
        throws FileNotFoundException, IOException {

    try {/*  w  w w. j  a v a2  s.co  m*/

        EmailAttachment attachment = new EmailAttachment();
        attachment.setPath(filename);
        attachment.setDisposition(EmailAttachment.ATTACHMENT);

        attachment.setDescription("rezultat TC-uri");
        attachment.setName("rezultat TC-uri");

        MultiPartEmail email = new MultiPartEmail();

        email.setHostName("smtp.gmail.com");
        email.setSmtpPort(465);

        email.setAuthenticator(new DefaultAuthenticator("anda.cristea@contentspeed.ro", "testtest"));
        email.setSSLOnConnect(true);

        email.addTo(grupSephora);

        //email.addBcc(grupSephora);
        email.setFrom(from, "Teste Automate");
        email.setSubject(subject);
        email.setMsg(subject);

        // add the attachment
        //email.attach(attachment);
        StringWriter writer = new StringWriter();
        IOUtils.copy(new FileInputStream(new File(filename)), writer);

        email.setContent(writer.toString(), "text/html");
        email.attach(attachment);

        email.send();
        writer.close();
    } catch (Exception ex) {
        System.out.println("eroare trimitere email-uri");
        ex.printStackTrace();

    }

}

From source file:Main.java

public static String toXml(Class className, Object object) {
    String strXml = "";
    StringWriter writer = null;
    try {/*from ww  w . j ava 2  s  .c om*/
        writer = new StringWriter();
        JAXBContext context = JAXBContext.newInstance(className);
        Marshaller marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(object, writer);
        strXml = writer.toString();
        writer.flush();

        strXml = strXml.replace("&lt;", "<");
        strXml = strXml.replace("&gt;", ">");
    } catch (Exception e) {
    } finally {
        if (writer != null) {
            try {
                writer.close();
                writer = null;
            } catch (Exception e) {
            }
        }
    }
    return strXml;
}

From source file:frameworkcontentspeed.Utils.SendEmailAtachament.java

public static void SendEmail(String from, String to1, String to2, String subject, String filename)
        throws FileNotFoundException, IOException {

    try {/*from w  ww . j  a v  a  2s . c  o  m*/

        EmailAttachment attachment = new EmailAttachment();
        attachment.setPath(filename);
        attachment.setDisposition(EmailAttachment.ATTACHMENT);

        attachment.setDescription("rezultat TC-uri");
        attachment.setName("rezultat TC-uri");

        MultiPartEmail email = new MultiPartEmail();

        email.setHostName("smtp.gmail.com");
        email.setSmtpPort(465);
        //email.setAuthenticator(new DefaultAuthenticator("test@contentspeed.ro", "andaanda"));
        email.setAuthenticator(new DefaultAuthenticator("anda.cristea@contentspeed.ro", "anda.cristea"));
        email.setSSLOnConnect(true);

        email.addBcc(to1);
        email.addBcc(to2);
        email.setFrom(from, "Teste Automate");
        email.setSubject(subject);
        email.setMsg(subject);

        // add the attachment
        //email.attach(attachment);
        StringWriter writer = new StringWriter();
        IOUtils.copy(new FileInputStream(new File(filename)), writer);

        email.setContent(writer.toString(), "text/html");
        email.attach(attachment);

        // send the email
        email.send();
        writer.close();
    } catch (Exception ex) {
        System.out.println("eroare trimitere email-uri");
        System.out.println(ex.getMessage());

    }

}

From source file:org.glite.slcs.pki.bouncycastle.Codec.java

/**
 * Returns the PEM encoded String of the X509 certificate. OpenSSL
 * compatible./*from  ww w.ja va  2 s . c  om*/
 * 
 * @param cert
 *            The X509 certificate.
 * @return The PEM encoded String.
 */
static public String getPEMEncoded(X509Certificate cert) {
    StringWriter sw = new StringWriter();
    PEMWriter pem = new PEMWriter(sw);
    try {
        pem.writeObject(cert);
    } catch (IOException e) {
        LOG.warn("Failed to write PKCS7 in PEM format", e);
        return null;
    } finally {
        try {
            pem.close();
            sw.close();
        } catch (IOException e) {
            // ignored
            LOG.warn(e);
        }
    }
    return sw.toString();
}

From source file:com.glaf.core.util.IOUtils.java

/**
 * read string.//from ww  w  .  j a  v a2 s . c o m
 * 
 * @param reader
 *            Reader instance.
 * @return String.
 * @throws IOException
 */
public static String read(Reader reader) throws IOException {
    StringWriter writer = new StringWriter();
    try {
        write(reader, writer);
        return writer.getBuffer().toString();
    } finally {
        writer.close();
    }
}

From source file:org.rhq.enterprise.server.xmlschema.ConfigurationInstanceDescriptorUtilTest.java

private static void logInstance(String message, ConfigurationInstanceDescriptor instance)
        throws JAXBException, IOException {
    StringWriter wrt = new StringWriter();
    try {//from   w ww  . j a v a2  s .c  o  m
        CONFIGURATION_INSTANCE_MARSHALLER.marshal(StandaloneConfigurationInstance.createFrom(instance), wrt);
        LOG.debug(message + "\n" + wrt.toString());
    } finally {
        wrt.close();
    }
}

From source file:com.github.jsonj.tools.JsonSerializer.java

/**
 * @param json/*w w  w  .j a va 2  s . co m*/
 *            a {@link JsonElement}
 * @param pretty
 *            if true, a properly indented version of the json is returned
 * @return string representation of the json
 */
public static String serialize(final JsonElement json, final boolean pretty) {
    StringWriter sw = new StringWriter();
    if (pretty) {
        try {
            serialize(sw, json, pretty);
        } catch (IOException e) {
            throw new IllegalStateException("cannot serialize json to a string", e);
        } finally {
            try {
                sw.close();
            } catch (IOException e) {
                throw new IllegalStateException("cannot serialize json to a string", e);
            }
        }
        return sw.getBuffer().toString();
    } else {
        try {
            json.serialize(sw);
            return sw.getBuffer().toString();
        } catch (IOException e) {
            throw new IllegalStateException("cannot serialize json to a string", e);
        }
    }
}

From source file:net.sf.jasperreports.util.CastorUtil.java

/**
 * @deprecated Replaced by {@link #writeToString(Object)}.
 */// w ww  .  jav a2s. c  o m
public static String write(Object object, Mapping mapping) {
    StringWriter writer = new StringWriter();

    try {
        write(object, mapping, writer);
    } finally {
        try {
            writer.close();
        } catch (IOException e) {
        }
    }

    return writer.toString();
}

From source file:net.sf.jasperreports.util.CastorUtil.java

/**
 * @deprecated Replaced by {@link #writeToString(Object)}.
 *//*from w  w  w  .j  a v a  2 s  .  c o  m*/
public static String write(Object object, String mappingFile) {
    StringWriter writer = new StringWriter();

    try {
        write(object, mappingFile, writer);
    } finally {
        try {
            writer.close();
        } catch (IOException e) {
        }
    }

    return writer.toString();
}