Example usage for org.apache.commons.vfs2 FileObject close

List of usage examples for org.apache.commons.vfs2 FileObject close

Introduction

In this page you can find the example usage for org.apache.commons.vfs2 FileObject close.

Prototype

@Override
void close() throws FileSystemException;

Source Link

Document

Closes this file, and its content.

Usage

From source file:ShowProperties.java

public static void main(String[] args) throws FileSystemException {
    if (args.length == 0) {
        System.err.println("Please pass the name of a file as parameter.");
        System.err.println("e.g. java org.apache.commons.vfs2.example.ShowProperties LICENSE.txt");
        return;/*from  www  .j a  va2s .  c o m*/
    }
    for (int i = 0; i < args.length; i++) {
        try {
            FileSystemManager mgr = VFS.getManager();
            System.out.println();
            System.out.println("Parsing: " + args[i]);
            FileObject file = mgr.resolveFile(args[i]);
            System.out.println("URL: " + file.getURL());
            System.out.println("getName(): " + file.getName());
            System.out.println("BaseName: " + file.getName().getBaseName());
            System.out.println("Extension: " + file.getName().getExtension());
            System.out.println("Path: " + file.getName().getPath());
            System.out.println("Scheme: " + file.getName().getScheme());
            System.out.println("URI: " + file.getName().getURI());
            System.out.println("Root URI: " + file.getName().getRootURI());
            System.out.println("Parent: " + file.getName().getParent());
            System.out.println("Type: " + file.getType());
            System.out.println("Exists: " + file.exists());
            System.out.println("Readable: " + file.isReadable());
            System.out.println("Writeable: " + file.isWriteable());
            System.out.println("Root path: " + file.getFileSystem().getRoot().getName().getPath());
            if (file.exists()) {
                if (file.getType().equals(FileType.FILE)) {
                    System.out.println("Size: " + file.getContent().getSize() + " bytes");
                } else if (file.getType().equals(FileType.FOLDER) && file.isReadable()) {
                    FileObject[] children = file.getChildren();
                    System.out.println("Directory with " + children.length + " files");
                    for (int iterChildren = 0; iterChildren < children.length; iterChildren++) {
                        System.out.println("#" + iterChildren + ": " + children[iterChildren].getName());
                        if (iterChildren > 5) {
                            break;
                        }
                    }
                }
                System.out.println("Last modified: "
                        + DateFormat.getInstance().format(new Date(file.getContent().getLastModifiedTime())));
            } else {
                System.out.println("The file does not exist");
            }
            file.close();
        } catch (FileSystemException ex) {
            ex.printStackTrace();
        }
    }
}

From source file:com.google.code.docbook4j.FileObjectUtils.java

public static final void closeFileObjectQuietly(FileObject fo) {
    if (fo != null) {
        try {/*www  . j a v a2s  .  c o  m*/
            fo.close();
        } catch (FileSystemException e) {
            log.error("Error closing file object: " + fo, e);
        }
    }

}

From source file:net.sf.jabb.util.vfs.VfsUtility.java

/**
 * Close the FileObject./*from www . ja v  a2 s .com*/
 * @param fo the FileObject to be closed. It can be null.
 */
static public void close(FileObject fo) {
    if (fo != null) {
        try {
            fo.close();
        } catch (FileSystemException e) {
            log.debug("Exception when closing FileObject: " + fo.getName(), e);
        }
    }
}

From source file:maspack.fileutil.ZipUtility.java

public static void unzip(URIx src, final File dest) throws IOException {
    dest.mkdirs();/*from w  ww .  j  a  v  a2 s. com*/

    final FileSystemManager fileSystemManager = VFS.getManager();
    final FileObject zipFileObject = fileSystemManager.resolveFile(src.toString());

    try {
        final FileObject fileSystem = fileSystemManager.createFileSystem(zipFileObject);
        try {
            fileSystemManager.toFileObject(dest).copyFrom(fileSystem, new AllFileSelector());
        } finally {
            fileSystem.close();
        }
    } finally {
        zipFileObject.close();
    }
}

From source file:com.sludev.commons.vfs2.provider.azure.AzTestUtils.java

public static void uploadFile(String accntName, String accntHost, String accntKey, String containerName,
        Path localFile, Path remotePath) throws FileSystemException {
    DefaultFileSystemManager currMan = new DefaultFileSystemManager();
    currMan.addProvider(AzConstants.AZSBSCHEME, new AzFileProvider());
    currMan.addProvider("file", new DefaultLocalFileProvider());
    currMan.init();/*  w ww.  j  ava2 s . c  o  m*/

    StaticUserAuthenticator auth = new StaticUserAuthenticator("", accntName, accntKey);
    FileSystemOptions opts = new FileSystemOptions();
    DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(opts, auth);

    String currUriStr = String.format("%s://%s/%s/%s", AzConstants.AZSBSCHEME, accntHost, containerName,
            remotePath);
    FileObject currFile = currMan.resolveFile(currUriStr, opts);
    FileObject currFile2 = currMan.resolveFile(String.format("file://%s", localFile));

    currFile.copyFrom(currFile2, Selectors.SELECT_SELF);

    currFile.close();
    currMan.close();
}

From source file:com.sludev.commons.vfs2.provider.s3.SS3TestUtils.java

public static void uploadFile(String accntName, String acctHost, String accntKey, String containerName,
        Path localFile, Path remotePath) throws FileSystemException {
    DefaultFileSystemManager currMan = new DefaultFileSystemManager();
    currMan.addProvider(SS3Constants.S3SCHEME, new SS3FileProvider());
    currMan.addProvider("file", new DefaultLocalFileProvider());
    currMan.init();/*  w ww .  j a  v a  2s. com*/

    StaticUserAuthenticator auth = new StaticUserAuthenticator("", accntName, accntKey);
    FileSystemOptions opts = new FileSystemOptions();
    DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(opts, auth);

    String currUriStr = String.format("%s://%s/%s/%s", SS3Constants.S3SCHEME, acctHost, containerName,
            remotePath);
    FileObject currFile = currMan.resolveFile(currUriStr, opts);
    FileObject currFile2 = currMan.resolveFile(String.format("file://%s", localFile));

    currFile.copyFrom(currFile2, Selectors.SELECT_SELF);

    currFile.close();
    currMan.close();
}

From source file:com.google.code.docbook4j.XSLTUtils.java

public static final String dumpCss(String baseDir, String location) {

    try {//from w w w  . j  a v a2s  .c o  m

        FileObject fo = FileObjectUtils.resolveFile(location, baseDir);

        StringBuffer sb = new StringBuffer();
        sb.append("<!--\n");
        sb.append(IOUtils.toString(fo.getContent().getInputStream())).append("\n");
        sb.append("-->\n");

        fo.close();

        return sb.toString();

    } catch (Exception e) {
        log.error("Error reading css file: " + location, e);
    }

    return "";

}

From source file:com.google.code.docbook4j.XSLTUtils.java

public static final String toBase64(String baseDir, String location) {

    try {//from   www .  j  av a2 s .co  m

        FileObject fo = FileObjectUtils.resolveFile(location, baseDir);

        byte[] data = IOUtils.toByteArray(fo.getContent().getInputStream());

        StringBuffer sb = new StringBuffer();
        sb.append("data:");
        sb.append(determineMimeType(location));
        sb.append(";base64,");
        sb.append(Base64.encode(data));

        fo.close();
        return sb.toString();

    } catch (Exception e) {
        log.error("Error reading image file: " + location, e);
    }

    return location;
}

From source file:com.ewcms.publication.deploy.provider.LocalDeployOperatorTest.java

@Test
public void testGetTargtRoot() throws Exception {

    DeployOperatorable operator = new LocalDeployOperator.Builder("e:/tmp").build();
    LocalDeployOperator localOperator = (LocalDeployOperator) operator;

    FileObject target = localOperator.getRootFileObject();
    Assert.notNull(target);/*ww w.j  av  a 2 s  . c  o m*/
    target.close();
}

From source file:com.ewcms.publication.deploy.provider.SftpDeployOperatorTest.java

@Test
public void testGetTargtRoot() throws Exception {
    String hostname = "127.0.0.1";
    String username = "wangwei";
    String password = "hhywangwei";
    String path = "/tmp";
    SftpDeployOperator operator = (SftpDeployOperator) new SftpDeployOperator.Builder(hostname, path)
            .setUsername(username).setPassword(password).build();

    FileObject target = operator.getRootFileObject();
    Assert.assertNotNull(target);//from   ww w  .ja v a2s . c  o  m
    target.close();
}