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:net.amigocraft.mpt.util.MiscUtil.java

public static boolean unzip(ZipFile zip, File dest, List<String> files) throws MPTException {
    boolean returnValue = true;
    try {/*from w  w  w.ja  v  a2s . c o m*/
        List<String> existingDirs = new ArrayList<>();
        Enumeration<? extends ZipEntry> en = zip.entries();
        entryLoop: while (en.hasMoreElements()) {
            ZipEntry entry = en.nextElement();
            String name = entry.getName().startsWith("./")
                    ? entry.getName().substring(2, entry.getName().length())
                    : entry.getName();
            File file = new File(dest, name);
            if (entry.isDirectory()) {
                if (file.exists()) {
                    if (DISALLOW_MERGE) {
                        existingDirs.add(name);
                        if (VERBOSE)
                            Main.log.warning("Refusing to extract directory " + name + ": already exists");
                    }
                }
            } else {
                files.add(name);
                for (String dir : DISALLOWED_DIRECTORIES) {
                    if (file.getPath().startsWith(dir)) {
                        if (VERBOSE)
                            Main.log.warning("Refusing to extract " + name + " from " + zip.getName()
                                    + ": parent directory \"" + dir + "\" is not allowed");
                        continue entryLoop;
                    }
                }
                if (DISALLOW_MERGE) {
                    for (String dir : existingDirs) {
                        if (file.getPath().substring(2, file.getPath().length()).replace(File.separator, "/")
                                .startsWith(dir)) {
                            continue entryLoop;
                        }
                    }
                }
                if (!DISALLOW_OVERWRITE || !file.exists()) {
                    file.getParentFile().mkdirs();
                    for (String ext : DISALLOWED_EXTENSIONS) {
                        if (file.getName().endsWith(ext)) {
                            if (VERBOSE)
                                Main.log.warning("Refusing to extract " + name + " from " + zip.getName()
                                        + ": extension \"" + ext + "\" is not allowed");
                            returnValue = false;
                            continue entryLoop;
                        }
                    }
                    BufferedInputStream bIs = new BufferedInputStream(zip.getInputStream(entry));
                    int b;
                    byte[] buffer = new byte[1024];
                    FileOutputStream fOs = new FileOutputStream(file);
                    BufferedOutputStream bOs = new BufferedOutputStream(fOs, 1024);
                    while ((b = bIs.read(buffer, 0, 1024)) != -1)
                        bOs.write(buffer, 0, b);
                    bOs.flush();
                    bOs.close();
                    bIs.close();
                } else {
                    if (VERBOSE)
                        Main.log.warning(
                                "Refusing to extract " + name + " from " + zip.getName() + ": already exists");
                    returnValue = false;
                }
            }
        }
    } catch (Exception ex) {
        ex.printStackTrace(); //TODO
        throw new MPTException(ERROR_COLOR + "Failed to extract archive!");
    }
    return returnValue;
}

From source file:Main.java

public static void copyFile(String sourcePath, String toPath) {
    File sourceFile = new File(sourcePath);
    File targetFile = new File(toPath);
    createDipPath(toPath);//from  w  ww . jav a2 s  .  co m
    try {
        BufferedInputStream inBuff = null;
        BufferedOutputStream outBuff = null;
        try {
            inBuff = new BufferedInputStream(new FileInputStream(sourceFile));

            outBuff = new BufferedOutputStream(new FileOutputStream(targetFile));

            byte[] b = new byte[1024 * 5];
            int len;
            while ((len = inBuff.read(b)) != -1) {
                outBuff.write(b, 0, len);
            }
            outBuff.flush();
        } finally {
            if (inBuff != null)
                inBuff.close();
            if (outBuff != null)
                outBuff.close();
        }
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:outfox.dict.contest.util.FileUtils.java

/**
 * //from www .j  a  v  a 2 s .c  om
 * @param b
 * @param outputFile
 * @return
 */
public static File getFileFromBytes(byte[] b, String outputFile) {
    File file = null;
    BufferedOutputStream stream = null;
    try {

        file = new File(outputFile);
        FileOutputStream fstream = new FileOutputStream(file);
        stream = new BufferedOutputStream(fstream);
        stream.write(b);
    } catch (Exception e) {
        LOG.error("FileUtils.getFileFromBytes in catch error...", e);
    } finally {
        if (stream != null) {
            try {
                stream.close();
            } catch (IOException e) {
                LOG.error("FileUtils.getFileFromBytes in finally error...", e);
            }
        }
    }
    return file;
}

From source file:com.ipcglobal.fredimport.util.FredUtils.java

/**
 * Creates a tar.gz file at the specified path with the contents of the specified directory.
 *
 * @param directoryPath the directory path
 * @param tarGzPath the tar gz path/*from ww  w. j av a  2s .c  o m*/
 * @throws IOException             If anything goes wrong
 */
public static void createTarGzOfDirectory(String directoryPath, String tarGzPath) throws IOException {
    FileOutputStream fOut = null;
    BufferedOutputStream bOut = null;
    GzipCompressorOutputStream gzOut = null;
    TarArchiveOutputStream tOut = null;
    try {
        fOut = new FileOutputStream(new File(tarGzPath));
        bOut = new BufferedOutputStream(fOut);
        gzOut = new GzipCompressorOutputStream(bOut);
        tOut = new TarArchiveOutputStream(gzOut);
        addFileToTarGz(tOut, directoryPath, "/");
    } finally {
        tOut.finish();
        tOut.close();
        gzOut.close();
        bOut.close();
        fOut.close();
    }
}

From source file:jp.co.opentone.bsol.linkbinder.util.AttachmentUtil.java

/**
 * ?????./* w w w  . j  a v a2  s .c  o m*/
 * @param randomId ?????????
 * @param baseName ?????. ?????
 * @param in ????
 * @throws IOException ??
 * @return ????
 */
public static String createTempporaryFile(String randomId, String baseName, InputStream in) throws IOException {

    String result = createFileName(randomId, baseName);
    InputStream bin = null;
    BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(result));
    try {
        bin = new BufferedInputStream(in);
        //CHECKSTYLE:OFF
        byte[] buf = new byte[1024 * 4];
        //CHECKSTYLE:ON
        int len;
        while ((len = bin.read(buf, 0, buf.length)) != -1) {
            out.write(buf, 0, len);
        }

        return result;
    } finally {
        if (bin != null) {
            bin.close();
        }
        out.close();
    }
}

From source file:com.microsoft.intellij.AzurePlugin.java

private static void extractFile(ZipInputStream zipIn, String filePath) throws IOException {
    BufferedOutputStream bos = new BufferedOutputStream(new java.io.FileOutputStream(filePath));
    byte[] bytesIn = new byte[1024 * 10];
    int read = 0;
    while ((read = zipIn.read(bytesIn)) != -1) {
        bos.write(bytesIn, 0, read);// w  w  w  .  jav a 2  s  . co  m
    }
    bos.close();
}

From source file:fr.paris.lutece.plugins.updater.service.UpdateService.java

/**
 * Copies data from an input stream to an output stream.
 * @param inStream The input stream//from   ww w  .  j a v a 2s .com
 * @param outStream The output stream
 * @throws IOException If an I/O error occurs
 */
private static void copyStream(InputStream inStream, OutputStream outStream) throws IOException {
    BufferedInputStream inBufferedStream = new BufferedInputStream(inStream);
    BufferedOutputStream outBufferedStream = new BufferedOutputStream(outStream);

    int nByte;

    while ((nByte = inBufferedStream.read()) > -1) {
        outBufferedStream.write(nByte);
    }

    outBufferedStream.close();
    inBufferedStream.close();
}

From source file:Main.java

public static void copyFile(File oldLocation, File newLocation) throws IOException {
    if (oldLocation.exists()) {
        BufferedInputStream reader = new BufferedInputStream(new FileInputStream(oldLocation));
        BufferedOutputStream writer = new BufferedOutputStream(new FileOutputStream(newLocation, false));
        try {//from w  w  w.j a  va2  s .  co m
            byte[] buff = new byte[8192];
            int numChars;
            while ((numChars = reader.read(buff, 0, buff.length)) != -1) {
                writer.write(buff, 0, numChars);
            }
        } catch (IOException ex) {
            throw new IOException(
                    "IOException when transferring " + oldLocation.getPath() + " to " + newLocation.getPath());
        } finally {
            try {
                if (reader != null) {
                    writer.close();
                    reader.close();
                }
            } catch (IOException ex) {
                Log.e(TAG, "Error closing files when transferring " + oldLocation.getPath() + " to "
                        + newLocation.getPath());
            }
        }
    } else {
        throw new IOException("Old location does not exist when transferring " + oldLocation.getPath() + " to "
                + newLocation.getPath());
    }
}

From source file:com.jaspersoft.jasperserver.war.OlapGetChart.java

/**
 * Binary streams the specified file to the HTTP response in 1KB chunks
 *
 * @param file The file to be streamed.//from  w w  w. j a  va  2  s.c om
 * @param response The HTTP response object.
 * @param mimeType The mime type of the file, null allowed.
 *
 * @throws IOException  if there is an I/O problem.
 * @throws FileNotFoundException  if the file is not found.
 */
public static void sendTempFile(File file, HttpServletResponse response, String mimeType)
        throws IOException, FileNotFoundException {

    if (file.exists()) {
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));

        //  Set HTTP headers
        if (mimeType != null) {
            response.setHeader("Content-Type", mimeType);
        }
        response.setHeader("Content-Length", String.valueOf(file.length()));
        SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z");
        response.setHeader("Last-Modified", sdf.format(new Date(file.lastModified())));

        BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream());
        byte[] input = new byte[1024];
        boolean eof = false;
        while (!eof) {
            int length = bis.read(input);
            if (length == -1) {
                eof = true;
            } else {
                bos.write(input, 0, length);
            }
        }
        bos.flush();
        bis.close();
        bos.close();
    } else {
        throw new FileNotFoundException(file.getAbsolutePath());
    }
    return;
}

From source file:net.vexelon.myglob.utils.Utils.java

/**
 * Write input stream data to PRIVATE internal storage file.
 * @param context/*from www . j  a v  a  2  s .  c o m*/
 * @param source
 * @param internalStorageName
 * @throws IOException
 */
public static void writeToInternalStorage(Context context, InputStream source, String internalStorageName)
        throws IOException {

    FileOutputStream fos = null;
    try {
        fos = context.openFileOutput(internalStorageName, Context.MODE_PRIVATE);
        BufferedOutputStream bos = new BufferedOutputStream(fos);

        BufferedInputStream bis = new BufferedInputStream(source);
        byte[] buffer = new byte[4096];
        int read = -1;
        while ((read = bis.read(buffer)) != -1) {
            bos.write(buffer, 0, read);
        }
        bos.flush();
        bos.close();
    } catch (FileNotFoundException e) {
        throw new IOException(e.getMessage());
    } finally {
        try {
            if (fos != null)
                fos.close();
        } catch (IOException e) {
        }
        try {
            if (source != null)
                source.close();
        } catch (IOException e) {
        }
    }
}