List of usage examples for java.io BufferedOutputStream close
@Override public void close() throws IOException
From source file:coral.reef.web.FileUploadController.java
/** * Extracts a zip entry (file entry)/*from w w w .j a va 2 s .c o m*/ * * @param zipIn * @param filePath * @throws IOException */ private void extractFile(ZipInputStream zipIn, File filePath) throws IOException { filePath.getParentFile().mkdirs(); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath)); byte[] bytesIn = new byte[BUFFER_SIZE]; int read = 0; while ((read = zipIn.read(bytesIn)) != -1) { bos.write(bytesIn, 0, read); } bos.close(); }
From source file:eu.mrbussy.security.crypto.pgp.PGPDecryptor.java
public void decryptFile(InputStream in, OutputStream out) throws Exception { BufferedOutputStream bOut = new BufferedOutputStream(out); InputStream unc = decryptFile(in); int ch;// w ww. ja v a 2 s. c o m while ((ch = unc.read()) >= 0) { bOut.write(ch); } bOut.close(); }
From source file:com.lin.umws.service.impl.UserServiceImpl.java
public void badUpload(byte[] file) { BufferedOutputStream bos = new BufferedOutputStream(System.out); try {/*from www . j a v a 2 s. com*/ bos.write(file); bos.flush(); } catch (IOException e) { e.printStackTrace(); } finally { try { bos.close(); } catch (IOException e) { e.printStackTrace(); } } }
From source file:hudson.plugins.jobConfigHistory.FileHistoryDao.java
/** * Saves a copy of this project's {@literal config.xml} into {@literal timestampedDir}. * * @param currentConfig// w w w .ja va 2s . co m * which we want to copy. * @param timestampedDir * the directory where to save the copy. * @throws FileNotFoundException * if initiating the file holding the copy fails. * @throws IOException * if writing the file holding the copy fails. */ static void copyConfigFile(final File currentConfig, final File timestampedDir) throws FileNotFoundException, IOException { final BufferedOutputStream configCopy = new BufferedOutputStream( new FileOutputStream(new File(timestampedDir, currentConfig.getName()))); try { final FileInputStream configOriginal = new FileInputStream(currentConfig); try { // in is buffered by copyStream. Util.copyStream(configOriginal, configCopy); } finally { configOriginal.close(); } } finally { configCopy.close(); } }
From source file:com.kalai.controller.FileUploadController.java
@RequestMapping(value = "uploadMultipleFiles", method = RequestMethod.POST) public @ResponseBody String handleFileUpload(@RequestParam("files") MultipartFile files[]) { try {/*from w w w.j a v a2 s .c o m*/ String filepath = "D:/spring_file_upload/"; StringBuffer result = new StringBuffer(); byte[] bytes = null; result.append("Uploading of files(s)"); for (int i = 0; i < files.length; i++) { if (!files[i].isEmpty()) { bytes = files[i].getBytes(); BufferedOutputStream stream = new BufferedOutputStream( new FileOutputStream(new File(filepath + files[i].getOriginalFilename()))); stream.write(bytes); stream.close(); result.append(files[i].getOriginalFilename() + "OK."); } else { result.append(files[i].getOriginalFilename() + "Failed."); } } return result.toString(); } catch (Exception e) { return e.getMessage(); } }
From source file:com.foreignreader.util.WordNetExtractor.java
@Override protected Void doInBackground(InputStream... streams) { assert streams.length == 1; try {//from ww w. j a v a 2s. c o m BufferedInputStream in = new BufferedInputStream(streams[0]); GzipCompressorInputStream gzIn = new GzipCompressorInputStream(in); TarArchiveInputStream tar = new TarArchiveInputStream(gzIn); TarArchiveEntry tarEntry; File dest = LongTranslationHelper.getWordNetDict().getParentFile(); while ((tarEntry = tar.getNextTarEntry()) != null) { File destPath = new File(dest, tarEntry.getName()); if (tarEntry.isDirectory()) { destPath.mkdirs(); } else { destPath.createNewFile(); byte[] btoRead = new byte[1024 * 10]; BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(destPath)); int len = 0; while ((len = tar.read(btoRead)) != -1) { bout.write(btoRead, 0, len); } bout.close(); } } tar.close(); } catch (Exception e) { e.printStackTrace(); } return null; }
From source file:hu.bme.iit.quiz.service.QuizServiceImpl.java
@Override public String validateAndCreateQuizForUserFromFile(MultipartFile file, String quizName, String loginName) { String originalFileName = file.getOriginalFilename(); if (!file.isEmpty()) { try {/* www.j a v a 2s . c o m*/ byte[] bytes = file.getBytes(); //Only XML files, no mime check, basic check if (!file.getOriginalFilename().toLowerCase().endsWith(".xml")) { return "You failed to upload " + originalFileName + " because it's not an XML file."; } // 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(); } SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd-HH-mm-ss"); Date now = new Date(); String innerFileLocation = dateFormat.format(now) + "_" + originalFileName; // Create the file on server File serverFile = new File(dir.getAbsolutePath() + File.separator + innerFileLocation); BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(serverFile)); stream.write(bytes); stream.close(); //Persist it in db Quiz quiz = new Quiz(); quiz.setName(quizName); quiz.setBlobdata(bytes); quiz.setLocation(innerFileLocation); quiz.setUploaded(new Date()); quiz.setInnerkey(UUID.randomUUID().toString()); quiz.setOwner(userDAO.getUser(loginName)); if (getQuizDataFromXML(quiz) != null) { quizDAO.addQuiz(quiz); } else { return "Invalid XML file!"; } logger.info("XML file uploaded: " + originalFileName); return "You successfully uploaded '" + originalFileName + "'"; } catch (Exception e) { logger.error(e); e.printStackTrace(); return "You failed to upload " + originalFileName + " => " + e.getMessage(); } } else { return "You failed to upload " + originalFileName + " because the file was empty."; } }
From source file:com.groupon.odo.controllers.BackupController.java
/** * Restore backup data//from w w w. ja v a 2 s.com * * @param fileData - json file with restore data * @return * @throws Exception */ @RequestMapping(value = "/api/backup", method = RequestMethod.POST) public @ResponseBody Backup processBackup(@RequestParam("fileData") MultipartFile fileData) throws Exception { // Method taken from: http://spring.io/guides/gs/uploading-files/ if (!fileData.isEmpty()) { try { byte[] bytes = fileData.getBytes(); BufferedOutputStream stream = new BufferedOutputStream( new FileOutputStream(new File("backup-uploaded.json"))); stream.write(bytes); stream.close(); } catch (Exception e) { } } File f = new File("backup-uploaded.json"); BackupService.getInstance().restoreBackupData(new FileInputStream(f)); return BackupService.getInstance().getBackupData(); }
From source file:com.amalto.core.servlet.LogViewerServlet.java
private void writeLogFileChunk(long position, int maxLines, HttpServletResponse response) throws IOException { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream); FileChunkLoader loader = new FileChunkLoader(file); FileChunkInfo chunkInfo = loader.loadChunkTo(bufferedOutputStream, position, maxLines); bufferedOutputStream.close(); response.setContentType("text/plain;charset=" + charset); //$NON-NLS-1$ response.setCharacterEncoding(charset); response.setHeader("X-Log-Position", String.valueOf(chunkInfo.nextPosition)); //$NON-NLS-1$ response.setHeader("X-Log-Lines", String.valueOf(chunkInfo.lines)); //$NON-NLS-1$ OutputStream responseOutputStream = response.getOutputStream(); outputStream.writeTo(responseOutputStream); responseOutputStream.close();/*from w w w . j a v a 2 s . c o m*/ }
From source file:de.uka.ilkd.key.dl.gui.initialdialog.gui.ToolInstaller.java
/** * @param tmp/* w w w .j a v a 2 s . c om*/ * @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(); }