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

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

Introduction

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

Prototype

FileContent getContent() throws FileSystemException;

Source Link

Document

Returns this file's content.

Usage

From source file:org.apache.accumulo.start.classloader.vfs.providers.ReadOnlyHdfsFileProviderTest.java

@Test
public void testGetInputStream() throws Exception {
    FileObject fo = manager.resolveFile(TEST_DIR1);
    Assert.assertNotNull(fo);//from  ww w  . j  av  a 2 s  .c om
    Assert.assertFalse(fo.exists());

    // Create the test file
    FileObject file = createTestFile(hdfs);
    file.getContent().getInputStream().close();
}

From source file:org.apache.accumulo.start.classloader.vfs.providers.ReadOnlyHdfsFileProviderTest.java

@Test
public void testLastModificationTime() throws Exception {
    FileObject fo = manager.resolveFile(TEST_DIR1);
    Assert.assertNotNull(fo);//from  www. j  a v  a 2 s  . c  o m
    Assert.assertFalse(fo.exists());

    // Create the test file
    FileObject file = createTestFile(hdfs);
    Assert.assertFalse(-1 == file.getContent().getLastModifiedTime());
}

From source file:org.apache.accumulo.start.classloader.vfs.providers.ReadOnlyHdfsFileProviderTest.java

@Test
public void testGetAttributes() throws Exception {
    FileObject fo = manager.resolveFile(TEST_DIR1);
    Assert.assertNotNull(fo);/* ww  w . j a  v  a 2  s . com*/
    Assert.assertFalse(fo.exists());

    // Create the test file
    FileObject file = createTestFile(hdfs);
    Map<String, Object> attributes = file.getContent().getAttributes();
    Assert.assertTrue(attributes.containsKey(HdfsFileAttributes.BLOCK_SIZE.toString()));
    Assert.assertTrue(attributes.containsKey(HdfsFileAttributes.GROUP.toString()));
    Assert.assertTrue(attributes.containsKey(HdfsFileAttributes.LAST_ACCESS_TIME.toString()));
    Assert.assertTrue(attributes.containsKey(HdfsFileAttributes.LENGTH.toString()));
    Assert.assertTrue(attributes.containsKey(HdfsFileAttributes.MODIFICATION_TIME.toString()));
    Assert.assertTrue(attributes.containsKey(HdfsFileAttributes.OWNER.toString()));
    Assert.assertTrue(attributes.containsKey(HdfsFileAttributes.PERMISSIONS.toString()));
}

From source file:org.apache.accumulo.start.classloader.vfs.providers.ReadOnlyHdfsFileProviderTest.java

@Test(expected = FileSystemException.class)
public void testRandomAccessContent() throws Exception {
    FileObject fo = manager.resolveFile(TEST_DIR1);
    Assert.assertNotNull(fo);//from  w  w  w  . j a  v  a  2  s. co m
    Assert.assertFalse(fo.exists());

    // Create the test file
    FileObject file = createTestFile(hdfs);
    file.getContent().getRandomAccessContent(RandomAccessMode.READWRITE).close();
}

From source file:org.apache.accumulo.start.classloader.vfs.providers.ReadOnlyHdfsFileProviderTest.java

@Test
public void testRandomAccessContent2() throws Exception {
    FileObject fo = manager.resolveFile(TEST_DIR1);
    Assert.assertNotNull(fo);/*from  w w w . ja va 2 s . c  om*/
    Assert.assertFalse(fo.exists());

    // Create the test file
    FileObject file = createTestFile(hdfs);
    file.getContent().getRandomAccessContent(RandomAccessMode.READ).close();
}

From source file:org.apache.commons.vfs2.example.ChangeLastModificationTime.java

public static void main(final String[] args) throws Exception {
    if (args.length == 0) {
        System.err.println("Please pass the name of a file as parameter.");
        return;//from   www. jav a2  s . c  o m
    }

    final FileObject fo = VFS.getManager().resolveFile(args[0]);
    final long setTo = System.currentTimeMillis();
    System.err.println("set to: " + setTo);
    fo.getContent().setLastModifiedTime(setTo);
    System.err.println("after set: " + fo.getContent().getLastModifiedTime());
}

From source file:org.apache.commons.vfs2.example.Shell.java

/**
 * Does an 'ls' command./*from  w  ww  .j a v  a  2 s.c o m*/
 */
private void ls(final String[] cmd) throws FileSystemException {
    int pos = 1;
    final boolean recursive;
    if (cmd.length > pos && cmd[pos].equals("-R")) {
        recursive = true;
        pos++;
    } else {
        recursive = false;
    }

    final FileObject file;
    if (cmd.length > pos) {
        file = mgr.resolveFile(cwd, cmd[pos]);
    } else {
        file = cwd;
    }

    if (file.getType() == FileType.FOLDER) {
        // List the contents
        System.out.println("Contents of " + file.getName());
        listChildren(file, recursive, "");
    } else {
        // Stat the file
        System.out.println(file.getName());
        final FileContent content = file.getContent();
        System.out.println("Size: " + content.getSize() + " bytes.");
        final DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM);
        final String lastMod = dateFormat.format(new Date(content.getLastModifiedTime()));
        System.out.println("Last modified: " + lastMod);
    }
}

From source file:org.apache.commons.vfs2.example.Shell.java

/**
 * Does a 'touch' command./*from  w  w  w.j a  v  a2 s  .  co m*/
 */
private void touch(final String[] cmd) throws Exception {
    if (cmd.length < 2) {
        throw new Exception("USAGE: touch <path>");
    }
    final FileObject file = mgr.resolveFile(cwd, cmd[1]);
    if (!file.exists()) {
        file.createFile();
    }
    file.getContent().setLastModifiedTime(System.currentTimeMillis());
}

From source file:org.apache.commons.vfs2.example.ShowProperties.java

public static void main(final String[] args) {
    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 w ww .j  a  va 2s. c  om
    }
    for (final String arg : args) {
        try {
            final FileSystemManager mgr = VFS.getManager();
            System.out.println();
            System.out.println("Parsing: " + arg);
            final FileObject file = mgr.resolveFile(arg);
            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()) {
                    final 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 > SHOW_MAX) {
                            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 (final FileSystemException ex) {
            ex.printStackTrace();
        }
    }
}

From source file:org.apache.hadoop.gateway.topology.file.FileTopologyProvider.java

private static Topology loadTopology(FileObject file) throws IOException, SAXException, URISyntaxException {
    log.loadingTopologyFile(file.getName().getFriendlyURI());
    Digester digester = digesterLoader.newDigester();
    FileContent content = file.getContent();
    TopologyBuilder topologyBuilder = digester.parse(content.getInputStream());
    Topology topology = topologyBuilder.build();
    topology.setUri(file.getURL().toURI());
    topology.setName(FilenameUtils.removeExtension(file.getName().getBaseName()));
    topology.setTimestamp(content.getLastModifiedTime());
    return topology;
}