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

private static String sha256Hex(final String filePath) throws NoSuchAlgorithmException, IOException {
    final InputStream fis = new BufferedInputStream(new FileInputStream(filePath));
    final MessageDigest md = MessageDigest.getInstance("SHA-256");

    final byte[] dataBytes = new byte[1024];

    int nread;//from  ww w  .  j a v a2 s.  com
    while ((nread = fis.read(dataBytes)) != -1)
        md.update(dataBytes, 0, nread);
    final byte[] mdbytes = md.digest();

    final StringBuilder sb = new StringBuilder();
    for (final byte b : mdbytes)
        sb.append(Integer.toString((b & 0xff) + 0x100, 16).substring(1));

    return sb.toString();
}

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);/* ww w. j a  v  a2 s  .  c o m*/
    }
    bout.flush();
}

From source file:Main.java

public static void downLoadFile(final String strUrl, final String fileURL, final int bufferLength) {
    BufferedInputStream in = null;
    BufferedOutputStream out = null;
    try {//from w  ww  . j a  v  a2  s .  c o  m

        in = new BufferedInputStream(new URL(strUrl).openStream());
        File img = new File(fileURL);
        out = new BufferedOutputStream(new FileOutputStream(img));
        byte[] buf = new byte[bufferLength];
        int count = in.read(buf);
        while (count != -1) {
            out.write(buf, 0, count);
            count = in.read(buf);
        }
        in.close();
        out.close();

    } catch (Exception e) {
        e.printStackTrace();

    }

}

From source file:Main.java

public static Image getImage(Class relativeClass, String filename) {
    Image returnValue = null;/*from  ww  w.j  a  v  a2s  . 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 void readZipFile(String file) throws Exception {
    ZipFile zf = new ZipFile(file);
    InputStream in = new BufferedInputStream(new FileInputStream(file));
    ZipInputStream zin = new ZipInputStream(in);
    ZipEntry ze;//from  ww w .j a  v a2s .  c o m
    while ((ze = zin.getNextEntry()) != null) {
        if (ze.isDirectory()) {
        } else {
            System.err.println("file - " + ze.getName() + " : " + ze.getSize() + " bytes");
            long size = ze.getSize();
            if (size > 0) {
                BufferedReader br = new BufferedReader(new InputStreamReader(zf.getInputStream(ze)));
                String line;
                while ((line = br.readLine()) != null) {
                    System.out.println(line);
                }
                br.close();
            }
            System.out.println();
        }
    }
    zin.closeEntry();
}

From source file:Main.java

public static byte[] readBytes(InputStream in) throws IOException {
    if (!(in instanceof BufferedInputStream)) {
        in = new BufferedInputStream(in);
    }/*  w  ww  .  ja  va2s. co  m*/
    ByteArrayOutputStream out = null;
    try {
        out = new ByteArrayOutputStream();
        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) != -1) {
            out.write(buf, 0, len);
        }
    } finally {
        closeQuietly(out);
    }
    return out.toByteArray();
}

From source file:Main.java

public static String getCharset(File file) {
    String charset = "GBK";
    byte[] first3Bytes = new byte[3];
    try {/*  w  w  w .j ava2 s.c o m*/
        boolean checked = false;
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
        bis.mark(0);
        int read = bis.read(first3Bytes, 0, 3);
        if (read == -1)
            return charset;
        if (first3Bytes[0] == (byte) 0xFF && first3Bytes[1] == (byte) 0xFE) {
            charset = "UTF-16LE";
            checked = true;
        } else if (first3Bytes[0] == (byte) 0xFE && first3Bytes[1] == (byte) 0xFF) {
            charset = "UTF-16BE";
            checked = true;
        } else if (first3Bytes[0] == (byte) 0xEF && first3Bytes[1] == (byte) 0xBB
                && first3Bytes[2] == (byte) 0xBF) {
            charset = "UTF-8";
            checked = true;
        }
        bis.reset();
        if (!checked) {
            int loc = 0;
            while ((read = bis.read()) != -1) {
                loc++;
                if (read >= 0xF0)
                    break;
                if (0x80 <= read && read <= 0xBF)
                    break;
                if (0xC0 <= read && read <= 0xDF) {
                    read = bis.read();
                    if (0x80 <= read && read <= 0xBF)
                        continue;
                    else
                        break;
                } else if (0xE0 <= read && read <= 0xEF) {
                    read = bis.read();
                    if (0x80 <= read && read <= 0xBF) {
                        read = bis.read();
                        if (0x80 <= read && read <= 0xBF) {
                            charset = "UTF-8";
                            break;
                        } else
                            break;
                    } else
                        break;
                }
            }
        }
        bis.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return charset;
}

From source file:Main.java

public static Object deserializeObjectFromFile(final String filePath)
        throws FileNotFoundException, IOException, ClassNotFoundException {

    try (FileInputStream file = new FileInputStream(filePath);
            BufferedInputStream buffer = new BufferedInputStream(file);
            XMLDecoder input = new XMLDecoder(buffer);) {

        return input.readObject();

    }//  www  .j  av a2 s. co  m
}

From source file:Main.java

private static int copyFile(final File src, final File dst) throws IOException {
    int totalBytes = 0;
    final InputStream in = new BufferedInputStream(new FileInputStream(src));
    try {//from  w ww . ja  va2s.c o m
        final OutputStream out = new BufferedOutputStream(new FileOutputStream(dst));
        try {
            final byte[] buf = new byte[bufferSize];
            while (true) {
                int count;
                if ((count = in.read(buf)) == -1) {
                    break;
                }
                out.write(buf, 0, count);
                totalBytes += count;
            }
        } finally {
            out.close();
        }
    } finally {
        in.close();
    }
    return totalBytes;
}

From source file:Main.java

public static void copyFile(File sourceFile, File targetFile) throws IOException {
    BufferedInputStream inBuff = null;
    BufferedOutputStream outBuff = null;
    try {//from   ww w. j av  a 2  s.c om
        inBuff = new BufferedInputStream(new FileInputStream(sourceFile));
        outBuff = new BufferedOutputStream(new FileOutputStream(targetFile));
        byte[] buffer = new byte[BUFFER];
        int length;
        while ((length = inBuff.read(buffer)) != -1) {
            outBuff.write(buffer, 0, length);
        }
        outBuff.flush();
    } finally {
        if (inBuff != null) {
            inBuff.close();
        }
        if (outBuff != null) {
            outBuff.close();
        }
    }
}