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

/**
 * Compress a String to a zip file that has only one entry like zipName
 * The name shouldn't have .zip//from w w  w .  j  a va2 s  .c o m
 * 
 * @param content
 * @param fName
 * @throws IOException 
 */
public static void zipAContentAsFileName(String fName, String content, String charset) throws IOException {
    ByteArrayInputStream bais = new ByteArrayInputStream(content.getBytes(charset));
    BufferedInputStream bis = new BufferedInputStream(bais);

    File f = new File(fName);
    File parentFile = f.getParentFile();
    if (!parentFile.exists()) {
        parentFile.mkdirs();
    }
    ZipOutputStream zf = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(f + ".zip")));

    ZipEntry entry = new ZipEntry(f.getName());
    zf.putNextEntry(entry);
    byte[] barr = new byte[8192];
    int len = 0;
    while ((len = bis.read(barr)) != -1) {
        zf.write(barr, 0, len);
    }
    zf.flush();
    zf.close();
    bis.close();
    bais.close();
}

From source file:azkaban.utils.FileIOUtils.java

public static Pair<Integer, Integer> readUtf8File(File file, int offset, int length, OutputStream stream)
        throws IOException {
    byte[] buffer = new byte[length];

    FileInputStream fileStream = new FileInputStream(file);

    long skipped = fileStream.skip(offset);
    if (skipped < offset) {
        fileStream.close();/*  w ww .j  a  va2 s. c o m*/
        return new Pair<Integer, Integer>(0, 0);
    }

    BufferedInputStream inputStream = null;
    try {
        inputStream = new BufferedInputStream(fileStream);
        inputStream.read(buffer);
    } finally {
        IOUtils.closeQuietly(inputStream);
    }

    Pair<Integer, Integer> utf8Range = getUtf8Range(buffer, 0, length);
    stream.write(buffer, utf8Range.getFirst(), utf8Range.getSecond());

    return new Pair<Integer, Integer>(offset + utf8Range.getFirst(), utf8Range.getSecond());
}

From source file:dk.microting.softkeyboard.autoupdateapk.AutoUpdateApk.java

private static String MD5Hex(String filename) {
    final int BUFFER_SIZE = 8192;
    byte[] buf = new byte[BUFFER_SIZE];
    int length;//w w  w.j av a  2s  .c  om
    try {
        FileInputStream fis = new FileInputStream(filename);
        BufferedInputStream bis = new BufferedInputStream(fis);
        MessageDigest md = java.security.MessageDigest.getInstance("MD5");
        while ((length = bis.read(buf)) != -1) {
            md.update(buf, 0, length);
        }

        byte[] array = md.digest();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < array.length; ++i) {
            sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100).substring(1, 3));
        }
        Log.v(TAG, "md5sum: " + sb.toString());
        return sb.toString();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return "md5bad";
}

From source file:most.voip.api.Utils.java

/**
 * Load UTF8withBOM or any ansi text file.
 * @param filename/*from ww w .  ja  v  a 2s  . c  om*/
 * @return  
 * @throws java.io.IOException
 */
public static String loadFileAsString(String filename) throws java.io.IOException {
    final int BUFLEN = 1024;
    BufferedInputStream is = new BufferedInputStream(new FileInputStream(filename), BUFLEN);
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream(BUFLEN);
        byte[] bytes = new byte[BUFLEN];
        boolean isUTF8 = false;
        int read, count = 0;
        while ((read = is.read(bytes)) != -1) {
            if (count == 0 && bytes[0] == (byte) 0xEF && bytes[1] == (byte) 0xBB && bytes[2] == (byte) 0xBF) {
                isUTF8 = true;
                baos.write(bytes, 3, read - 3); // drop UTF8 bom marker
            } else {
                baos.write(bytes, 0, read);
            }
            count += read;
        }
        return isUTF8 ? new String(baos.toByteArray(), "UTF-8") : new String(baos.toByteArray());
    } finally {
        try {
            is.close();
        } catch (Exception ex) {
        }
    }
}

From source file:Main.java

public static boolean downloadFile(File file, URL url) throws IOException {
    BufferedInputStream bis = null;
    BufferedOutputStream bos = null;
    HttpURLConnection conn = null;
    try {// w w w  . j  a  v  a2 s .co m
        conn = (HttpURLConnection) url.openConnection();
        bis = new BufferedInputStream(conn.getInputStream());
        bos = new BufferedOutputStream(new FileOutputStream(file));

        int contentLength = conn.getContentLength();

        byte[] buffer = new byte[BUFFER_SIZE];
        int read = 0;
        int count = 0;
        while ((read = bis.read(buffer)) != -1) {
            bos.write(buffer, 0, read);
            count += read;
        }
        if (count < contentLength)
            return false;
        int responseCode = conn.getResponseCode();
        if (responseCode / 100 != 2) {
            // error
            return false;
        }
        // finished
        return true;
    } finally {
        try {
            bis.close();
            bos.close();
            conn.disconnect();
        } catch (Exception e) {
        }
    }
}

From source file:com.runwaysdk.controller.ErrorUtility.java

private static String decompress(String message) {
    try {/*from   w  w  w.  ja  v a2 s.c om*/
        byte[] bytes = Base64.decode(message);

        ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
        BufferedInputStream bufis = new BufferedInputStream(new GZIPInputStream(bis));
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        byte[] buf = new byte[1024];
        int len;
        while ((len = bufis.read(buf)) > 0) {
            bos.write(buf, 0, len);
        }
        String retval = bos.toString();
        bis.close();
        bufis.close();
        bos.close();
        return retval;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:com.sec.ose.osi.util.tools.FileOperator.java

public static void copyFile(File pSource, File pDest) throws FileNotFoundException {

    if (pSource == null)
        throw new FileNotFoundException("Source file is not found");

    if (pSource.exists() == false)
        throw new FileNotFoundException(pSource.getPath());

    if (pDest == null)
        throw new FileNotFoundException("Report file is not found");

    BufferedInputStream bi = null;
    BufferedOutputStream bo = null;

    try {/* w  w w . j av a 2  s.  c  o  m*/
        bi = new BufferedInputStream(new FileInputStream(pSource)); // FileNotFoundExeption
        bo = new BufferedOutputStream(new FileOutputStream(pDest));

        byte buffer[] = new byte[BUF_SIZE];

        int readByte = 0;

        while ((readByte = bi.read(buffer)) != -1) { // IOException
            bo.write(buffer, 0, readByte);

        }

    } catch (FileNotFoundException e) {
        log.warn(e);
        throw e;
    } catch (IOException e) {
        log.warn(e);
    } finally {

        if (bo != null) {
            try {
                bo.flush();

            } catch (IOException e) {
                // TODO Auto-generated catch block
                log.warn(e);
            }
            try {
                bo.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                log.warn(e);
            }

            bo = null;
        }
        if (bi != null) {
            try {
                bi.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                log.warn(e);
            }
            bi = null;
        }
    }

}

From source file:azkaban.utils.FileIOUtils.java

public static LogData readUtf8File(File file, int fileOffset, int length) throws IOException {
    byte[] buffer = new byte[length];
    FileInputStream fileStream = new FileInputStream(file);

    long skipped = fileStream.skip(fileOffset);
    if (skipped < fileOffset) {
        fileStream.close();/*ww w  .  ja  v a 2s .c o  m*/
        return new LogData(fileOffset, 0, "");
    }

    BufferedInputStream inputStream = null;
    int read = 0;
    try {
        inputStream = new BufferedInputStream(fileStream);
        read = inputStream.read(buffer);
    } finally {
        IOUtils.closeQuietly(inputStream);
    }

    if (read <= 0) {
        return new LogData(fileOffset, 0, "");
    }
    Pair<Integer, Integer> utf8Range = getUtf8Range(buffer, 0, read);
    String outputString = new String(buffer, utf8Range.getFirst(), utf8Range.getSecond());

    return new LogData(fileOffset + utf8Range.getFirst(), utf8Range.getSecond(), outputString);
}

From source file:azkaban.utils.FileIOUtils.java

public static JobMetaData readUtf8MetaDataFile(File file, int fileOffset, int length) throws IOException {
    byte[] buffer = new byte[length];
    FileInputStream fileStream = new FileInputStream(file);

    long skipped = fileStream.skip(fileOffset);
    if (skipped < fileOffset) {
        fileStream.close();/*  w ww .j  a va  2s  .co  m*/
        return new JobMetaData(fileOffset, 0, "");
    }

    BufferedInputStream inputStream = null;
    int read = 0;
    try {
        inputStream = new BufferedInputStream(fileStream);
        read = inputStream.read(buffer);
    } finally {
        IOUtils.closeQuietly(inputStream);
    }

    if (read <= 0) {
        return new JobMetaData(fileOffset, 0, "");
    }
    Pair<Integer, Integer> utf8Range = getUtf8Range(buffer, 0, read);
    String outputString = new String(buffer, utf8Range.getFirst(), utf8Range.getSecond());

    return new JobMetaData(fileOffset + utf8Range.getFirst(), utf8Range.getSecond(), outputString);
}

From source file:com.all.client.data.Hashcoder.java

public static String createHashCode(File file) {
    if (file == null) {
        return null;
    }/*w ww . ja v  a  2  s .co  m*/
    byte[] hashCode = null;
    try {
        FileInputStream fis = new FileInputStream(file);
        BufferedInputStream bis = new BufferedInputStream(fis);

        byte[] buffer = new byte[65536];

        bis.read(buffer);

        bis.close();
        fis.close();

        hashCode = md.digest(buffer);

    } catch (FileNotFoundException e) {
        log.error(e.getMessage(), e);
    } catch (IOException e) {
        log.error(e.getMessage(), e);
    }
    return toHex(hashCode);
}