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

Source Link

Document

Reads up to b.length bytes of data from this input stream into an array of bytes.

Usage

From source file:Main.java

public static byte[] readFromInputStream(InputStream inputStream) throws IOException {

    if (inputStream == null)
        return null;

    BufferedInputStream bin = new BufferedInputStream(inputStream);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();

    byte[] buffer = new byte[1024 * 8];
    int len = 0;//  w w  w .j ava  2  s. c  o  m

    while ((len = bin.read(buffer)) != -1) {
        bos.write(buffer, 0, len);
    }

    bin.close();

    return bos.toByteArray();
}

From source file:AnnotationClient.java

protected static String readFileAsString(File file) throws IOException {
    byte[] buffer = new byte[(int) file.length()];
    BufferedInputStream f = new BufferedInputStream(new FileInputStream(file));
    f.read(buffer);
    return new String(buffer);
}

From source file:Main.java

public static boolean copyFile(File src, File tar) throws Exception {
    if (src.isFile()) {
        InputStream is = new FileInputStream(src);
        OutputStream op = new FileOutputStream(tar);
        BufferedInputStream bis = new BufferedInputStream(is);
        BufferedOutputStream bos = new BufferedOutputStream(op);
        byte[] bt = new byte[1024 * 8];
        int len = bis.read(bt);
        while (len != -1) {
            bos.write(bt, 0, len);/*from   www .  java 2  s . com*/
            len = bis.read(bt);
        }
        bis.close();
        bos.close();
    }
    if (src.isDirectory()) {
        File[] f = src.listFiles();
        tar.mkdir();
        for (int i = 0; i < f.length; i++) {
            copyFile(f[i].getAbsoluteFile(), new File(tar.getAbsoluteFile() + File.separator + f[i].getName()));
        }
    }
    return true;
}

From source file:documentToVector.spotlight.evaluation.external.AnnotationClient.java

protected static String readFileAsString(File file) throws IOException {
    byte[] buffer = new byte[(int) file.length()];
    BufferedInputStream f = new BufferedInputStream(new FileInputStream(file));
    f.read(buffer);
    f.close();//from  w  ww . j  a v a  2 s .c om
    return new String(buffer);
}

From source file:br.com.edu.dbpediaspotlight.AnnotationClient.java

protected static String readFileAsString(File file) throws IOException {
    byte[] buffer = new byte[(int) file.length()];
    @SuppressWarnings("resource")
    BufferedInputStream f = new BufferedInputStream(new FileInputStream(file));
    f.read(buffer);
    return new String(buffer);
}

From source file:Main.java

/**
 * unzip some gzip compressed data/*from  www.  j a  v a2  s.  c om*/
 * @param bytes the data to uncompress
 * @return the uncompressed data
 */
public static String gzipDecompress(byte[] bytes) {
    ByteArrayInputStream stream = new ByteArrayInputStream(bytes);
    try {
        GZIPInputStream gs = new GZIPInputStream(stream);
        BufferedInputStream bufis = new BufferedInputStream(gs);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        byte[] buf = new byte[1024];
        int len;
        while ((len = bufis.read(buf)) > 0) {
            bos.write(buf, 0, len);
        }
        return bos.toString();
    } catch (IOException e) {
        return null;
    }
}

From source file:Main.java

public static byte[] getFileBytes(File file) throws IOException {
    BufferedInputStream bis = null;
    try {//from  ww  w  . jav a 2s . c  om
        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:simple.crawler.http.HttpClientUtil.java

public static String getContentBodyAsString(HttpResponse res) throws IOException {
    InputStream is = res.getEntity().getContent();
    BufferedInputStream bis = new BufferedInputStream(is);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] buff = new byte[1024];
    for (int l = bis.read(buff); l != -1; l = bis.read(buff)) {
        baos.write(buff, 0, buff.length);
        buff = new byte[1024];
    }//from w  w w  . jav  a  2s.  co  m
    return new String(baos.toByteArray(), "UTF-8");
}

From source file:com.eryansky.common.utils.io.IoUtils.java

public static String readFileAsString(String filePath) {
    byte[] buffer = new byte[(int) getFile(filePath).length()];
    BufferedInputStream inputStream = null;
    try {//  w ww .java2s .  c om
        inputStream = new BufferedInputStream(new FileInputStream(getFile(filePath)));
        inputStream.read(buffer);
    } catch (Exception e) {
        //      throw new ServiceException("Couldn't read file " + filePath + ": " + e.getMessage());
    } finally {
        IoUtils.closeSilently(inputStream);
    }
    return new String(buffer);
}

From source file:org.liberty.android.fantastischmemo.downloader.DownloaderUtils.java

public static File downloadFile(String url, String savedPath) throws IOException {
    File outFile = new File(savedPath);
    OutputStream out;// w w w. j  a  v a 2 s .  co m

    // Delete and backup if the file exists
    AMUtil.deleteFileWithBackup(savedPath);

    // Create the dir if necessary
    File parentDir = outFile.getParentFile();
    parentDir.mkdirs();

    outFile.createNewFile();
    out = new FileOutputStream(outFile);

    Log.i(TAG, "URL to download is: " + url);
    URL myURL = new URL(url);
    URLConnection ucon = myURL.openConnection();
    byte[] buf = new byte[8192];

    InputStream is = ucon.getInputStream();
    BufferedInputStream bis = new BufferedInputStream(is, 8192);
    int len = 0;
    while ((len = bis.read(buf)) != -1) {
        out.write(buf, 0, len);
    }
    out.close();
    is.close();
    return outFile;
}