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

/**
 * Detect the XML encoding of the document
 * //w  ww  . j a  v a  2  s .  co  m
 * @param is The input stream
 * @return The encoding
 * @throws IOException
 */
public static String getEncoding(InputStream is) throws IOException {
    if (!is.markSupported())
        is = new BufferedInputStream(is);

    byte[] buffer = readBuffer(is);
    return getXMLEncoding(buffer);
}

From source file:Main.java

@SuppressWarnings("SameParameterValue")
public static Bitmap fetchAndRescaleBitmap(String uri, int width, int height) throws IOException {
    URL url = new URL(uri);
    BufferedInputStream is = null;
    try {/*w w  w.jav  a2s.  c  om*/
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        is = new BufferedInputStream(urlConnection.getInputStream());
        is.mark(MAX_READ_LIMIT_PER_IMG);
        int scaleFactor = findScaleFactor(width, height, is);
        Log.d(TAG, "Scaling bitmap " + uri + " by factor " + scaleFactor + " to support " + width + "x" + height
                + "requested dimension");
        is.reset();
        return scaleBitmap(scaleFactor, is);
    } finally {
        if (is != null) {
            is.close();
        }
    }
}

From source file:Main.java

public static Document parseDoc(final InputStream is)
        throws ParserConfigurationException, SAXException, IOException {
    try {/*from  w w  w .ja  v  a  2 s.  c o  m*/
        BufferedInputStream in = new BufferedInputStream(is);
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        InputSource source = new InputSource(in);
        return builder.parse(source);
    } finally {
        is.close();
    }
}

From source file:Main.java

/**
 * Reads an object from an XML file./*  www. j  av a2 s. com*/
 * 
 * @param filename the filename
 * 
 * @return the object
 * 
 * @throws IOException Signals that an I/O exception has occurred.
 */
public static Object readBeanFromFile(String filename) throws IOException {
    XMLDecoder decoder = null;
    try {
        decoder = new XMLDecoder(new BufferedInputStream(new FileInputStream(filename)));
        return decoder.readObject();
    } finally {
        if (decoder != null) {
            decoder.close();
        }
    }
}

From source file:Main.java

/**
 * This method deserializes an object from an input stream.
 *
 * @param file The input file/*ww w. java 2s.c om*/
 * @return The deserialized object
 * @exception IOException IOError
 */
public static Object deserializeObject(File file) throws IOException, ClassNotFoundException {
    FileInputStream fis = new FileInputStream(file);
    Object object = null;
    try {
        ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(fis));
        object = ois.readObject();
    } finally {
        fis.close();
    }
    return object;
}

From source file:Main.java

/**
 * @param url/*www  .  j ava  2  s . c o m*/
 * @return
 */
public static Bitmap getBitMapFromURL(URL url) {
    Bitmap mBitmap = null;

    try {
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setDoInput(true);
        conn.connect();
        conn.getContentLength();
        InputStream is = conn.getInputStream();
        BufferedInputStream bis = new BufferedInputStream(is);
        mBitmap = BitmapFactory.decodeStream(bis);
        bis.close();
        is.close();
    } catch (Exception e) {
        Log.e("Exception in MapScrollActivity.getBitmapFromURL", "" + e.getMessage());
        e.printStackTrace();
    }
    return mBitmap;
}

From source file:Main.java

public static Document parseDoc(final InputStream is)
        throws ParserConfigurationException, SAXException, IOException {
    try {//  w w  w . j  ava  2 s.  c o m
        BufferedInputStream in = new BufferedInputStream(is);
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setFeature("http://xml.org/sax/features/external-general-entities", false);
        factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
        factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
        DocumentBuilder builder = factory.newDocumentBuilder();
        InputSource source = new InputSource(in);
        return builder.parse(source);
    } finally {
        is.close();
    }
}

From source file:Main.java

public static String getBinaryHash(File apk, String algo) {
    FileInputStream fis = null;/*  ww w  .  ja  v a  2s  .  c o m*/
    BufferedInputStream bis = null;
    try {
        MessageDigest md = MessageDigest.getInstance(algo);
        fis = new FileInputStream(apk);
        bis = new BufferedInputStream(fis);

        byte[] dataBytes = new byte[524288];
        int nread = 0;

        while ((nread = bis.read(dataBytes)) != -1)
            md.update(dataBytes, 0, nread);

        byte[] mdbytes = md.digest();
        return toHexString(mdbytes);
    } catch (IOException e) {
        Log.e("FDroid", "Error reading \"" + apk.getAbsolutePath() + "\" to compute " + algo + " hash.");
        return null;
    } catch (NoSuchAlgorithmException e) {
        Log.e("FDroid", "Device does not support " + algo + " MessageDisgest algorithm");
        return null;
    } finally {
        closeQuietly(fis);
    }
}

From source file:Main.java

/**
 *
 * @param f/*ww  w.j a  v  a 2s. com*/
 * @throws Exception
 */
private static X509Certificate readCertificate(File f) throws CertificateException, IOException {
    CertificateFactory cf = CertificateFactory.getInstance("X.509");

    // Use BufferedInputStream (which supports mark and reset) so that each
    // generateCertificate call consumes one certificate.
    BufferedInputStream in = new BufferedInputStream(new FileInputStream(f));
    X509Certificate cert = (X509Certificate) cf.generateCertificate(in);
    in.close();
    return cert;
}

From source file:Main.java

public static boolean unzip(InputStream inputStream, String dest, boolean replaceIfExists) {

    final int BUFFER_SIZE = 4096;

    BufferedOutputStream bufferedOutputStream = null;

    boolean succeed = true;

    try {//from   ww w  . j  a v a  2 s. c o m
        ZipInputStream zipInputStream = new ZipInputStream(new BufferedInputStream(inputStream));
        ZipEntry zipEntry;

        while ((zipEntry = zipInputStream.getNextEntry()) != null) {

            String zipEntryName = zipEntry.getName();

            //             if(!zipEntry.isDirectory()) {
            //                 File fil = new File(dest + zipEntryName);
            //                 fil.getParent()
            //             }

            // file exists ? delete ?
            File file2 = new File(dest + zipEntryName);

            if (file2.exists()) {
                if (replaceIfExists) {

                    try {
                        boolean b = deleteDir(file2);
                        if (!b) {
                            Log.e("Haggle", "Unzip failed to delete " + dest + zipEntryName);
                        } else {
                            Log.d("Haggle", "Unzip deleted " + dest + zipEntryName);
                        }
                    } catch (Exception e) {
                        Log.e("Haggle", "Unzip failed to delete " + dest + zipEntryName, e);
                    }
                }
            }

            // extract
            File file = new File(dest + zipEntryName);

            if (file.exists()) {

            } else {
                if (zipEntry.isDirectory()) {
                    file.mkdirs();
                    chmod(file, 0755);

                } else {

                    // create parent file folder if not exists yet
                    if (!file.getParentFile().exists()) {
                        file.getParentFile().mkdirs();
                        chmod(file.getParentFile(), 0755);
                    }

                    byte buffer[] = new byte[BUFFER_SIZE];
                    bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file), BUFFER_SIZE);
                    int count;

                    while ((count = zipInputStream.read(buffer, 0, BUFFER_SIZE)) != -1) {
                        bufferedOutputStream.write(buffer, 0, count);
                    }

                    bufferedOutputStream.flush();
                    bufferedOutputStream.close();
                }
            }

            // enable standalone python
            if (file.getName().endsWith(".so") || file.getName().endsWith(".xml")
                    || file.getName().endsWith(".py") || file.getName().endsWith(".pyc")
                    || file.getName().endsWith(".pyo")) {
                chmod(file, 0755);
            }

            Log.d("Haggle", "Unzip extracted " + dest + zipEntryName);
        }

        zipInputStream.close();

    } catch (FileNotFoundException e) {
        Log.e("Haggle", "Unzip error, file not found", e);
        succeed = false;
    } catch (Exception e) {
        Log.e("Haggle", "Unzip error: ", e);
        succeed = false;
    }

    return succeed;
}