Example usage for java.io OutputStream close

List of usage examples for java.io OutputStream close

Introduction

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

Prototype

public void close() throws IOException 

Source Link

Document

Closes this output stream and releases any system resources associated with this stream.

Usage

From source file:com.kcs.core.utilities.FtpUtil.java

public static boolean getFile(String hostname, int port, String username, String password,
        String remoteFileName, String localFileName) throws Exception {
    FTPClient ftp = null;/*from  w w  w . j  a v  a2 s. c  o  m*/
    boolean result = false;
    try {
        System.out.println("File has been start downloaded.");
        ftp = FtpUtil.openFtpConnect(hostname, port, username, password);
        if (null != ftp && ftp.isConnected()) {
            File downloadFile = new File(localFileName);
            if (!FtpUtil.checkFileExists(remoteFileName, ftp)) {
                System.out.println("File not found in server.");
                throw new Exception("File not found in server.");
            }
            OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(downloadFile));
            result = ftp.retrieveFile(remoteFileName, outputStream);
            outputStream.close();
        }
        System.out.println("File [" + remoteFileName + "] has been downloaded Complete.");
    } catch (Exception e) {
        e.printStackTrace();
        throw new Exception("Error : " + e.getMessage());
    } finally {
        FtpUtil.closeFtpServer(ftp);
    }
    return result;
}

From source file:com.seleniumtests.util.FileUtility.java

/**
 * Unzip file to a temp directory//  w  w  w .  j a va  2s  .  c  o  m
 * @param zippedFile
 * @return the output folder
 * @throws IOException
 */
public static File unzipFile(final File zippedFile) throws IOException {
    File outputFolder = Files.createTempDirectory("tmp").toFile();
    try (ZipFile zipFile = new ZipFile(zippedFile)) {
        final Enumeration<? extends ZipEntry> entries = zipFile.entries();
        while (entries.hasMoreElements()) {
            final ZipEntry entry = entries.nextElement();
            final File entryDestination = new File(outputFolder, entry.getName());
            if (entry.isDirectory()) {
                //noinspection ResultOfMethodCallIgnored
                entryDestination.mkdirs();
            } else {
                //noinspection ResultOfMethodCallIgnored
                entryDestination.getParentFile().mkdirs();
                final InputStream in = zipFile.getInputStream(entry);
                final OutputStream out = new FileOutputStream(entryDestination);
                IOUtils.copy(in, out);
                IOUtils.closeQuietly(in);
                out.close();
            }
        }
    }
    return outputFolder;
}

From source file:Main.java

public static void saveXML(Document doc, OutputStream stream) {
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer;/*from   w w  w .j  ava2s.  c o m*/
    try {
        transformer = tFactory.newTransformer();
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(stream);
        transformer.transform(source, result);
        stream.flush();
        stream.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

}

From source file:com.pongasoft.util.io.IOUtils.java

/**
 * Saves the content in the resource.// w w  w  .j  ava  2 s.co  m
 *
 * @param resource the resource to save the content
 * @param content the content to save
 * @throws IOException if there is a problem saving
 */
public static void saveContent(FileObject resource, byte[] content) throws IOException {
    OutputStream os = resource.getContent().getOutputStream();
    try {
        saveContent(os, content);
    } finally {
        os.close();
    }
}

From source file:com.moss.appsnap.keeper.windows.MSWindowsDesktopIntegrationStrategy.java

private static void writeToFile(InputStream in, File dest) throws IOException {
    byte[] b = new byte[1024 * 1024];
    OutputStream out = new FileOutputStream(dest);
    for (int x = in.read(b); x != -1; x = in.read(b)) {
        out.write(b, 0, x);/*from   ww w .j  a  v a2s.co m*/
    }
    in.close();
    out.close();
    log.info("Copied to " + dest.getAbsolutePath());
}

From source file:de.handtwerk.android.IoHelper.java

public static void copy(InputStream pIn, OutputStream pOut) throws IOException {

    byte[] buffer = new byte[1024];
    int read;//from w w  w.  j a  v  a  2s  . co m
    while ((read = pIn.read(buffer)) != -1) {
        pOut.write(buffer, 0, read);
    }

    pIn.close();
    pOut.flush();
    pOut.close();
}

From source file:com.priocept.jcr.server.UploadServlet.java

protected static void close(InputStream iStream, OutputStream oStream) throws IOException {
    try {// w  w w.jav a  2s .  com
        if (iStream != null) {
            iStream.close();
        }
    } finally {
        if (oStream != null) {
            oStream.close();
        }
    }
}

From source file:it.geosolutions.geofence.gui.server.utility.IoUtility.java

/**
 * Save compressed stream.//  w w w  .ja v  a  2 s  .  c o m
 *
 * @param buffer
 *            the buffer
 * @param out
 *            the out
 * @param len
 *            the len
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 */
public static void saveCompressedStream(final byte[] buffer, final OutputStream out, final int len)
        throws IOException {
    try {
        out.write(buffer, 0, len);

    } catch (Exception e) {
        out.flush();
        out.close();

        IOException ioe = new IOException("Not valid archive file type.");
        ioe.initCause(e);
        throw ioe;
    }
}

From source file:com.ibm.jaggr.core.util.CopyUtil.java

public static void copy(String str, OutputStream out) throws IOException {
    Reader reader = new StringReader(str);
    try {/*w w  w  . j  a v a 2 s  .co m*/
        IOUtils.copy(reader, out);
    } finally {
        try {
            reader.close();
        } catch (Exception ignore) {
        }
        try {
            out.close();
        } catch (Exception ignore) {
        }
    }
}

From source file:com.sangupta.jerry.appupdate.ApplicationUpdater.java

/**
 * Method that checks if there is an application update available on the web or not. If yes, the 
 * method will download the application update (JAR/WAR etc) to the local disk.
 * /*from w  w w  .  j  a v  a2  s.  com*/
 * @param applicationUpdateUrl the update URL where update.xml definition can be found
 * 
 * @param applicationVersion the current application version number
 * 
 * @param downloadDirectory the directory to download the file in. If the supplied directory is empty, the method
 * will download the file in the current directory as returned by the system.
 */
public static void checkAndDownloadForApplicationUpdates(String applicationUpdateUrl, String applicationVersion,
        File downloadDirectory) {
    // check if we have an update available
    XmlUpdateDefinition definition = updatesAvailable(applicationUpdateUrl, applicationVersion);

    // if not, exit
    if (definition == null) {
        return;
    }

    // if yes, download the file
    logger.info("Downloading application version: " + definition.getVersion() + " from url: "
            + definition.getUrl() + "...");

    // go ahead and download the updated file
    WebResponse response = WebInvoker.getResponse(definition.getUrl());
    if (response == null) {
        return;
    }

    byte[] updatedFile = response.getBytes();
    if (updatedFile != null) {
        // write this file to disk
        // form the name of the file
        String fileName = UriUtils.extractFileName(applicationUpdateUrl);

        try {
            File jarFile;
            if (downloadDirectory == null) {
                jarFile = new File(fileName);
            } else {
                jarFile = new File(downloadDirectory, fileName);
            }

            // write the file to disk
            OutputStream out = null;
            try {
                out = new FileOutputStream(jarFile, false);
                out.write(updatedFile);
                out.close(); // don't swallow close Exception if copy completes normally
            } finally {
                IOUtils.closeQuietly(out);
            }

            logger.info("Updated application written to disk as: " + jarFile.getAbsolutePath());
        } catch (IOException e) {
            logger.error("Unable to write downloaded JAR file.", e);
        }
    }
}