Example usage for java.io BufferedOutputStream BufferedOutputStream

List of usage examples for java.io BufferedOutputStream BufferedOutputStream

Introduction

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

Prototype

public BufferedOutputStream(OutputStream out) 

Source Link

Document

Creates a new buffered output stream to write data to the specified underlying output stream.

Usage

From source file:Main.java

public static File getFileFromBytes(byte[] b, String outputFile) {
    File ret = null;/*from  w  w w .ja v a2 s.c  o  m*/
    BufferedOutputStream stream = null;
    try {
        ret = new File(outputFile);
        if (!ret.exists()) {
            if (!ret.mkdirs()) {
                throw new FileNotFoundException("can't create folder" + outputFile);
            }

        }
        FileOutputStream fstream = new FileOutputStream(ret);
        stream = new BufferedOutputStream(fstream);
        stream.write(b);
    } catch (Exception e) {
        // log.error("helper:get file from byte process error!");
        e.printStackTrace();
    } finally {
        if (stream != null) {
            try {
                stream.close();
            } catch (IOException e) {
                // log.error("helper:get file from byte process error!");
                e.printStackTrace();
            }
        }
    }
    return ret;
}

From source file:Main.java

public static void saveImageToSD(Context ctx, String filePath, Bitmap bitmap, int quality) throws IOException {
    if (bitmap != null) {
        File file = new File(filePath.substring(0, filePath.lastIndexOf(File.separator)));
        if (!file.exists()) {
            file.mkdirs();/*from www.j ava2  s. c o m*/
        }
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));
        bitmap.compress(CompressFormat.JPEG, quality, bos);
        bos.flush();
        bos.close();
        if (ctx != null) {
            scanPhoto(ctx, filePath);
        }
    }
}

From source file:Main.java

private static final void copyInputStream(InputStream in, String fileName) throws FileNotFoundException {
    File file = new File(fileName);
    File parentFile = file.getParentFile();
    parentFile.mkdirs();/*from  w w w. j a v a2  s . co  m*/
    System.out.println("Creating parent directory... " + parentFile.getAbsolutePath());
    byte[] buffer = new byte[1024];
    FileOutputStream fos = new FileOutputStream(file);
    BufferedOutputStream out = new BufferedOutputStream(fos);
    int len;
    try {
        while ((len = in.read(buffer)) >= 0) {
            out.write(buffer, 0, len);
        }
    } catch (IOException ex) {

    } finally {
        try {
            in.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            out.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}

From source file:Main.java

public static boolean downloadFile(File file, URL url) throws IOException {
    BufferedInputStream bis = null;
    BufferedOutputStream bos = null;
    HttpURLConnection conn = null;
    try {//from  w w w.j a  v  a  2 s.c  o m
        conn = (HttpURLConnection) url.openConnection();
        bis = new BufferedInputStream(conn.getInputStream());
        bos = new BufferedOutputStream(new FileOutputStream(file));

        int contentLength = conn.getContentLength();

        byte[] buffer = new byte[BUFFER_SIZE];
        int read = 0;
        int count = 0;
        while ((read = bis.read(buffer)) != -1) {
            bos.write(buffer, 0, read);
            count += read;
        }
        if (count < contentLength)
            return false;
        int responseCode = conn.getResponseCode();
        if (responseCode / 100 != 2) {
            // error
            return false;
        }
        // finished
        return true;
    } finally {
        try {
            bis.close();
            bos.close();
            conn.disconnect();
        } catch (Exception e) {
        }
    }
}

From source file:Main.java

public static void saveImageToSD(Context ctx, String filePath, Bitmap bitmap, int quality) throws IOException {
    if (bitmap != null) {
        File file = new File(filePath);
        File dir = new File(filePath.substring(0, filePath.lastIndexOf(File.separator)));
        if (!dir.exists()) {
            dir.mkdirs();//from  w w w .  ja va2 s .c o  m
        }
        file.createNewFile();

        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
        bitmap.compress(Bitmap.CompressFormat.JPEG, quality, bos);
        bos.flush();
        bos.close();
    }
}

From source file:Main.java

public static boolean saveFile(Bitmap bm, String fileDir, String fileName) {
    try {//from w  w  w. j a v  a 2  s  . c om
        File dirFile = new File(fileDir);
        if (!dirFile.exists()) {
            dirFile.mkdirs();
        }
        File myCaptureFile = new File(fileDir, fileName + ".jpg");
        if (myCaptureFile.exists()) {
            myCaptureFile = new File(fileDir, fileName + System.currentTimeMillis() + ".jpg");
        }
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));
        bm.compress(CompressFormat.JPEG, 100, bos);
        bos.flush();
        bos.close();
        return true;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}

From source file:Main.java

public static void copyToMemory(Context context, String srcFilePath, String dictFileName) throws IOException {
    File srcFile = new File(srcFilePath);
    if (!srcFile.exists() || srcFile.isDirectory()) {
        return;//w ww.  jav a2  s . c o  m
    }
    BufferedInputStream inBufferedInputStream = new BufferedInputStream(new FileInputStream(srcFile));
    FileOutputStream fos = context.openFileOutput(dictFileName, 0);
    BufferedOutputStream bos = new BufferedOutputStream(fos);
    byte[] b = new byte[1024 * 4];
    int len;
    while ((len = inBufferedInputStream.read(b)) != -1) {
        bos.write(b, 0, len);
        bos.flush();
    }
    inBufferedInputStream.close();
    bos.close();
}

From source file:Main.java

public static void fastBufferFileCopy(File source, File target) {
    BufferedInputStream bis = null;
    BufferedOutputStream bos = null;
    long start = System.currentTimeMillis();
    FileInputStream fis = null;/*w  ww  .j a va  2 s .co m*/
    FileOutputStream fos = null;
    long size = source.length();
    try {
        fis = new FileInputStream(source);
        bis = new BufferedInputStream(fis);
        fos = new FileOutputStream(target);
        bos = new BufferedOutputStream(fos);

        byte[] barr = new byte[Math.min((int) size, 1 << 20)];
        int read = 0;
        while ((read = bis.read(barr)) != -1) {
            bos.write(barr, 0, read);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        close(fis);
        close(fos);
        close(bis);
        close(bos);
    }
    long end = System.currentTimeMillis();

    String srcPath = source.getAbsolutePath();
    Log.d("Copied " + srcPath + " to " + target,
            ", took " + (end - start) / 1000 + " ms, total size " + nf.format(size) + " Bytes, speed "
                    + ((end - start > 0) ? nf.format(size / (end - start)) : 0) + "KB/s");
}

From source file:Main.java

public static void zip(String zipFileName, String[] zipEntries) {

    try (ZipOutputStream zos = new ZipOutputStream(
            new BufferedOutputStream(new FileOutputStream(zipFileName)))) {

        // Set the compression level to best compression
        zos.setLevel(Deflater.BEST_COMPRESSION);

        for (int i = 0; i < zipEntries.length; i++) {
            File entryFile = new File(zipEntries[i]);
            if (!entryFile.exists()) {
                System.out.println("The entry file  " + entryFile.getAbsolutePath() + "  does  not  exist");
                System.out.println("Aborted   processing.");
                return;
            }//from  w  w w  . j  a va  2  s  .c  om
            ZipEntry ze = new ZipEntry(zipEntries[i]);
            zos.putNextEntry(ze);
            addEntryContent(zos, zipEntries[i]);
            zos.closeEntry();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void serialize(Object object, OutputStream outputStream) {
    try {//  w  ww.j  a v  a  2  s  .c  o m
        final OutputStream buffer = new BufferedOutputStream(outputStream);
        final ObjectOutput output = new ObjectOutputStream(buffer);
        try {
            output.writeObject(object);
        } finally {
            output.close();
        }
    } catch (final IOException e) {
        throw new RuntimeException(e);
    }
}