Example usage for java.io BufferedOutputStream write

List of usage examples for java.io BufferedOutputStream write

Introduction

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

Prototype

@Override
public synchronized void write(byte b[], int off, int len) throws IOException 

Source Link

Document

Writes len bytes from the specified byte array starting at offset off to this buffered output stream.

Usage

From source file:com.aurel.track.lucene.util.FileUtil.java

public static void unzipFile(File uploadZip, File unzipDestinationDirectory) throws IOException {
    if (!unzipDestinationDirectory.exists()) {
        unzipDestinationDirectory.mkdirs();
    }/*  ww  w  .ja v a  2s .  com*/
    final int BUFFER = 2048;
    // Open Zip file for reading
    ZipFile zipFile = new ZipFile(uploadZip, ZipFile.OPEN_READ);

    // Create an enumeration of the entries in the zip file
    Enumeration zipFileEntries = zipFile.entries();

    // Process each entry
    while (zipFileEntries.hasMoreElements()) {
        // grab a zip file entry
        ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();
        String currentEntry = entry.getName();
        File destFile = new File(unzipDestinationDirectory, currentEntry);
        // grab file's parent directory structure
        File destinationParent = destFile.getParentFile();

        // create the parent directory structure if needed
        destinationParent.mkdirs();

        // extract file if not a directory
        if (!entry.isDirectory()) {
            BufferedInputStream is = new BufferedInputStream(zipFile.getInputStream(entry));
            int currentByte;
            // establish buffer for writing file
            byte data[] = new byte[BUFFER];

            // write the current file to disk
            FileOutputStream fos = new FileOutputStream(destFile);
            BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER);

            // read and write until last byte is encountered
            while ((currentByte = is.read(data, 0, BUFFER)) != -1) {
                dest.write(data, 0, currentByte);
            }
            dest.flush();
            dest.close();
            is.close();
        }
    }
    zipFile.close();
}

From source file:com.twemyeez.picklr.InstallManager.java

public static void unzip() {

    // Firstly get the working directory
    String workingDirectory = Minecraft.getMinecraft().mcDataDir.getAbsolutePath();

    // If it ends with a . then remove it
    if (workingDirectory.endsWith(".")) {
        workingDirectory = workingDirectory.substring(0, workingDirectory.length() - 1);
    }//w  w  w .  jav a 2  s  . co  m

    // If it doesn't end with a / then add it
    if (!workingDirectory.endsWith("/") && !workingDirectory.endsWith("\\")) {
        workingDirectory = workingDirectory + "/";
    }

    // Use a test file to see if libraries installed
    File file = new File(workingDirectory + "mods/mp3spi1.9.5.jar");

    // If the libraries are installed, return
    if (file.exists()) {
        System.out.println("Checking " + file.getAbsolutePath());
        System.out.println("Target file exists, so not downloading API");
        return;
    }

    // Now try to download the libraries
    try {

        String location = "http://www.javazoom.net/mp3spi/sources/mp3spi1.9.5.zip";

        // Define the URL
        URL url = new URL(location);

        // Get the ZipInputStream
        ZipInputStream zipInput = new ZipInputStream(new BufferedInputStream((url).openStream()));

        // Use a temporary ZipEntry as a buffer
        ZipEntry zipFile;

        // While there are more file entries
        while ((zipFile = zipInput.getNextEntry()) != null) {
            // Check if it is one of the file names that we want to copy
            Boolean required = false;
            if (zipFile.getName().indexOf("mp3spi1.9.5.jar") != -1) {
                required = true;
            }
            if (zipFile.getName().indexOf("jl1.0.1.jar") != -1) {
                required = true;
            }
            if (zipFile.getName().indexOf("tritonus_share.jar") != -1) {
                required = true;
            }
            if (zipFile.getName().indexOf("LICENSE.txt") != -1) {
                required = true;
            }

            // If it is, then we shall now copy it
            if (!zipFile.getName().replace("MpegAudioSPI1.9.5/", "").equals("") && required) {

                // Get the file location
                String tempFile = new File(zipFile.getName()).getName();

                tempFile = tempFile.replace("LICENSE.txt", "MpegAudioLicence.txt");

                // Initialise the target file
                File targetFile = (new File(
                        workingDirectory + "mods/" + tempFile.replace("MpegAudioSPI1.9.5/", "")));

                // Print a debug/alert message
                System.out.println("Picklr is extracting to " + workingDirectory + "mods/"
                        + tempFile.replace("MpegAudioSPI1.9.5/", ""));

                // Make parent directories if required
                targetFile.getParentFile().mkdirs();

                // If the file does not exist, create it
                if (!targetFile.exists()) {
                    targetFile.createNewFile();
                }

                // Create a buffered output stream to the destination
                BufferedOutputStream destinationOutput = new BufferedOutputStream(
                        new FileOutputStream(targetFile, false), 2048);

                // Store the data read
                int bytesRead;

                // Data buffer
                byte dataBuffer[] = new byte[2048];

                // While there is still data to write
                while ((bytesRead = zipInput.read(dataBuffer, 0, 2048)) != -1) {
                    // Write it to the output stream
                    destinationOutput.write(dataBuffer, 0, bytesRead);
                }

                // Flush the output
                destinationOutput.flush();

                // Close the output stream
                destinationOutput.close();
            }

        }
        // Close the zip input
        zipInput.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

}

From source file:Main.java

public static boolean copyAssetFile(Context context, String originFileName, String destFilePath,
        String destFileName) {/*from   ww  w  .  ja v  a2 s. c om*/
    InputStream is = null;
    BufferedOutputStream bos = null;
    try {
        is = context.getAssets().open(originFileName);
        File destPathFile = new File(destFilePath);
        if (!destPathFile.exists()) {
            destPathFile.mkdirs();
        }

        File destFile = new File(destFilePath + File.separator + destFileName);
        if (!destFile.exists()) {
            destFile.createNewFile();
        }

        FileOutputStream fos = new FileOutputStream(destFile);
        bos = new BufferedOutputStream(fos);

        byte[] buffer = new byte[256];
        int length = 0;
        while ((length = is.read(buffer)) > 0) {
            bos.write(buffer, 0, length);
        }
        bos.flush();

        return true;
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        if (null != is) {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        if (null != bos) {
            try {
                bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    return false;
}

From source file:com.eviware.soapui.support.Tools.java

public static int copyFile(File source, File target, boolean overwrite) throws IOException {
    int bytes = 0;

    if (target.exists()) {
        if (overwrite) {
            target.delete();//from ww  w .  j  ava 2s. com
        } else {
            return -1;
        }
    } else {
        String path = target.getAbsolutePath();
        int ix = path.lastIndexOf(File.separatorChar);
        if (ix != -1) {
            path = path.substring(0, ix);
            File pathFile = new File(path);
            if (!pathFile.exists()) {
                pathFile.mkdirs();
            }
        }
    }

    BufferedInputStream in = new BufferedInputStream(new FileInputStream(source));
    BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(target));

    int read = in.read(copyBuffer);
    while (read != -1) {
        if (read > 0) {
            out.write(copyBuffer, 0, read);
            bytes += read;
        }
        read = in.read(copyBuffer);
    }

    in.close();
    out.close();

    return bytes;
}

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 w  w w . j a  v a  2s.c o  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:com.glaf.core.entity.mybatis.MyBatisSessionFactory.java

public static Map<String, byte[]> getZipBytesMap(ZipInputStream zipInputStream) {
    Map<String, byte[]> zipMap = new java.util.HashMap<String, byte[]>();
    java.util.zip.ZipEntry zipEntry = null;
    ByteArrayOutputStream baos = null;
    BufferedOutputStream bos = null;
    byte tmpByte[] = null;
    try {//from  w  w  w .j ava 2s .c  o m
        while ((zipEntry = zipInputStream.getNextEntry()) != null) {
            String name = zipEntry.getName();
            if (StringUtils.endsWith(name, "Mapper.xml")) {
                tmpByte = new byte[BUFFER];
                baos = new ByteArrayOutputStream();
                bos = new BufferedOutputStream(baos, BUFFER);
                int i = 0;
                while ((i = zipInputStream.read(tmpByte, 0, BUFFER)) != -1) {
                    bos.write(tmpByte, 0, i);
                }
                bos.flush();
                byte[] bytes = baos.toByteArray();
                IOUtils.closeStream(baos);
                IOUtils.closeStream(baos);
                zipMap.put(zipEntry.getName(), bytes);
            }
        }
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    } finally {
        IOUtils.closeStream(baos);
        IOUtils.closeStream(baos);
    }
    return zipMap;
}

From source file:Main.java

public static void downloadImage(String imageUrl) {
    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
        Log.d("TAG", "monted sdcard");
    } else {/*from   w w  w  .j av  a2s  .  c o  m*/
        Log.d("TAG", "has no sdcard");
    }
    HttpURLConnection con = null;
    FileOutputStream fos = null;
    BufferedOutputStream bos = null;
    BufferedInputStream bis = null;
    File imageFile = null;
    try {
        URL url = new URL(imageUrl);
        con = (HttpURLConnection) url.openConnection();
        con.setConnectTimeout(5 * 1000);
        con.setReadTimeout(15 * 1000);
        con.setDoInput(true);
        con.setDoOutput(true);
        bis = new BufferedInputStream(con.getInputStream());
        imageFile = new File(getImagePath(imageUrl));
        fos = new FileOutputStream(imageFile);
        bos = new BufferedOutputStream(fos);
        byte[] b = new byte[1024];
        int length;
        while ((length = bis.read(b)) != -1) {
            bos.write(b, 0, length);
            bos.flush();
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (bis != null) {
                bis.close();
            }
            if (bos != null) {
                bos.close();
            }
            if (con != null) {
                con.disconnect();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    if (imageFile != null) {
    }
}

From source file:edu.ku.brc.specify.web.HttpLargeFileTransfer.java

/**
 * @param infileName/*w  w  w  .  j  a va2s .  c o m*/
 * @param outFileName
 * @param changeListener
 * @return
 */
public static boolean uncompressFile(final String infileName, final String outFileName) {
    File file = new File(infileName);
    if (file.exists()) {
        long fileSize = file.length();
        if (fileSize > 0) {
            try {
                GZIPInputStream fis = new GZIPInputStream(new FileInputStream(infileName));
                BufferedOutputStream fos = new BufferedOutputStream(new FileOutputStream(outFileName));

                byte[] bytes = new byte[BUFFER_SIZE * 10];

                while (true) {
                    int len = fis.read(bytes);
                    if (len > 0) {
                        fos.write(bytes, 0, len);
                    } else {
                        break;
                    }
                }

                fis.close();
                fos.close();

                return true;

            } catch (IOException ex) {
                ex.printStackTrace();
            }
        } else {
            // file doesn't exist
        }
    } else {
        // file doesn't exist
    }
    return false;
}

From source file:fr.gael.dhus.server.http.TomcatServer.java

private static void expand(InputStream input, File file) throws IOException {
    BufferedOutputStream output = null;
    try {/*  w w w . j a va  2s . co  m*/
        output = new BufferedOutputStream(new FileOutputStream(file));
        byte buffer[] = new byte[2048];
        while (true) {
            int n = input.read(buffer);
            if (n <= 0) {
                break;
            }
            output.write(buffer, 0, n);
        }
    } finally {
        if (output != null) {
            try {
                output.close();
            } catch (IOException e) {
                // Ignore
            }
        }
    }
}

From source file:com.headswilllol.basiclauncher.Launcher.java

public static void unzip(ZipFile zip, ZipEntry entry, File dest) { // convenience method for unzipping from archive
    if (dest.exists())
        dest.delete();/*from w w w. j ava2s .co m*/
    dest.getParentFile().mkdirs();
    try {
        BufferedInputStream bIs = new BufferedInputStream(zip.getInputStream(entry));
        int b;
        byte buffer[] = new byte[1024];
        FileOutputStream fOs = new FileOutputStream(dest);
        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();
    } catch (Exception ex) {
        ex.printStackTrace();
        createExceptionLog(ex);
        progress = "Failed to unzip " + entry.getName();
        fail = "Errors occurred; see log file for details";
        launcher.paintImmediately(0, 0, width, height);
    }
}