List of usage examples for java.io BufferedOutputStream close
@Override public void close() throws IOException
From source file:Main.java
public static Bitmap loadBitmap(String url) { Log.d(TAG, "Start Load Url : " + url); //Bitmap bitmap = null; InputStream in = null;// w ww .ja v a 2s. c o m BufferedOutputStream out = null; try { in = new BufferedInputStream(new URL(url).openStream()); final ByteArrayOutputStream dataStream = new ByteArrayOutputStream(); int nRead; byte[] data = new byte[16384]; while ((nRead = in.read(data, 0, data.length)) != -1) { dataStream.write(data, 0, nRead); } dataStream.flush(); final byte[] newData = dataStream.toByteArray(); BitmapFactory.Options options = new BitmapFactory.Options(); bitmapResult = BitmapFactory.decodeByteArray(newData, 0, newData.length, options); } catch (IOException e) { Log.e("My fault", "Could not load Bitmap from: " + url); } finally { if (in != null) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } if (out != null) { try { out.close(); } catch (IOException e) { e.printStackTrace(); } } } return bitmapResult; }
From source file:Main.java
public static void unZip(String path) { int count = -1; int index = -1; String savepath = ""; savepath = path.substring(0, path.lastIndexOf(".")); try {/*from w w w . ja v a 2s . co m*/ BufferedOutputStream bos = null; ZipEntry entry = null; FileInputStream fis = new FileInputStream(path); ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis)); while ((entry = zis.getNextEntry()) != null) { byte data[] = new byte[buffer]; String temp = entry.getName(); index = temp.lastIndexOf("/"); if (index > -1) temp = temp.substring(index + 1); String tempDir = savepath + "/zip/"; File dir = new File(tempDir); dir.mkdirs(); temp = tempDir + temp; File f = new File(temp); f.createNewFile(); FileOutputStream fos = new FileOutputStream(f); bos = new BufferedOutputStream(fos, buffer); while ((count = zis.read(data, 0, buffer)) != -1) { bos.write(data, 0, count); } bos.flush(); bos.close(); } zis.close(); } catch (Exception e) { e.printStackTrace(); } }
From source file:Main.java
public static void downloadImage(String imageUrl) { if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { Log.d("TAG", "monted sdcard"); } else {// w w w . j a v a2 s . co m Log.d("TAG", "has no sdcard"); } HttpURLConnection con = null; FileOutputStream fos = null; BufferedOutputStream bos = null; BufferedInputStream bis = null; File imageFile = null; try { URL url = new URL(imageUrl); con = (HttpURLConnection) url.openConnection(); con.setConnectTimeout(5 * 1000); con.setReadTimeout(15 * 1000); con.setDoInput(true); con.setDoOutput(true); bis = new BufferedInputStream(con.getInputStream()); imageFile = new File(getImagePath(imageUrl)); fos = new FileOutputStream(imageFile); bos = new BufferedOutputStream(fos); byte[] b = new byte[1024]; int length; while ((length = bis.read(b)) != -1) { bos.write(b, 0, length); bos.flush(); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (bis != null) { bis.close(); } if (bos != null) { bos.close(); } if (con != null) { con.disconnect(); } } catch (IOException e) { e.printStackTrace(); } } if (imageFile != null) { } }
From source file:com.vmware.photon.controller.deployer.helpers.TestHelper.java
public static File createSourceFile(String sourceFileName, File sourceDirectory) throws IOException { sourceDirectory.mkdirs();//ww w . j a v a2s.c o m if (sourceFileName == null) { sourceFileName = "esxcloud-" + UUID.randomUUID().toString() + ".vib"; } File sourceFile = new File(sourceDirectory, sourceFileName); sourceFile.createNewFile(); OutputStream outputStream = new FileOutputStream(sourceFile); BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream); Random random = new Random(); byte[] randomBytes = new byte[1024]; for (int i = 0; i < 10 * 1024; i++) { random.nextBytes(randomBytes); bufferedOutputStream.write(randomBytes); } bufferedOutputStream.close(); outputStream.close(); return sourceFile; }
From source file:autohit.common.Utils.java
/** * Copy files. I can't believe I have to write this. It will always * overwrite an existing file.//from w w w . j av a 2 s . c o m * @param source source file path * @param dest destination file path * @return string containing errors or messages */ public static String copy(String source, String dest) { String overwrite = " "; BufferedOutputStream bos = null; try { File destf = new File(dest); if (destf.exists()) { destf.delete(); overwrite = " [overwrite] "; } byte[] buf = new byte[1024]; int sbuf; BufferedInputStream bis = new BufferedInputStream(new FileInputStream(source)); bos = new BufferedOutputStream(new FileOutputStream(dest)); sbuf = bis.read(buf, 0, 1024); while (sbuf > 0) { bos.write(buf, 0, sbuf); sbuf = bis.read(buf, 0, 1024); } } catch (Exception e) { } try { bos.close(); } catch (Exception e) { return "Copy failed for=" + source + Constants.CRUDE_SEPERATOR; } return "Copied file=" + source + overwrite + Constants.CRUDE_SEPERATOR; }
From source file:com.sangupta.jerry.util.ZipUtils.java
/** * Read a given file from the ZIP file and store it in a temporary file. The * temporary file is set to be deleted on exit of application. * //from ww w. j a v a 2 s . co m * @param zipFile * the zip file from which the file needs to be read * * @param fileName * the name of the file that needs to be extracted * * @return the {@link File} handle for the extracted file in the temp * directory * * @throws IllegalArgumentException * if the zipFile is <code>null</code> or the fileName is * <code>null</code> or empty. */ public static File readFileFromZip(File zipFile, String fileName) throws FileNotFoundException, IOException { if (zipFile == null) { throw new IllegalArgumentException("zip file to extract from cannot be null"); } if (AssertUtils.isEmpty(fileName)) { throw new IllegalArgumentException("the filename to extract cannot be null/empty"); } LOGGER.debug("Reading {} from {}", fileName, zipFile.getAbsolutePath()); ZipInputStream stream = null; BufferedOutputStream outStream = null; File tempFile = null; try { byte[] buf = new byte[1024]; stream = new ZipInputStream(new FileInputStream(zipFile)); ZipEntry entry; while ((entry = stream.getNextEntry()) != null) { String entryName = entry.getName(); if (entryName.equals(fileName)) { tempFile = File.createTempFile(FilenameUtils.getName(entryName), FilenameUtils.getExtension(entryName)); tempFile.deleteOnExit(); outStream = new BufferedOutputStream(new FileOutputStream(tempFile)); int readBytes; while ((readBytes = stream.read(buf, 0, 1024)) > -1) { outStream.write(buf, 0, readBytes); } stream.close(); outStream.close(); return tempFile; } } } finally { IOUtils.closeQuietly(stream); IOUtils.closeQuietly(outStream); } return tempFile; }
From source file:com.sun.socialsite.util.Utilities.java
public static void copyInputToOutput(InputStream input, OutputStream output) throws IOException { BufferedInputStream in = new BufferedInputStream(input); BufferedOutputStream out = new BufferedOutputStream(output); byte buffer[] = new byte[8192]; for (int count = 0; count != -1;) { count = in.read(buffer, 0, 8192); if (count != -1) out.write(buffer, 0, count); }//from w w w. j a v a 2 s. c o m try { in.close(); out.close(); } catch (IOException ex) { throw new IOException("Closing file streams, " + ex.getMessage()); } }
From source file:com.anthony.forumspring.controller.FileUploadController.java
@RequestMapping(value = "/uploadFile", method = RequestMethod.POST) public @ResponseBody String uploadFildHander(@RequestParam(value = "name") String name, @RequestParam("file") MultipartFile file) { if (!file.isEmpty()) { try {/*from w w w . j ava 2 s .co m*/ byte[] bytes = file.getBytes(); // Creating the directory to store file String rootPath = System.getProperty("catalina.home"); File dir = new File(rootPath + File.separator + "tmpFiles"); if (!dir.exists()) { dir.mkdirs(); } // Create the file on server File serverFile = new File(dir.getAbsolutePath() + File.separator + name); BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(serverFile)); stream.write(bytes); stream.close(); //logger.info("Server File Location=" // + serverFile.getAbsolutePath()); return "You successfully uploaded file=" + name; } catch (Exception e) { return "You failed to upload " + name + " => " + e.getMessage(); } } else { return "You failed to upload " + name + " because the file was empty."; } }
From source file:com.mvdb.etl.actions.ActionUtils.java
public static void zipFullDirectory(String sourceDir, String targetZipFile) { FileOutputStream fos = null;//from w w w. j a va2 s . c om BufferedOutputStream bos = null; ZipOutputStream zos = null; try { fos = new FileOutputStream(targetZipFile); bos = new BufferedOutputStream(fos); zos = new ZipOutputStream(bos); zipDir(sourceDir, new File(sourceDir), zos); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (zos != null) { try { zos.flush(); zos.close(); } catch (IOException e) { e.printStackTrace(); } } if (bos != null) { try { bos.flush(); bos.close(); } catch (IOException e) { e.printStackTrace(); } } if (fos != null) { try { fos.flush(); fos.close(); } catch (IOException e) { e.printStackTrace(); } } } }
From source file:org.alfresco.selenium.FetchUtil.java
/** * Retrieve file with authentication, using {@link WebDriver} cookie we * keep the session and use HttpClient to download files that requires * user to be authenticated. //from w w w. ja va2 s. c om * @param resourceUrl path to file to download * @param driver {@link WebDriver} * @param output path to output the file * @throws IOException if error */ protected static File retrieveFile(final String resourceUrl, final CloseableHttpClient client, final File output) throws IOException { HttpGet httpGet = new HttpGet(resourceUrl); BufferedOutputStream bos = null; BufferedInputStream bis = null; HttpResponse response = null; try { response = client.execute(httpGet); HttpEntity entity = response.getEntity(); bos = new BufferedOutputStream(new FileOutputStream(output)); bis = new BufferedInputStream(entity.getContent()); int inByte; while ((inByte = bis.read()) != -1) bos.write(inByte); HttpClientUtils.closeQuietly(response); } catch (Exception e) { logger.error("Unable to fetch file " + resourceUrl, e); } finally { if (response != null) { HttpClientUtils.closeQuietly(response); } if (bis != null) { bis.close(); } if (bos != null) { bos.close(); } } return output; }