Example usage for java.io Writer close

List of usage examples for java.io Writer close

Introduction

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

Prototype

public abstract void close() throws IOException;

Source Link

Document

Closes the stream, flushing it first.

Usage

From source file:br.com.pontocontrol.controleponto.controller.impl.ArquivoController.java

@Override
public boolean salvarArquivo(Writer writer) {
    try {/*  w  w  w .j av a2s.  c  o  m*/
        writer.flush();
        writer.close();
        return true;
    } catch (IOException ex) {
        LOG.log(Level.SEVERE, "Erro ao fechar escritor de arquivos", ex);
        return false;
    }
}

From source file:net.sf.jasperreports.engine.fonts.SimpleFontExtensionHelper.java

/**
 *
 *///from w  ww  .  j a  v  a 2  s. co m
public static void writeFontExtensionsProperties(String fontRegistryFactoryPropertyName,
        String fontRegistryFactoryPropertyValue, String fontFamiliesPropertyName,
        String fontFamiliesPropertyValue, OutputStream outputStream) throws JRException {
    Writer out = null;
    try {
        out = new OutputStreamWriter(outputStream, DEFAULT_ENCODING);
        out.write(fontRegistryFactoryPropertyName + "=" + fontRegistryFactoryPropertyValue + "\n");
        out.write(fontFamiliesPropertyName + "=" + fontFamiliesPropertyValue + "\n");
        out.flush();
    } catch (Exception e) {
        throw new JRException(EXCEPTION_MESSAGE_KEY_OUTPUT_STREAM_WRITER_ERROR, null, e);
    } finally {
        if (out != null) {
            try {
                out.close();
            } catch (IOException e) {
            }
        }
    }
}

From source file:fr.opensagres.xdocreport.core.io.IOUtils.java

/**
 * Unconditionally close a <code>Writer</code>.
 * <p>//from w w w .j a v a2s  .  c  o  m
 * Equivalent to {@link Writer#close()}, except any exceptions will be ignored. This is typically used in finally
 * blocks.
 * 
 * @param output the Writer to close, may be null or already closed
 */
public static void closeQuietly(Writer output) {
    try {
        if (output != null) {
            output.close();
        }
    } catch (IOException ioe) {
        logger.warning("Writer - exception ignored - exception: " + ioe); //$NON-NLS-1$

        // ignore
    }
}

From source file:fr.opensagres.odfdom.converter.core.utils.IOUtils.java

/**
 * Unconditionally close a <code>Writer</code>.
 * <p>/* w  ww .ja  v a2s . c o m*/
 * Equivalent to {@link Writer#close()}, except any exceptions will be ignored. This is typically used in finally
 * blocks.
 * 
 * @param output the Writer to close, may be null or already closed
 */
public static void closeQuietly(Writer output) {
    try {
        if (output != null) {
            output.close();
        }
    } catch (IOException ioe) {
        //logger.warning( "Writer - exception ignored - exception: " + ioe ); //$NON-NLS-1$

        // ignore
    }
}

From source file:com.github.peterjanes.node.NpmPackageJsonMojo.java

/**
 *
 * @throws MojoExecutionException if anything unexpected happens.
 *///  w  w w  . j  a va  2  s  .  c o  m
public void execute() throws MojoExecutionException {
    if (!outputDirectory.exists()) {
        outputDirectory.mkdirs();
    }

    File packageJsonFile = new File(outputDirectory, "package.json");

    try {
        packageJsonFile.getParentFile().mkdirs();
        JSONObject packageJson = new JSONObject();
        packageJson.put("name", mavenProject.getName());
        packageJson.put("version", mavenProject.getProperties().getProperty("node.project.version"));
        packageJson.put("description", mavenProject.getDescription());
        packageJson.put("main", mainModule);

        List<Artifact> dependencyArtifacts = getNodeArtifacts(ResolutionScope.COMPILE);
        JSONObject dependencies = new JSONObject();
        // TODO: dependency.getVersion() may (and often will) return a
        // -SNAPSHOT. Figure out how to open the .tgz and pull in its
        // actual version, or rewrite the version as a range.
        for (Artifact dependency : dependencyArtifacts) {
            dependencies.put(dependency.getArtifactId(), dependency.getVersion());
        }
        addNpmDependencies(dependencies, npmDependencies);
        packageJson.put("dependencies", dependencies);

        List<Artifact> devDependencyArtifacts = getNodeArtifacts(ResolutionScope.TEST);
        JSONObject devDependencies = new JSONObject();
        for (Artifact devDependency : devDependencyArtifacts) {
            devDependencies.put(devDependency.getArtifactId(), devDependency.getVersion());
        }
        addNpmDependencies(devDependencies, npmTestDependencies);
        packageJson.put("devDependencies", devDependencies);

        JSONObject scripts = new JSONObject();
        scripts.put("test", "mocha -u exports -R xunit-file --recursive");
        scripts.put("coverage", "mocha -u exports -R mocha-lcov-reporter-file --recursive");
        packageJson.put("scripts", scripts);

        Writer w = new FileWriter(packageJsonFile);
        w.write(packageJson.toString());
        w.flush();
        w.close();
    } catch (IOException e) {
        throw new MojoExecutionException("Could not write package.json to " + packageJsonFile.getAbsolutePath(),
                e);
    }
}

From source file:net.bluemix.newsaggregator.api.FeedServlet.java

protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    try {//from w ww  .j av  a2 s  . c o  m
        SyndFeed feed = new SyndFeedImpl();
        feed.setFeedType("rss_2.0");

        feed.setTitle("Bluemix.info");
        feed.setLink("http://www.bluemix.info");
        feed.setDescription("Bluemix.info - News Aggregator for IBM Bluemix Developers");

        List<SyndEntry> entries = new ArrayList<SyndEntry>();
        SyndEntry entry;
        SyndContent description;

        List<NewsEntry> newsEntries = API.getSingleton().getCuratedNewsEntriesLastMonthByCurationDate();
        if (newsEntries != null) {
            int maxAmount = 20;
            int amount = 20;
            if (newsEntries.size() <= maxAmount) {
                amount = newsEntries.size();
            }
            for (int i = 0; i < amount; i++) {
                NewsEntry newsEntry = newsEntries.get(i);

                entry = new SyndEntryImpl();
                entry.setTitle(newsEntry.getTitle());
                entry.setLink(newsEntry.getLink());
                entry.setPublishedDate(newsEntry.getCurationDate());
                description = new SyndContentImpl();
                description.setType("text/plain");
                description.setValue(newsEntry.getFirstSentences());
                entry.setDescription(description);

                Person p = API.getSingleton().getPersonById(newsEntry.getPersonId());
                if (p == null) {
                    entry.setAuthor("Unknown");
                } else {
                    entry.setAuthor(p.getDisplayName());
                }
                entries.add(entry);
            }
        }
        feed.setEntries(entries);

        Writer writer = response.getWriter();
        SyndFeedOutput output = new SyndFeedOutput();
        output.output(feed, writer);
        writer.close();

    } catch (Throwable e) {
        e.printStackTrace();
    }
}

From source file:com.cubusmail.gwtui.server.services.AttachmentUploadServlet.java

@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    boolean isMultipart = ServletFileUpload.isMultipartContent(request);

    // Create a new file upload handler
    if (isMultipart) {
        ServletFileUpload upload = new ServletFileUpload();

        try {/*from  w w w  .j  ava 2s . c  om*/
            // Parse the request
            FileItemIterator iter = upload.getItemIterator(request);
            while (iter.hasNext()) {
                FileItemStream item = iter.next();
                String name = item.getFieldName();
                InputStream stream = item.openStream();
                if (item.isFormField()) {
                    System.out.println(
                            "Form field " + name + " with value " + Streams.asString(stream) + " detected.");
                } else {
                    System.out
                            .println("File field " + name + " with file name " + item.getName() + " detected.");
                    DataSource source = createDataSource(item);
                    SessionManager.get().getCurrentComposeMessage().addComposeAttachment(source);
                }

                JSONObject jsonResponse = null;
                try {
                    jsonResponse = new JSONObject();
                    jsonResponse.put("success", true);
                    jsonResponse.put("error", "Upload successful");
                } catch (Exception e) {

                }

                Writer w = new OutputStreamWriter(response.getOutputStream());
                w.write(jsonResponse.toString());
                w.close();

                stream.close();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    response.setStatus(HttpServletResponse.SC_OK);
}

From source file:java2typescript.jackson.module.DefinitionGeneratorTest.java

@Test
public void internalModuleFormat() throws IOException {
    // Arrange// w w  w  .j  a v a2 s .  co  m
    Module module = createTestModule();
    Writer out = new StringWriter();

    // Act
    module.write(out); // for backwards compatibility the same as `new InternalModuleFormatWriter().write(module, out);`
    out.close();
    System.out.println(out);

    // Assert
    ExpectedOutputChecker.checkOutputFromFile(out);
}

From source file:com.haulmont.yarg.formatters.impl.HtmlFormatter.java

protected void writeHtmlDocument(BandData rootBand, OutputStream outputStream) {
    Map templateModel = getTemplateModel(rootBand);

    Template htmlTemplate = getTemplate();
    Writer htmlWriter = new OutputStreamWriter(outputStream);

    try {/*from w  ww. ja va2s. c om*/
        htmlTemplate.process(templateModel, htmlWriter);
        htmlWriter.close();
    } catch (TemplateException fmException) {
        throw wrapWithReportingException("FreeMarkerException: " + fmException.getMessage());
    } catch (ReportingException e) {
        throw e;
    } catch (Exception e) {
        throw wrapWithReportingException("An error occurred while rendering html document.", e);
    }
}

From source file:com.netxforge.oss2.config.ChartConfigFactory.java

/** {@inheritDoc} */
protected void saveXml(String xml) throws IOException {
    if (xml != null) {
        Writer fileWriter = new OutputStreamWriter(new FileOutputStream(m_chartConfigFile), "UTF-8");
        fileWriter.write(xml);//from  ww w. j  av a  2  s. c o  m
        fileWriter.flush();
        fileWriter.close();
    }
}