Example usage for org.apache.commons.vfs FileContent getSize

List of usage examples for org.apache.commons.vfs FileContent getSize

Introduction

In this page you can find the example usage for org.apache.commons.vfs FileContent getSize.

Prototype

long getSize() throws FileSystemException;

Source Link

Document

Determines the size of the file, in bytes.

Usage

From source file:egovframework.rte.fdl.filehandling.EgovFileUtil.java

/**
 * <p>// ww  w .  j a v  a  2  s  .c o m
 * byte ?? ? ?.
 * <p>
 * @param file
 *        <code>FileObject</code>
 * @return ? 
 * @throws IOException
 */
public static byte[] getContent(final FileObject file) throws IOException {
    final FileContent content = file.getContent();
    final int size = (int) content.getSize();
    final byte[] buf = new byte[size];

    final InputStream in = content.getInputStream();
    try {
        int read = 0;
        for (int pos = 0; pos < size && read >= 0; pos += read) {
            read = in.read(buf, pos, size - pos);
        }
    } finally {
        in.close();
    }

    return buf;
}

From source file:egovframework.rte.fdl.filehandling.FilehandlingServiceTest.java

/**
 *  ?  ??  ? ? ./*  w w w.  ja v a2  s.c  o m*/
 * ?    ? ?    ?? ?.
 * @throws Exception
 */
@Test
public void testAccessFile() throws Exception {

    FileSystemManager manager = VFS.getManager();

    FileObject baseDir = manager.resolveFile(System.getProperty("user.dir"));
    FileObject file = manager.resolveFile(baseDir, "testfolder/file1.txt");

    //  ? 
    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();
    } catch (Exception e) {
        throw new Exception(e);
    } finally {
        os.close();
    }
    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));

        for (String line = ""; (line = reader.readLine()) != null; sb.append(line))
            ;

    } catch (Exception e) {
        throw new Exception(e);
    } finally {
        is.close();
    }

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

From source file:egovframework.rte.fdl.filehandling.EgovFileUtil.java

/**
 * <p>//from  ww w  .  jav a  2s.com
 *  ? ??? .
 * </p>
 * @param cmd
 *        <code>String[]</code>
 * @return ? ? ?
 * @throws FileSystemException
 */
public List ls(final String[] cmd) throws FileSystemException {
    List list = new ArrayList();

    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 = manager.resolveFile(basefile, cmd[pos]);
    } else {
        file = basefile;
    }

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

    return list;
}

From source file:net.sf.jvifm.ui.ZipLister.java

private void changeCurrentNode() {

    boolean hasMatchSelectedName = false;
    FileObject[] children = null;
    try {//  w ww .  j  a  va2 s  . c o  m
        children = currentFileObject.getChildren();
    } catch (Exception e) {
        e.printStackTrace();
    }
    if (children == null)
        return;

    sortFiles(children);

    String selectedName = historyManager.getSelectedItem(currentFileObject.getName().getPath());
    table.removeAll();
    TableItem item;

    for (int i = 0; i < children.length; i++) {
        FileName fileName = children[i].getName();

        if (fileName.getBaseName().equals(selectedName)) {
            currentRow = i;
            hasMatchSelectedName = true;
        }

        item = new TableItem(table, SWT.NONE);
        item.setData("fileObject", children[i]);
        item.setText(fileName.getBaseName());

        try {
            FileType fileType = children[i].getType();
            FileContent fileContent = children[i].getContent();

            if (fileType.equals(FileType.FOLDER)) {
                item.setImage(folderImage);
                item.setText(1, "--");
                item.setText(2, StringUtil.formatDate(fileContent.getLastModifiedTime()));
            } else {
                item.setImage(fileImage);
                item.setText(1, StringUtil.formatSize(fileContent.getSize()));
                item.setText(2, StringUtil.formatDate(fileContent.getLastModifiedTime()));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    if (!hasMatchSelectedName)
        currentRow = 0;
    table.setSelection(currentRow);
    table.setFocus();

}

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

/**
 * Does an 'ls' command./*from w  ww .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:org.jboss.dashboard.ui.formatters.FileNavigationFileListFormatter.java

protected void renderFiles(List sortedChildren) throws FileSystemException {
    if (!sortedChildren.isEmpty()) {
        renderFragment("outputStart");
        for (int i = 0; i < sortedChildren.size(); i++) {
            FileObject fileObject = (FileObject) sortedChildren.get(i);
            FileContent content = fileObject.getContent();
            setAttribute("fileObject", fileObject);
            setAttribute("name", fileObject.getName().getBaseName());
            setAttribute("fileSize", content.getSize());
            String path = fileObject.getName().getPath();
            setAttribute("path", path);
            setAttribute("current", path.equals(getFileNavigationHandler().getCurrentFilePath()));
            String contentType = content.getContentInfo().getContentType();
            String extension = fileObject.getName().getExtension();
            if (contentType == null) {
                contentType = extension;
            }//from  ww w.  ja v a  2s  .  c om
            setAttribute("contentType", contentType);
            setAttribute("imageURL",
                    getFileNavigationHandler().getFilesystemManager().getThumbnailPath(extension));
            renderFragment("output");
        }
        renderFragment("outputEnd");
    }

}

From source file:org.jclouds.vfs.tools.blobstore.BlobStoreShell.java

private void ls(FileSystemManager mg, FileObject wd, final String[] cmd) throws FileSystemException {
    int pos = 1;/*from   w w  w  . j av  a 2s. com*/
    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 = mg.resolveFile(wd, cmd[pos]);
    } else {
        file = wd;
    }

    if (file.getType() == FileType.FOLDER) {
        // List the contents
        System.out.println("Contents of " + file.getName().getFriendlyURI());
        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.sonatype.gshell.commands.vfs.FileInfoCommand.java

public Object execute(final CommandContext context) throws Exception {
    assert context != null;
    IO io = context.getIo();/*from w ww . j  a  va2s  .c  o  m*/

    FileObject file = resolveFile(context, path);

    io.println("URL: {}", file.getURL());
    io.println("Name: {}", file.getName());
    io.println("BaseName: {}", file.getName().getBaseName());
    io.println("Extension: {}", file.getName().getExtension());
    io.println("Path: {}", file.getName().getPath());
    io.println("Scheme: {}", file.getName().getScheme());
    io.println("URI: {}", file.getName().getURI());
    io.println("Root URI: {}", file.getName().getRootURI());
    io.println("Parent: {}", file.getName().getParent());
    io.println("Type: {}", file.getType());
    io.println("Exists: {}", file.exists());
    io.println("Readable: {}", file.isReadable());
    io.println("Writeable: {}", file.isWriteable());
    io.println("Root path: {}", file.getFileSystem().getRoot().getName().getPath());

    if (file.exists()) {
        FileContent content = file.getContent();
        FileContentInfo contentInfo = content.getContentInfo();
        io.println("Content type: {}", contentInfo.getContentType());
        io.println("Content encoding: {}", contentInfo.getContentEncoding());

        try {
            // noinspection unchecked
            Map<String, Object> attrs = content.getAttributes();
            if (attrs != null && !attrs.isEmpty()) {
                io.println("Attributes:");
                for (Map.Entry<String, Object> entry : attrs.entrySet()) {
                    io.println("    {}='{}'", entry.getKey(), entry.getValue());
                }
            }
        } catch (FileSystemException e) {
            io.println("File attributes are NOT supported");
        }

        try {
            Certificate[] certs = content.getCertificates();
            if (certs != null && certs.length != 0) {
                io.println("Certificate:");
                for (Certificate cert : certs) {
                    io.println("    {}", cert);
                }
            }
        } catch (FileSystemException e) {
            io.println("File certificates are NOT supported");
        }

        if (file.getType().equals(FileType.FILE)) {
            io.println("Size: {} bytes", content.getSize());
        } else if (file.getType().hasChildren() && file.isReadable()) {
            FileObject[] children = file.getChildren();
            io.println("Directory with {} files", children.length);

            for (int iterChildren = 0; iterChildren < children.length; iterChildren++) {
                io.println("#{}:{}", iterChildren, children[iterChildren].getName());
                if (iterChildren > 5) {
                    break;
                }
            }
        }

        io.println("Last modified: {}",
                DateFormat.getInstance().format(new Date(content.getLastModifiedTime())));
    } else {
        io.println("The file does not exist");
    }

    FileObjects.close(file);

    return Result.SUCCESS;
}