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:com.spectralogic.ds3client.helpers.channels.WindowedChannelFactory_Test.java

@Test
public void getReturnsWindow() throws Exception {
    try (final ByteArraySeekableByteChannel channel = new ByteArraySeekableByteChannel()) {
        final Writer writer = Channels.newWriter(channel, "UTF-8");
        writer.write("0123456789");
        writer.close();

        try (final WindowedChannelFactory windowedChannelFactory = new WindowedChannelFactory(channel)) {
            try (final SeekableByteChannel window = windowedChannelFactory.get(2L, 6L)) {
                assertThat(IOUtils.toString(Channels.newReader(window, "UTF-8")), is("234567"));
            }/*w ww.j av  a  2s . c  o m*/
        }
    }
}

From source file:dk.ange.octave.util.TeeWriter.java

@Override
public void close() throws IOException {
    IOException ioe = null;/*w w  w  . j  av a  2s.c om*/
    for (final Writer writer : writers) {
        try {
            writer.close();
        } catch (final IOException e) {
            log.debug("Exception during close()", e);
            ioe = e;
        }
    }
    if (ioe != null) {
        throw ioe;
    }
}

From source file:com.microsoft.tfs.jni.helpers.FileCopyHelper.java

/**
 * Copies a text file and any extended attributes, converting charsets along
 * the way. If any provided charsets are <code>null</code>, the default
 * charset is assumed./*from www  .  j a v  a  2  s  .  c  om*/
 *
 * This method should not be used for binary files, instead
 * {@link FileCopyHelper#copy(String, String)} should be used.
 *
 * @param source
 *        The path to the source file
 * @param sourceCharset
 *        The charset of the source file (may be <code>null</code>)
 * @param destination
 *        The path to the target
 * @param destinationCharset
 *        The charset of the target (may be <code>null</code>)
 * @throws FileNotFoundException
 *         If the source does not exist, or the target's parent folder does
 *         not exist
 * @throws IOException
 *         If there was an IOException reading the source or writing the
 *         target
 */
public static void copyText(final String source, Charset sourceCharset, final String destination,
        Charset destinationCharset)
        throws FileNotFoundException, IOException, MalformedInputException, UnmappableCharacterException {
    Check.notNull(source, "source"); //$NON-NLS-1$
    Check.notNull(destination, "destination"); //$NON-NLS-1$

    if (sourceCharset == null) {
        sourceCharset = Charset.defaultCharset();
    }

    if (destinationCharset == null) {
        destinationCharset = Charset.defaultCharset();
    }

    Reader in = null;
    Writer out = null;

    try {
        final CharsetDecoder decoder = sourceCharset.newDecoder();
        decoder.onMalformedInput(CodingErrorAction.REPORT);
        decoder.onUnmappableCharacter(CodingErrorAction.REPORT);

        in = new BufferedReader(new InputStreamReader(new FileInputStream(source), decoder));
        out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(destination), destinationCharset));

        copy(in, out);
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (final IOException e) {
                log.warn(MessageFormat.format("Could not close {0} for reading: {1}", source, e.getMessage())); //$NON-NLS-1$
            }
        }

        if (out != null) {
            try {
                out.close();
            } catch (final IOException e) {
                log.warn(MessageFormat.format("Could not close {0} for writing: {1}", destination, //$NON-NLS-1$
                        e.getMessage()));
            }
        }
    }

    copyAttributes(source, destination);
}

From source file:IOUtils.java

/**
 * Unconditionally close a <code>Writer</code>.
 * <p>//w  w  w .ja v a2 s.  c om
 * 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) {
        // ignore
    }
}

From source file:fr.opensagres.xdocreport.template.freemarker.XDocArchiveWithFreemarkerTest.java

public void testWrite() throws IOException, XDocReportException {

    XDocArchive archive = new XDocArchive();
    Writer contentWriter = archive.getEntryWriter("content.xml");
    contentWriter.write("bla bla bla ${var}");
    contentWriter.close();

    ITemplateEngine templateEngine = FreemarkerTemplateEngine.getDefault();
    IContext context = templateEngine.createContext();
    context.put("var", "AAA");

    contentWriter = archive.getEntryWriter("content.xml");
    Reader contentReader = archive.getEntryReader("content.xml");

    templateEngine.process("", context, contentReader, contentWriter, null);

    contentReader = archive.getEntryReader("content.xml");
    assertEquals("bla bla bla AAA", IOUtils.toString(contentReader));
}

From source file:com.wavemaker.runtime.server.view.DownloadView.java

@Override
protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request,
        HttpServletResponse response) throws Exception {

    Object result = model.get(ServerConstants.RESULTS_PART);

    if (result == null) {
        return;/*from w w  w  . j  a  va2s .c o m*/
    }

    if (result instanceof Downloadable) {
        sendDownloadable((Downloadable) result, response);
        return;
    }

    Writer writer = response.getWriter();
    writer.write(result.toString());
    writer.close();
}

From source file:com.marklogic.samplestack.web.security.SamplestackAccessDeniedHandler.java

@Override
/**/*w  ww  .  ja  v  a 2s  .co m*/
 * Handler override to return 403s on the HttpResponse.
 */
public void handle(HttpServletRequest request, HttpServletResponse response,
        AccessDeniedException accessDeniedException) throws IOException, ServletException {
    HttpServletResponseWrapper responseWrapper = new HttpServletResponseWrapper(response);
    responseWrapper.setStatus(HttpStatus.SC_FORBIDDEN);

    Writer out = responseWrapper.getWriter();
    errors.writeJsonResponse(out, HttpStatus.SC_FORBIDDEN, "Forbidden");
    out.close();
}

From source file:miage.ecom.web.security.ExtJsAuthenticationFailureHandler.java

@Override
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response,
        AuthenticationException exception) throws IOException, ServletException {

    HttpServletResponseWrapper responseWrapper = new HttpServletResponseWrapper(response);

    Writer out = responseWrapper.getWriter();

    out.write("{success:false, errors: { reason: 'Login failed. Try again.' }}");
    out.close();

}

From source file:com.discursive.jccook.io.CopyExample.java

public void start() {
    try {//from  w w  w.  ja  v a  2s.  co  m
        Writer writer = new FileWriter("test.dat");
        InputStream inputStream = getClass().getResourceAsStream("./test.resource");
        CopyUtils.copy(inputStream, writer);
        writer.close();
        inputStream.close();
    } catch (IOException e) {
        System.out.println("Error copying data");
    }

}

From source file:com.marklogic.samplestack.web.security.SamplestackAuthenticationEntryPoint.java

@Override
/**/*from  ww w  .j  ava 2  s  . c o  m*/
 * Override handler that returns 401 for any unauthenticated
 * request to a secured endpoint.
 */
public void commence(HttpServletRequest request, HttpServletResponse response,
        AuthenticationException authException) throws IOException {
    HttpServletResponseWrapper responseWrapper = new HttpServletResponseWrapper(response);
    responseWrapper.setStatus(HttpStatus.SC_UNAUTHORIZED);

    Writer out = responseWrapper.getWriter();
    errors.writeJsonResponse(out, HttpStatus.SC_UNAUTHORIZED, "Unauthorized");
    out.close();
}