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 byte[] downloadImageFromURL(String strUrl) throws Exception {
    InputStream in;/*from  w w  w . j  a v a  2 s  . c om*/
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    URL url = new URL(strUrl);
    in = new BufferedInputStream(url.openStream());
    byte[] buf = new byte[2048];
    int n = 0;
    while (-1 != (n = in.read(buf))) {
        out.write(buf, 0, n);
    }
    out.close();
    in.close();
    byte[] response = out.toByteArray();
    FileOutputStream fos = new FileOutputStream("/Users/image.jpg");
    fos.write(response);
    fos.close();
    return response;
}

From source file:Main.java

public static Bitmap revitionImageSize(String path) throws IOException {
    BufferedInputStream in = new BufferedInputStream(new FileInputStream(new File(path)));
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;/*ww  w  .  java  2 s. c  o  m*/
    BitmapFactory.decodeStream(in, null, options);
    in.close();
    int i = 0;
    Bitmap bitmap = null;
    while (true) {
        if ((options.outWidth >> i <= 1000) && (options.outHeight >> i <= 1000)) {
            in = new BufferedInputStream(new FileInputStream(new File(path)));
            options.inSampleSize = (int) Math.pow(2.0D, i);
            options.inJustDecodeBounds = false;
            bitmap = BitmapFactory.decodeStream(in, null, options);
            break;
        }
        i += 1;
    }
    return bitmap;
}

From source file:Main.java

public static byte[] readFromInputStream(InputStream inputStream) throws IOException {

    if (inputStream == null)
        return null;

    BufferedInputStream bin = new BufferedInputStream(inputStream);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();

    byte[] buffer = new byte[1024 * 8];
    int len = 0;//from w  ww  .j  av a  2  s. c  om

    while ((len = bin.read(buffer)) != -1) {
        bos.write(buffer, 0, len);
    }

    bin.close();

    return bos.toByteArray();
}

From source file:Main.java

public static InputStream openCacheFileForInput(File root, String... path) throws FileNotFoundException {
    return new BufferedInputStream(new FileInputStream(openPath(false, root, path)));
}

From source file:Main.java

public static Bitmap revitionImageSize(String path) {
    Bitmap bitmap = null;/*from   w w w  . jav a2  s  .  com*/
    try {
        BufferedInputStream in = new BufferedInputStream(new FileInputStream(new File(path)));
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(in, null, options);
        in.close();
        int i = 0;

        while (true) {
            if ((options.outWidth >> i <= 1000) && (options.outHeight >> i <= 1000)) {
                in = new BufferedInputStream(new FileInputStream(new File(path)));
                options.inSampleSize = (int) Math.pow(2.0D, i);
                options.inJustDecodeBounds = false;
                bitmap = BitmapFactory.decodeStream(in, null, options);
                break;
            }
            i += 1;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return bitmap;
}

From source file:Main.java

public static String getStringFromConnection(HttpURLConnection connection) throws IOException {
    InputStream inputStream = new BufferedInputStream(connection.getInputStream());
    String jsonString = convertStreamToString(inputStream);
    inputStream.close();//from  www  .  ja v a 2  s  . c  o m

    return jsonString;
}

From source file:Util.java

public static byte[] readFile(File file) throws IOException {
    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
    int bytes = (int) file.length();
    byte[] buffer = new byte[bytes];

    int readBytes = bis.read(buffer);
    bis.close();/*from w ww. j  av  a 2s.  co  m*/
    return buffer;
}

From source file:Main.java

private static void compressFile(File file, ZipOutputStream out, String basedir) {
    if (!file.exists()) {
        return;/*  w w  w .  j  a v  a2s.c o  m*/
    }
    try {
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
        ZipEntry entry = new ZipEntry(basedir + file.getName());
        out.putNextEntry(entry);
        int count;
        byte data[] = new byte[BUFFER];
        while ((count = bis.read(data, 0, BUFFER)) != -1) {
            out.write(data, 0, count);
        }
        bis.close();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static Bitmap bitmapFromFile(File file) throws FileNotFoundException {
    return BitmapFactory.decodeStream(new BufferedInputStream(new FileInputStream(file)));
}

From source file:MainClass.java

public static String md(String f) throws Exception {
    BufferedInputStream file = new BufferedInputStream(new FileInputStream(f));
    MessageDigest md = MessageDigest.getInstance("MD5");
    DigestInputStream in = new DigestInputStream(file, md);
    int i;//from   w  ww.j a v  a2  s  .co m
    byte[] buffer = new byte[BUFFER_SIZE];
    do {
        i = in.read(buffer, 0, BUFFER_SIZE);
    } while (i == BUFFER_SIZE);
    md = in.getMessageDigest();
    in.close();

    return new String(md.digest());
}