List of usage examples for org.apache.commons.vfs2 FileObject exists
boolean exists() throws FileSystemException;
From source file:org.renjin.primitives.files.Files.java
private static int checkAccess(FileObject file, int mode) throws FileSystemException { boolean ok = true; if ((mode & CHECK_ACCESS_EXISTENCE) != 0 && !file.exists()) { ok = false;/*from ww w .j a v a 2 s . c om*/ } if ((mode & CHECK_ACCESS_READ) != 0 && !file.isReadable()) { ok = false; } if ((mode & CHECK_ACCESS_WRITE) != 0 & !file.isWriteable()) { ok = false; } //case CHECK_ACCESS_EXECUTE: // return -1; // don't know if this is possible to check with VFS // } return ok ? 0 : -1; }
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// w w w . j a v a 2s . com * @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
@Invisible @Internal// w ww . ja v a2 s . c o m public static String setwd(@Current Context context, String workingDirectoryName) throws FileSystemException { FileObject newWorkingDirectory = context.resolveFile(workingDirectoryName); if (!newWorkingDirectory.exists() || newWorkingDirectory.getType() != FileType.FOLDER) { throw new EvalException("cannot change working directory"); } String previous = context.getSession().getWorkingDirectory().getName().getURI(); context.getSession().setWorkingDirectory(newWorkingDirectory); return previous; }
From source file:org.renjin.primitives.files.Files.java
private static void delete(FileObject file, boolean recursive) throws FileSystemException { if (file.exists()) { if (file.getType() == FileType.FILE) { file.delete();//from ww w. j a v a2 s. c o m } else if (file.getType() == FileType.FOLDER) { if (file.getChildren().length == 0) { file.delete(); } else if (recursive) { file.delete(); } } } }
From source file:org.renjin.primitives.files.Files.java
/** * Helper function to extract a zip entry to the given folder. *///from w w w. j ava 2 s. co 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
/** * 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. */// w w w . ja va2 s. com @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.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 ww .jav a2 s . 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./*from ww w . j a va 2s . c o m*/ * * @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 ww w . j a v a2s. com*/ * * @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; }
From source file:org.wso2.carbon.connector.FileArchives.java
/** * @param messageContext The message context that is generated for processing the file * @param source The file to be archived * @param destination Destination of the archived file * @return return status/*from ww w .j ava 2 s .co m*/ * @throws SynapseException */ private boolean fileCompress(MessageContext messageContext, String source, String destination) { boolean resultStatus = false; StandardFileSystemManager manager; FileSystemOptions opts = FileConnectorUtils.init(messageContext); try { manager = FileConnectorUtils.getManager(); FileObject fileObj = manager.resolveFile(source, opts); FileObject destObj = manager.resolveFile(destination, opts); if (fileObj.exists()) { if (fileObj.getType() == FileType.FOLDER) { List<FileObject> fileList = new ArrayList<FileObject>(); getAllFiles(fileObj, fileList, messageContext); writeZipFiles(fileObj, destObj, fileList, messageContext); } 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 (Exception e) { log.error("Unable to compress a file." + e.getMessage()); } finally { try { if (outputStream != null) { outputStream.close(); } } catch (IOException e) { log.error("Error while closing ZipOutputStream: " + e.getMessage(), e); } try { if (fileIn != null) { fileIn.close(); } } catch (IOException e) { log.error("Error while closing InputStream: " + e.getMessage(), e); } manager.close(); } } resultStatus = true; if (log.isDebugEnabled()) { log.debug("File archiving completed." + destination); } } else { log.error("The File location does not exist."); resultStatus = false; } } catch (IOException e) { handleException("Unable to process the zip file", e, messageContext); } return resultStatus; }