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 InputStream readFile(File file) {
    try {/*from  ww  w  .  j  ava2 s .c o m*/
        InputStream in = new BufferedInputStream(new FileInputStream(file));
        return in;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static int countLines(String filename) throws IOException {
    InputStream is = new BufferedInputStream(new FileInputStream(filename));
    try {/*w w w  .j a  va 2 s  .co m*/
        byte[] c = new byte[1024];
        int count = 0;
        int readChars = 0;
        boolean empty = true;
        while ((readChars = is.read(c)) != -1) {
            empty = false;
            for (int i = 0; i < readChars; ++i) {
                if (c[i] == '\n') {
                    ++count;
                }
            }
        }
        return (count == 0 && !empty) ? 1 : count;
    } finally {
        is.close();
    }
}

From source file:Main.java

public static byte[] readFileContents(InputStream input) throws IOException {
    byte contents[] = new byte[10000], buf[] = new byte[1024];
    InputStream in = new BufferedInputStream(input);
    int bytes_read = 0;

    for (;;) {/* ww w . j  a va 2s  .  c  o m*/
        int tmp = in.read(buf, 0, buf.length);
        if (tmp == -1)
            break;
        System.arraycopy(buf, 0, contents, bytes_read, tmp);
        bytes_read += tmp;
    }

    byte[] retval = new byte[bytes_read];
    System.arraycopy(contents, 0, retval, 0, bytes_read);
    return retval;
}

From source file:Util.java

public static void download(URL url, File to) throws IOException {
    BufferedInputStream in = new BufferedInputStream(url.openStream());
    BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(to));
    int bit = -1;
    while ((bit = in.read()) != -1) {
        out.write(bit);/*from  w w w . j  av a  2  s  . c  o m*/
    }
    in.close();
    out.close();
}

From source file:Main.java

public static boolean contentEquals(InputStream input1, InputStream input2) throws IOException {
    if (!(input1 instanceof BufferedInputStream)) {
        input1 = new BufferedInputStream(input1);
    }//from  w w w.ja v a  2 s  .c om
    if (!(input2 instanceof BufferedInputStream)) {
        input2 = new BufferedInputStream(input2);
    }

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

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

From source file:Main.java

public static byte[] getFileBytes(File file) throws IOException {
    BufferedInputStream bis = null;
    try {/*w  w w.j  a  v a  2s .com*/
        bis = new BufferedInputStream(new FileInputStream(file));
        int bytes = (int) file.length();
        byte[] buffer = new byte[bytes];
        int readBytes = bis.read(buffer);
        if (readBytes != buffer.length) {
            throw new IOException("Entire file not read");
        }
        return buffer;
    } finally {
        if (bis != null) {
            bis.close();
        }
    }
}

From source file:Main.java

public static InputStream getInitializedXMLInputStream(File f) throws IOException {
    return new BufferedInputStream(new FileInputStream(f));
}

From source file:Main.java

public static String readString(InputStream inputStream) throws Exception {
    BufferedInputStream stream = new BufferedInputStream(inputStream);
    StringBuilder answer = new StringBuilder();
    BufferedReader reader = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
    String line;// w  w  w  .  ja  va2  s .c om
    while ((line = reader.readLine()) != null) {
        answer.append(line);
    }
    reader.close();
    return answer.toString();
}

From source file:Main.java

public static int inputStreamToInt(InputStream inputStream) throws IOException {
    InputStream in = new BufferedInputStream(inputStream);
    BufferedReader reader = new BufferedReader(new InputStreamReader(in));

    try {//  www .ja  v  a2 s.c  o  m
        String strValue = reader.readLine();
        int intValue = Integer.parseInt(strValue);
        return intValue;
    } catch (IOException e) {
        throw e;
    }
}

From source file:Main.java

private static Reader getUTF8Reader(InputStream f) throws IOException {
    BufferedInputStream bis = new BufferedInputStream(f);
    assert bis.markSupported();
    bis.mark(3);//ww w .  j  av a 2  s .  c  o  m
    boolean reset = true;
    byte[] t = new byte[3];
    bis.read(t);
    if (t[0] == ((byte) 0xef) && t[1] == ((byte) 0xbb) && t[2] == ((byte) 0xbf)) {
        reset = false;
    }
    if (reset) {
        bis.reset();
    }
    return new InputStreamReader(bis, "UTF-8");
}