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 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 {//w  w  w  . j av  a2 s . com
            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:Main.java

public static void unzip(File zipFile, File targetDirectory) throws IOException {
    ZipInputStream zis = new ZipInputStream(new BufferedInputStream(new FileInputStream(zipFile)));
    try {//w  ww.ja v a  2 s .  c o m
        ZipEntry ze;
        int count;
        byte[] buffer = new byte[8192];
        while ((ze = zis.getNextEntry()) != null) {
            File file = new File(targetDirectory, ze.getName());
            File dir = ze.isDirectory() ? file : file.getParentFile();
            if (!dir.isDirectory() && !dir.mkdirs())
                throw new FileNotFoundException("Failed to ensure directory: " + dir.getAbsolutePath());
            if (ze.isDirectory())
                continue;
            FileOutputStream fout = new FileOutputStream(file);
            try {
                while ((count = zis.read(buffer)) != -1)
                    fout.write(buffer, 0, count);
            } finally {
                fout.close();
            }
        }
    } finally {
        zis.close();
    }
}

From source file:Main.java

public static Object readBeanFromXml(String path) throws Exception {
    FileInputStream fis = null;//from   w w w. j  a v  a  2 s .  c  o  m
    Object aplicacion = null;
    BufferedInputStream bis = null;
    try {
        fis = new FileInputStream(path);
        bis = new BufferedInputStream(fis);
        XMLDecoder xmlDecoder = new XMLDecoder(bis);
        aplicacion = (Object) xmlDecoder.readObject();

    } catch (FileNotFoundException ex) {
        throw new Exception("File to read not found.", ex);
    } finally {
        if (fis != null) {
            try {
                fis.close();
            } catch (IOException ex) {
                throw new Exception("File to close FileInputStream after read.", ex);
            }
        }

        if (bis != null) {
            try {
                bis.close();
            } catch (IOException ex) {
                throw new Exception("File to close BufferedInputStream after read.", ex);
            }
        }
    }

    return aplicacion;

}

From source file:Main.java

public static byte[] readFile(File file) throws IOException {
    int len = (int) file.length();
    if (len == 0) {
        return new byte[0];
    }/*from  w w  w.j ava 2 s  .  co m*/

    byte[] data = null;
    BufferedInputStream bis = null;
    try {
        FileInputStream fis = new FileInputStream(file);
        bis = new BufferedInputStream(fis);
        data = new byte[len];
        bis.read(data);
    } finally {
        if (bis != null) {
            try {
                bis.close();
            } catch (IOException e) {
            }
        }
    }

    return data;
}

From source file:Main.java

public static Bitmap getBitmap(String biturl) {

    Bitmap bitmap = null;//from ww w  .j a va2  s  .c  om

    try {

        URL url = new URL(biturl);
        URLConnection conn = url.openConnection();
        InputStream in = conn.getInputStream();
        bitmap = BitmapFactory.decodeStream(new BufferedInputStream(in));

    } catch (Exception e) {

        e.printStackTrace();
    }
    return bitmap;

}

From source file:Main.java

static String getDataDir(final Context c) {
    final String defaultDataDir = String.format("%s/.bitcoin", getDir(c).getAbsolutePath());
    try {/*  w  ww.  j  a v a 2s .c o  m*/
        final Properties p = new Properties();
        p.load(new BufferedInputStream(new FileInputStream(getBitcoinConf(c))));
        return p.getProperty("datadir", defaultDataDir);
    } catch (final IOException e) {
        return defaultDataDir;
    }
}

From source file:Main.java

public static void unzip(InputStream fin, String targetPath, Context context) throws IOException {
    ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fin));
    try {// w  w  w.j  av  a2  s  . com
        ZipEntry zipEntry;
        int count;
        byte[] buffer = new byte[8192];
        while ((zipEntry = zis.getNextEntry()) != null) {
            File file = new File(targetPath, zipEntry.getName());
            File dir = zipEntry.isDirectory() ? file : file.getParentFile();
            if (!dir.isDirectory() && !dir.mkdirs()) {
                throw new FileNotFoundException("Failed to get directory: " + dir.getAbsolutePath());
            }
            if (zipEntry.isDirectory()) {
                continue;
            }
            FileOutputStream fout = new FileOutputStream(file);
            try {
                while ((count = zis.read(buffer)) != -1)
                    fout.write(buffer, 0, count);
            } finally {
                fout.close();
            }
            Log.d("TEST", "Unzipped " + file.getAbsolutePath());
        }
    } finally {
        zis.close();
    }
}

From source file:Main.java

public static void saveISToFile(InputStream is, String fileName) throws IOException {
    File file = new File(fileName);
    file.getParentFile().mkdirs();/*from   www .  j av  a  2 s .com*/
    File tempFile = new File(fileName + ".tmp");
    FileOutputStream fos = new FileOutputStream(tempFile);
    BufferedOutputStream bos = new BufferedOutputStream(fos);
    BufferedInputStream bis = new BufferedInputStream(is);
    byte[] barr = new byte[32768];
    int read = 0;
    while ((read = bis.read(barr)) > 0) {
        bos.write(barr, 0, read);
    }
    bis.close();
    bos.flush();
    fos.flush();
    bos.close();
    fos.close();
    file.delete();
    tempFile.renameTo(file);
}

From source file:com.sosee.util.PropertyUtil.java

public static String readValue(String key) {
    Properties props = new Properties();
    try {// w w w . j av  a  2 s.  co m
        InputStream in = new BufferedInputStream(new FileInputStream(getFilePath()));
        props.load(in);
        String value = props.getProperty(key);
        return value == null ? "" : value;
    } catch (Exception e) {
        return "";
    }
}

From source file:Main.java

public static String getResourceText(Context context, int resId) {
    InputStream is = null;/*from w w w. j a  v  a  2s  .  c o  m*/
    BufferedInputStream bis;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        is = context.getResources().openRawResource(resId);
        bis = new BufferedInputStream(is);
        int result = bis.read();
        while (result != -1) {
            byte b = (byte) result;
            baos.write(b);
            result = bis.read();
        }
    } catch (IOException e) {
    } finally {
        try {
            if (is != null)
                is.close();
        } catch (IOException e) {
        }
    }
    return baos.toString();
}