Example usage for java.io BufferedInputStream BufferedInputStream

List of usage examples for java.io BufferedInputStream BufferedInputStream

Introduction

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

Prototype

public BufferedInputStream(InputStream in) 

Source Link

Document

Creates a BufferedInputStream and saves its argument, the input stream in, for later use.

Usage

From source file:Main.java

public static boolean copyFile(final File srcFile, final File saveFile) {
    File parentFile = saveFile.getParentFile();
    if (!parentFile.exists()) {
        if (!parentFile.mkdirs())
            return false;
    }//  ww w  . ja v a2  s  .com

    BufferedInputStream inputStream = null;
    BufferedOutputStream outputStream = null;
    try {
        inputStream = new BufferedInputStream(new FileInputStream(srcFile));
        outputStream = new BufferedOutputStream(new FileOutputStream(saveFile));
        byte[] buffer = new byte[1024 * 4];
        int len;
        while ((len = inputStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, len);
        }
        outputStream.flush();
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    } finally {
        close(inputStream, outputStream);
    }
    return true;
}

From source file:Main.java

public static Document parseXml(InputStream in) throws IOException, SAXException, ParserConfigurationException {
    DocumentBuilder builder = getBuilder();
    InputStream bin = new BufferedInputStream(in);
    Document ret = builder.parse(new InputSource(bin));
    return ret;/*from ww  w .  j a  v a 2s . com*/
}

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  .j  a v  a2s. c  o  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:Main.java

public static byte[] downloadImage(String imageUrl) {
    if (imageUrl.endsWith(".jpg") || imageUrl.endsWith(".bmp") || imageUrl.endsWith(".png")
            || imageUrl.endsWith(".gif")) {
        try {//  w  w w.  j  a v a  2s  .  c om
            URL url = new URL(imageUrl);
            URLConnection urlConn = url.openConnection();
            InputStream is = urlConn.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(is);
            ByteArrayBuffer baf = new ByteArrayBuffer(50);
            int current = 0;

            while ((current = bis.read()) != -1) {
                baf.append((byte) current);
            }

            return baf.toByteArray();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    return null;
}

From source file:Main.java

public static Document createDocument(File file)
        throws SAXException, ParserConfigurationException, IOException {
    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
    return createDocument(bis);
}

From source file:Main.java

public static boolean copyFile(File src, File tar) throws Exception {
    if (src.isFile()) {
        InputStream is = new FileInputStream(src);
        OutputStream op = new FileOutputStream(tar);
        BufferedInputStream bis = new BufferedInputStream(is);
        BufferedOutputStream bos = new BufferedOutputStream(op);
        byte[] bt = new byte[1024 * 8];
        int len = bis.read(bt);
        while (len != -1) {
            bos.write(bt, 0, len);/*  www  . j  a  v a2  s. c  o  m*/
            len = bis.read(bt);
        }
        bis.close();
        bos.close();
    }
    if (src.isDirectory()) {
        File[] f = src.listFiles();
        tar.mkdir();
        for (int i = 0; i < f.length; i++) {
            copyFile(f[i].getAbsoluteFile(), new File(tar.getAbsoluteFile() + File.separator + f[i].getName()));
        }
    }
    return true;
}

From source file:Main.java

/**
 * parse a certificate file into ArrayList of certificates
 *//*w ww  . j  av a  2s  . c om*/
public static ArrayList<Certificate> readCertificate(File f) throws CertificateException {
    ArrayList<Certificate> certs = new ArrayList<Certificate>();
    CertificateFactory cf = CertificateFactory.getInstance("X.509");
    BufferedInputStream in;
    try {
        in = new BufferedInputStream(new FileInputStream(f));
        while (in.available() > 0) {
            Certificate cert = cf.generateCertificate(in);
            certs.add(cert);
        }
        in.close();
        return certs;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

/**
 * Saves a file from the given URL using HTTPS to the given filename and returns the file
 * @param link URL to file/*w w  w. ja  va  2  s .c  om*/
 * @param fileName Name to save the file
 * @return The file
 * @throws IOException Thrown if any IOException occurs
 */
public static void saveFileFromNetHTTPS(URL link, String fileName) throws IOException {
    HttpsURLConnection con = (HttpsURLConnection) link.openConnection();

    InputStream in = new BufferedInputStream(con.getInputStream());
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    byte[] buf = new byte[1024];
    int n = 0;
    while (-1 != (n = in.read(buf))) {
        out.write(buf, 0, n);
    }
    out.close();
    in.close();
    byte[] response = out.toByteArray();

    File f = new File(fileName);
    if (f.getParentFile() != null) {
        if (f.getParentFile().mkdirs()) {
            System.out.println("Created Directory Structure");
        }
    }

    FileOutputStream fos = new FileOutputStream(f);
    fos.write(response);
    fos.close();
}

From source file:Main.java

public static Bitmap loadBitmap(String url) {
    Log.d(TAG, "Start Load Url : " + url);
    //Bitmap bitmap = null;
    InputStream in = null;/* w  w  w .  j a va2s . c  o m*/
    BufferedOutputStream out = null;
    try {
        in = new BufferedInputStream(new URL(url).openStream());
        final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
        int nRead;
        byte[] data = new byte[16384];

        while ((nRead = in.read(data, 0, data.length)) != -1) {
            dataStream.write(data, 0, nRead);
        }
        dataStream.flush();
        final byte[] newData = dataStream.toByteArray();
        BitmapFactory.Options options = new BitmapFactory.Options();
        bitmapResult = BitmapFactory.decodeByteArray(newData, 0, newData.length, options);
    } catch (IOException e) {
        Log.e("My fault", "Could not load Bitmap from: " + url);
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {

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

From source file:Main.java

public static void unZip(String path) {
    int count = -1;
    int index = -1;

    String savepath = "";

    savepath = path.substring(0, path.lastIndexOf("."));
    try {/*from   w ww . ja v  a 2 s . com*/
        BufferedOutputStream bos = null;
        ZipEntry entry = null;
        FileInputStream fis = new FileInputStream(path);
        ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));

        while ((entry = zis.getNextEntry()) != null) {
            byte data[] = new byte[buffer];

            String temp = entry.getName();
            index = temp.lastIndexOf("/");
            if (index > -1)
                temp = temp.substring(index + 1);
            String tempDir = savepath + "/zip/";
            File dir = new File(tempDir);
            dir.mkdirs();
            temp = tempDir + temp;
            File f = new File(temp);
            f.createNewFile();

            FileOutputStream fos = new FileOutputStream(f);
            bos = new BufferedOutputStream(fos, buffer);

            while ((count = zis.read(data, 0, buffer)) != -1) {
                bos.write(data, 0, count);
            }

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

        zis.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
}