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

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

Introduction

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

Prototype

long getSize() throws FileSystemException;

Source Link

Document

Determines the size of the file, in bytes.

Usage

From source file:com.wipro.ats.bdre.tdimport.FileMonitor.java

private static void putEligibleFileInfoInMap(String fileName, FileContent fc) {
    // *Start*   Eligible files moved to data structure for ingestion to HDFS
    FileCopyInfo fileCopyInfo = new FileCopyInfo();
    try {/*from   www.  j  av  a 2 s  . co  m*/
        fileCopyInfo.setFileName(fileName);
        fileCopyInfo.setSubProcessId(TDImportRunnableMain.getSubProcessId());
        fileCopyInfo.setServerId(Integer.toString(123461));
        fileCopyInfo.setSrcLocation(fc.getFile().getName().getPath());
        fileCopyInfo.setTdTable(TDImportRunnableMain.getTdTable());
        fileCopyInfo.setFileHash(DigestUtils.md5Hex(fc.getInputStream()));
        fileCopyInfo.setFileSize(fc.getSize());
        fileCopyInfo.setTimeStamp(fc.getLastModifiedTime());
        fileCopyInfo.setTdDB(TDImportRunnableMain.getTdDB());
        fileCopyInfo.setTdUserName(TDImportRunnableMain.getTdUserName());
        fileCopyInfo.setTdPassword(TDImportRunnableMain.getTdPassword());
        fileCopyInfo.setTdDelimiter(TDImportRunnableMain.getTdDelimiter());
        fileCopyInfo.setTdTpdid(TDImportRunnableMain.getTdTpdid());
        // putting element to structure
        addToQueue(fileName, fileCopyInfo);
    } catch (Exception err) {
        LOGGER.error("Error adding file to queue ", err);
        throw new BDREException(err);
    }
    // *End*   Eligible files moved to data structure for ingestion to HDFS
}

From source file:com.wipro.ats.bdre.filemon.FileMonitor.java

private static void putEligibleFileInfoInMap(String fileName, FileContent fc) {
    // *Start*   Eligible files moved to data structure for ingestion to HDFS
    FileCopyInfo fileCopyInfo = new FileCopyInfo();
    try {//from ww  w.  j a v  a2  s . co  m
        fileCopyInfo.setFileName(fileName);
        fileCopyInfo.setSubProcessId(FileMonRunnableMain.getSubProcessId());
        fileCopyInfo.setServerId(Integer.toString(123461));
        fileCopyInfo.setSrcLocation(fc.getFile().getName().getPath());
        fileCopyInfo.setDstLocation(new java.io.File(fileName).getParent()
                .replace(FileMonRunnableMain.getMonitoredDirName(), FileMonRunnableMain.getHdfsUploadDir()));
        fileCopyInfo.setFileHash(DigestUtils.md5Hex(fc.getInputStream()));
        fileCopyInfo.setFileSize(fc.getSize());
        fileCopyInfo.setTimeStamp(fc.getLastModifiedTime());
        // putting element to structure
        addToQueue(fileName, fileCopyInfo);
    } catch (Exception err) {
        LOGGER.error("Error adding file to queue ", err);
        throw new BDREException(err);
    }
    // *End*   Eligible files moved to data structure for ingestion to HDFS
}

From source file:net.sourceforge.fullsync.fs.connection.CommonsVfsConnection.java

private File buildNode(final File parent, final FileObject file)
        throws org.apache.commons.vfs2.FileSystemException {
    String name = file.getName().getBaseName();

    File n = new AbstractFile(this, name, parent, file.getType() == FileType.FOLDER, true);
    if (file.getType() == FileType.FILE) {
        FileContent content = file.getContent();
        n.setLastModified(content.getLastModifiedTime());
        n.setSize(content.getSize());
    }// ww  w .j a  v  a2  s  .co  m
    return n;
}

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

@Test
public void A006_testContent() throws Exception {
    String currAccountStr = testProperties.getProperty("azure.account.name");
    String currKey = testProperties.getProperty("azure.account.key");
    String currContainerStr = testProperties.getProperty("azure.test0001.container.name");
    String currHost = testProperties.getProperty("azure.host"); // <account>.blob.core.windows.net
    String currFileNameStr;/*from  w ww .  j  ava2  s .  c  o m*/

    DefaultFileSystemManager currMan = new DefaultFileSystemManager();
    currMan.addProvider(AzConstants.AZSBSCHEME, new AzFileProvider());
    currMan.init();

    StaticUserAuthenticator auth = new StaticUserAuthenticator("", currAccountStr, currKey);
    FileSystemOptions opts = new FileSystemOptions();
    DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(opts, auth);

    currFileNameStr = "file05";
    String currUriStr = String.format("%s://%s/%s/%s", AzConstants.AZSBSCHEME, currHost, currContainerStr,
            currFileNameStr);
    FileObject currFile = currMan.resolveFile(currUriStr, opts);

    FileContent content = currFile.getContent();
    long size = content.getSize();
    Assert.assertTrue(size >= 0);

    long modTime = content.getLastModifiedTime();
    Assert.assertTrue(modTime > 0);
}

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

@Test
public void A004_getContentSize() throws Exception {
    String currAccountStr = testProperties.getProperty("azure.account.name");
    String currKey = testProperties.getProperty("azure.account.key");
    String currContainerStr = testProperties.getProperty("azure.test0001.container.name");
    String currHost = testProperties.getProperty("azure.host"); // <account>.blob.core.windows.net
    String currFileNameStr;//from w  ww  . j  a  va 2  s.  co m

    DefaultFileSystemManager currMan = new DefaultFileSystemManager();
    currMan.addProvider(AzConstants.AZSBSCHEME, new AzFileProvider());
    currMan.init();

    StaticUserAuthenticator auth = new StaticUserAuthenticator("", currAccountStr, currKey);
    FileSystemOptions opts = new FileSystemOptions();
    DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(opts, auth);

    currFileNameStr = "test01.tmp";
    String currUriStr = String.format("%s://%s/%s/%s", AzConstants.AZSBSCHEME, currHost, currContainerStr,
            currFileNameStr);
    FileObject currFile = currMan.resolveFile(currUriStr, opts);

    log.info(String.format("exist() file '%s'", currUriStr));

    FileContent cont = currFile.getContent();
    long contSize = cont.getSize();

    Assert.assertTrue(contSize > 0);

}

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

@Test
public void A005_testContent() throws Exception {
    String currAccountStr = testProperties.getProperty("s3.access.id");
    String currKey = testProperties.getProperty("s3.access.secret");
    String currContainerStr = testProperties.getProperty("s3.test0001.bucket.name");
    String currHost = testProperties.getProperty("s3.host");
    String currRegion = testProperties.getProperty("s3.region");
    String currFileNameStr;// w  w w  . jav  a 2 s. com

    SS3FileProvider currSS3 = new SS3FileProvider();

    DefaultFileSystemManager currMan = new DefaultFileSystemManager();
    currMan.addProvider(SS3Constants.S3SCHEME, currSS3);
    currMan.init();

    StaticUserAuthenticator auth = new StaticUserAuthenticator("", currAccountStr, currKey);
    FileSystemOptions opts = new FileSystemOptions();
    DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(opts, auth);

    currFileNameStr = "file05";
    String currUriStr = String.format("%s://%s/%s/%s", SS3Constants.S3SCHEME, currHost, currContainerStr,
            currFileNameStr);
    FileObject currFile = currMan.resolveFile(currUriStr, opts);

    FileContent content = currFile.getContent();
    long size = content.getSize();
    Assert.assertTrue(size >= 0);

    long modTime = content.getLastModifiedTime();
    Assert.assertTrue(modTime > 0);
}

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

@Test
public void A004_getContentSize() throws Exception {
    String currAccountStr = testProperties.getProperty("s3.access.id");
    String currKey = testProperties.getProperty("s3.access.secret");
    String currContainerStr = testProperties.getProperty("s3.test0001.bucket.name");
    String currHost = testProperties.getProperty("s3.host");
    String currRegion = testProperties.getProperty("s3.region");
    String currFileNameStr;/*from   ww  w  .j  a v  a  2 s  .co  m*/

    SS3FileProvider currSS3 = new SS3FileProvider();

    DefaultFileSystemManager currMan = new DefaultFileSystemManager();
    currMan.addProvider(SS3Constants.S3SCHEME, currSS3);
    currMan.init();

    StaticUserAuthenticator auth = new StaticUserAuthenticator("", currAccountStr, currKey);
    FileSystemOptions opts = new FileSystemOptions();
    DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(opts, auth);

    currFileNameStr = "test01.tmp";
    String currUriStr = String.format("%s://%s/%s/%s", SS3Constants.S3SCHEME, currHost, currContainerStr,
            currFileNameStr);
    FileObject currFile = currMan.resolveFile(currUriStr, opts);

    log.info(String.format("getContent() file '%s'", currUriStr));

    FileContent cont = currFile.getContent();
    long contSize = cont.getSize();

    Assert.assertTrue(contSize > 0);

}

From source file:com.sludev.commons.vfs.simpleshell.SimpleShell.java

/**
 * Does an 'ls' command./*from   w  w w .j a v  a  2  s  .c o  m*/
 * 
 * @param cmd
 * @throws org.apache.commons.vfs2.FileSystemException
 */
public 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;
    }

    switch (file.getType()) {
    case FOLDER:
        // List the contents
        System.out.println("Contents of " + file.getName());
        listChildren(file, recursive, "");
        break;

    case FILE:
        // 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);
        break;

    case IMAGINARY:
        System.out.println(String.format("File '%s' is IMAGINARY", file.getName()));
        break;

    default:
        log.error(String.format("Unkown type '%d' on '%s'", file.getType(), file.getName()));
        break;
    }
}

From source file:net.sourceforge.fullsync.ui.FileObjectChooser.java

private void populateList() throws FileSystemException {
    FileObject[] children = activeFileObject.getChildren();
    Arrays.sort(children, (o1, o2) -> {
        try {//from w  ww . j  a  va 2s  .  c om
            if ((o1.getType() == FileType.FOLDER) && (o2.getType() == FileType.FILE)) {
                return -1;
            } else if ((o1.getType() == FileType.FILE) && (o2.getType() == FileType.FOLDER)) {
                return 1;
            }
            return o1.getName().getBaseName().compareTo(o2.getName().getBaseName());
        } catch (FileSystemException fse) {
            fse.printStackTrace();
            return 0;
        }
    });

    DateFormat df = DateFormat.getDateTimeInstance();

    for (int i = 0; i < children.length; i++) {
        FileObject data = children[i];

        TableItem item;
        if (tableItems.getItemCount() <= i) {
            item = new TableItem(tableItems, SWT.NULL);
        } else {
            item = tableItems.getItem(i);
        }

        item.setText(0, data.getName().getBaseName());
        String type = data.getType().getName(); //FIXME: translate type name {file,folder}

        if (data.getType().hasContent()) {
            FileContent content = data.getContent();
            String contentType = content.getContentInfo().getContentType();
            if (null != contentType) {
                type += " (" + contentType + ")"; //$NON-NLS-1$ //$NON-NLS-2$
            }
            item.setText(1, String.valueOf(content.getSize()));
            item.setText(3, df.format(new Date(content.getLastModifiedTime())));
        } else {
            item.setText(1, ""); //$NON-NLS-1$
            item.setText(3, ""); //$NON-NLS-1$
        }
        item.setText(2, type);

        if (data.getType() == FileType.FOLDER) {
            item.setImage(imageRepository.getImage("FS_Folder_Collapsed.gif")); //$NON-NLS-1$
        } else {
            item.setImage(imageRepository.getImage("FS_File_text_plain.gif")); //$NON-NLS-1$
        }

        item.setData(data);
    }
    tableItems.setItemCount(children.length);
}

From source file:dslab.crawler.pack.CrawlerPack.java

public String getFromRemote(String uri) {

    // clear cache
    fileSystem.getFilesCache().close();//from   w  w w . j  av a  2s. c om

    String remoteContent;
    String remoteEncoding = "utf-8";

    log.debug("Loading remote URI:" + uri);
    FileContent fileContent;

    try {
        // set cookie if cookies set
        if (0 < this.cookies.size()) {
            FileSystemOptions fsOptions = new FileSystemOptions();
            HttpFileSystemConfigBuilder.getInstance().setCookies(fsOptions, getCookies(uri));
            fileContent = fileSystem.resolveFile(uri, fsOptions).getContent();
        } else
            fileContent = fileSystem.resolveFile(uri).getContent();

        // 2016-03-22 only pure http/https auto detect encoding
        if ("http".equalsIgnoreCase(uri.substring(0, 4))) {
            fileContent.getSize(); // pass a bug {@link https://issues.apache.org/jira/browse/VFS-427}
            remoteEncoding = fileContent.getContentInfo().getContentEncoding();
        }

        if (null == remoteEncoding)
            remoteEncoding = "utf-8";

        if (!"utf".equalsIgnoreCase(remoteEncoding.substring(0, 3))) {
            log.debug("remote content encoding: " + remoteEncoding);

            // force charset encoding if setRemoteEncoding set
            if (!"utf".equalsIgnoreCase(encoding.substring(0, 3))) {
                remoteEncoding = encoding;
            } else {
                // auto detecting encoding
                remoteEncoding = detectCharset(IOUtils.toByteArray(fileContent.getInputStream()));
                log.info("real encoding: " + remoteEncoding);
            }
        }

        // 2016-02-29 fixed
        remoteContent = IOUtils.toString(fileContent.getInputStream(), remoteEncoding);

    } catch (FileSystemException fse) {
        log.warn(fse.getMessage());
        remoteContent = null;
    } catch (IOException ioe) {
        // return empty
        log.warn(ioe.getMessage());
        remoteContent = null;
    } catch (StringIndexOutOfBoundsException stre) {
        log.warn("uri: " + uri);
        log.warn(stre.getMessage());
        remoteContent = null;
    }

    clearCookies();

    // any exception will return "null"
    return remoteContent;
}