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:sf.net.experimaestro.manager.js.JavaScriptChecker.java

static String getFileContent(FileObject file) throws IOException {
    InputStreamReader reader = new InputStreamReader(file.getContent().getInputStream());
    char[] cbuf = new char[8192];
    int read;/*  w  ww. j a va2s .com*/
    StringBuilder builder = new StringBuilder();
    while ((read = reader.read(cbuf, 0, cbuf.length)) > 0)
        builder.append(cbuf, 0, read);
    return builder.toString();
}

From source file:sf.net.experimaestro.scheduler.CommandTest.java

@Test(description = "CommandOutput handling")
public void commandOutputTest() throws IOException, InterruptedException, LaunchException {
    try (TemporaryDirectory directory = new TemporaryDirectory("commandtest", "test")) {
        directory.setAutomaticDelete(false);

        final File dataFile = new File(directory.getFile(), "data");
        try (FileWriter out = new FileWriter(dataFile)) {
            out.write("hello\n");
            out.write("world\n");
        }//from   ww w . ja  v  a2  s  .c o  m
        final Commands commands = new Commands();
        final Command command = new Command();

        final LocalhostConnector connector = LocalhostConnector.getInstance();

        final String dataPath = connector.resolve(dataFile.getAbsolutePath());

        final Command subCommand = new Command();
        subCommand.add("/bin/cat", new Command.Path(dataPath));
        final Command.CommandOutput output = subCommand.output();

        command.add("/usr/bin/paste", output, output);
        commands.add(command);

        final String locatorPath = new File(directory.getFile(), "task").getAbsolutePath();
        ResourceLocator locator = new ResourceLocator(connector, locatorPath);
        final FileObject runFile = locator.resolve(connector, Resource.RUN_EXTENSION);

        XPMScriptProcessBuilder builder = connector.scriptProcessBuilder(connector, runFile);
        builder.directory(locator.getFile().getParent());

        // Add commands
        builder.commands(commands);

        builder.redirectInput(AbstractCommandBuilder.Redirect.INHERIT);
        final FileObject out = connector.resolveFile(new File(directory.getFile(), "output").getAbsolutePath());
        builder.redirectOutput(AbstractCommandBuilder.Redirect.to(out));
        builder.redirectError(AbstractCommandBuilder.Redirect.INHERIT);

        final XPMProcess process = builder.start();
        int code = process.waitFor();
        Assert.assertEquals(code, 0, "Exit code is not 0");

        // Checks the output

        try (BufferedReader in = new BufferedReader(new InputStreamReader(out.getContent().getInputStream()))) {
            Assert.assertEquals(in.readLine(), "hello\thello");
            Assert.assertEquals(in.readLine(), "world\tworld");
        }

    }
}

From source file:tain.kr.test.vfs.v01.ChangeLastModificationTime.java

private static void test01(String[] args) throws Exception {

    if (flag)//w w w.j a v a 2s .co  m
        new ChangeLastModificationTime();

    if (flag) {

        if (args.length == 0) {
            System.err.println("Please pass the name of a file as parameter.");
            return;
        }

        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:tain.kr.test.vfs.v01.Shell.java

/**
 * Does an 'ls' command.//from ww  w .  j  a v a  2s  . com
 */
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:tain.kr.test.vfs.v01.Shell.java

/**
 * Does a 'touch' command.//w  w  w  .  ja  v a2  s . com
 */
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:tain.kr.test.vfs.v01.ShowProperties.java

private static void test01(String[] args) throws Exception {

    if (flag)// ww w.  j  a  va2  s.  c o  m
        new ShowProperties();

    if (flag) {

        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;
        }

        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:tain.kr.test.vfs.v01.TestVfs2FilehandleService.java

@Test
public void testAccessFile() throws Exception {

    FileSystemManager manager = VFS.getManager();

    FileObject baseDir = manager.resolveFile(this.absoluteFilePath);
    FileObject file = manager.resolveFile(baseDir, "testfolder/file1.txt");

    //   //w ww . j a v  a  2  s  . c  om
    file.delete(Selectors.SELECT_FILES);
    assertFalse(file.exists());

    //  
    file.createFile();
    assertTrue(file.exists());

    FileContent fileContent = file.getContent();
    assertEquals(0, fileContent.getSize());

    //  
    String string = "test.";
    OutputStream os = fileContent.getOutputStream();

    try {
        os.write(string.getBytes());
        os.flush();
    } finally {
        if (os != null) {
            try {
                os.close();
            } catch (Exception ignore) {
                // no-op
            }
        }
    }
    assertNotSame(0, fileContent.getSize());

    //  
    StringBuffer sb = new StringBuffer();
    FileObject writtenFile = manager.resolveFile(baseDir, "testfolder/file1.txt");
    FileContent writtenContents = writtenFile.getContent();
    InputStream is = writtenContents.getInputStream();

    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));

        String line = "";
        while ((line = reader.readLine()) != null) {
            sb.append(line);
        }
    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (Exception ignore) {
                // no-op
            }
        }
    }

    //  
    assertEquals(sb.toString(), string);
}