/** * Copy input stream to output stream and close them both * * @param is input stream * @param os output stream * @throws IOException for any error */ public static void copyStreamAndClose(InputStream is, OutputStream os) throws IOException { copyStreamAndClose(is, os, DEFAULT_BUFFER_SIZE); }
/** * Write the content from the given {@link InputStream} to the given virtual file, replacing its current contents (if any) or creating a new file if * one does not exist. * * @param virtualFile the virtual file to write * @param is the input stream * @throws IOException if an error occurs */ public static void writeFile(VirtualFile virtualFile, InputStream is) throws IOException { final File file = virtualFile.getPhysicalFile(); file.getParentFile().mkdirs(); final FileOutputStream fos = new FileOutputStream(file); copyStreamAndClose(is, fos); }
/** * Do copy. * * @param fileToAdd file to add * @param newFile new file location * @return new timestamp * @throws IOException for any error */ protected static long copy(VirtualFile fileToAdd, File newFile) throws IOException { FileOutputStream out = new FileOutputStream(newFile); VFSUtils.copyStreamAndClose(fileToAdd.openStream(), out); return newFile.lastModified(); }
/** * Recursively copy a file or directory from one location to another. * * @param original the original file or directory * @param destDir the destination directory * @throws IOException if an I/O error occurs before the copy is complete */ public static void recursiveCopy(File original, File destDir) throws IOException { final String name = original.getName(); final File destFile = new File(destDir, name); if (original.isDirectory()) { destFile.mkdir(); for (File file : original.listFiles()) { recursiveCopy(file, destFile); } } else { final OutputStream os = new FileOutputStream(destFile); try { final InputStream is = new FileInputStream(original); copyStreamAndClose(is, os); } finally { // in case the input stream open fails safeClose(os); } } }
/** * {@inheritDoc} */ public File getFile(VirtualFile mountPoint, VirtualFile target) throws IOException { final ZipNode zipNode = getExistingZipNode(mountPoint, target); // check if we have cached one already File cachedFile = zipNode.cachedFile; if (cachedFile != null) { return cachedFile; } synchronized (zipNode) { // double-check cachedFile = zipNode.cachedFile; if (cachedFile != null) { return cachedFile; } // nope, create a cached temp final JarEntry zipEntry = zipNode.entry; String name = target.getPathNameRelativeTo(mountPoint); cachedFile = buildFile(contentsDir, name); if (zipEntry == null) { cachedFile.mkdir(); } else { VFSUtils.copyStreamAndClose(zipFile.getInputStream(zipEntry), new BufferedOutputStream(new FileOutputStream(cachedFile))); } zipNode.cachedFile = cachedFile; return cachedFile; } }
/** * Recursively copy a file or directory from one location to another. * * @param original the original file or directory * @param destDir the destination directory * @throws IOException if an I/O error occurs before the copy is complete */ public static void recursiveCopy(File original, VirtualFile destDir) throws IOException { final String name = original.getName(); final File destFile = destDir.getChild(name).getPhysicalFile(); if (original.isDirectory()) { destFile.mkdir(); for (File file : original.listFiles()) { recursiveCopy(file, destFile); } } else { final OutputStream os = new FileOutputStream(destFile); try { final InputStream is = new FileInputStream(original); copyStreamAndClose(is, os); } finally { // in case the input stream open fails safeClose(os); } } }
/** * Recursively copy a file or directory from one location to another. * * @param original the original virtual file or directory * @param destDir the destination directory * @throws IOException if an I/O error occurs before the copy is complete */ public static void recursiveCopy(VirtualFile original, File destDir) throws IOException { final String name = original.getName(); final File destFile = new File(destDir, name); if (original.isDirectory()) { destFile.mkdir(); for (VirtualFile file : original.getChildren()) { recursiveCopy(file, destFile); } } else { final OutputStream os = new FileOutputStream(destFile); try { final InputStream is = original.openStream(); copyStreamAndClose(is, os); } finally { // in case the input stream open fails safeClose(os); } } }
/** * Recursively copy a file or directory from one location to another. * * @param original the original virtual file or directory * @param destDir the destination virtual directory * @throws IOException if an I/O error occurs before the copy is complete */ public static void recursiveCopy(VirtualFile original, VirtualFile destDir) throws IOException { final String name = original.getName(); final File destFile = destDir.getChild(name).getPhysicalFile(); if (original.isDirectory()) { destFile.mkdir(); for (VirtualFile file : original.getChildren()) { recursiveCopy(file, destFile); } } else { final OutputStream os = new FileOutputStream(destFile); try { final InputStream is = original.openStream(); copyStreamAndClose(is, os); } finally { // in case the input stream open fails safeClose(os); } } }