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:Main.java

/**
 * Copy configuration file from assets to data folder.
 *
 * @param file File to copy/*from  w  w  w . j a  v a 2  s.c  om*/
 */
private static void copyAssetToData(File file) {
    try {
        InputStream myInput = mContext.getAssets().open(file.getName());
        String outFileName = file.getPath();
        OutputStream myOutput = new FileOutputStream(outFileName);
        byte[] buffer = new byte[1024];
        int length;
        while ((length = myInput.read(buffer)) > 0) {
            myOutput.write(buffer, 0, length);
        }
        myOutput.flush();
        myOutput.close();
        myInput.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.lightboxtechnologies.spectrum.MRCoffeeClient.java

protected static void extract_lib(String src, File dst) throws IOException {
    InputStream in = null;/*  w w w  .  j  a  v a2  s  .c om*/
    try {
        in = MRCoffeeJob.class.getResourceAsStream(src);
        if (in == null) {
            throw new IOException(src + " not found!");
        }

        OutputStream out = null;
        try {
            out = new FileOutputStream(dst);
            IOUtils.copy(in, out);
            out.close();
        } finally {
            IOUtils.closeQuietly(out);
        }

        in.close();
    } finally {
        IOUtils.closeQuietly(in);
    }
}

From source file:Main.java

public static void DownloadFromUrl(String thisUrl, String path, String filename) {
    Log.d("DownloadFromUrl", "url: " + thisUrl);
    Log.d("DownloadFromUrl", "path: " + path);
    Log.d("DownloadFromUrl", "filename: " + filename);
    try {/* w ww .j  a v  a 2  s .c  o m*/
        URL url = new URL(thisUrl);
        new File(path).mkdirs();
        File file = new File(path, filename);

        /* Open a connection to that URL. */
        URLConnection ucon = url.openConnection();

        Log.d("DownloadFromUrl", "about to write file");
        /*
         * Define InputStreams to read from the URLConnection.
         */
        InputStream is = ucon.getInputStream();
        try {
            OutputStream os = new FileOutputStream(file);
            try {
                byte[] buffer = new byte[4096];
                for (int n; (n = is.read(buffer)) != -1;)
                    os.write(buffer, 0, n);
            } finally {
                os.close();
            }
        } finally {
            is.close();
        }
        Log.d("DownloadFromUrl", "finished");

    } catch (IOException e) {
        Log.d("DownloadFromUrl", "Error: " + e);
    }
}

From source file:com.ettrema.httpclient.Utils.java

public static void close(OutputStream out) {
    try {//  w  ww .  jav a  2 s. com
        if (out == null) {
            return;
        }
        out.close();
    } catch (IOException ex) {
        log.warn("Exception closing stream: " + ex.getMessage());
    }
}

From source file:com.aqnote.shared.cryptology.cert.io.PKCSWriter.java

public static void storePKCS12File(PKCS12PfxPdu pfxPdu, OutputStream ostream) throws Exception {
    if (pfxPdu == null || ostream == null)
        return;/*from  w  w w.j  a v a2s.  c o  m*/

    ostream.write(pfxPdu.getEncoded(ASN1Encoding.DER));
    ostream.close();
}

From source file:models.logic.CipherDecipher.java

public static void doCopy(InputStream is, OutputStream os) throws IOException {
    byte[] bytes = new byte[64];
    int numBytes;
    while ((numBytes = is.read(bytes)) != -1) {
        os.write(bytes, 0, numBytes);//w  w w .j av a 2s  . c om
    }
    os.flush();
    os.close();
    is.close();
}

From source file:io.undertow.server.handlers.ChunkedResponseTrailersTestCase.java

@BeforeClass
public static void setup() {
    final BlockingHandler blockingHandler = new BlockingHandler();
    DefaultServer.setRootHandler(blockingHandler);
    blockingHandler.setRootHandler(new HttpHandler() {
        @Override/*from  w w w .  j  a  v  a  2s .  c  om*/
        public void handleRequest(final HttpServerExchange exchange) {
            try {
                if (connection == null) {
                    connection = exchange.getConnection();
                } else if (!DefaultServer.isAjp() && !DefaultServer.isProxy()
                        && connection != exchange.getConnection()) {
                    final OutputStream outputStream = exchange.getOutputStream();
                    outputStream.write("Connection not persistent".getBytes());
                    outputStream.close();
                    return;
                }
                HeaderMap trailers = new HeaderMap();
                exchange.putAttachment(HttpAttachments.RESPONSE_TRAILERS, trailers);
                trailers.put(HttpString.tryFromString("foo"), "fooVal");
                trailers.put(HttpString.tryFromString("bar"), "barVal");
                new StringWriteChannelListener(message).setup(exchange.getResponseChannel());

            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    });
}

From source file:com.github.rholder.gradle.dependency.DependencyConversionUtil.java

private static void acumenTemplateFromClasspath(File extractedJarFile, File outputFile) throws IOException {
    InputStream input = DependencyConversionUtil.class.getResourceAsStream("/init-acumen.gradle");

    // replace token with extracted file, replace '\' with '/' to handle Windows paths
    String processed = IOUtils.toString(input).replace("#ACUMEN_JAR#", extractedJarFile.getAbsolutePath())
            .replace("\\", "/");
    input.close();/*from w w  w . j ava2  s.c om*/

    InputStream processedInput = IOUtils.toInputStream(processed);

    OutputStream output = new BufferedOutputStream(new FileOutputStream(outputFile));
    IOUtils.copy(processedInput, output);

    output.close();
    processedInput.close();
}

From source file:com.varaneckas.hawkscope.util.IOUtils.java

/**
 * Copies a file/*from  w  w  w .j  a va  2 s  .c  om*/
 * 
 * @param in source file
 * @param out target file
 * @return boolean on success
 */
public static synchronized boolean copyFile(final InputStream in, final OutputStream out) {
    try {
        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        in.close();
        out.close();
        return true;
    } catch (final Exception e) {
        log.error("Failed copying file: " + in + " -> " + out, e);
        return false;
    }
}

From source file:Main.java

public static void copyFile(File from, File toFile, PrintStream reportStream) {
    if (reportStream != null) {
        reportStream.println("Coping file " + from.getAbsolutePath() + " to " + toFile.getAbsolutePath());
    }//from   ww w  .  jav a 2s.co m
    if (!from.exists()) {
        throw new IllegalArgumentException("File " + from.getPath() + " does not exist.");
    }
    if (from.isDirectory()) {
        throw new IllegalArgumentException(from.getPath() + " is a directory. Should be a file.");
    }
    try {
        final InputStream in = new FileInputStream(from);
        if (!toFile.getParentFile().exists()) {
            toFile.getParentFile().mkdirs();
        }
        if (!toFile.exists()) {
            toFile.createNewFile();
        }
        final OutputStream out = new FileOutputStream(toFile);

        final byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        in.close();
        out.close();
    } catch (final IOException e) {
        throw new RuntimeException(
                "IO exception occured while copying file " + from.getPath() + " to " + toFile.getPath(), e);
    }

}