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

public static boolean saveFileFromAssetsToSystem(Context context, String path, File destFile) {
    BufferedInputStream bis = null;
    BufferedOutputStream fos = null;
    try {//from   w w w.ja  va  2s.  c  om
        InputStream is = context.getAssets().open(path);
        bis = new BufferedInputStream(is);
        fos = new BufferedOutputStream(new FileOutputStream(destFile));
        byte[] buf = new byte[2048];
        int i;
        while ((i = bis.read(buf)) != -1) {
            fos.write(buf, 0, i);
        }
        fos.flush();
        return true;
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (bis != null) {
                bis.close();
            }
            if (fos != null) {
                fos.close();
            }
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }
    return false;
}

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  .j a  va  2s. co  m*/
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        is = new BufferedInputStream(urlConnection.getInputStream());
        is.mark(MAX_READ_LIMIT_PER_IMG);
        int scaleFactor = findScaleFactor(width, height, is);
        is.reset();
        return scaleBitmap(scaleFactor, is);
    } finally {
        if (is != null) {
            is.close();
        }
    }
}

From source file:Main.java

/**
 * Parses the given {@link File} with the specified {@link XMLReader}.
 * // w ww .  ja va  2 s  .com
 * <p>This method assumes the XML is in 'UTF-8', it will not sniff the XML to
 * determine the encoding to use.
 * 
 * @param xmlreader The XML reader to use.
 * @param file      The file to parse
 * 
 * @throws FileNotFoundException If the file does not exists
 * @throws SAXException          Should an SAX exception occur
 * @throws IOException           Should an I/O exception occur
 */
public static void parse(XMLReader xmlreader, File file)
        throws FileNotFoundException, SAXException, IOException {
    // create the input source
    InputStream bin = new BufferedInputStream(new FileInputStream(file));
    Reader reader = new InputStreamReader(bin, "utf-8");
    InputSource source = new InputSource(reader);
    // parse the file
    xmlreader.parse(source);
    reader.close();
}

From source file:com.opengamma.util.money.StandardCurrencyPairs.java

private static void parseCSV(String filename, InputStream is) {
    CSVReader reader = new CSVReader(new InputStreamReader(new BufferedInputStream(is)));
    List<String[]> rows;
    try {//from  w  w w.j  a  va  2 s. com
        rows = reader.readAll();
        int line = 1;
        for (String[] row : rows) {
            String numerator = row[0].trim();
            String denominator = row[1].trim();
            try {
                s_currencyPairs.add(Pair.of(Currency.of(numerator), Currency.of(denominator)));
            } catch (IllegalArgumentException iae) {
                s_logger.warn("Couldn't create currency from " + filename + ":" + line);
            }
            line++;
        }
    } catch (IOException ex) {
        s_logger.warn("Couldn't read " + filename);
    }
}

From source file:com.codemarvels.ant.aptrepotask.utils.Utils.java

/**
 * Compute the given message digest for a file.
 * /* w  w  w.j  av  a  2 s  .c om*/
 * @param hashType algorithm to be used (as {@code String})
 * @param file File to compute the digest for (as {@code File}).
 * @return A {@code String} for the hex encoded digest.
 * @throws MojoExecutionException
 */
public static String getDigest(String hashType, File file) {
    try {
        FileInputStream fis = new FileInputStream(file);
        BufferedInputStream bis = new BufferedInputStream(fis);
        MessageDigest digest = MessageDigest.getInstance(hashType);
        DigestInputStream dis = new DigestInputStream(bis, digest);
        @SuppressWarnings("unused")
        int ch;
        while ((ch = dis.read()) != -1)
            ;
        String hex = new String(Hex.encodeHex(digest.digest()));
        fis.close();
        bis.close();
        dis.close();
        return hex;
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException("could not create digest", e);
    } catch (FileNotFoundException e) {
        throw new RuntimeException("could not create digest", e);
    } catch (IOException e) {
        throw new RuntimeException("could not create digest", e);
    }
}

From source file:com.mirth.connect.model.converters.DICOMConverter.java

public static DicomObject byteArrayToDicomObject(byte[] bytes, boolean decodeBase64) throws IOException {
    DicomObject basicDicomObject = new BasicDicomObject();
    DicomInputStream dis = null;//from   w w w  .j a  va 2  s .com

    try {
        ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
        InputStream inputStream;
        if (decodeBase64) {
            inputStream = new BufferedInputStream(new Base64InputStream(bais));
        } else {
            inputStream = bais;
        }
        dis = new DicomInputStream(inputStream);
        /*
         * This parameter was added in dcm4che 2.0.28. We use it to retain the memory allocation
         * behavior from 2.0.25.
         * http://www.mirthcorp.com/community/issues/browse/MIRTH-2166
         * http://www.dcm4che.org/jira/browse/DCM-554
         */
        dis.setAllocateLimit(-1);
        dis.readDicomObject(basicDicomObject, -1);
    } catch (IOException e) {
        throw e;
    } finally {
        IOUtils.closeQuietly(dis);
    }

    return basicDicomObject;
}

From source file:Main.java

public static String unzip(String filename) throws IOException {
    BufferedOutputStream origin = null;
    String path = null;/*from  w ww.  j a va  2  s  .  c  om*/
    Integer BUFFER_SIZE = 20480;
    if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) {

        File flockedFilesFolder = new File(
                Environment.getExternalStorageDirectory() + File.separator + "FlockLoad");
        System.out.println("FlockedFileDir: " + flockedFilesFolder);
        String compressedFile = flockedFilesFolder.toString() + "/" + filename;
        System.out.println("compressed File name:" + compressedFile);
        //String uncompressedFile = flockedFilesFolder.toString()+"/"+"flockUnZip";
        File purgeFile = new File(compressedFile);

        try {
            ZipInputStream zin = new ZipInputStream(new FileInputStream(compressedFile));
            BufferedInputStream bis = new BufferedInputStream(zin);
            try {
                ZipEntry ze = null;
                while ((ze = zin.getNextEntry()) != null) {
                    byte data[] = new byte[BUFFER_SIZE];
                    path = flockedFilesFolder.toString() + "/" + ze.getName();
                    System.out.println("path is:" + path);
                    if (ze.isDirectory()) {
                        File unzipFile = new File(path);
                        if (!unzipFile.isDirectory()) {
                            unzipFile.mkdirs();
                        }
                    } else {
                        FileOutputStream fout = new FileOutputStream(path, false);
                        origin = new BufferedOutputStream(fout, BUFFER_SIZE);
                        try {
                            /* for (int c = bis.read(data, 0, BUFFER_SIZE); c != -1; c = bis.read(data)) {
                            origin.write(c);
                             }
                             */
                            int count;
                            while ((count = bis.read(data, 0, BUFFER_SIZE)) != -1) {
                                origin.write(data, 0, count);
                            }
                            zin.closeEntry();
                        } finally {
                            origin.close();
                            fout.close();

                        }
                    }
                }
            } finally {
                bis.close();
                zin.close();
                purgeFile.delete();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return path;
    }
    return null;
}

From source file:Main.java

public static Object unmarshall(String cntxtPkg, URL url) throws JAXBException, SAXException, IOException {
    InputStream is = url.openStream();
    return unmarshall(cntxtPkg, new BufferedInputStream(is));
}

From source file:Main.java

public static Bitmap loadMpoBitmapFromFile(File file, long offset, int maxWidth, int maxHeight)
        throws IOException {
    // First, decode the width and height of the image, so that we know how much to scale
    // it down when loading it into our ImageView (so we don't need to do a huge allocation).
    BitmapFactory.Options opts = new BitmapFactory.Options();
    InputStream fs = null;//from  ww w  .ja  v a 2 s  . c  om
    try {
        fs = new BufferedInputStream(new FileInputStream(file));
        fs.skip(offset);
        opts.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(fs, null, opts);
    } finally {
        if (fs != null) {
            try {
                fs.close();
            } catch (IOException e) {
                // don't worry
            }
        }
    }
    int scale = 1;
    if (opts.outHeight > maxHeight || opts.outWidth > maxWidth) {
        scale = (int) Math.pow(2, (int) Math
                .round(Math.log(maxWidth / (double) Math.max(opts.outHeight, opts.outWidth)) / Math.log(0.5)));
    }
    if ((opts.outHeight <= 0) || (opts.outWidth <= 0)) {
        return null;
    }
    // Decode the image for real, but now with a sampleSize.
    // We have to reopen the file stream, and re-skip to the correct offset, since
    // FileInputStream doesn't support reset().
    Bitmap bmp = null;
    fs = null;
    try {
        fs = new BufferedInputStream(new FileInputStream(file));
        fs.skip(offset);
        BitmapFactory.Options opts2 = new BitmapFactory.Options();
        opts2.inSampleSize = scale;
        bmp = BitmapFactory.decodeStream(fs, null, opts2);
    } finally {
        if (fs != null) {
            try {
                fs.close();
            } catch (IOException e) {
                // don't worry
            }
        }
    }
    return bmp;
}

From source file:com.boundary.zoocreeper.Restore.java

public static void main(String[] args) throws IOException, InterruptedException, KeeperException {
    RestoreOptions options = new RestoreOptions();
    CmdLineParser parser = new CmdLineParser(options);
    try {/*from ww  w  .ja  v  a  2  s  . co  m*/
        parser.parseArgument(args);
        if (options.help) {
            usage(parser, 0);
        }
    } catch (CmdLineException e) {
        if (!options.help) {
            System.err.println(e.getLocalizedMessage());
        }
        usage(parser, options.help ? 0 : 1);
    }
    if (options.verbose) {
        LoggingUtils.enableDebugLogging(Restore.class.getPackage().getName());
    }
    InputStream is = null;
    try {
        if ("-".equals(options.inputFile)) {
            LOGGER.info("Restoring from stdin");
            is = new BufferedInputStream(System.in);
        } else {
            is = new BufferedInputStream(new FileInputStream(options.inputFile));
        }
        if (options.compress) {
            is = new GZIPInputStream(is);
        }
        Restore restore = new Restore(options);
        restore.restore(is);
    } finally {
        Closeables.close(is, true);
    }
}