Example usage for java.io File getUsableSpace

List of usage examples for java.io File getUsableSpace

Introduction

In this page you can find the example usage for java.io File getUsableSpace.

Prototype

public long getUsableSpace() 

Source Link

Document

Returns the number of bytes available to this virtual machine on the partition named by this abstract pathname.

Usage

From source file:org.apache.bookkeeper.util.TestDiskChecker.java

@Test(expected = DiskWarnThresholdException.class)
public void testDiskWarnThresholdException() throws IOException {
    File file = createTempDir("DiskCheck", "test");
    long usableSpace = file.getUsableSpace();
    long totalSpace = file.getTotalSpace();
    float diskSpaceThreshold = (1f - ((float) usableSpace / (float) totalSpace));
    float diskWarnThreshold = (1f - ((float) usableSpace / (float) totalSpace)) * 0.5f;
    diskChecker.setDiskSpaceThreshold(diskSpaceThreshold, diskWarnThreshold);
    diskChecker.checkDiskFull(file);/*from  w  w  w.  jav a  2  s  .  c o  m*/
}

From source file:org.apache.bookkeeper.util.TestDiskChecker.java

/**
 * Check disk full on non exist file. in this case it should check for
 * parent file/*www  .  ja v a 2 s.c o  m*/
 */
@Test(timeout = 30000, expected = DiskOutOfSpaceException.class)
public void testCheckDiskFullOnNonExistFile() throws IOException {
    File file = createTempDir("DiskCheck", "test");
    long usableSpace = file.getUsableSpace();
    long totalSpace = file.getTotalSpace();
    float threshold = (1f - ((float) usableSpace / (float) totalSpace)) * 0.5f;
    diskChecker.setDiskSpaceThreshold(threshold, threshold);
    assertTrue(file.delete());
    diskChecker.checkDiskFull(file);
}

From source file:pl.psnc.synat.dsa.fs.FSDataStorageClient.java

@Override
public void updateFreeSpace() {
    File file = new File(root);
    freeSpace = file.getUsableSpace();
}

From source file:org.apache.activemq.usage.PercentDiskUsageLimitTest.java

protected int getFreePercentage() {
    File storeDir = StoreUtil.findParentDirectory(adapter.getDirectory());
    return (int) (((double) storeDir.getUsableSpace() / storeDir.getTotalSpace()) * 100);
}

From source file:com.android.tradefed.util.FileUtil.java

private static void verifyDiskSpace(File file) {
    // Based on empirical testing File.getUsableSpace is a low cost
    // operation (~ 100 us for
    // local disk, ~ 100 ms for network disk). Therefore call it every time
    // tmp file is
    // created/*from ww  w.j  ava 2s.co m*/
    if (file.getUsableSpace() < MIN_DISK_SPACE) {
        throw new LowDiskSpaceException(String.format("Available space on %s is less than %s MB",
                file.getAbsolutePath(), MIN_DISK_SPACE_MB));
    }
}

From source file:fr.gael.dhus.datastore.eviction.EvictionManager.java

/**
 * Compute space disk usage on partition where the eviction works.
 *
 * @return space disk usage in bytes on disk partition.
 *//*www. j  a  v  a2  s  .  com*/
public long getUsableSpace() {
    String path = getPath();
    File fpath = new File(path);
    return fpath.getUsableSpace();
}

From source file:dk.netarkivet.common.utils.DefaultFreeSpaceProvider.java

/**
 * Returns the number of bytes free on the file system that the given file
 * resides on. Will return 0 on non-existing files.
 *
 * @param f a given file//from w w  w.j a v  a2  s  .  co m
 * @return the number of bytes free on the file system where file f resides.
 * 0 if the file cannot be found.
 */
public long getBytesFree(File f) {
    ArgumentNotValid.checkNotNull(f, "File f");
    if (!f.exists()) {
        log.warn("The file '" + f.getAbsolutePath() + "' does not exist. The value 0 returned.");
        return 0;
    }
    return f.getUsableSpace();
}

From source file:org.apache.hadoop.yarn.server.nodemanager.TestLocalDirsHandlerService.java

@Test
public void testGetFullDirs() throws Exception {
    Configuration conf = new YarnConfiguration();

    conf.set(CommonConfigurationKeys.FS_PERMISSIONS_UMASK_KEY, "077");
    FileContext localFs = FileContext.getLocalFSFileContext(conf);

    String localDir1 = new File(testDir, "localDir1").getPath();
    String localDir2 = new File(testDir, "localDir2").getPath();
    String logDir1 = new File(testDir, "logDir1").getPath();
    String logDir2 = new File(testDir, "logDir2").getPath();
    Path localDir1Path = new Path(localDir1);
    Path logDir1Path = new Path(logDir1);
    FsPermission dirPermissions = new FsPermission((short) 0410);
    localFs.mkdir(localDir1Path, dirPermissions, true);
    localFs.mkdir(logDir1Path, dirPermissions, true);

    conf.set(YarnConfiguration.NM_LOCAL_DIRS, localDir1 + "," + localDir2);
    conf.set(YarnConfiguration.NM_LOG_DIRS, logDir1 + "," + logDir2);
    conf.setFloat(YarnConfiguration.NM_MAX_PER_DISK_UTILIZATION_PERCENTAGE, 0.0f);
    NodeManagerMetrics nm = NodeManagerMetrics.create();
    LocalDirsHandlerService dirSvc = new LocalDirsHandlerService(nm);
    dirSvc.init(conf);/*  w  w w  .java  2  s.co  m*/
    Assert.assertEquals(0, dirSvc.getLocalDirs().size());
    Assert.assertEquals(0, dirSvc.getLogDirs().size());
    Assert.assertEquals(1, dirSvc.getDiskFullLocalDirs().size());
    Assert.assertEquals(1, dirSvc.getDiskFullLogDirs().size());
    // check the metrics
    Assert.assertEquals(2, nm.getBadLocalDirs());
    Assert.assertEquals(2, nm.getBadLogDirs());
    Assert.assertEquals(0, nm.getGoodLocalDirsDiskUtilizationPerc());
    Assert.assertEquals(0, nm.getGoodLogDirsDiskUtilizationPerc());

    Assert.assertEquals("", dirSvc.getConfig().get(LocalDirsHandlerService.NM_GOOD_LOCAL_DIRS));
    Assert.assertEquals("", dirSvc.getConfig().get(LocalDirsHandlerService.NM_GOOD_LOG_DIRS));
    Assert.assertEquals(localDir1 + "," + localDir2, dirSvc.getConfig().get(YarnConfiguration.NM_LOCAL_DIRS));
    Assert.assertEquals(logDir1 + "," + logDir2, dirSvc.getConfig().get(YarnConfiguration.NM_LOG_DIRS));

    conf.setFloat(YarnConfiguration.NM_MAX_PER_DISK_UTILIZATION_PERCENTAGE, 100.0f);
    nm = NodeManagerMetrics.create();
    dirSvc = new LocalDirsHandlerService(nm);
    dirSvc.init(conf);
    Assert.assertEquals(1, dirSvc.getLocalDirs().size());
    Assert.assertEquals(1, dirSvc.getLogDirs().size());
    Assert.assertEquals(0, dirSvc.getDiskFullLocalDirs().size());
    Assert.assertEquals(0, dirSvc.getDiskFullLogDirs().size());
    // check the metrics
    File dir = new File(localDir1);
    int utilizationPerc = (int) ((dir.getTotalSpace() - dir.getUsableSpace()) * 100 / dir.getTotalSpace());
    Assert.assertEquals(1, nm.getBadLocalDirs());
    Assert.assertEquals(1, nm.getBadLogDirs());
    Assert.assertEquals(utilizationPerc, nm.getGoodLocalDirsDiskUtilizationPerc());
    Assert.assertEquals(utilizationPerc, nm.getGoodLogDirsDiskUtilizationPerc());

    Assert.assertEquals(localDir2, dirSvc.getConfig().get(LocalDirsHandlerService.NM_GOOD_LOCAL_DIRS));
    Assert.assertEquals(logDir2, dirSvc.getConfig().get(LocalDirsHandlerService.NM_GOOD_LOG_DIRS));
    Assert.assertEquals(localDir1 + "," + localDir2, dirSvc.getConfig().get(YarnConfiguration.NM_LOCAL_DIRS));
    Assert.assertEquals(logDir1 + "," + logDir2, dirSvc.getConfig().get(YarnConfiguration.NM_LOG_DIRS));

    FileUtils.deleteDirectory(new File(localDir1));
    FileUtils.deleteDirectory(new File(localDir2));
    FileUtils.deleteDirectory(new File(logDir1));
    FileUtils.deleteDirectory(new File(logDir2));
    dirSvc.close();
}

From source file:org.apache.activemq.usage.PeriodicDiskUsageLimitTest.java

protected int getFreePercentage(File directory) {
    File storeDir = StoreUtil.findParentDirectory(directory);
    return (int) (((double) storeDir.getUsableSpace() / storeDir.getTotalSpace()) * 100);
}

From source file:com.ikon.servlet.admin.StatsGraphServlet.java

/**
 * Generate disk statistics/* w w  w  .  jav a  2s  .co  m*/
 */
public JFreeChart diskStats() throws IOException, ServletException {
    String repHome = null;

    // Allow absolute repository path
    if ((new File(Config.REPOSITORY_HOME)).isAbsolute()) {
        repHome = Config.REPOSITORY_HOME;
    } else {
        repHome = Config.HOME_DIR + File.separator + Config.REPOSITORY_HOME;
    }

    File df = new File(repHome);
    long total = df.getTotalSpace();
    long usable = df.getUsableSpace();
    long used = total - usable;
    String title = "Disk: " + FormatUtil.formatSize(total);

    log.debug("Total space: {}", FormatUtil.formatSize(total));
    log.debug("Usable space: {}", FormatUtil.formatSize(usable));
    log.debug("Used space: {}", FormatUtil.formatSize(used));

    DefaultPieDataset dataset = new DefaultPieDataset();
    dataset.setValue("Available (" + FormatUtil.formatSize(usable) + ")", usable * 100 / total);
    dataset.setValue("Used (" + FormatUtil.formatSize(used) + ")", used * 100 / total);

    return ChartFactory.createPieChart(title, dataset, true, false, false);
}