List of usage examples for org.apache.commons.vfs2 FileObject getContent
FileContent getContent() throws FileSystemException;
From source file:org.fuin.vfs2.filter.AgeFileFilter.java
/** * Tests if the specified <code>File</code> is newer than the specified time * reference./* ww w. j a v a 2 s .co m*/ * * @param fileObject * the <code>File</code> of which the modification date must be * compared, must not be {@code null} * @param timeMillis * the time reference measured in milliseconds since the epoch * (00:00:00 GMT, January 1, 1970) * @return true if the <code>File</code> exists and has been modified after * the given time reference. * @throws IllegalArgumentException * if the file is {@code null} */ private static boolean isFileNewer(final FileObject fileObject, final long timeMillis) { if (fileObject == null) { throw new IllegalArgumentException("No specified file"); } try { if (!fileObject.exists()) { return false; } final FileContent content = fileObject.getContent(); try { final long lastModified = content.getLastModifiedTime(); return lastModified > timeMillis; } finally { content.close(); } } catch (final FileSystemException ex) { throw new RuntimeException(ex); } }
From source file:org.fuin.vfs2.filter.AgeFileFilter.java
/** * Constructs a new age file filter for files on any one side of a certain * File (whose last modification time will be used as reference). * /*from w w w.ja v a 2 s.c o m*/ * @param cutoffReference * the file whose last modification time is usesd as the * threshold age of the files * @param acceptOlder * if true, older files (at or before the cutoff) are accepted, * else newer ones (after the cutoff). * * @throws FileSystemException * Error reading the last modification time from the reference * file object. */ public AgeFileFilter(final FileObject cutoffReference, final boolean acceptOlder) throws FileSystemException { this(cutoffReference.getContent().getLastModifiedTime(), acceptOlder); }
From source file:org.fuin.vfs2.filter.EmptyFileFilter.java
/** * Checks to see if the file is empty. A non-existing file is also * considered empty./*from w ww . j a v a 2 s .c o m*/ * * @param fileInfo * the file or directory to check * * @return {@code true} if the file or directory is <i>empty</i>, otherwise * {@code false}. */ @Override public boolean accept(final FileSelectInfo fileInfo) { final FileObject file = fileInfo.getFile(); try { if (!file.exists()) { return true; } if (file.getType() == FileType.FOLDER) { final FileObject[] files = file.getChildren(); return files == null || files.length == 0; } final FileContent content = file.getContent(); try { return content.getSize() == 0; } finally { content.close(); } } catch (final FileSystemException ex) { throw new RuntimeException(ex); } }
From source file:org.fuin.vfs2.filter.SizeFileFilter.java
/** * Checks to see if the size of the file is favorable. * <p>//from ww w .j av a2s.c o m * If size equals threshold and smaller files are required, file <b>IS * NOT</b> selected. If size equals threshold and larger files are required, * file <b>IS</b> selected. * <p> * Non-existing files return always false (will never be accepted). * * @param fileInfo * the File to check * * @return true if the filename matches */ @Override public boolean accept(final FileSelectInfo fileInfo) { try { final FileObject file = fileInfo.getFile(); if (!file.exists()) { return false; } final FileContent content = file.getContent(); try { final long length = content.getSize(); final boolean smaller = length < size; return acceptLarger ? !smaller : smaller; } finally { content.close(); } } catch (final FileSystemException ex) { throw new RuntimeException(ex); } }
From source file:org.geoserver.backuprestore.utils.BackupUtils.java
/** * Compress {@code sourceFolder} to the archive file {@code archiveFile}; both shall previously exist. * //from ww w . j a va 2s . com * @param sourceFolder * @param archiveFile * @throws IOException */ public static void compressTo(Resource sourceFolder, Resource archiveFile) throws IOException { // See https://commons.apache.org/proper/commons-vfs/filesystems.html // for the supported filesystems FileSystemManager manager = VFS.getManager(); FileObject sourceDir = manager .createVirtualFileSystem(manager.resolveFile(sourceFolder.dir().getAbsolutePath())); try { if ("zip".equalsIgnoreCase(FileUtils.getExtension(archiveFile.path()))) { // apache VFS does not support ZIP as writable FileSystem OutputStream fos = archiveFile.out(); // Create access to zip. ZipOutputStream zos = new ZipOutputStream(fos); // add entry/-ies. for (FileObject sourceFile : sourceDir.getChildren()) { writeEntry(zos, sourceFile, null); } // Close streams zos.flush(); zos.close(); fos.close(); } else { // Create access to archive. FileObject zipFile = manager.resolveFile(resolveArchiveURI(archiveFile)); zipFile.createFile(); ZipOutputStream zos = new ZipOutputStream(zipFile.getContent().getOutputStream()); // add entry/-ies. for (FileObject sourceFile : sourceDir.getChildren()) { writeEntry(zos, sourceFile, null); } // Close streams zos.flush(); zos.close(); zipFile.close(); manager.closeFileSystem(zipFile.getFileSystem()); } } finally { manager.closeFileSystem(sourceDir.getFileSystem()); } }
From source file:org.geoserver.backuprestore.utils.BackupUtils.java
/** * @param zos/*from ww w. ja v a 2 s .c o m*/ * @param sourceFile * @throws FileSystemException * @throws IOException */ private static void writeEntry(ZipOutputStream zos, FileObject sourceFile, String baseDir) throws FileSystemException, IOException { if (sourceFile.getType() == FileType.FOLDER) { // add entry/-ies. for (FileObject file : sourceFile.getChildren()) { writeEntry(zos, file, Paths.path(baseDir, sourceFile.getName().getBaseName())); } } else { String fileName = (baseDir != null ? Paths.path(baseDir, sourceFile.getName().getBaseName()) : sourceFile.getName().getBaseName()); ZipEntry zipEntry = new ZipEntry(fileName); InputStream is = sourceFile.getContent().getInputStream(); // Write to zip. byte[] buf = new byte[1024]; zos.putNextEntry(zipEntry); for (int readNum; (readNum = is.read(buf)) != -1;) { zos.write(buf, 0, readNum); } zos.closeEntry(); is.close(); } }
From source file:org.helios.ember.sftp.FileObjectWrapper.java
/** * Creates a new FileObjectWrapper//from w w w . j a v a2s . co m * @param fo The file object to wrap */ public FileObjectWrapper(FileObject fo) { this.fo = fo; try { this.content = fo.getContent(); } catch (FileSystemException e) { throw new RuntimeException(e); } }
From source file:org.horiam.ResourceManager.model.TestObjectToXmlToObject.java
@Test public void aTest() throws JAXBException, IOException { System.out.println("\nTest marshall User...\n"); JAXBContext context = JAXBContext.newInstance(User.class); Marshaller marshaller = context.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); for (User user : users) { FileObject file = fsManager.resolveFile("ram://" + user.getId() + ".xml"); OutputStream os = file.getContent().getOutputStream(); marshaller.marshal(user, System.out); marshaller.marshal(user, os);//from www .j ava2s . c om xmls.put(user.getId(), file); os.close(); } }
From source file:org.horiam.ResourceManager.model.TestObjectToXmlToObject.java
@Test public void bTest() throws JAXBException, IOException { System.out.println("\nTest marshall Resource...\n"); JAXBContext context = JAXBContext.newInstance(Resource.class); Marshaller marshaller = context.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); for (Resource resource : resources) { FileObject file = fsManager.resolveFile("ram://" + resource.getId() + ".xml"); OutputStream os = file.getContent().getOutputStream(); marshaller.marshal(resource, System.out); marshaller.marshal(resource, os); xmls.put(resource.getId(), file); os.close();//from w w w .ja va2s . c om } }
From source file:org.horiam.ResourceManager.model.TestObjectToXmlToObject.java
@Test public void cTest() throws JAXBException, IOException { System.out.println("\nTest marshall Task...\n"); JAXBContext context = JAXBContext.newInstance(Task.class); Marshaller marshaller = context.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); for (Task task : tasks) { FileObject file = fsManager.resolveFile("ram://" + task.getId() + ".xml"); OutputStream os = file.getContent().getOutputStream(); marshaller.marshal(task, System.out); marshaller.marshal(task, os);//from w w w . jav a2s . co m xmls.put(task.getId(), file); os.close(); } }