Example usage for java.io BufferedOutputStream close

List of usage examples for java.io BufferedOutputStream close

Introduction

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

Prototype

@Override
public void close() throws IOException 

Source Link

Document

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

Usage

From source file:com.uberspot.storageutils.StorageUtils.java

/** Save the given string to a file in external storage
 * @param obj the object to save/*from w w w.  j  av  a  2  s .c  om*/
 * @param directory the directory in the SD card to save it into
 * @param fileName the name of the file
 * @param overwrite if set to true the file will be overwritten if it already exists
 * @return true if the file was written successfully, false otherwise
 */
public static boolean saveStringToExternalStorage(String obj, String directory, String fileName,
        boolean overwrite) {
    if (!directory.startsWith(File.separator))
        directory = File.separator + directory;

    File dir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + directory);
    if (!dir.exists())
        dir.mkdirs();

    File file = new File(dir, fileName);
    if (file.exists() && !overwrite)
        return false;
    BufferedOutputStream output = null;
    try {
        output = new BufferedOutputStream(new FileOutputStream(file));
        output.write(obj.getBytes());
        output.flush();
        return true;
    } catch (Exception e) {
        e.printStackTrace(System.out);
    } finally {
        if (output != null)
            try {
                output.close();
            } catch (IOException e) {
            }
    }
    return false;
}

From source file:it.cnr.icar.eric.common.Utility.java

public static File createTempFile(byte[] bytes, String prefix, String extension, boolean deleteOnExit)
        throws IOException {
    File temp = File.createTempFile(prefix, extension);

    // Delete temp file when program exits.
    if (deleteOnExit) {
        temp.deleteOnExit();//from   ww  w  .  ja va  2  s.  co m
    }

    // Write to temp file
    BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(temp));
    out.write(bytes, 0, bytes.length);
    out.close();

    return temp;
}

From source file:Main.java

/**
 * Save URL contents to a file.// w  w w. j ava 2  s. c om
 */
public static boolean copy(URL from, File to) {
    BufferedInputStream urlin = null;
    BufferedOutputStream fout = null;
    try {
        int bufSize = 8 * 1024;
        urlin = new BufferedInputStream(from.openConnection().getInputStream(), bufSize);
        fout = new BufferedOutputStream(new FileOutputStream(to), bufSize);
        copyPipe(urlin, fout, bufSize);
    } catch (IOException ioex) {
        return false;
    } catch (SecurityException sx) {
        return false;
    } finally {
        if (urlin != null) {
            try {
                urlin.close();
            } catch (IOException cioex) {
            }
        }
        if (fout != null) {
            try {
                fout.close();
            } catch (IOException cioex) {
            }
        }
    }
    return true;
}

From source file:net.duckling.ddl.util.FileUtil.java

/**
 * Brief Intro Here//from   w  w  w .j  a  va 2s  .  c om
 * ?
 * @param
 */
public static void copyFile(File sourceFile, File targetFile) throws IOException {
    // ?
    FileInputStream input = new FileInputStream(sourceFile);
    BufferedInputStream inBuff = new BufferedInputStream(input);

    // ?
    FileOutputStream output = new FileOutputStream(targetFile);
    BufferedOutputStream outBuff = new BufferedOutputStream(output);

    // 
    byte[] b = new byte[1024 * 5];
    int len;
    while ((len = inBuff.read(b)) != -1) {
        outBuff.write(b, 0, len);
    }
    // ?
    outBuff.flush();

    // ?
    inBuff.close();
    outBuff.close();
    output.close();
    input.close();
}

From source file:com.arcusys.liferay.vaadinplugin.util.ControlPanelPortletUtil.java

/**
 * Extracts the jarEntry from the jarFile to the target directory.
 *
 * @param jarFile// w w w .ja  v a  2 s . c  o m
 * @param jarEntry
 * @param targetDir
 * @return true if extraction was successful, false otherwise
 */
public static boolean extractJarEntry(JarFile jarFile, JarEntry jarEntry, String targetDir) {
    boolean extractSuccessful = false;
    File file = new File(targetDir);
    if (!file.exists()) {
        file.mkdir();
    }
    if (jarEntry != null) {
        InputStream inputStream = null;
        try {
            inputStream = jarFile.getInputStream(jarEntry);
            file = new File(targetDir + jarEntry.getName());
            if (jarEntry.isDirectory()) {
                file.mkdir();
            } else {
                int size;
                byte[] buffer = new byte[2048];

                FileOutputStream fileOutputStream = new FileOutputStream(file);
                BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream,
                        buffer.length);

                try {
                    while ((size = inputStream.read(buffer, 0, buffer.length)) != -1) {
                        bufferedOutputStream.write(buffer, 0, size);
                    }
                    bufferedOutputStream.flush();
                } finally {
                    bufferedOutputStream.close();
                }
            }
            extractSuccessful = true;
        } catch (Exception e) {
            log.warn(e);
        } finally {
            close(inputStream);
        }
    }
    return extractSuccessful;
}

From source file:Main.java

public static Bitmap getBitmapFromURL(String urlString) {
    Bitmap bitmap = null;//from   w  w w  .  j av a  2 s.co m
    InputStream in = null;
    BufferedOutputStream out = null;
    try {
        in = new BufferedInputStream(new URL(urlString).openStream(), 1024 * 4);
        final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
        out = new BufferedOutputStream(dataStream, 1024 * 4);
        byte[] buffer = new byte[1024];
        int len = -1;
        while ((len = in.read(buffer)) != -1) {
            out.write(buffer, 0, len);
        }
        out.flush();
        byte[] data = dataStream.toByteArray();
        bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
        data = null;
        return bitmap;
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            in.close();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return bitmap;
}

From source file:cn.kk.exia.MangaDownloader.java

public final static HttpURLConnection getUrlConnection(final String url, final boolean post,
        final String output) throws IOException {
    int retries = 0;
    HttpURLConnection conn;/*from  www.j a  v a2 s .  c om*/
    while (true) {
        try {
            final URL urlObj = new URL(url);
            conn = (HttpURLConnection) urlObj.openConnection();
            conn.setConnectTimeout(15000);
            conn.setReadTimeout(30000);
            if (post) {
                conn.setRequestMethod("POST");
            }
            final String referer;
            final int pathIdx;
            if ((pathIdx = url.lastIndexOf('/')) > "https://".length()) {
                referer = url.substring(0, pathIdx);
            } else {
                referer = url;
            }
            conn.setRequestProperty("Referer", referer);
            final Set<String> keys = MangaDownloader.DEFAULT_CONN_HEADERS.keySet();
            for (final String k : keys) {
                final String value = MangaDownloader.DEFAULT_CONN_HEADERS.get(k);
                if (value != null) {
                    conn.setRequestProperty(k, value);
                }
            }
            // conn.setUseCaches(false);
            if (output != null) {
                conn.setDoOutput(true);
                final BufferedOutputStream out = new BufferedOutputStream(conn.getOutputStream());
                out.write(output.getBytes(MangaDownloader.CHARSET_UTF8));
                out.close();
            }
            if (MangaDownloader.appendCookies(MangaDownloader.cookie, conn)) {
                MangaDownloader.putConnectionHeader("Cookie", MangaDownloader.cookie.toString());
            }
            break;
        } catch (final Throwable e) {
            // 
            System.err.println(e.toString());
            if (retries++ > 10) {
                throw new IOException(e);
            } else {
                try {
                    Thread.sleep((60 * retries * MangaDownloader.sleepBase)
                            + ((int) Math.random() * MangaDownloader.sleepBase * 60 * retries));
                } catch (final InterruptedException e1) {
                    e1.printStackTrace();
                }
            }
        }
    }
    return conn;
}

From source file:it.greenvulcano.util.bin.BinaryUtils.java

/**
 * Write the content of a <code>byte</code> array into a file on the local
 * filesystem./*  w w w  .  ja v a  2 s .c  o m*/
 * 
 * @param contentArray
 *        the <code>byte</code> array to be written to file
 * @param filename
 *        the name of the file to be written to
 * @param append
 *        If true the data are appended to existent file
 * @throws IOException
 *         if any I/O error occurs
 */
public static void writeBytesToFile(byte[] contentArray, String filename, boolean append) throws IOException {
    BufferedOutputStream bufOut = null;
    try {
        filename = TextUtils.adjustPath(filename);
        bufOut = new BufferedOutputStream(new FileOutputStream(filename, append), 10240);
        IOUtils.write(contentArray, bufOut);
        bufOut.flush();
    } finally {
        if (bufOut != null) {
            bufOut.close();
        }
    }
}

From source file:de.nrw.hbz.deepzoomer.fileUtil.FileUtil.java

/**
 * <p><em>Title: Save InputSream to an temporary File</em></p>
 * <p>Description: </p>// w  w w .  j a  va 2  s  .c  o m
 * 
 * @return 
 */
public static void saveInputStreamToTempFile(InputStream is, String fileName) {

    File outputFile = new File(Configuration.getTempDirPath() + fileName);
    BufferedInputStream bis = new BufferedInputStream(is);
    BufferedOutputStream bos = null;
    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(outputFile);
        bos = new BufferedOutputStream(fos);
        int i = -1;
        while ((i = bis.read()) != -1) {
            bos.write(i);
        }
        bos.flush();
    } catch (Exception e) {
        log.error(e);
    } finally {
        if (bos != null) {
            try {
                bos.close();
            } catch (IOException ioExc) {
                log.error(ioExc);
            }
        }
        if (fos != null) {
            try {
                fos.close();
            } catch (IOException ioExc) {
                log.error(ioExc);
            }
        }
        if (bis != null) {
            try {
                bis.close();
            } catch (IOException ioExc) {
                log.error(ioExc);
            }
        }
        if (is != null) {
            try {
                is.close();
            } catch (IOException ioExc) {
                log.error(ioExc);
            }
        }

    }

}

From source file:com.panet.imeta.job.entries.getpop.JobEntryGetPOP.java

public static void saveFile(String foldername, String filename, InputStream input) throws IOException {

    // LogWriter log = LogWriter.getInstance();

    if (filename == null) {
        filename = File.createTempFile("xx", ".out").getName(); //$NON-NLS-1$ //$NON-NLS-2$
    }//from ww w .ja v  a2s .co  m
    // Do no overwrite existing file
    File file = new File(foldername, filename);
    for (int i = 0; file.exists(); i++) {
        file = new File(foldername, filename + i);
    }
    FileOutputStream fos = new FileOutputStream(file);
    BufferedOutputStream bos = new BufferedOutputStream(fos);

    BufferedInputStream bis = new BufferedInputStream(input);
    int aByte;
    while ((aByte = bis.read()) != -1) {
        bos.write(aByte);
    }

    bos.flush();
    bos.close();
    bis.close();
}