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:io.hawkcd.agent.AgentConfiguration.java

private static String generateAgentId(Properties properties) {
    UUID agentId = UUID.randomUUID();

    try {/*from ww w . ja  v a 2s . c  om*/
        File configFile = new File(ConfigConstants.AGENT_CONFIG_FILE_LOCATION);
        OutputStream output = new FileOutputStream(configFile);
        properties.setProperty("agentId", agentId.toString());
        properties.store(output, null);
        output.close();
    } catch (IOException io) {
        io.printStackTrace();
    }

    return agentId.toString();
}

From source file:cn.vlabs.clb.api.io.FileUtil.java

public static void copy(InputStream in, OutputStream out) throws IOException {
    try {//from  w w w .j av  a2  s  .  c om
        byte[] buff = new byte[4096];
        int count = -1;
        while ((count = in.read(buff)) != -1) {
            out.write(buff, 0, count);
        }
    } finally {
        in.close();
        out.close();
    }
}

From source file:com.iggroup.oss.restdoclet.plugin.io.IOUtils.java

/**
 * Copies a <i>large</i> input-stream to an output-stream.
 * //from  ww w  . j a  v  a 2 s  .com
 * @param input the <i>large</i> input-stream.
 * @param output the output-stream.
 * @param close <code>true</code> if the output-stream has to be closed
 *           after copying, <code>false</code> otherwise.
 * @throws IOException if an input-output exception occurs.
 */
public static void copyLarge(final InputStream input, final OutputStream output, final boolean close)
        throws IOException {
    org.apache.commons.io.IOUtils.copyLarge(input, output);
    input.close();
    if (close) {
        output.close();
    }
}

From source file:com.sangupta.jerry.util.ArchiveUtils.java

/**
 * Unpack the TAR file into the given output directory.
 * /*from   w w w. j av a2 s.  c  o m*/
 * @param tarFile
 *            the tar file that needs to be unpacked
 * 
 * @param outputDir
 *            the directory in which the entire file is unpacked
 * 
 * @throws ArchiveException
 *             if the TAR file is corrupt
 * 
 * @throws IOException
 *             if error occurs reading TAR file
 * 
 * @return a list of {@link File} objects representing the files unpacked
 *         from the TAR file
 * 
 */
public static List<File> unpackTAR(final File tarFile, final File outputDir)
        throws ArchiveException, IOException {
    LOGGER.info("Untaring {} to dir {}", tarFile.getAbsolutePath(), outputDir.getAbsolutePath());

    final List<File> untaredFiles = new LinkedList<File>();

    InputStream fileInputStream = null;
    TarArchiveInputStream tarInputStream = null;

    try {
        fileInputStream = new FileInputStream(tarFile);
        tarInputStream = (TarArchiveInputStream) new ArchiveStreamFactory().createArchiveInputStream("tar",
                fileInputStream);

        TarArchiveEntry entry = null;
        while ((entry = (TarArchiveEntry) tarInputStream.getNextEntry()) != null) {
            final File outputFile = new File(outputDir, entry.getName());
            if (entry.isDirectory()) {
                LOGGER.debug("Attempting to write output directory {}", outputFile.getAbsolutePath());

                if (!outputFile.exists()) {
                    LOGGER.debug("Attempting to create output directory {}", outputFile.getAbsolutePath());

                    if (!outputFile.mkdirs()) {
                        throw new IllegalStateException(
                                "Couldn't create directory: " + outputFile.getAbsolutePath());
                    }
                }

                // next file
                continue;
            }

            // write the plain file
            LOGGER.debug("Creating output file {}", outputFile.getAbsolutePath());

            final OutputStream outputFileStream = new FileOutputStream(outputFile);
            IOUtils.copy(tarInputStream, outputFileStream);
            outputFileStream.close();

            // add to the list of written files
            untaredFiles.add(outputFile);
        }
    } finally {
        org.apache.commons.io.IOUtils.closeQuietly(tarInputStream);
        org.apache.commons.io.IOUtils.closeQuietly(fileInputStream);
    }

    return untaredFiles;
}

From source file:Main.java

public static void copyAssets(Context pContext, String pAssetFilePath, String pDestDirPath) {
    AssetManager assetManager = pContext.getAssets();
    InputStream in = null;//from w w w  .  ja v a 2  s  . co  m
    OutputStream out = null;
    try {
        in = assetManager.open(pAssetFilePath);
        File outFile = new File(pDestDirPath, pAssetFilePath);
        out = new FileOutputStream(outFile);
        copyFile(in, out);
        in.close();
        in = null;
        out.flush();
        out.close();
        out = null;
    } catch (IOException e) {
        Log.e("tag", "Failed to copy asset file: " + pAssetFilePath, e);
    }
}

From source file:de.nava.informa.utils.FileUtils.java

/**
 * Copies a file from <code>inFile</code> to <code>outFile</code>.
 *//* w  w w  .j a v a 2 s  .  c om*/
public static void copyFile(File inFile, File outFile) {
    try {
        logger.debug("Copying file " + inFile + " to " + outFile);
        InputStream in = new FileInputStream(inFile);
        OutputStream out = new FileOutputStream(outFile);
        byte[] buf = new byte[8 * 1024];
        int n;
        while ((n = in.read(buf)) >= 0) {
            out.write(buf, 0, n);
            out.flush();
        }
        in.close();
        out.close();
    } catch (Exception e) {
        logger.warn("Error occurred while copying file " + inFile + " to " + outFile);
        e.printStackTrace();
    }
}

From source file:in.goahead.apps.util.URLUtils.java

public static void AppendStream(InputStream inputStream, String outputFileName, long skip) throws IOException {
    long actualSkip = 0;
    Logger.debug("Skip length: " + skip + " Actual skip: " + actualSkip);
    actualSkip = inputStream.skip(skip);
    Logger.debug("Skip length: " + skip + " Actual skip: " + actualSkip);
    if (actualSkip == skip) {
        Logger.debug("Skip length: " + skip + " Actual skip: " + actualSkip);
        OutputStream os = new FileOutputStream(outputFileName, true);
        DownloadStream(inputStream, os);
        os.flush();/*from w  w w.  ja  v a 2 s. co m*/
        os.close();
    } else {
        Logger.debug("Skip length: " + skip + " Actual skip: " + actualSkip);
    }

}

From source file:alluxio.cli.AlluxioFrameworkIntegrationTest.java

private static boolean processExists(String processName) throws Exception {
    Process ps = Runtime.getRuntime().exec(new String[] { "ps", "ax" });
    InputStream psOutput = ps.getInputStream();

    Process processGrep = Runtime.getRuntime().exec(new String[] { "grep", processName });
    OutputStream processGrepInput = processGrep.getOutputStream();
    IOUtils.copy(psOutput, processGrepInput);
    InputStream processGrepOutput = processGrep.getInputStream();
    processGrepInput.close();

    // Filter out the grep process itself.
    Process filterGrep = Runtime.getRuntime().exec(new String[] { "grep", "-v", "grep" });
    OutputStream filterGrepInput = filterGrep.getOutputStream();
    IOUtils.copy(processGrepOutput, filterGrepInput);
    filterGrepInput.close();//  ww w  . ja  v a 2 s.co m

    return IOUtils.readLines(filterGrep.getInputStream()).size() >= 1;
}

From source file:Main.java

public static void transform(InputStream xslis, InputStream xmlis, OutputStream xmlos) {
    try {/*  w  ww.ja v a 2s.  co  m*/
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer(new StreamSource(xslis));
        transformer.transform(new StreamSource(xmlis), new StreamResult(xmlos));
        xslis.close();
        xmlis.close();
        xmlos.close();
    } catch (Exception e) {
        throw new RuntimeException("Fail to do XSLT transformation", e);
    }
}

From source file:big.zip.java

/**
 * When given a zip file, this method will open it up and extract all files
 * inside into a specific folder on disk. Typically on BIG archives, the zip
 * file only contains a single file with the original file name.
 * @param fileZip           The zip file that we want to extract files from
 * @param folderToExtract   The folder where the extract file will be placed 
 * @return True if no problems occurred, false otherwise
 *//*from  ww  w . j av  a  2s  .com*/
public static boolean extract(final File fileZip, final File folderToExtract) {
    // preflight check
    if (fileZip.exists() == false) {
        System.out.println("ZIP126 - Zip file not found: " + fileZip.getAbsolutePath());
        return false;
    }
    // now proceed to extract the files
    try {
        final InputStream inputStream = new FileInputStream(fileZip);
        final ArchiveInputStream ArchiveStream;
        ArchiveStream = new ArchiveStreamFactory().createArchiveInputStream("zip", inputStream);
        final ZipArchiveEntry entry = (ZipArchiveEntry) ArchiveStream.getNextEntry();
        final OutputStream outputStream = new FileOutputStream(new File(folderToExtract, entry.getName()));
        IOUtils.copy(ArchiveStream, outputStream);

        // flush and close all files
        outputStream.flush();
        outputStream.close();
        ArchiveStream.close();
        inputStream.close();
    } catch (ArchiveException ex) {
        Logger.getLogger(zip.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(zip.class.getName()).log(Level.SEVERE, null, ex);
    }
    return true;
}