List of usage examples for org.apache.commons.vfs2 FileObject getContent
FileContent getContent() throws FileSystemException;
From source file:org.renjin.primitives.files.Files.java
/** * Utility function to extract information about files on the user's file systems. * * @param context current call Context/*from w w w . j a v a 2 s . co m*/ * @param paths the list of files for which to return information * @return list column-oriented table of file information * @throws FileSystemException */ @Internal("file.info") public static ListVector fileInfo(@Current Context context, StringVector paths) throws FileSystemException { DoubleArrayVector.Builder size = new DoubleArrayVector.Builder(); LogicalArrayVector.Builder isdir = new LogicalArrayVector.Builder(); IntArrayVector.Builder mode = (IntArrayVector.Builder) new IntArrayVector.Builder() .setAttribute(Symbols.CLASS, StringVector.valueOf("octmode")); DoubleArrayVector.Builder mtime = new DoubleArrayVector.Builder(); StringVector.Builder exe = new StringVector.Builder(); for (String path : paths) { if (StringVector.isNA(path)) { throw new EvalException("invalid filename argument"); } FileObject file = context.resolveFile(path); if (file.exists()) { if (file.getType() == FileType.FILE) { size.add((int) file.getContent().getSize()); } else { size.add(0); } isdir.add(file.getType() == FileType.FOLDER); mode.add(mode(file)); try { mtime.add(file.getContent().getLastModifiedTime()); } catch (Exception e) { mtime.add(0); } exe.add(file.getName().getBaseName().endsWith(".exe") ? "yes" : "no"); } else { size.addNA(); isdir.addNA(); mode.addNA(); mtime.addNA(); exe.addNA(); } } return ListVector.newNamedBuilder().add("size", size).add("isdir", isdir).add("mode", mode) .add("mtime", mtime).add("ctime", mtime).add("atime", mtime).add("exe", exe).build(); }
From source file:org.renjin.primitives.files.Files.java
/** * Helper function to extract a zip entry to the given folder. *///from w w w .ja v a 2 s.c o m private static void unzipExtract(ZipInputStream zin, ZipEntry entry, FileObject exdir, boolean junkpaths, boolean overwrite) throws IOException { if (junkpaths) { throw new EvalException("unzip(junpaths=false) not yet implemented"); } FileObject exfile = exdir.resolveFile(entry.getName()); if (exfile.exists() && !overwrite) { throw new EvalException("file to be extracted '%s' already exists", exfile.getName().getURI()); } OutputStream out = exfile.getContent().getOutputStream(); try { byte buffer[] = new byte[64 * 1024]; int bytesRead; while ((bytesRead = zin.read(buffer)) != -1) { out.write(buffer, 0, bytesRead); } } finally { out.close(); } }
From source file:org.renjin.primitives.files.Files.java
@Internal("file.create") @DataParallel// w w w . j av a 2s . c o m public static boolean fileCreate(@Current Context context, @Recycle String fileName, @Recycle(false) boolean showWarnings) throws IOException { try { FileObject file = context.resolveFile(fileName); // VFS will create the parent folder if it doesn't exist, // which the R method is not supposed to do if (!file.getParent().exists()) { throw new IOException("No such file or directory"); } file.getContent().getOutputStream().close(); return true; } catch (Exception e) { if (showWarnings) { Warning.invokeWarning(context, "cannot create file '%s', reason '%s'", fileName, e.getMessage()); } return false; } }
From source file:org.renjin.primitives.files.Files.java
/** * file.append attempts to append the files named by its second * argument to those named by its first. The R subscript recycling * rule is used to align names given in vectors of different lengths. *///from w ww. jav a 2 s.c om @Internal("file.append") @DataParallel public static boolean fileAppend(@Current Context context, String destFileName, String sourceFileName) { try { FileObject sourceFile = context.resolveFile(sourceFileName); if (!sourceFile.exists()) { return false; } FileObject destFile = context.resolveFile(destFileName); OutputStream out = destFile.getContent().getOutputStream(true); try { InputStream in = sourceFile.getContent().getInputStream(); try { ByteStreams.copy(in, out); } finally { try { in.close(); } catch (Exception ignored) { } } } finally { try { out.close(); } catch (Exception ignored) { } } return true; } catch (Exception e) { return false; } }
From source file:org.sitemap4j.dom4j.Dom4jSiteMapIndexWriterTest.java
@Test public void test() throws Exception { // Given/*from w ww .j a v a 2 s. co m*/ final MutableSiteMapIndex siteMapIndex = ArrayListMutableSiteMapIndex.create(); siteMapIndex.add(SiteMapLocation.create(getRandomUrl())); siteMapIndex.add(SiteMapLocation.create(getRandomUrl(), new DateTime(DateTimeZone.UTC))); final FileObject siteMapIndexFile = VFS.getManager().resolveFile("ram:///sitemap-index.xml"); siteMapIndexFile.createFile(); final OutputStream outputStream = siteMapIndexFile.getContent().getOutputStream(); // When final SiteMapIndexWriter writer = Dom4jSiteMapIndexWriter.forSiteMapIndex(siteMapIndex); writer.write(outputStream); // Then final SiteMapIndexReader siteMapIndexReader = Dom4jSiteMapIndexReader.create(); assertEquals(siteMapIndex, siteMapIndexReader .read(RawSiteMap.fromInputStream(siteMapIndexFile.getContent().getInputStream()))); }
From source file:org.sitemap4j.dom4j.Dom4jSiteMapWriterTest.java
@Test public void test() throws Exception { // Given/*from w ww. j av a 2 s.c o m*/ final FileObject siteMapFile = VFS.getManager().resolveFile("ram:///sitemap.xml"); siteMapFile.createFile(); final OutputStream outputStream = siteMapFile.getContent().getOutputStream(); final MutableSiteMap siteMap = ArrayListMutableSiteMap.create(); siteMap.add(getSiteMapUrl().build()); siteMap.add(getSiteMapUrl().build()); // When final SiteMapWriter writer = Dom4jSiteMapWriter.forSiteMap(siteMap); writer.write(outputStream); // Then final SiteMapReader siteMapReader = Dom4jSiteMapReader.create(); assertEquals(siteMap, siteMapReader.read(RawSiteMap.fromInputStream(siteMapFile.getContent().getInputStream()))); }
From source file:org.sonar.plugins.ndepend.ndproj.NdprojCreator.java
/** * @return an InputStream for the loaded rules file * @throws FileSystemException/* ww w. j a va 2 s .c o m*/ */ private InputStream getRulesInputStream() throws FileSystemException { String rulesUrl = settings.getString(NdependConfig.NDEPEND_RULES_URL_KEY); InputStream in; if (rulesUrl.trim().isEmpty()) { LOG.info("No rules configured. Using default rules"); in = getClass().getResourceAsStream("rules.xml"); } else { LOG.info("Loading rules from {}", rulesUrl); FileSystemManager vfs = VFS.getManager(); FileObject rules = vfs.resolveFile(rulesUrl); in = rules.getContent().getInputStream(); } return in; }
From source file:org.wso2.carbon.connector.FileAppend.java
/** * @param destination Location if the file * @param content Content that is going to be added * @param encoding Encoding type//from w w w.jav a 2s .c o m * @param messageContext The message context that is generated for processing the file * @return true/false */ private boolean appendFile(String destination, String content, String encoding, MessageContext messageContext) { OutputStream out = null; boolean resultStatus = false; FileObject fileObj = null; StandardFileSystemManager manager = null; try { manager = FileConnectorUtils.getManager(); fileObj = manager.resolveFile(destination, FileConnectorUtils.init(messageContext)); if (!fileObj.exists()) { fileObj.createFile(); } out = fileObj.getContent().getOutputStream(true); if (StringUtils.isEmpty(encoding)) { IOUtils.write(content, out, DEFAULT_ENCODING); } else { IOUtils.write(content, out, encoding); } resultStatus = true; if (log.isDebugEnabled()) { log.debug("File appending completed. " + destination); } } catch (IOException e) { handleException("Error while appending a file.", e, messageContext); } finally { try { if (fileObj != null) { //close the file object fileObj.close(); } } catch (FileSystemException e) { log.error("Error while closing FileObject: " + e.getMessage(), e); } try { if (out != null) { //close the output stream out.close(); } } catch (IOException e) { log.error("Error while closing OutputStream: " + e.getMessage(), e); } if (manager != null) { //close the StandardFileSystemManager manager.close(); } } return resultStatus; }
From source file:org.wso2.carbon.connector.FileAppendConnector.java
/** * Add the content into file./*www. j a va2s . com*/ * * @param messageContext The message context that is generated for processing the file. * @return true, if the content is successfully appended. * @throws FileSystemException On error parsing the file name, determining if the file exists and creating the * file. */ private boolean appendFile(MessageContext messageContext) throws FileSystemException { String destination = (String) ConnectorUtils.lookupTemplateParamater(messageContext, FileConstants.NEW_FILE_LOCATION); String content = (String) ConnectorUtils.lookupTemplateParamater(messageContext, FileConstants.CONTENT); String encoding = (String) ConnectorUtils.lookupTemplateParamater(messageContext, FileConstants.ENCODING); if (StringUtils.isEmpty(encoding)) { encoding = FileConstants.DEFAULT_ENCODING; } FileSystemOptions opts = FileConnectorUtils.init(messageContext); OutputStream out = null; FileObject fileObj = null; StandardFileSystemManager manager = FileConnectorUtils.getManager(); try { fileObj = manager.resolveFile(destination, opts); if (!fileObj.exists()) { fileObj.createFile(); } // True, if the content should be appended. out = fileObj.getContent().getOutputStream(true); IOUtils.write(content, out, encoding); if (log.isDebugEnabled()) { log.debug("File appending completed. " + destination); } } catch (IOException e) { throw new SynapseException("Error while appending content", e); } finally { try { if (fileObj != null) { // close the file object fileObj.close(); } } catch (FileSystemException e) { log.error("Error while closing FileObject", e); } try { if (out != null) { // close the output stream out.close(); } } catch (IOException e) { log.error("Error while closing OutputStream", e); } // close the StandardFileSystemManager manager.close(); } return true; }
From source file:org.wso2.carbon.connector.FileArchiveConnector.java
/** * Archive a file/folder./*from w ww .j av a 2s.c o m*/ * * @param messageContext The message context that is generated for processing the file. * @return return true, if the file/folder is successfully archived, false, if not. * @throws FileSystemException On error parsing the file name, determining if the file exists and getting file type. */ private boolean fileCompress(MessageContext messageContext) throws FileSystemException { String source = (String) ConnectorUtils.lookupTemplateParamater(messageContext, FileConstants.FILE_LOCATION); String destination = (String) ConnectorUtils.lookupTemplateParamater(messageContext, FileConstants.NEW_FILE_LOCATION); StandardFileSystemManager manager = FileConnectorUtils.getManager(); FileSystemOptions opts = FileConnectorUtils.init(messageContext); FileObject fileObj = manager.resolveFile(source, opts); FileObject destObj = manager.resolveFile(destination, opts); if (!fileObj.exists()) { log.error("The File location does not exist."); return false; } if (FileType.FOLDER.equals(fileObj.getType())) { List<FileObject> fileList = new ArrayList<>(); addAllFilesToList(fileObj, fileList); writeZipFiles(fileObj, destObj, fileList); } else { ZipOutputStream outputStream = null; InputStream fileIn = null; try { outputStream = new ZipOutputStream(destObj.getContent().getOutputStream()); fileIn = fileObj.getContent().getInputStream(); ZipEntry zipEntry = new ZipEntry(fileObj.getName().getBaseName()); outputStream.putNextEntry(zipEntry); int length; while ((length = fileIn.read(bytes)) != -1) { outputStream.write(bytes, 0, length); } } catch (IOException e) { throw new SynapseException("Error while writing an array of bytes to the ZipOutputStream", e); } finally { try { // close the file object fileObj.close(); } catch (FileSystemException e) { log.error("Error while closing the source FileObject", e); } try { // close the file object destObj.close(); } catch (FileSystemException e) { log.error("Error while closing the destination FileObject", e); } try { if (outputStream != null) { outputStream.close(); } } catch (IOException e) { log.error("Error while closing ZipOutputStream", e); } try { if (fileIn != null) { fileIn.close(); } } catch (IOException e) { log.error("Error while closing InputStream:", e); } // close the StandardFileSystemManager manager.close(); } } if (log.isDebugEnabled()) { log.debug("File archiving completed." + destination); } return true; }