Java Code Examples for java.io.File#getTotalSpace()

The following examples show how to use java.io.File#getTotalSpace() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DLedgerUtils.java    From openmessaging-storage-dledger with Apache License 2.0 6 votes vote down vote up
public static double getDiskPartitionSpaceUsedPercent(final String path) {
    if (null == path || path.isEmpty())
        return -1;

    try {
        File file = new File(path);

        if (!file.exists())
            return -1;

        long totalSpace = file.getTotalSpace();

        if (totalSpace > 0) {
            long freeSpace = file.getFreeSpace();
            long usedSpace = totalSpace - freeSpace;

            return usedSpace / (double) totalSpace;
        }
    } catch (Exception e) {
        return -1;
    }
    return -1;
}
 
Example 2
Source File: UtilAll.java    From DDMQ with Apache License 2.0 6 votes vote down vote up
public static double getDiskPartitionSpaceUsedPercent(final String path) {
    if (null == path || path.isEmpty())
        return -1;

    try {
        File file = new File(path);

        if (!file.exists())
            return -1;

        long totalSpace = file.getTotalSpace();

        if (totalSpace > 0) {
            long freeSpace = file.getFreeSpace();
            long usedSpace = totalSpace - freeSpace;

            return usedSpace / (double) totalSpace;
        }
    } catch (Exception e) {
        return -1;
    }

    return -1;
}
 
Example 3
Source File: GetXSpace.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
private static void compareZeroExist() {
    try {
        File f = File.createTempFile("tmp", null, new File("."));

        long [] s = { f.getTotalSpace(), f.getFreeSpace(), f.getUsableSpace() };

        for (int i = 0; i < s.length; i++) {
            if (s[i] == 0L)
                fail(f.getName(), s[i], "==", 0L);
            else
                pass();
        }
    } catch (IOException x) {
        fail("Couldn't create temp file for test");
    }
}
 
Example 4
Source File: GetXSpace.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private static void compareZeroExist() {
    try {
        File f = File.createTempFile("tmp", null, new File("."));

        long [] s = { f.getTotalSpace(), f.getFreeSpace(), f.getUsableSpace() };

        for (int i = 0; i < s.length; i++) {
            if (s[i] == 0L)
                fail(f.getName(), s[i], "==", 0L);
            else
                pass();
        }
    } catch (IOException x) {
        fail("Couldn't create temp file for test");
    }
}
 
Example 5
Source File: UtilAll.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 6 votes vote down vote up
/**
 * 获取磁盘分区空间使用率
 */
public static double getDiskPartitionSpaceUsedPercent(final String path) {
    if (null == path || path.isEmpty())
        return -1;

    try {
        File file = new File(path);

        if (!file.exists())
            return -1;

        long totalSpace = file.getTotalSpace();

        if (totalSpace > 0) {
            long freeSpace = file.getFreeSpace();
            long usedSpace = totalSpace - freeSpace;

            return usedSpace / (double) totalSpace;
        }
    } catch (Exception e) {
        return -1;
    }

    return -1;
}
 
Example 6
Source File: UtilAll.java    From rocketmq with Apache License 2.0 6 votes vote down vote up
public static double getDiskPartitionSpaceUsedPercent(final String path) {
    if (null == path || path.isEmpty())
        return -1;

    try {
        File file = new File(path);

        if (!file.exists())
            return -1;

        long totalSpace = file.getTotalSpace();

        if (totalSpace > 0) {
            long freeSpace = file.getFreeSpace();
            long usedSpace = totalSpace - freeSpace;

            return usedSpace / (double) totalSpace;
        }
    } catch (Exception e) {
        return -1;
    }

    return -1;
}
 
Example 7
Source File: Util.java    From feeyo-redisproxy with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public static double getDiskPartitionSpaceUsedPercent(final String path) {
    if (null == path || path.isEmpty())
        return -1;

    try {
    	
        File file = new File(path);
        if (!file.exists())
            return -1;

        long totalSpace = file.getTotalSpace();
        if (totalSpace > 0) {
            long freeSpace = file.getFreeSpace();
            long usedSpace = totalSpace - freeSpace;
            return usedSpace / (double) totalSpace;
        }
    } catch (Exception e) {
        return -1;
    }
    return -1;
}
 
Example 8
Source File: SpillServiceImpl.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
private boolean isHealthy(Path spillDirPath) {
  if (healthCheckEnabled) {
    final File disk = new File(Path.getPathWithoutSchemeAndAuthority(spillDirPath).toString());
    final double totalSpace = (double) disk.getTotalSpace();
    minDiskSpace = options.minDiskSpace();
    minDiskSpacePercentage = options.minDiskSpacePercentage();
    logger.debug("Check isHealthy for {} minDiskSpace: {} minDiskSpacePercentage: {}",
      spillDirPath.getName(), minDiskSpace, minDiskSpacePercentage);
    final long threshold = Math.max((long) ((totalSpace / 100.0) * minDiskSpacePercentage), minDiskSpace);
    final long available = disk.getFreeSpace();
    if (available < threshold) {
      return false;
    }
  }
  return true;
}
 
Example 9
Source File: DUHelper.java    From hadoop with Apache License 2.0 5 votes vote down vote up
public String check(String folder) {
  if (folder == null)
    throw new IllegalArgumentException("folder");
  File f = new File(folder);

  folderSize = getFileSize(f);
  usage = 1.0*(f.getTotalSpace() - f.getFreeSpace())/ f.getTotalSpace();
  return String.format("used %d files %d disk in use %f", folderSize, fileCount, usage);
}
 
Example 10
Source File: GetXSpace.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
private static void compare(Space s) {
    File f = new File(s.name());
    long ts = f.getTotalSpace();
    long fs = f.getFreeSpace();
    long us = f.getUsableSpace();

    out.format("%s:%n", s.name());
    String fmt = "  %-4s total= %12d free = %12d usable = %12d%n";
    out.format(fmt, "df", s.total(), 0, s.free());
    out.format(fmt, "getX", ts, fs, us);

    // if the file system can dynamically change size, this check will fail
    if (ts != s.total())
        fail(s.name(), s.total(), "!=", ts);
    else
        pass();

    // unix df returns statvfs.f_bavail
    long tsp = (!name.startsWith("Windows") ? us : fs);
    if (!s.woomFree(tsp))
        fail(s.name(), s.free(), "??", tsp);
    else
        pass();

    if (fs > s.total())
        fail(s.name(), s.total(), ">", fs);
    else
        pass();

    if (us > s.total())
        fail(s.name(), s.total(), ">", us);
    else
        pass();
}
 
Example 11
Source File: AbstractSystemCommander.java    From Jpom with MIT License 5 votes vote down vote up
/**
 * 磁盘占用
 *
 * @return 磁盘占用
 */
protected static String getHardDisk() {
    File[] files = File.listRoots();
    double totalSpace = 0;
    double useAbleSpace = 0;
    for (File file : files) {
        double total = file.getTotalSpace();
        totalSpace += total;
        useAbleSpace += total - file.getUsableSpace();
    }
    return String.format("%.2f", useAbleSpace / totalSpace * 100);
}
 
Example 12
Source File: RawLocalFileSystem.java    From big-c with Apache License 2.0 5 votes vote down vote up
@Override
public FsStatus getStatus(Path p) throws IOException {
  File partition = pathToFile(p == null ? new Path("/") : p);
  //File provides getUsableSpace() and getFreeSpace()
  //File provides no API to obtain used space, assume used = total - free
  return new FsStatus(partition.getTotalSpace(), 
    partition.getTotalSpace() - partition.getFreeSpace(),
    partition.getFreeSpace());
}
 
Example 13
Source File: GetXSpace.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private static void compare(Space s) {
    File f = new File(s.name());
    long ts = f.getTotalSpace();
    long fs = f.getFreeSpace();
    long us = f.getUsableSpace();

    out.format("%s:%n", s.name());
    String fmt = "  %-4s total= %12d free = %12d usable = %12d%n";
    out.format(fmt, "df", s.total(), 0, s.free());
    out.format(fmt, "getX", ts, fs, us);

    // if the file system can dynamically change size, this check will fail
    if (ts != s.total())
        fail(s.name(), s.total(), "!=", ts);
    else
        pass();

    // unix df returns statvfs.f_bavail
    long tsp = (!name.startsWith("Windows") ? us : fs);
    if (!s.woomFree(tsp))
        fail(s.name(), s.free(), "??", tsp);
    else
        pass();

    if (fs > s.total())
        fail(s.name(), s.total(), ">", fs);
    else
        pass();

    if (us > s.total())
        fail(s.name(), s.total(), ">", us);
    else
        pass();
}
 
Example 14
Source File: DiskSpaceGcTimer.java    From Dragonfly with Apache License 2.0 5 votes vote down vote up
@PostConstruct
public void init() {
    long totalSpace = 0;
    try {
        File file = new File(Constants.DOWNLOAD_HOME);
        totalSpace = file.getTotalSpace();
    } catch (Exception e) {
    }
    long youngGcThreshold = totalSpace > 0 && totalSpace / 4 < 100 * 1024 * 1024 * 1024L ? totalSpace / 4
        : 100 * 1024 * 1024 * 1024L;
    downSpaceCleaner.fillConf(DownSpaceCleaner.SPACE_TYPE_DISK, Constants.DOWNLOAD_HOME,
        5 * 1024 * 1024 * 1024L,
        youngGcThreshold, 1, 2 * 3600 * 1000L);
}
 
Example 15
Source File: DUHelper.java    From big-c with Apache License 2.0 5 votes vote down vote up
public String check(String folder) {
  if (folder == null)
    throw new IllegalArgumentException("folder");
  File f = new File(folder);

  folderSize = getFileSize(f);
  usage = 1.0*(f.getTotalSpace() - f.getFreeSpace())/ f.getTotalSpace();
  return String.format("used %d files %d disk in use %f", folderSize, fileCount, usage);
}
 
Example 16
Source File: GetXSpace.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
private static void compare(Space s) {
    File f = new File(s.name());
    long ts = f.getTotalSpace();
    long fs = f.getFreeSpace();
    long us = f.getUsableSpace();

    out.format("%s:%n", s.name());
    String fmt = "  %-4s total= %12d free = %12d usable = %12d%n";
    out.format(fmt, "df", s.total(), 0, s.free());
    out.format(fmt, "getX", ts, fs, us);

    // if the file system can dynamically change size, this check will fail
    if (ts != s.total())
        fail(s.name(), s.total(), "!=", ts);
    else
        pass();

    // unix df returns statvfs.f_bavail
    long tsp = (!name.startsWith("Windows") ? us : fs);
    if (!s.woomFree(tsp))
        fail(s.name(), s.free(), "??", tsp);
    else
        pass();

    if (fs > s.total())
        fail(s.name(), s.total(), ">", fs);
    else
        pass();

    if (us > s.total())
        fail(s.name(), s.total(), ">", us);
    else
        pass();
}
 
Example 17
Source File: ClipDownloadControllerImpl.java    From arcusandroid with Apache License 2.0 5 votes vote down vote up
protected boolean hasAvailableStorage(long expectedSize) {
    File content = getContentFile();
    double totalSpace = (double) content.getTotalSpace();
    double freeSpace  = (double) content.getFreeSpace();
    double percentFull = 100 - ((freeSpace * 100) / totalSpace);
    double afterSavedSize = (totalSpace - freeSpace) + expectedSize;
    logger.debug("Reporting -> %[{}] Full (download size of [{}] bytes to be added)", percentFull, expectedSize);

    // See: https://developer.android.com/training/basics/data-storage/files.html#GetFreeSpace
    // If the number returned is a few MB more than the size of the data you want to save, or if the file system is
    // less than 90% full, then it's probably safe to proceed. Otherwise, you probably shouldn't write to storage.
    return afterSavedSize < totalSpace && percentFull <= 90D;
}
 
Example 18
Source File: Files.java    From chipster with MIT License 4 votes vote down vote up
public static boolean partitionHasUsableSpacePercentage(File file, int percentage) {
	return ((double)file.getUsableSpace()/(double)file.getTotalSpace())*100 >= percentage;
}
 
Example 19
Source File: Ovm3StoragePool.java    From cloudstack with Apache License 2.0 4 votes vote down vote up
/**
 * Copy the systemvm.iso in if it doesn't exist or the size differs.
 *
 * @param storageUrl
 * @param poolUuid
 * @param host
 */
private void prepareSecondaryStorageStore(String storageUrl,
        String poolUuid, String host) {
    String mountPoint = storageUrl;

    GlobalLock lock = GlobalLock.getInternLock("prepare.systemvm");
    try {
        /* double check */
        if (config.getAgentHasMaster() && config.getAgentInOvm3Pool()) {
            LOGGER.debug("Skip systemvm iso copy, leave it to the master");
            return;
        }
        if (lock.lock(3600)) {
            try {
                /*
                 * save src iso real name for reuse, so we don't depend on
                 * other happy little accidents.
                 */
                File srcIso = getSystemVMPatchIsoFile();
                String destPath = mountPoint + "/ISOs/";
                try {
                    StoragePlugin sp = new StoragePlugin(c);
                    FileProperties fp = sp.storagePluginGetFileInfo(
                            poolUuid, host, destPath + "/"
                                    + srcIso.getName());
                    if (fp.getSize() != srcIso.getTotalSpace()) {
                        LOGGER.info(" System VM patch ISO file already exists: "
                                + srcIso.getAbsolutePath().toString()
                                + ", destination: " + destPath);
                    }
                } catch (Exception e) {
                    LOGGER.info("Copy System VM patch ISO file to secondary storage. source ISO: "
                            + srcIso.getAbsolutePath()
                            + ", destination: "
                            + destPath);
                    try {
                        /* Perhaps use a key instead ? */
                        SshHelper
                                .scpTo(c.getIp(), 22, config
                                        .getAgentSshUserName(), null,
                                        config.getAgentSshPassword(),
                                        destPath, srcIso.getAbsolutePath()
                                                .toString(), "0644");
                    } catch (Exception es) {
                        LOGGER.error("Unexpected exception ", es);
                        String msg = "Unable to copy systemvm ISO on secondary storage. src location: "
                                + srcIso.toString()
                                + ", dest location: "
                                + destPath;
                        LOGGER.error(msg);
                        throw new CloudRuntimeException(msg, es);
                    }
                }
            } finally {
                lock.unlock();
            }
        }
    } finally {
        lock.releaseRef();
    }
}
 
Example 20
Source File: JavaStorageLayer.java    From cosmic with Apache License 2.0 4 votes vote down vote up
@Override
public long getUsedSpace(final String path) {
    final File file = new File(path);
    return file.getTotalSpace() - file.getFreeSpace();
}