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 String readStr(InputStream in, String charset) throws IOException {
    if (TextUtils.isEmpty(charset))
        charset = "UTF-8";

    if (!(in instanceof BufferedInputStream)) {
        in = new BufferedInputStream(in);
    }// w w  w .  j a  v a  2 s.c om
    Reader reader = new InputStreamReader(in, charset);
    StringBuilder sb = new StringBuilder();
    char[] buf = new char[1024];
    int len;
    while ((len = reader.read(buf)) >= 0) {
        sb.append(buf, 0, len);
    }
    return sb.toString().trim();
}

From source file:Main.java

/**
 * Saves a file from the given URL to the given filename and returns the file
 * @param link URL to file/*from w w w  . j a v  a  2  s.c  o  m*/
 * @param fileName Name to save the file
 * @return The file
 * @throws IOException Thrown if any IOException occurs
 */
public static File saveFileFromNet(URL link, String fileName) throws IOException {
    InputStream in = new BufferedInputStream(link.openStream());
    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 file = new File(fileName);
    if (!file.exists()) {
        if (file.getParentFile() != null) {
            file.getParentFile().mkdirs();
        }
        file.createNewFile();
    }
    FileOutputStream fos = new FileOutputStream(file);
    fos.write(response);
    fos.close();

    return new File(fileName);
}

From source file:Main.java

private static File saveFile(InputStream is, String destDir, String fileName) throws IOException {
    File dir = new File(destDir);
    fileName = getString(fileName);//from  w w  w  . j  a v  a 2  s.  c  o  m
    if (!dir.exists()) {
        dir.mkdirs();
    }
    BufferedInputStream bis = new BufferedInputStream(is);
    BufferedOutputStream bos = null;
    try {
        File saveFile = new File(destDir, fileName);
        bos = new BufferedOutputStream(new FileOutputStream(saveFile));
        int len = -1;
        while ((len = bis.read()) != -1) {
            bos.write(len);
            bos.flush();
        }
        bos.close();
        bis.close();
        return saveFile;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    return null;
}

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;/*from   www. jav a  2s .c  o 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

/**
 * Determine whether a file is a ZIP File.
 *//*from   w  w w.  j a  v a  2  s.  c  om*/
public static boolean isZipFile(File file) throws IOException {
    if (file.isDirectory()) {
        return false;
    }
    if (!file.canRead()) {
        throw new IOException("Cannot read file " + file.getAbsolutePath());
    }
    if (file.length() < 4) {
        return false;
    }
    DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream(file)));
    int test = in.readInt();
    in.close();
    return test == 0x504b0304;
}

From source file:Main.java

public static String getJsonCurrencyObject(String myurl) {
    StringBuilder strBuilder = new StringBuilder();

    try {/*from   w w w.  ja  v  a 2 s  .  c om*/
        URL url = new URL(myurl);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();

        InputStream inputStream = new BufferedInputStream(connection.getInputStream());

        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
        String line;
        while ((line = reader.readLine()) != null) {
            strBuilder.append(line);
        }
        inputStream.close();

    } catch (MalformedURLException ex) {
        Log.d("HttpURLConnection", "getJsonCurrencyObject: " + ex.getMessage());
    }

    catch (IOException e) {
        Log.d("readJSONFeed", e.getLocalizedMessage());
        e.printStackTrace();
    } catch (Exception ex) {
        Log.d("GeneralException", ex.getMessage());
    }

    return strBuilder.toString();
}

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 ww  w . j a  va2s.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

/**
 * Makes a GET call to the server./*  w w w  .  j  a va  2s  .c  o  m*/
 * @return The serve response (expected is JSON)
 */
public static String httpGet(String urlStr) {
    try {
        URL url = new URL(urlStr);

        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        InputStream in = new BufferedInputStream(urlConnection.getInputStream());
        String r = readStream(in);
        urlConnection.disconnect();
        return r;
    } catch (MalformedURLException e) {
        Log.v("MyLog", "GET: Bad URL");
        e.printStackTrace();
        return "GET: Bad URL";
    } catch (IOException e) {
        Log.v("MyLog", "GET: Bad Con");
        e.printStackTrace();
        return "GET: Bad Con [" + urlStr + "]";
    }
}

From source file:Main.java

public static byte[] readZipEntry(File zfile, ZipEntry entry) throws ZipException, IOException {
    Log.d("file3: ", zfile.toString());
    Log.d("zipEntry3: ", entry.toString());
    ZipFile zipFile = new ZipFile(zfile);
    if (entry != null && !entry.isDirectory()) {
        byte[] barr = new byte[(int) entry.getSize()];
        int read = 0;
        int len = 0;
        InputStream is = zipFile.getInputStream(entry);
        BufferedInputStream bis = new BufferedInputStream(is);
        int length = barr.length;
        while ((len = bis.read(barr, read, length - read)) != -1) {
            read += len;/*w  ww.j  a v  a  2 s .c o m*/
        }
        bis.close();
        is.close();
        zipFile.close();
        return barr;
    } else {
        zipFile.close();
        return new byte[0];
    }
}

From source file:Main.java

public static Bitmap revitionImageSize(String path, int width, int height) throws IOException {
    BufferedInputStream in = new BufferedInputStream(new FileInputStream(new File(path)));
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;//  w w w .  ja v a 2 s.co  m
    BitmapFactory.decodeStream(in, null, options);
    in.close();
    int i = 0;
    Bitmap bitmap = null;
    while (true) {
        if ((options.outWidth >> i <= width) && (options.outHeight >> i <= height)) {
            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;
}