List of usage examples for java.io BufferedOutputStream flush
@Override public synchronized void flush() throws IOException
From source file:com.sastix.cms.common.services.htmltopdf.PdfImpl.java
private File saveAs(String path, byte[] document) throws IOException { File file = new File(path); BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file)); bufferedOutputStream.write(document); bufferedOutputStream.flush(); bufferedOutputStream.close();//from w w w.ja v a 2 s . c om return file; }
From source file:com.aurel.track.util.PluginUtils.java
/** * Unzip this file into the given directory. If the zipFile is called "zipFile.zip", * the files will be placed into "targetDir/zipFile". * @param zipFile//w ww . ja v a 2 s.c om * @param targetDir */ public static void unzipFileIntoDirectory(File zipFile, File targetDir) { try { LOGGER.debug("Expanding Genji extension file " + zipFile.getName()); int BUFFER = 2048; File file = zipFile; ZipFile zip = new ZipFile(file); String newPath = targetDir + File.separator + zipFile.getName().substring(0, zipFile.getName().length() - 4); new File(newPath).mkdir(); Enumeration zipFileEntries = zip.entries(); // Process each entry while (zipFileEntries.hasMoreElements()) { // grab a zip file entry ZipEntry entry = (ZipEntry) zipFileEntries.nextElement(); String currentEntry = entry.getName(); File destFile = new File(newPath, currentEntry); //destFile = new File(newPath, destFile.getName()); File destinationParent = destFile.getParentFile(); // create the parent directory structure if needed destinationParent.mkdirs(); if (!entry.isDirectory()) { BufferedInputStream is = new BufferedInputStream(zip.getInputStream(entry)); int currentByte; // establish buffer for writing file byte data[] = new byte[BUFFER]; // write the current file to disk LOGGER.debug("Unzipping " + destFile.getAbsolutePath()); FileOutputStream fos = new FileOutputStream(destFile); BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER); // read and write until last byte is encountered while ((currentByte = is.read(data, 0, BUFFER)) != -1) { dest.write(data, 0, currentByte); } dest.flush(); dest.close(); is.close(); } } } catch (Exception e) { LOGGER.error(ExceptionUtils.getStackTrace(e)); } }
From source file:com.lugia.timetable.SubjectList.java
public boolean saveToFile(Context context) { try {//from w w w .j a v a 2 s .com FileOutputStream out = context.openFileOutput(SAVEFILE, Context.MODE_PRIVATE); BufferedOutputStream stream = new BufferedOutputStream(out); stream.write(generateJSON().toString().getBytes()); stream.flush(); stream.close(); out.close(); } catch (Exception e) { // something went wrong Log.e(TAG, "Error on save!", e); return false; } return true; }
From source file:de.rub.syssec.saaf.analysis.steps.extract.ApkUnzipper.java
/** * Extracts the given apk into the given destination directory * // w w w. j av a 2 s. c om * @param archive * - the file to extract * @param dest * - the destination directory * @throws IOException */ public static void extractApk(File archive, File dest) throws IOException { @SuppressWarnings("resource") // Closing it later results in an error ZipFile zipFile = new ZipFile(archive); Enumeration<? extends ZipEntry> entries = zipFile.entries(); BufferedOutputStream bos = null; BufferedInputStream bis = null; while (entries.hasMoreElements()) { ZipEntry entry = entries.nextElement(); String entryFileName = entry.getName(); byte[] buffer = new byte[16384]; int len; File dir = buildDirectoryHierarchyFor(entryFileName, dest);// destDir if (!dir.exists()) { dir.mkdirs(); } if (!entry.isDirectory()) { if (entry.getSize() == 0) { LOGGER.warn("Found ZipEntry \'" + entry.getName() + "\' with size 0 in " + archive.getName() + ". Looks corrupted."); continue; } try { bos = new BufferedOutputStream(new FileOutputStream(new File(dest, entryFileName)));// destDir,... bis = new BufferedInputStream(zipFile.getInputStream(entry)); while ((len = bis.read(buffer)) > 0) { bos.write(buffer, 0, len); } bos.flush(); } catch (IOException ioe) { LOGGER.warn("Failed to extract entry \'" + entry.getName() + "\' from archive. Results for " + archive.getName() + " may not be accurate"); } finally { if (bos != null) bos.close(); if (bis != null) bis.close(); // if (zipFile != null) zipFile.close(); } } } }
From source file:com.macdonst.ftpclient.FtpClient.java
/** * Downloads a file from a ftp server./*from ww w .j a va 2s.c o m*/ * @param filename the name to store the file locally * @param url the url of the server * @throws IOException */ private void get(String filename, URL url) throws IOException { FTPClient f = setup(url); BufferedOutputStream buffOut = null; buffOut = new BufferedOutputStream(new FileOutputStream(filename)); f.retrieveFile(extractFileName(url), buffOut); buffOut.flush(); buffOut.close(); teardown(f); }
From source file:com.celements.photo.utilities.Unzip.java
private ByteArrayOutputStream findAndExtractFile(String filename, ZipInputStream zipIn) throws IOException { ByteArrayOutputStream out = null; for (ZipEntry entry = zipIn.getNextEntry(); zipIn.available() > 0; entry = zipIn.getNextEntry()) { if (!entry.isDirectory() && entry.getName().equals(filename)) { // read the data and write it to the OutputStream int count; byte[] data = new byte[BUFFER]; out = new ByteArrayOutputStream(); BufferedOutputStream byteOut = new BufferedOutputStream(out, BUFFER); while ((count = zipIn.read(data, 0, BUFFER)) != -1) { byteOut.write(data, 0, count); }// www .j a va 2s.c o m byteOut.flush(); break; } } zipIn.close(); return out; }
From source file:de.uka.ilkd.key.dl.gui.initialdialog.gui.ToolInstaller.java
/** * @param tmp//from www . j ava2 s.com * @param * @throws FileNotFoundException * @throws IOException */ private static void unzip(File file, File dir, PropertySetter ps, JProgressBar bar) throws FileNotFoundException, IOException { final int BUFFER = 2048; BufferedOutputStream dest = null; FileInputStream fis = new FileInputStream(file); ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis)); ZipEntry entry; int value = 0; while ((entry = zis.getNextEntry()) != null) { bar.setValue(value++); int count; byte data[] = new byte[BUFFER]; // write the files to the disk String outputFile = dir.getAbsolutePath() + File.separator + entry.getName(); if (entry.isDirectory()) { new File(outputFile).mkdirs(); } else { FileOutputStream fos = new FileOutputStream(outputFile); dest = new BufferedOutputStream(fos, BUFFER); while ((count = zis.read(data, 0, BUFFER)) != -1) { dest.write(data, 0, count); } dest.flush(); dest.close(); File oFile = new File(outputFile); if (OSInfosDefault.INSTANCE.getOs() == OperatingSystem.OSX) { // FIXME: we need to make everything executable as somehow // the executable bit is not preserved in // OSX oFile.setExecutable(true); } if (ps.filterFilename(oFile)) { ps.setProperty(outputFile); } } } zis.close(); file.delete(); }
From source file:net.solarnetwork.node.backup.ZipStreamBackupResource.java
@Override public InputStream getInputStream() throws IOException { // to support calling getInputStream() more than once, tee the input to a temp file // the first time, and subsequent times if (tempFile != null) { return new BufferedInputStream(new FileInputStream(tempFile)); }/*from w ww . j ava2 s.c om*/ tempFile = File.createTempFile(entry.getName(), ".tmp"); final BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(tempFile)); return new TeeInputStream(new FilterInputStream(stream) { @Override public void close() throws IOException { out.flush(); out.close(); } }, out, false); }
From source file:org.apache.hama.manager.LogView.java
/** * download log file.// www . j av a 2 s.c o m * * @param filPath log file path. * @throws Exception */ public static void downloadFile(HttpServletResponse response, String filePath) throws ServletException, IOException { File file = new File(filePath); if (!file.exists()) { throw new ServletException("File doesn't exists."); } String headerKey = "Content-Disposition"; String headerValue = "attachment; filename=\"" + file.getName() + "\""; BufferedInputStream in = null; BufferedOutputStream out = null; try { response.setContentType("application/octet-stream"); response.setContentLength((int) file.length()); response.setHeader(headerKey, headerValue); in = new BufferedInputStream(new FileInputStream(file)); out = new BufferedOutputStream(response.getOutputStream()); byte[] buffer = new byte[4 * 1024]; int read = -1; while ((read = in.read(buffer)) != -1) { out.write(buffer, 0, read); } } catch (SocketException e) { // download cancel.. } catch (Exception e) { response.reset(); response.sendError(HttpURLConnection.HTTP_INTERNAL_ERROR, e.toString()); } finally { if (in != null) { in.close(); } if (out != null) { out.flush(); out.close(); } } }
From source file:edu.ur.util.FileUtil.java
/** * Create a file in the specified directory with the * specified name and place in it the specified contents. * * @param directory - directory in which to create the file * @param fileName - name of the file to create * @param contents - Simple string to create the file *///from w w w. j a va 2 s . c o m public File creatFile(File directory, String fileName, String contents) { File f = new File(directory.getAbsolutePath() + IOUtils.DIR_SEPARATOR + fileName); // create the file BufferedOutputStream bos = null; FileOutputStream fos = null; try { f.createNewFile(); fos = new FileOutputStream(f); bos = new BufferedOutputStream(fos); bos.write(contents.getBytes()); bos.flush(); bos.close(); fos.close(); } catch (Exception e) { if (bos != null) { try { bos.close(); } catch (Exception ec) { } } if (fos != null) { try { fos.close(); } catch (Exception ec) { } } throw new RuntimeException(e); } return f; }