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(byte b[], int off, int len) throws IOException 

Source Link

Document

Reads bytes from this byte-input stream into the specified byte array, starting at the given offset.

Usage

From source file:Main.java

public static String getCharset(File file) {
    String charset = "GBK";
    byte[] first3Bytes = new byte[3];
    try {/*from w ww.j  a v  a  2  s. com*/
        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 String readFromFile(String file) {
    try {/*  www. j  a v a2s.  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:jeevlet.representation.BlobRepresentation.java

private static void copy(InputStream in, OutputStream output) throws IOException {
    BufferedInputStream input = new BufferedInputStream(in);
    try {/*from www.  ja v  a 2  s.c  om*/
        byte buffer[] = new byte[BUF_SIZE];
        int nRead;
        do {
            nRead = input.read(buffer, 0, BUF_SIZE);
            output.write(buffer, 0, nRead);

        } while (nRead == BUF_SIZE);
        input.close();
    } catch (IOException e) {
        input.close();
        throw e;
    }
    output.flush();
}

From source file:Main.java

public static byte[] retrieveImageData_fromUrl(String imageUrl) throws IOException {
    URL url = new URL(imageUrl);
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestProperty("User-Agent",
            System.getProperties().getProperty("http.agent") + " FacebookAndroidSDK");

    // determine the image size and allocate a buffer
    int fileSize = connection.getContentLength();
    byte[] imageData = new byte[fileSize];

    // download the file
    Log.d(TAG, "fetching image " + imageUrl + " (" + fileSize + ")");

    if (fileSize > 0) {

        BufferedInputStream istream = new BufferedInputStream(connection.getInputStream());
        int bytesRead = 0;
        int offset = 0;
        while (bytesRead != -1 && offset < fileSize) {
            bytesRead = istream.read(imageData, offset, fileSize - offset);
            offset += bytesRead;/*  ww  w  . j  ava 2 s . c o  m*/
        }

        istream.close();
    } else
        Log.d(TAG, "fileSize is 0! skipping");

    // clean up
    connection.disconnect();

    return imageData;
}

From source file:Main.java

public static String readTextFile(URL url) throws IOException {
    if (url == null)
        return "";
    BufferedInputStream bis = null;
    StringBuffer sb = new StringBuffer();
    try {/*from w  ww .ja  va 2 s .  c  o  m*/
        bis = new BufferedInputStream(url.openStream());
        byte buff[] = new byte[2048];
        int bytesRead;
        while (-1 != (bytesRead = bis.read(buff, 0, buff.length)))
            sb.append(new String(buff, 0, bytesRead));
    } catch (IOException e) {
        throw e;
    }
    return sb.toString();
}

From source file:jeeves.utils.BLOB.java

private static void copy(InputStream in, OutputStream output) throws IOException {
    BufferedInputStream input = new BufferedInputStream(in);
    try {//from  w  w  w  . jav  a  2  s .c  o  m
        byte buffer[] = new byte[BUF_SIZE];
        int nRead;
        do {
            nRead = input.read(buffer, 0, BUF_SIZE);
            output.write(buffer, 0, nRead);

        } while (nRead == BUF_SIZE);
        input.close();
    } catch (IOException e) {
        input.close();
        throw e;
    }
}

From source file:Main.java

private static void compressFile(File file, ZipOutputStream out, String basedir) {
    if (!file.exists()) {
        return;/*from   w w  w  .j  av  a2 s . co m*/
    }
    try {
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
        ZipEntry entry = new ZipEntry(basedir + file.getName());
        out.putNextEntry(entry);
        int count;
        byte data[] = new byte[BUFFER];
        while ((count = bis.read(data, 0, BUFFER)) != -1) {
            out.write(data, 0, count);
        }
        bis.close();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

/**
 * Compress files to a zip/*from   w  ww. ja  va2  s . c  om*/
 * @param zip
 * @param files
 * @throws IOException 
 */
public static void compressToZip(File zip, File[] files) throws IOException {
    byte data[] = new byte[BUFFER];
    FileOutputStream fozip = new FileOutputStream(zip);
    ZipOutputStream zo = new ZipOutputStream(new BufferedOutputStream(fozip));
    for (int i = 0; i < files.length; i++) {
        System.out.println("Adding:" + files[i]);
        FileInputStream fi = new FileInputStream(files[i]);
        BufferedInputStream origin = new BufferedInputStream(fi, BUFFER);
        ZipEntry zipentry = new ZipEntry(files[i].getName());
        zo.putNextEntry(zipentry);
        int count;
        while ((count = origin.read(data, 0, BUFFER)) != -1) {
            zo.write(data, 0, count);
        }
        origin.close();
    }
    zo.close();
}

From source file:Main.java

public static void copyInputToFile(InputStream in, String path) {
    BufferedInputStream bis = null;
    FileOutputStream fos = null;//from  w w  w . ja v a  2 s  .c om
    try {
        byte[] buffer = new byte[10 * 1024];
        bis = new BufferedInputStream(in);
        fos = new FileOutputStream(path);
        int a = bis.read(buffer, 0, buffer.length);
        while (a != -1) {
            fos.write(buffer, 0, a);
            fos.flush();
            a = bis.read(buffer, 0, buffer.length);
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        closeOutputStream(fos);
        closeInputStream(bis);
        closeInputStream(in);
    }
}

From source file:Main.java

private static boolean addEntryInputStream(ZipOutputStream zos, String entryName, InputStream inputStream) {
    final ZipEntry zipEntry = new ZipEntry(entryName);
    try {//from  w  w  w . j av a  2s  .  c  om
        zos.putNextEntry(zipEntry);
    } catch (final ZipException e) {

        // Ignore duplicate entry - no overwriting
        return false;
    } catch (final IOException e) {
        throw new RuntimeException("Error while adding zip entry " + zipEntry, e);
    }
    final int buffer = 2048;
    final BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream, buffer);
    int count;
    try {
        final byte data[] = new byte[buffer];
        while ((count = bufferedInputStream.read(data, 0, buffer)) != -1) {
            zos.write(data, 0, count);
        }
        bufferedInputStream.close();
        inputStream.close();
        zos.closeEntry();
        return true;
    } catch (final IOException e) {
        throw new RuntimeException(e);
    }
}