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

public static boolean saveFileFromAssetsToSystem(Context context, String path, File destFile) {
    BufferedInputStream bis = null;
    BufferedOutputStream fos = null;
    try {/* w w w .  j  ava 2 s.c o m*/
        InputStream is = context.getAssets().open(path);
        bis = new BufferedInputStream(is);
        fos = new BufferedOutputStream(new FileOutputStream(destFile));
        byte[] buf = new byte[2048];
        int i;
        while ((i = bis.read(buf)) != -1) {
            fos.write(buf, 0, i);
        }
        fos.flush();
        return true;
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (bis != null) {
                bis.close();
            }
            if (fos != null) {
                fos.close();
            }
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }
    return false;
}

From source file:com.geewhiz.pacify.test.TestUtil.java

private static ByteArrayOutputStream readContent(ArchiveInputStream ais) throws IOException {
    byte[] content = new byte[2048];
    ByteArrayOutputStream result = new ByteArrayOutputStream();
    BufferedOutputStream bos = new BufferedOutputStream(result);

    int len;/*from w  w w . j  a v  a2 s.  c o m*/
    while ((len = ais.read(content)) != -1) {
        bos.write(content, 0, len);
    }
    bos.close();
    content = null;

    return result;
}

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);/*w w w.j  a v  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:de.rub.syssec.saaf.analysis.steps.extract.ApkUnzipper.java

/**
 * Extracts the given archive into the given destination directory
 * /*from   w  w w. j a va2s.  co  m*/
 * @param archive
 *            - the file to extract
 * @param dest
 *            - the destination directory
 * @throws Exception
 */
public static void extractArchive(File archive, File destDir) throws IOException {

    if (!destDir.exists()) {
        destDir.mkdir();
    }

    ZipFile zipFile = new ZipFile(archive);
    Enumeration<? extends ZipEntry> entries = zipFile.entries();

    byte[] buffer = new byte[16384];
    int len;
    while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();

        String entryFileName = entry.getName();

        File dir = buildDirectoryHierarchyFor(entryFileName, destDir);
        if (!dir.exists()) {
            dir.mkdirs();
        }

        if (!entry.isDirectory()) {

            File file = new File(destDir, entryFileName);

            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));

            BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(entry));

            while ((len = bis.read(buffer)) > 0) {
                bos.write(buffer, 0, len);
            }

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

From source file:org.openo.nfvo.jujuvnfmadapter.common.DownloadCsarManager.java

/**
 * unzip CSAR packge/*from  www  .  ja  va  2 s  .  c o  m*/
 * @param fileName filePath
 * @return
 */
public static int unzipCSAR(String fileName, String filePath) {
    final int BUFFER = 2048;
    int status = 0;

    try {
        ZipFile zipFile = new ZipFile(fileName);
        Enumeration emu = zipFile.entries();
        int i = 0;
        while (emu.hasMoreElements()) {
            ZipEntry entry = (ZipEntry) emu.nextElement();
            //read directory as file first,so only need to create directory
            if (entry.isDirectory()) {
                new File(filePath + entry.getName()).mkdirs();
                continue;
            }
            BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(entry));
            File file = new File(filePath + entry.getName());
            //Because that is random to read zipfile,maybe the file is read first
            //before the directory is read,so we need to create directory first.
            File parent = file.getParentFile();
            if (parent != null && (!parent.exists())) {
                parent.mkdirs();
            }
            FileOutputStream fos = new FileOutputStream(file);
            BufferedOutputStream bos = new BufferedOutputStream(fos, BUFFER);

            int count;
            byte data[] = new byte[BUFFER];
            while ((count = bis.read(data, 0, BUFFER)) != -1) {
                bos.write(data, 0, count);
            }
            bos.flush();
            bos.close();
            bis.close();

            if (entry.getName().endsWith(".zip")) {
                File subFile = new File(filePath + entry.getName());
                if (subFile.exists()) {
                    int subStatus = unzipCSAR(filePath + entry.getName(), subFile.getParent() + "/");
                    if (subStatus != 0) {
                        LOG.error("sub file unzip fail!" + subFile.getName());
                        status = Constant.UNZIP_FAIL;
                        return status;
                    }
                }
            }
        }
        status = Constant.UNZIP_SUCCESS;
        zipFile.close();
    } catch (Exception e) {
        status = Constant.UNZIP_FAIL;
        e.printStackTrace();
    }
    return status;
}

From source file:com.dc.util.file.FileSupport.java

public static void writeBinaryFile(File file, byte[] data) throws IOException {
    FileOutputStream fileOutputStream = null;
    BufferedOutputStream bufferedOutputStream = null;
    try {// w ww  . j  a v  a  2s.  c  o  m
        fileOutputStream = new FileOutputStream(file);
        bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
        bufferedOutputStream.write(data, 0, data.length);
        bufferedOutputStream.flush();
    } finally {
        if (bufferedOutputStream != null) {
            fileOutputStream.close();
        }
        if (bufferedOutputStream != null) {
            bufferedOutputStream.close();
        }
    }
}

From source file:Main.java

public static void copyDatabase2FileDir(Context context, String dbName) {
    InputStream stream = null;/*ww w  .  j  a  v a  2s  .co  m*/
    BufferedOutputStream outputStream = null;
    try {

        stream = context.getAssets().open(dbName);
        File file = new File(context.getFilesDir(), dbName);

        outputStream = new BufferedOutputStream(new FileOutputStream(file));
        int len = 0;
        byte[] buf = new byte[1024];
        while ((len = stream.read(buf)) != -1) {
            outputStream.write(buf, 0, len);
            outputStream.flush();
        }

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

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

From source file:eu.scape_project.spacip.ContainerProcessing.java

/**
 * Write ARC record content to output stream
 *
 * @param nativeArchiveRecord//  w  w w . jav a 2 s . c  o  m
 * @param outputStream Output stream
 * @throws IOException
 */
public static void recordToOutputStream(ArchiveRecord nativeArchiveRecord, OutputStream outputStream)
        throws IOException {
    ARCRecord arcRecord = (ARCRecord) nativeArchiveRecord;
    ARCRecordMetaData metaData = arcRecord.getMetaData();
    long contentBegin = metaData.getContentBegin();
    BufferedInputStream bis = new BufferedInputStream(arcRecord);
    BufferedOutputStream bos = new BufferedOutputStream(outputStream);
    byte[] tempBuffer = new byte[BUFFER_SIZE];
    int bytesRead;
    // skip record header
    bis.skip(contentBegin);
    while ((bytesRead = bis.read(tempBuffer)) != -1) {
        bos.write(tempBuffer, 0, bytesRead);
    }
    bos.flush();
    bis.close();
    bos.close();
}

From source file:Main.java

public static Bitmap getBitmapFromURL(String urlString) {
    Bitmap bitmap = null;/*from w w w . ja  v  a2  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:it.geosolutions.tools.compress.file.reader.TarReader.java

private static void writeFile(File curr_dest, TarArchiveInputStream tis) throws IOException {
    byte[] buf = new byte[Conf.getBufferSize()];
    FileOutputStream fos = null;/*from ww  w  .  j  a  v  a2 s  .co m*/
    BufferedOutputStream bos_inner = null;
    try {
        fos = new FileOutputStream(curr_dest);
        bos_inner = new BufferedOutputStream(fos, Conf.getBufferSize());
        int read_sz = 0;
        while ((read_sz = tis.read(buf)) != -1) {
            bos_inner.write(buf, 0, read_sz);
            bos_inner.flush();
        }
        //        } catch (IOException e){
    } finally {
        if (bos_inner != null) {
            try {
                bos_inner.close();
            } catch (IOException e) {
            }
        }
        if (fos != null) {
            try {
                fos.close();
            } catch (IOException e) {
            }
        }
    }
}