下面列出了java.nio.file.FileStore#getUsableSpace ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public boolean isStorageAvailable() {
try {
FileStore fileStore = Files.getFileStore(contentDir);
long usableSpace = fileStore.getUsableSpace();
boolean readOnly = fileStore.isReadOnly();
boolean result = !readOnly && usableSpace > 0;
if (!result) {
log.warn("File blob store '{}' is not writable. Read only: {}. Usable space: {}",
getBlobStoreConfiguration().getName(), readOnly, usableSpace);
}
return result;
}
catch (IOException e) {
log.warn("File blob store '{}' is not writable.", getBlobStoreConfiguration().getName(), e);
return false;
}
}
private boolean hasEnoughDiskSpace(Path path)
{
try {
FileStore fileStore = getFileStore(path);
return fileStore.getUsableSpace() > fileStore.getTotalSpace() * (1.0 - maxUsedSpaceThreshold);
}
catch (IOException e) {
throw new PrestoException(OUT_OF_SPILL_SPACE, "Cannot determine free space for spill", e);
}
}
private static long getUsableSpace() {
// Ensure that the directory exists.
rootDirectory.mkdirs();
// Get the usable space.
long freeSpace = 0L;
Path path = Paths.get(rootDirectory.getAbsolutePath());
try {
FileStore store = Files.getFileStore(path);
freeSpace = store.getUsableSpace();
} catch (Exception ignored) { }
return freeSpace;
}
@Override
public void sense(final MetricContext metricContext) throws SenseException
{
try {
// Determine the file store for the directory the JVM was started in
FileStore fileStore = Files.getFileStore(Paths.get(System.getProperty("user.dir")));
long total = fileStore.getTotalSpace();
long free = fileStore.getUsableSpace();
double percent_free = 100.0 * ((double)(total-free)/(double)total);
metricContext.record(DISK_USED, percent_free, Unit.PERCENT);
} catch (IOException e) {
throw new SenseException("Problem reading disk space", e);
}
}
private static Long getUsableSpace(String dir) {
try {
File file = new File(dir);
if (!file.exists()) {
file = file.getParentFile();
if (!file.exists()) {//this is not a disk directory . so just pretend that there is enough space
return Long.MAX_VALUE;
}
}
FileStore fileStore = Files.getFileStore(file.toPath());
return fileStore.getUsableSpace();
} catch (IOException e) {
throw new SolrException(ErrorCode.SERVER_ERROR, "Could not free disk space", e);
}
}
public long getFreeSpace() {
long freeSpace = 0;
try {
FileStore fileStore = Files.getFileStore(Paths.get("."));
freeSpace = fileStore.getUsableSpace();
} catch (IOException ioEx) {
logger.log(Level.WARNING, "Cannot calculate free/total disk space", ioEx);
}
return freeSpace;
}
public void tick() {
synchronized (monitorLock) {
boolean over = false;
long usableSpace = 0;
long totalSpace = 0;
for (FileStore store : stores) {
try {
usableSpace = store.getUsableSpace();
totalSpace = getTotalSpace(store);
over = calculateUsage(usableSpace, totalSpace) > maxUsage;
if (over) {
break;
}
} catch (IOException ioe) {
ioCriticalErrorListener.onIOException(ioe, "IO Error while calculating disk usage", null);
} catch (Exception e) {
logger.warn(e.getMessage(), e);
}
}
for (Callback callback : callbackList) {
callback.tick(usableSpace, totalSpace);
if (over) {
callback.over(usableSpace, totalSpace);
} else {
callback.under(usableSpace, totalSpace);
}
}
}
}
public long getFreeSpace() {
long freeSpace = 0;
try {
FileStore fileStore = Files.getFileStore(Paths.get("."));
freeSpace = fileStore.getUsableSpace();
} catch (IOException ioEx) {
logger.log(Level.WARNING, "Cannot calculate free/total disk space", ioEx);
}
return freeSpace;
}
/**
* Simple test to check {@link FileStore} support.
*
* @throws IOException
*/
@Test
public void getFileStores() throws IOException {
Path pathToTest = Paths.get(clusterUri);
Iterable<FileStore> fileStores = pathToTest.getFileSystem().getFileStores();
for (FileStore store : fileStores) {
store.getUsableSpace();
assertNotNull(store.toString());
}
}
@Override
public FsStat getFsStat() throws IOException {
FileStore store = Files.getFileStore(_root);
long total = store.getTotalSpace();
long free = store.getUsableSpace();
return new FsStat(total, Long.MAX_VALUE, total-free, pathToInode.size());
}
public long freeDiskBytes() throws IOException {
Iterable<FileStore> fileStores = localFileSystem.getFileStores();
long totalUsableSpace = 0l;
for(FileStore fs:fileStores){
totalUsableSpace+=fs.getUsableSpace();
}
return totalUsableSpace;
}
public static String freeSpaceFormatted(FileStore store) throws Exception {
long mb = store.getUsableSpace() / (1000*1000); // 10^6 converts to megabytes.
return mb + " mb";
}
@Test
public void get_available_space_nio () throws IOException {
FileStore store = Files.getFileStore(source);
long availableSpace = store.getUsableSpace() / 1024;
assertTrue(availableSpace > 0);
}