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:gdv.xport.DatenpaketTest.java

private void checkExportWith(final String content) throws IOException {
    StringWriter swriter = new StringWriter(content.length());
    datenpaket.export(swriter);/*from   ww  w  .j a  va  2s  . c  om*/
    swriter.close();
    assertLines(content, swriter.toString());
}

From source file:org.bonitasoft.engine.api.internal.servlet.HttpAPIServletCall.java

private String toXML(final Object object, final XStream xstream) {
    final StringWriter stringWriter = new StringWriter();
    ObjectOutputStream out;//from  www.j  a  v a2s  .com
    try {
        out = xstream.createObjectOutputStream(stringWriter);
        try {
            out.writeObject(object);
        } catch (final IOException e) {
            throw new BonitaRuntimeException("unable to serialize object " + object.toString(), e);
        } finally {
            stringWriter.close();
            out.close();
        }
    } catch (final IOException e1) {
        throw new BonitaRuntimeException("unable to serialize object " + object.toString(), e1);
    }
    return stringWriter.toString();
}

From source file:org.apache.hadoop.lib.service.instrumentation.TestInstrumentationService.java

@Test
public void timer() throws Exception {
    InstrumentationService.Timer timer = new InstrumentationService.Timer(2);
    InstrumentationService.Cron cron = new InstrumentationService.Cron();

    long ownStart;
    long ownEnd;// w  ww  . j  a  v a  2s .c  o m
    long totalStart;
    long totalEnd;
    long ownDelta;
    long totalDelta;
    long avgTotal;
    long avgOwn;

    cron.start();
    ownStart = Time.now();
    totalStart = ownStart;
    ownDelta = 0;
    sleep(100);

    cron.stop();
    ownEnd = Time.now();
    ownDelta += ownEnd - ownStart;
    sleep(100);

    cron.start();
    ownStart = Time.now();
    sleep(100);

    cron.stop();
    ownEnd = Time.now();
    ownDelta += ownEnd - ownStart;
    totalEnd = ownEnd;
    totalDelta = totalEnd - totalStart;

    avgTotal = totalDelta;
    avgOwn = ownDelta;

    timer.addCron(cron);
    long[] values = timer.getValues();
    assertEquals(values[InstrumentationService.Timer.LAST_TOTAL], totalDelta, 20);
    assertEquals(values[InstrumentationService.Timer.LAST_OWN], ownDelta, 20);
    assertEquals(values[InstrumentationService.Timer.AVG_TOTAL], avgTotal, 20);
    assertEquals(values[InstrumentationService.Timer.AVG_OWN], avgOwn, 20);

    cron = new InstrumentationService.Cron();

    cron.start();
    ownStart = Time.now();
    totalStart = ownStart;
    ownDelta = 0;
    sleep(200);

    cron.stop();
    ownEnd = Time.now();
    ownDelta += ownEnd - ownStart;
    sleep(200);

    cron.start();
    ownStart = Time.now();
    sleep(200);

    cron.stop();
    ownEnd = Time.now();
    ownDelta += ownEnd - ownStart;
    totalEnd = ownEnd;
    totalDelta = totalEnd - totalStart;

    avgTotal = (avgTotal * 1 + totalDelta) / 2;
    avgOwn = (avgOwn * 1 + ownDelta) / 2;

    timer.addCron(cron);
    values = timer.getValues();
    assertEquals(values[InstrumentationService.Timer.LAST_TOTAL], totalDelta, 20);
    assertEquals(values[InstrumentationService.Timer.LAST_OWN], ownDelta, 20);
    assertEquals(values[InstrumentationService.Timer.AVG_TOTAL], avgTotal, 20);
    assertEquals(values[InstrumentationService.Timer.AVG_OWN], avgOwn, 20);

    avgTotal = totalDelta;
    avgOwn = ownDelta;

    cron = new InstrumentationService.Cron();

    cron.start();
    ownStart = Time.now();
    totalStart = ownStart;
    ownDelta = 0;
    sleep(300);

    cron.stop();
    ownEnd = Time.now();
    ownDelta += ownEnd - ownStart;
    sleep(300);

    cron.start();
    ownStart = Time.now();
    sleep(300);

    cron.stop();
    ownEnd = Time.now();
    ownDelta += ownEnd - ownStart;
    totalEnd = ownEnd;
    totalDelta = totalEnd - totalStart;

    avgTotal = (avgTotal * 1 + totalDelta) / 2;
    avgOwn = (avgOwn * 1 + ownDelta) / 2;

    cron.stop();
    timer.addCron(cron);
    values = timer.getValues();
    assertEquals(values[InstrumentationService.Timer.LAST_TOTAL], totalDelta, 20);
    assertEquals(values[InstrumentationService.Timer.LAST_OWN], ownDelta, 20);
    assertEquals(values[InstrumentationService.Timer.AVG_TOTAL], avgTotal, 20);
    assertEquals(values[InstrumentationService.Timer.AVG_OWN], avgOwn, 20);

    JSONObject json = (JSONObject) new JSONParser().parse(timer.toJSONString());
    assertEquals(json.size(), 4);
    assertEquals(json.get("lastTotal"), values[InstrumentationService.Timer.LAST_TOTAL]);
    assertEquals(json.get("lastOwn"), values[InstrumentationService.Timer.LAST_OWN]);
    assertEquals(json.get("avgTotal"), values[InstrumentationService.Timer.AVG_TOTAL]);
    assertEquals(json.get("avgOwn"), values[InstrumentationService.Timer.AVG_OWN]);

    StringWriter writer = new StringWriter();
    timer.writeJSONString(writer);
    writer.close();
    json = (JSONObject) new JSONParser().parse(writer.toString());
    assertEquals(json.size(), 4);
    assertEquals(json.get("lastTotal"), values[InstrumentationService.Timer.LAST_TOTAL]);
    assertEquals(json.get("lastOwn"), values[InstrumentationService.Timer.LAST_OWN]);
    assertEquals(json.get("avgTotal"), values[InstrumentationService.Timer.AVG_TOTAL]);
    assertEquals(json.get("avgOwn"), values[InstrumentationService.Timer.AVG_OWN]);
}

From source file:com.webcohesion.enunciate.modules.javascript_client.JavaScriptClientModule.java

/**
 * Processes the specified template with the given model.
 *
 * @param templateURL The template URL.//from w  w w  .  j a va  2 s  .c  o m
 * @param model       The root model.
 */
public String processTemplate(URL templateURL, Object model) throws IOException, TemplateException {
    debug("Processing template %s.", templateURL);
    Configuration configuration = new Configuration(Configuration.VERSION_2_3_22);

    configuration.setTemplateLoader(new URLTemplateLoader() {
        protected URL getURL(String name) {
            try {
                return new URL(name);
            } catch (MalformedURLException e) {
                return null;
            }
        }
    });

    configuration.setTemplateExceptionHandler(new TemplateExceptionHandler() {
        public void handleTemplateException(TemplateException templateException, Environment environment,
                Writer writer) throws TemplateException {
            throw templateException;
        }
    });

    configuration.setLocalizedLookup(false);
    configuration.setDefaultEncoding("UTF-8");
    configuration.setObjectWrapper(new JavaScriptClientObjectWrapper());
    Template template = configuration.getTemplate(templateURL.toString());
    StringWriter unhandledOutput = new StringWriter();
    template.process(model, unhandledOutput);
    unhandledOutput.close();
    return unhandledOutput.toString();
}

From source file:com.webcohesion.enunciate.modules.php_json_client.PHPJSONClientModule.java

/**
 * Processes the specified template with the given model.
 *
 * @param templateURL The template URL.//  w w  w  .  j a  v a2 s  . c o  m
 * @param model       The root model.
 */
public String processTemplate(URL templateURL, Object model) throws IOException, TemplateException {
    debug("Processing template %s.", templateURL);
    Configuration configuration = new Configuration(Configuration.VERSION_2_3_22);

    configuration.setTemplateLoader(new URLTemplateLoader() {
        protected URL getURL(String name) {
            try {
                return new URL(name);
            } catch (MalformedURLException e) {
                return null;
            }
        }
    });

    configuration.setTemplateExceptionHandler(new TemplateExceptionHandler() {
        public void handleTemplateException(TemplateException templateException, Environment environment,
                Writer writer) throws TemplateException {
            throw templateException;
        }
    });

    configuration.setLocalizedLookup(false);
    configuration.setDefaultEncoding("UTF-8");
    configuration.setObjectWrapper(new PHPJSONClientObjectWrapper());
    Template template = configuration.getTemplate(templateURL.toString());
    StringWriter unhandledOutput = new StringWriter();
    template.process(model, unhandledOutput);
    unhandledOutput.close();
    return unhandledOutput.toString();
}

From source file:org.ejbca.ui.web.protocol.StoreServletBase.java

private void printInfo(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    final StringWriter sw = new StringWriter();
    final PrintWriter pw = new HtmlPrintWriter(sw);
    printInfo(this.certCache.getRootCertificates(), "", pw, req.getRequestURL().toString());
    pw.flush();//from   ww  w .j  av  a2s .co  m
    pw.close();
    sw.flush();
    returnInfoPage(resp, sw.toString());
    sw.close();
}

From source file:com.webcohesion.enunciate.modules.php_xml_client.PHPXMLClientModule.java

/**
 * Processes the specified template with the given model.
 *
 * @param templateURL The template URL./*www. ja  v  a2  s .c  om*/
 * @param model       The root model.
 */
public String processTemplate(URL templateURL, Object model) throws IOException, TemplateException {
    debug("Processing template %s.", templateURL);
    Configuration configuration = new Configuration(Configuration.VERSION_2_3_22);

    configuration.setTemplateLoader(new URLTemplateLoader() {
        protected URL getURL(String name) {
            try {
                return new URL(name);
            } catch (MalformedURLException e) {
                return null;
            }
        }
    });

    configuration.setTemplateExceptionHandler(new TemplateExceptionHandler() {
        public void handleTemplateException(TemplateException templateException, Environment environment,
                Writer writer) throws TemplateException {
            throw templateException;
        }
    });

    configuration.setLocalizedLookup(false);
    configuration.setDefaultEncoding("UTF-8");
    configuration.setObjectWrapper(new PHPXMLClientObjectWrapper());
    Template template = configuration.getTemplate(templateURL.toString());
    StringWriter unhandledOutput = new StringWriter();
    template.process(model, unhandledOutput);
    unhandledOutput.close();
    return unhandledOutput.toString();
}

From source file:matteroverdrive.guide.MOGuideEntry.java

@SideOnly(Side.CLIENT)
public String getDescription() {
    String info;//  w  w  w.  j av  a  2  s. com
    StringWriter writer = new StringWriter();
    InputStream stream;

    try {

        stream = Minecraft.getMinecraft().getResourceManager().getResource(
                new ResourceLocation(getDescriptionPath(Minecraft.getMinecraft().gameSettings.language)))
                .getInputStream();
        info = IOUtils.toString(stream, "UTF-8");
        writer.close();
        stream.close();
    } catch (IOException e) {
        MatterOverdrive.log.warn("Language text %s for entry %s not found.",
                Minecraft.getMinecraft().gameSettings.language, name);
        try {
            stream = Minecraft.getMinecraft().getResourceManager()
                    .getResource(new ResourceLocation(getDescriptionPath("en_US"))).getInputStream();
            info = IOUtils.toString(stream, "UTF-8");
            writer.close();
            stream.close();
        } catch (IOException e1) {
            info = "There is no default text entry for " + name;
        }
    }

    return info;
}

From source file:com.redhat.jenkins.plugins.ci.integration.FedMsgMessagingPluginIntegrationTest.java

private String stringFrom(Process proc) throws InterruptedException, IOException {
    assertThat(proc.waitFor(), is(equalTo(0)));
    StringWriter writer = new StringWriter();
    IOUtils.copy(proc.getInputStream(), writer);
    String string = writer.toString();
    writer.close();
    return string;
}

From source file:com.webcohesion.enunciate.EnunciateConfiguration.java

public String readGeneratedCodeLicenseFile() {
    License license = getGeneratedCodeLicense();
    String filePath = license == null ? null : license.getFile();
    if (filePath == null) {
        return null;
    }/*  w  w w .  ja va2s.c  o  m*/

    File file = resolveFile(filePath);
    try {
        FileReader reader = new FileReader(file);
        StringWriter writer = new StringWriter();
        char[] chars = new char[100];
        int read = reader.read(chars);
        while (read >= 0) {
            writer.write(chars, 0, read);
        }
        reader.close();
        writer.close();
        return writer.toString();
    } catch (IOException e) {
        throw new EnunciateException(e);
    }
}