Example usage for java.io BufferedInputStream read

List of usage examples for java.io BufferedInputStream read

Introduction

In this page you can find the example usage for java.io BufferedInputStream read.

Prototype

public synchronized int read() throws IOException 

Source Link

Document

See the general contract of the read method of InputStream.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {

    InputStream inStream = new FileInputStream("c:/test.txt");

    BufferedInputStream bis = new BufferedInputStream(inStream);

    // read until a single byte is available
    while (bis.available() > 0) {
        // get the number of bytes available
        Integer nBytes = bis.available();
        System.out.println("Available bytes = " + nBytes);

        // read next available character
        char ch = (char) bis.read();

        // print the read character.
        System.out.println("The character read = " + ch);
    }/* w  w  w  . java 2 s .co m*/

}

From source file:Main.java

public static byte[] readFile(File f) throws IOException {
    FileInputStream fis = new FileInputStream(f);
    try {//from   ww  w.  j  a  v  a  2  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:ConsoleInput.java

public static String readLine() {
    StringBuffer response = new StringBuffer();
    try {/*from ww  w  .j a v  a  2  s. c o  m*/
        BufferedInputStream buff = new BufferedInputStream(System.in);
        int in = 0;
        char inChar;
        do {
            in = buff.read();
            inChar = (char) in;
            if ((in != -1) & (in != '\n') & (in != '\r')) {
                response.append(inChar);
            }
        } while ((in != -1) & (inChar != '\n') & (in != '\r'));
        buff.close();
        return response.toString();
    } catch (IOException e) {
        System.out.println("Exception: " + e.getMessage());
        return null;
    }
}

From source file:Main.java

public static byte[] readByteArray(Context context, String fileName) throws IOException {

    byte[] data;/*from  w  w  w. j a v  a 2  s  .c  o m*/
    int c;

    FileInputStream fis = context.openFileInput(fileName);
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    BufferedInputStream bos = new BufferedInputStream(fis);

    while ((c = bos.read()) != -1) {
        byteArrayOutputStream.write(c);
    }

    data = byteArrayOutputStream.toByteArray();

    bos.close();
    byteArrayOutputStream.close();
    fis.close();

    return data;
}

From source file:Main.java

public static String getCharset1(String fileName) throws IOException {
    BufferedInputStream bin = new BufferedInputStream(new FileInputStream(fileName));
    int p = (bin.read() << 8) + bin.read();

    String code;//from w  w  w .j av a 2s .co  m
    switch (p) {
    case 0xefbb:
        code = "UTF-8";
        break;
    case 0xfffe:
        code = "Unicode";
        break;
    case 0xfeff:
        code = "UTF-16BE";
        break;
    default:
        code = "GBK";
    }
    return code;
}

From source file:MainClass.java

public static void copy(InputStream in, OutputStream out) throws IOException {

    BufferedInputStream bin = new BufferedInputStream(in);
    BufferedOutputStream bout = new BufferedOutputStream(out);

    while (true) {
        int datum = bin.read();
        if (datum == -1)
            break;
        bout.write(datum);/*from w  ww .  j  a v a2 s  .c  o m*/
    }
    bout.flush();
}

From source file:Main.java

public static Document load(InputStream stream) {
    try {/*from ww  w. j  a  v a 2 s .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 Image getImage(Class relativeClass, String filename) {
    Image returnValue = null;/* w  w w  .j a  va  2  s  .  c o  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[] readBytes(InputStream is) throws IOException {
    try {// www  .  jav a 2 s  .  c  om
        BufferedInputStream bis = new BufferedInputStream(is);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        int ch;
        while ((ch = bis.read()) >= 0) {
            bos.write(ch);
        }
        bos.flush();
        return bos.toByteArray();
    } finally {
        is.close();
    }
}

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 a va2  s  .  c  o m
    }
    in.close();
    out.close();
}