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:com.qwazr.utils.HashUtils.java

public static String md5Hex(File file) throws IOException {
    FileInputStream fis = new FileInputStream(file);
    try {/*from   w  ww .j  a va  2s .c o  m*/
        BufferedInputStream bis = new BufferedInputStream(fis);
        try {
            return DigestUtils.md5Hex(bis);
        } finally {
            IOUtils.closeQuietly(bis);
        }
    } finally {
        IOUtils.closeQuietly(fis);
    }
}

From source file:ImageUtils.java

final public static ImageProperties getJpegProperties(File file) throws FileNotFoundException, IOException {
    BufferedInputStream in = null;
    try {/* w  ww.  j  a v a 2 s .  c  o  m*/
        in = new BufferedInputStream(new FileInputStream(file));

        // check for "magic" header
        byte[] buf = new byte[2];
        int count = in.read(buf, 0, 2);
        if (count < 2) {
            throw new RuntimeException("Not a valid Jpeg file!");
        }
        if ((buf[0]) != (byte) 0xFF || (buf[1]) != (byte) 0xD8) {
            throw new RuntimeException("Not a valid Jpeg file!");
        }

        int width = 0;
        int height = 0;
        char[] comment = null;

        boolean hasDims = false;
        boolean hasComment = false;
        int ch = 0;

        while (ch != 0xDA && !(hasDims && hasComment)) {
            /* Find next marker (JPEG markers begin with 0xFF) */
            while (ch != 0xFF) {
                ch = in.read();
            }
            /* JPEG markers can be padded with unlimited 0xFF's */
            while (ch == 0xFF) {
                ch = in.read();
            }
            /* Now, ch contains the value of the marker. */

            int length = 256 * in.read();
            length += in.read();
            if (length < 2) {
                throw new RuntimeException("Not a valid Jpeg file!");
            }
            /* Now, length contains the length of the marker. */

            if (ch >= 0xC0 && ch <= 0xC3) {
                in.read();
                height = 256 * in.read();
                height += in.read();
                width = 256 * in.read();
                width += in.read();
                for (int foo = 0; foo < length - 2 - 5; foo++) {
                    in.read();
                }
                hasDims = true;
            } else if (ch == 0xFE) {
                // that's the comment marker
                comment = new char[length - 2];
                for (int foo = 0; foo < length - 2; foo++)
                    comment[foo] = (char) in.read();
                hasComment = true;
            } else {
                // just skip marker
                for (int foo = 0; foo < length - 2; foo++) {
                    in.read();
                }
            }
        }
        return (new ImageProperties(width, height, comment, "jpeg"));

    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
            }
        }
    }
}

From source file:Main.java

public static Bitmap decode(InputStream in, int reqWidth, int reqHeight) throws IOException {
    BufferedInputStream inputStream = new BufferedInputStream(in);
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;//from w w  w . ja  va 2  s.c om
    inputStream.mark(64 * 1024);
    BitmapFactory.decodeStream(inputStream, null, options);
    inputStream.reset();
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
    options.inJustDecodeBounds = false;

    return BitmapFactory.decodeStream(inputStream, null, options);
}

From source file:com.dicksoft.ocr.util.HttpUtil.java

/**
 * Downloads the text from the specified URL.
 * //  w  w  w  .  j a  va2 s  .  c o  m
 * @param url
 *            the URL of the text to read
 * @return the text
 * @throws IOException
 *             if the URL is malformed, or problem reading the stream
 */
public static String fetchText(String url) throws IOException {
    if (LOG.isDebugEnabled())
        LOG.debug("Http fetch: " + url);
    StringBuffer result = new StringBuffer();
    URL urlReal = null;
    urlReal = new URL(url);
    BufferedInputStream in = null;
    in = new BufferedInputStream(urlReal.openStream());

    int data = 0;
    while (true) {
        data = in.read();
        if (data == -1)
            break;
        else
            result.append((char) data);
    }
    return result.toString();
}

From source file:com.amazonaws.util.Md5Utils.java

/**
 * Computes the MD5 hash of the data in the given input stream and returns
 * it as an array of bytes./*ww  w.j a  v  a 2  s  .co  m*/
 * Note this method closes the given input stream upon completion.
 */
public static byte[] computeMD5Hash(InputStream is) throws IOException {
    BufferedInputStream bis = new BufferedInputStream(is);
    try {
        MessageDigest messageDigest = MessageDigest.getInstance("MD5");
        byte[] buffer = new byte[SIXTEEN_K];
        int bytesRead;
        while ((bytesRead = bis.read(buffer, 0, buffer.length)) != -1) {
            messageDigest.update(buffer, 0, bytesRead);
        }
        return messageDigest.digest();
    } catch (NoSuchAlgorithmException e) {
        // should never get here
        throw new IllegalStateException(e);
    } finally {
        try {
            bis.close();
        } catch (Exception e) {
            LogFactory.getLog(Md5Utils.class).debug("Unable to close input stream of hash candidate: " + e);
        }
    }
}

From source file:Main.java

/**
 * Uncompresses zipped files/*  w  w  w  . j  a v a 2  s  . c o m*/
 * @param zippedFile The file to uncompress
 * @param destinationDir Where to put the files
 * @return  list of unzipped files
 * @throws java.io.IOException thrown if there is a problem finding or writing the files
 */
public static List<File> unzip(File zippedFile, File destinationDir) throws IOException {
    int buffer = 2048;

    List<File> unzippedFiles = new ArrayList<File>();

    BufferedOutputStream dest;
    BufferedInputStream is;
    ZipEntry entry;
    ZipFile zipfile = new ZipFile(zippedFile);
    Enumeration e = zipfile.entries();
    while (e.hasMoreElements()) {
        entry = (ZipEntry) e.nextElement();

        is = new BufferedInputStream(zipfile.getInputStream(entry));
        int count;
        byte data[] = new byte[buffer];

        File destFile;

        if (destinationDir != null) {
            destFile = new File(destinationDir, entry.getName());
        } else {
            destFile = new File(entry.getName());
        }

        FileOutputStream fos = new FileOutputStream(destFile);
        dest = new BufferedOutputStream(fos, buffer);
        try {
            while ((count = is.read(data, 0, buffer)) != -1) {
                dest.write(data, 0, count);
            }

            unzippedFiles.add(destFile);
        } finally {
            dest.flush();
            dest.close();
            is.close();
        }
    }

    return unzippedFiles;
}

From source file:Main.java

public static void unzipEPub(String inputZip, String destinationDirectory) throws IOException {
    int BUFFER = 2048;
    List zipFiles = new ArrayList();
    File sourceZipFile = new File(inputZip);
    File unzipDestinationDirectory = new File(destinationDirectory);
    unzipDestinationDirectory.mkdir();/*from   w  w w.j a v a 2s . c o  m*/

    ZipFile zipFile;
    zipFile = new ZipFile(sourceZipFile, ZipFile.OPEN_READ);
    Enumeration zipFileEntries = zipFile.entries();

    // Process each entry
    while (zipFileEntries.hasMoreElements()) {

        ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();
        String currentEntry = entry.getName();
        File destFile = new File(unzipDestinationDirectory, currentEntry);

        if (currentEntry.endsWith(".zip")) {
            zipFiles.add(destFile.getAbsolutePath());
        }

        File destinationParent = destFile.getParentFile();
        destinationParent.mkdirs();

        if (!entry.isDirectory()) {
            BufferedInputStream is = new BufferedInputStream(zipFile.getInputStream(entry));
            int currentByte;
            // buffer for writing file
            byte data[] = new byte[BUFFER];

            FileOutputStream fos = new FileOutputStream(destFile);
            BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER);

            while ((currentByte = is.read(data, 0, BUFFER)) != -1) {
                dest.write(data, 0, currentByte);
            }
            dest.flush();
            dest.close();
            is.close();

        }

    }
    zipFile.close();

    for (Iterator iter = zipFiles.iterator(); iter.hasNext();) {
        String zipName = (String) iter.next();
        unzipEPub(zipName,
                destinationDirectory + File.separatorChar + zipName.substring(0, zipName.lastIndexOf(".zip")));
    }
}

From source file:Main.java

/**
 * extracts a zip file to the given dir/*from w ww. j av  a  2s . c o m*/
 * @param archive
 * @param destDir
 * @throws IOException 
 * @throws ZipException 
 * @throws Exception
 */
public static void extractZipArchive(File archive, File destDir) throws ZipException, IOException {
    if (!destDir.exists()) {
        destDir.mkdir();
    }

    ZipFile zipFile = new ZipFile(archive);
    Enumeration<? extends ZipEntry> entries = zipFile.entries();

    byte[] buffer = new byte[16384];
    int len;
    while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();

        String entryFileName = entry.getName();

        File dir = buildDirectoryHierarchyFor(entryFileName, destDir);
        if (!dir.exists()) {
            dir.mkdirs();
        }

        if (!entry.isDirectory()) {
            BufferedOutputStream bos = new BufferedOutputStream(
                    new FileOutputStream(new File(destDir, entryFileName)));

            BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(entry));

            while ((len = bis.read(buffer)) > 0) {
                bos.write(buffer, 0, len);
            }

            bos.flush();
            bos.close();
            bis.close();
        }
    }
}

From source file:com.linkage.crm.csb.sign.CtSignature.java

/**
 * .//from w  ww.j a va2s.  c  om
 * 
 * @param pwd String 
 * @param alias String 
 * @param priKeyFile 
 * @return Signature 
 */
public static Signature createSignatureForSign(String pwd, String alias, String priKeyFile) {
    try {
        logger.debug("keypath=============" + priKeyFile);
        KeyStore ks = KeyStore.getInstance("JKS");
        FileInputStream ksfis = new FileInputStream(priKeyFile);
        BufferedInputStream ksbufin = new BufferedInputStream(ksfis);
        char[] kpass = pwd.toCharArray();
        ks.load(ksbufin, kpass);
        PrivateKey priKey = (PrivateKey) ks.getKey(alias, kpass);
        Signature rsa = Signature.getInstance("SHA1withDSA");
        rsa.initSign(priKey);
        return rsa;
    } catch (Exception ex) {
        logger.error("errors appeared while trying to signature", ex);
        return null;
    }
}

From source file:edu.unc.lib.dl.ui.util.FileIOUtil.java

public static void stream(OutputStream outStream, HttpMethodBase method) throws IOException {
    try (InputStream in = method.getResponseBodyAsStream();
            BufferedInputStream reader = new BufferedInputStream(in)) {
        byte[] buffer = new byte[4096];
        int count = 0;
        int length = 0;
        while ((length = reader.read(buffer)) >= 0) {
            try {
                outStream.write(buffer, 0, length);
                if (count++ % 5 == 0) {
                    outStream.flush();/*from w  w w . j a v  a  2 s  .  c om*/
                }
            } catch (IOException e) {
                // Differentiate between socket being closed when writing vs
                // reading
                throw new ClientAbortException(e);
            }
        }
        try {
            outStream.flush();
        } catch (IOException e) {
            throw new ClientAbortException(e);
        }
    }
}