List of usage examples for java.io BufferedInputStream close
public void close() throws IOException
From source file:Main.java
/** * Move the file in oldLocation to newLocation. *//* w ww. ja v a 2s. c o m*/ public static void moveFile(String tempLocation, String newLocation) throws IOException { File oldLocation = new File(tempLocation); if (oldLocation != null && oldLocation.exists()) { BufferedInputStream reader = new BufferedInputStream(new FileInputStream(oldLocation)); BufferedOutputStream writer = new BufferedOutputStream(new FileOutputStream(newLocation, false)); try { byte[] buff = new byte[8192]; int numChars; while ((numChars = reader.read(buff, 0, buff.length)) != -1) { writer.write(buff, 0, numChars); } } catch (IOException ex) { } finally { try { if (reader != null) { writer.close(); reader.close(); } } catch (IOException ex) { } } } else { } }
From source file:com.dc.util.file.FileSupport.java
public static void writeBinaryFile(InputStream in, File targetFile, boolean encrypted, String key) throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException { BufferedInputStream bis = null; BufferedOutputStream bos = null; try {// ww w. ja va 2 s. c o m bis = new BufferedInputStream(in); bos = new BufferedOutputStream(new FileOutputStream(targetFile)); if (encrypted) { copyEncryptedStream(bis, bos, key); } else { copyStream(bis, bos); } } finally { if (bis != null) { bis.close(); } if (bos != null) { bos.close(); } } }
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();// ww w . ja v a 2s. co 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
public static Bitmap getAndSetBitmapFromNet(String urlPath) { Bitmap bm = null;/*from w ww .java 2s . c o m*/ if (urlPath != null) { try { BufferedInputStream bis = new BufferedInputStream(new URL(urlPath).openStream(), 1024); final ByteArrayOutputStream dataStream = new ByteArrayOutputStream(); BufferedOutputStream out = new BufferedOutputStream(dataStream, 1024); copy(bis, out); out.flush(); final byte[] data = dataStream.toByteArray(); bm = BitmapFactory.decodeByteArray(data, 0, data.length); Log.i(I, "data.length: " + data.length); out.close(); dataStream.close(); bis.close(); bm = processBitmap(bm); } catch (IOException e) { Log.i(I, "URL Connection or Bitmap processing Exception"); e.printStackTrace(); } } return bm; }
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 ww. j a v a2s.c o m 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: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./*from w w w. j av a2 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
public static boolean copyFile(File fromFile, File toFile) { BufferedInputStream bis = null; BufferedOutputStream fos = null; try {// www . j a va 2 s .c om FileInputStream is = new FileInputStream(fromFile); bis = new BufferedInputStream(is); fos = new BufferedOutputStream(new FileOutputStream(toFile)); 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:com.bukanir.android.utils.Utils.java
public static void saveURL(String url, String filename) throws IOException { BufferedInputStream in = null; FileOutputStream fout = null; try {//from w w w . j a va2s . com in = new BufferedInputStream(new URL(url).openStream()); fout = new FileOutputStream(filename); int count; final byte data[] = new byte[1024]; while ((count = in.read(data, 0, 1024)) != -1) { fout.write(data, 0, count); } } finally { if (in != null) { in.close(); } if (fout != null) { fout.close(); } } }
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. j av a2 s .com 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:org.openmrs.module.report.util.FileUtils.java
public static boolean copyFile(File source, File dest) { try {//from w w w. j a v a 2 s . com BufferedInputStream in = new BufferedInputStream(new FileInputStream(source)); BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(dest)); byte[] data = new byte[2 * 1024]; int count; while ((count = in.read(data, 0, data.length)) != -1) { out.write(data, 0, count); } out.flush(); out.close(); in.close(); return true; } catch (FileNotFoundException e) { return false; } catch (IOException e) { return false; } }