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:MainClass.java

public static String md(String f) throws Exception {
    BufferedInputStream file = new BufferedInputStream(new FileInputStream(f));
    MessageDigest md = MessageDigest.getInstance("SHA-1");
    DigestInputStream in = new DigestInputStream(file, md);
    int i;//from   w ww. jav a 2  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());
}

From source file:Main.java

public static void copy(InputStream in, OutputStream out) throws IOException {
    if (!(in instanceof BufferedInputStream)) {
        in = new BufferedInputStream(in);
    }//from  www  .  j  a  va 2  s. c o  m
    if (!(out instanceof BufferedOutputStream)) {
        out = new BufferedOutputStream(out);
    }
    int len = 0;
    byte[] buffer = new byte[1024];
    while ((len = in.read(buffer)) != -1) {
        out.write(buffer, 0, len);
    }
    out.flush();
}

From source file:Main.java

public static Document load(InputStream stream) {
    try {//from ww  w  . j ava  2s.co m
        BufferedInputStream bs = new BufferedInputStream(stream);
        bs.mark(1);

        if (-1 == bs.read())
            return null;

        bs.reset();
        return newDocumentBuilder().parse(bs);
    } catch (Exception e) {
        return null;
    }
}

From source file:Main.java

public static Bitmap downloadImage(String urlStr) throws IOException {
    URL url = new URL(urlStr);
    URLConnection connection = url.openConnection();
    BufferedInputStream bis = new BufferedInputStream(connection.getInputStream());
    Bitmap result = BitmapFactory.decodeStream(bis);
    bis.close();// w  ww.j a  v  a  2 s.c o  m
    return result;
}

From source file:Main.java

public static Object convertByteArrayToObject(byte[] bytes) throws IOException, ClassNotFoundException {
    ObjectInputStream is = null;/*from   w w w.j  a v a2s  . c o m*/
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
    is = new ObjectInputStream(new BufferedInputStream(byteArrayInputStream));
    Object obj = is.readObject();
    is.close();
    return obj;

}

From source file:Main.java

static boolean isTestnet(final Context c) {
    try {/*from w  w w  .ja  va2  s.c  o m*/
        final Properties p = new Properties();
        p.load(new BufferedInputStream(new FileInputStream(getBitcoinConf(c))));
        return p.getProperty("testnet", p.getProperty("regtest", "0")).equals("1");
    } catch (final IOException e) {
        return false;
    }
}

From source file:Main.java

public static String readFromFile(String file) {
    try {//from w ww .  j a  v a2 s  . c o m
        File f = new File(file);

        if (!f.exists() || !f.canRead()) {
            return null;
        }

        BufferedInputStream ipt = new BufferedInputStream(new FileInputStream(f));

        byte[] buf = new byte[512];
        StringBuilder sb = new StringBuilder();

        while (ipt.available() > 0) {
            int len = ipt.read(buf, 0, 512);
            sb.append(new String(buf, 0, len, "UTF-8"));
        }

        ipt.close();
        return sb.toString();
    } catch (Exception e) {
        return null;
    }
}

From source file:Main.java

public static Image getImage(Class relativeClass, String filename) {
    Image returnValue = null;/*from   www . j a v  a  2 s .  co  m*/
    InputStream is = relativeClass.getResourceAsStream(filename);
    if (is != null) {
        BufferedInputStream bis = new BufferedInputStream(is);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            int ch;
            while ((ch = bis.read()) != -1) {
                baos.write(ch);
            }
            Toolkit t = Toolkit.getDefaultToolkit();
            returnValue = t.createImage(baos.toByteArray());
        } catch (IOException exception) {
            System.err.println("Error loading: " + filename);
        }
    }
    return returnValue;
}

From source file:Main.java

public static byte[] readFile(File f) throws IOException {
    FileInputStream fis = new FileInputStream(f);
    try {//w w  w  .  j  a v a2  s .  c  o m
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        BufferedInputStream bis = new BufferedInputStream(fis);
        int ch;
        while ((ch = bis.read()) >= 0) {
            baos.write(ch);
        }
        baos.flush();
        return baos.toByteArray();
    } finally {
        fis.close();
    }
}

From source file:Main.java

public static boolean contentEquals(InputStream input1, InputStream input2) throws IOException {
    if (!(input1 instanceof BufferedInputStream)) {
        input1 = new BufferedInputStream((InputStream) input1);
    }//w  w w. ja  va  2  s  .com

    if (!(input2 instanceof BufferedInputStream)) {
        input2 = new BufferedInputStream((InputStream) input2);
    }

    int ch2;
    for (int ch = ((InputStream) input1).read(); -1 != ch; ch = ((InputStream) input1).read()) {
        ch2 = ((InputStream) input2).read();
        if (ch != ch2) {
            return false;
        }
    }

    ch2 = ((InputStream) input2).read();
    return ch2 == -1;
}