List of usage examples for java.io BufferedOutputStream close
@Override public void close() throws IOException
From source file:ilarkesto.integration.ftp.FtpClient.java
public void downloadFile(String path, File destination, boolean deleteRemotey) { log.debug("download:", path, "->", destination.getPath()); IO.createDirectory(destination.getParentFile()); File tmpFile = new File(destination.getParent() + "/" + destination.getName() + ".~downloading"); BufferedOutputStream out; try {// w ww .j ava 2 s.c o m out = new BufferedOutputStream(new FileOutputStream(tmpFile)); } catch (FileNotFoundException ex) { throw new RuntimeException( "Downloading file failed. Writing local file failed: " + tmpFile.getAbsolutePath(), ex); } try { boolean loaded = client.retrieveFile(path, out); out.close(); if (!loaded) throw new RuntimeException("Downloading file failed: " + path); } catch (IOException ex) { IO.deleteQuiet(tmpFile); throw new RuntimeException("Downloading file failed: " + path, ex); } try { if (deleteRemotey) deleteFile(path); IO.move(tmpFile, destination, true); } finally { IO.deleteQuiet(tmpFile); } }
From source file:com.phonegap.plugin.files.ExtractZipFilePlugin.java
@Override public boolean execute(String action, JSONArray args, CallbackContext cbc) { PluginResult.Status status = PluginResult.Status.OK; try {/*from w ww . jav a2s .c om*/ String filename = args.getString(0); URL url; try { url = new URL(filename); } catch (MalformedURLException e) { throw new RuntimeException(e); } File file = new File(url.getFile()); String[] dirToSplit = url.getFile().split(File.separator); String dirToInsert = ""; for (int i = 0; i < dirToSplit.length - 1; i++) { dirToInsert += dirToSplit[i] + File.separator; } ZipEntry entry; ZipFile zipfile; try { zipfile = new ZipFile(file); Enumeration e = zipfile.entries(); while (e.hasMoreElements()) { entry = (ZipEntry) e.nextElement(); BufferedInputStream is = null; try { is = new BufferedInputStream(zipfile.getInputStream(entry)); int count; byte data[] = new byte[102222]; String fileName = dirToInsert + entry.getName(); File outFile = new File(fileName); if (entry.isDirectory()) { if (!outFile.exists() && !outFile.mkdirs()) { Log.v("info", "Unable to create directories: " + outFile.getAbsolutePath()); cbc.sendPluginResult(new PluginResult(IO_EXCEPTION)); return false; } } else { File parent = outFile.getParentFile(); if (parent.mkdirs()) { Log.v("info", "created directory leading to file"); } FileOutputStream fos = null; BufferedOutputStream dest = null; try { fos = new FileOutputStream(outFile); dest = new BufferedOutputStream(fos, 102222); while ((count = is.read(data, 0, 102222)) != -1) { dest.write(data, 0, count); } } finally { if (dest != null) { dest.flush(); dest.close(); } if (fos != null) { fos.close(); } } } } finally { if (is != null) { is.close(); } } } } catch (ZipException e1) { Log.v("error", e1.getMessage(), e1); cbc.sendPluginResult(new PluginResult(MALFORMED_URL_EXCEPTION)); return false; } catch (IOException e1) { Log.v("error", e1.getMessage(), e1); cbc.sendPluginResult(new PluginResult(IO_EXCEPTION)); return false; } } catch (JSONException e) { cbc.sendPluginResult(new PluginResult(JSON_EXCEPTION)); return false; } cbc.sendPluginResult(new PluginResult(status)); return true; }
From source file:edu.stanford.epadd.launcher.Main.java
public static void copy_stream_to_file(InputStream is, String filename) throws IOException { int bufsize = 64 * 1024; BufferedInputStream bis = null; BufferedOutputStream bos = null; try {//from w w w .j av a 2 s. c om File f = new File(filename); if (f.exists()) { // out.println ("File " + filename + " exists"); boolean b = f.delete(); // best effort to delete file if it exists. this is because windows often complains about perms if (!b) out.println("Warning: failed to delete " + filename); } bis = new BufferedInputStream(is, bufsize); bos = new BufferedOutputStream(new FileOutputStream(filename), bufsize); byte buf[] = new byte[bufsize]; while (true) { int n = bis.read(buf); if (n <= 0) break; bos.write(buf, 0, n); } } catch (IOException ioe) { out.println("ERROR trying to copy data to file: " + filename + ", forging ahead nevertheless"); } finally { if (bis != null) bis.close(); if (bos != null) bos.close(); } }
From source file:com.life.wuhan.util.ImageDownloader.java
public void writeFile(String url, Bitmap bitmap) { String hashedUrl = getMd5(url); FileOutputStream fos;//from ww w. j a va 2s. c om try { fos = mContext.openFileOutput(hashedUrl, Context.MODE_PRIVATE); } catch (FileNotFoundException e) { return; } final BufferedOutputStream bos = new BufferedOutputStream(fos, 16384); bitmap.compress(Bitmap.CompressFormat.JPEG, 80, bos); try { bos.flush(); bos.close(); fos.close(); } catch (IOException e) { } }
From source file:com.flagleader.builder.BuilderConfig.java
public static void jarExtracting(String paramString1, String paramString2) { int i1 = 2048; BufferedOutputStream localBufferedOutputStream = null; BufferedInputStream localBufferedInputStream = null; JarEntry localJarEntry = null; JarFile localJarFile = null;//from w ww . jav a 2 s .c o m Enumeration localEnumeration = null; try { localJarFile = new JarFile(paramString2); localEnumeration = localJarFile.entries(); while (localEnumeration.hasMoreElements()) { localJarEntry = (JarEntry) localEnumeration.nextElement(); if (localJarEntry.isDirectory()) { new File(paramString1 + localJarEntry.getName()).mkdirs(); } else { localBufferedInputStream = new BufferedInputStream(localJarFile.getInputStream(localJarEntry)); byte[] arrayOfByte = new byte[i1]; FileOutputStream localFileOutputStream = new FileOutputStream( paramString1 + localJarEntry.getName()); localBufferedOutputStream = new BufferedOutputStream(localFileOutputStream, i1); int i2; while ((i2 = localBufferedInputStream.read(arrayOfByte, 0, i1)) != -1) localBufferedOutputStream.write(arrayOfByte, 0, i2); localBufferedOutputStream.flush(); localBufferedOutputStream.close(); localBufferedInputStream.close(); } } } catch (Exception localException1) { localException1.printStackTrace(); try { if (localBufferedOutputStream != null) { localBufferedOutputStream.flush(); localBufferedOutputStream.close(); } if (localBufferedInputStream != null) localBufferedInputStream.close(); } catch (Exception localException2) { localException2.printStackTrace(); } } finally { try { if (localBufferedOutputStream != null) { localBufferedOutputStream.flush(); localBufferedOutputStream.close(); } if (localBufferedInputStream != null) localBufferedInputStream.close(); } catch (Exception localException3) { localException3.printStackTrace(); } } }
From source file:io.syndesis.project.converter.DefaultProjectGeneratorTest.java
private Path generate(GenerateProjectRequest request, ProjectGeneratorProperties generatorProperties) throws IOException { try (InputStream is = new DefaultProjectGenerator(new ConnectorCatalog(CATALOG_PROPERTIES), generatorProperties, registry).generate(request)) { Path ret = Files.createTempDirectory("integration-runtime"); try (TarArchiveInputStream tis = new TarArchiveInputStream(is)) { TarArchiveEntry tarEntry = tis.getNextTarEntry(); // tarIn is a TarArchiveInputStream while (tarEntry != null) {// create a file with the same name as the tarEntry File destPath = new File(ret.toFile(), tarEntry.getName()); if (tarEntry.isDirectory()) { destPath.mkdirs();/*from w w w . jav a 2 s .c o m*/ } else { destPath.getParentFile().mkdirs(); destPath.createNewFile(); byte[] btoRead = new byte[8129]; BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(destPath)); int len = tis.read(btoRead); while (len != -1) { bout.write(btoRead, 0, len); len = tis.read(btoRead); } bout.close(); } tarEntry = tis.getNextTarEntry(); } } return ret; } }
From source file:net.sourceforge.fenixedu.util.sibs.SibsOutgoingPaymentFile.java
public void save(final File destinationFile) { BufferedOutputStream outputStream = null; try {//w w w . j a v a2 s. c o m outputStream = new BufferedOutputStream(new FileOutputStream(destinationFile)); outputStream.write(render().getBytes()); outputStream.flush(); } catch (FileNotFoundException e) { throw new RuntimeException(e); } catch (IOException e) { throw new RuntimeException(e); } finally { if (outputStream != null) { try { outputStream.close(); } catch (IOException e) { throw new RuntimeException(e); } } } }
From source file:com.haru.ui.image.workers.MediaProcessorThread.java
private void copyFileToDir() throws Exception { try {// ww w .j a v a 2 s .com File file; file = new File(Uri.parse(filePath).getPath()); File copyTo = new File(foldername + File.separator + file.getName()); FileInputStream streamIn = new FileInputStream(file); BufferedOutputStream outStream = new BufferedOutputStream(new FileOutputStream(copyTo)); byte[] buf = new byte[2048]; int len; while ((len = streamIn.read(buf)) > 0) { outStream.write(buf, 0, len); } streamIn.close(); outStream.close(); filePath = copyTo.getAbsolutePath(); } catch (FileNotFoundException e) { e.printStackTrace(); throw new Exception("File not found"); } catch (IOException e) { e.printStackTrace(); throw e; } catch (Exception e) { e.printStackTrace(); throw new Exception("Corrupt or deleted file???"); } }
From source file:de.unirostock.sems.caroweb.Converter.java
private void checkout(HttpServletRequest request, HttpServletResponse response, Path storage, String req) throws ServletException, IOException { Path target = storage.resolve(req).toAbsolutePath().normalize(); if (!target.startsWith(storage) || !Files.exists(target)) { error(request, response, "you're not allowed to download that file."); return;//from w w w . jav a 2 s. co m } try { String mime = target.toString().endsWith("omex") ? "application/zip" : "application/vnd.wf4ever.robundle+zip"; response.reset(); response.setBufferSize(CaRoWebutils.DEFAULT_BUFFER_SIZE); response.setContentType(mime); response.setHeader("Content-Length", String.valueOf(target.toFile().length())); response.setHeader("Content-Disposition", "attachment; filename=\"" + req + "\""); response.setHeader("Expires", CaRoWebutils.downloadDateFormater .format(new Date(System.currentTimeMillis() + CaRoWebutils.CACHE_TIME * 1000))); response.setHeader("Cache-Control", "max-age=" + CaRoWebutils.CACHE_TIME); response.setHeader("Last-Modified", CaRoWebutils.downloadDateFormater .format(new Date(Files.getLastModifiedTime(target).toMillis()))); response.setHeader("ETag", GeneralTools.hash(target + "-" + Files.getLastModifiedTime(target))); BufferedInputStream input = new BufferedInputStream(new FileInputStream(target.toFile()), CaRoWebutils.DEFAULT_BUFFER_SIZE); BufferedOutputStream output = new BufferedOutputStream(response.getOutputStream(), CaRoWebutils.DEFAULT_BUFFER_SIZE); // pass the stream to client byte[] buffer = new byte[CaRoWebutils.DEFAULT_BUFFER_SIZE]; int length; while ((length = input.read(buffer)) > 0) { output.write(buffer, 0, length); } input.close(); output.close(); return; } catch (IOException e) { // whoops, that's our fault. shouldn't happen. hopefully. LOGGER.error("unable to dump file " + target + " (at least not in an expected form)"); } error(request, response, "couldn't dump file"); }
From source file:eu.scape_project.hawarp.webarchive.PayloadContent.java
private byte[] inputStreamToByteArray() { try {//from www . j a va 2s . c o m ByteArrayOutputStream baos = new ByteArrayOutputStream(); BufferedInputStream buffis = new BufferedInputStream(inputStream); BufferedOutputStream buffos = new BufferedOutputStream(baos); byte[] tempBuffer = new byte[8192]; int bytesRead; boolean firstByteArray = true; while ((bytesRead = buffis.read(tempBuffer)) != -1) { buffos.write(tempBuffer, 0, bytesRead); if (doPayloadIdentification && firstByteArray && tempBuffer != null && bytesRead > 0) { identified = identifyPayloadType(tempBuffer); } firstByteArray = false; } //buffis.close(); buffos.flush(); buffos.close(); return baos.toByteArray(); } catch (IOException ex) { LOG.error("Error while trying to read payload content", ex); this.error = true; return null; } }