下面列出了java.nio.file.FileStore#getTotalSpace ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Get the location of the DVD, or null if no DVD can be found.
* <p>
* @return the DVD location.
*/
private String getLocation() {
FileSystem fs = FileSystems.getDefault();
for(Path rootPath : fs.getRootDirectories()) {
try {
FileStore store = Files.getFileStore(rootPath);
if(store.type().toLowerCase().contains("udf")) {
if(store.getTotalSpace()>10000000000L) { //Blu-ray
return "bluray:///" + rootPath.toString();
}
else {
return "dvdsimple:///" + rootPath.toString(); //DVD
}
}
}
catch(IOException ex) {
//Never mind
}
}
return null;
}
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);
}
}
@Override
public TypedMap addContext(final TypedMap existing)
{
try {
// Determine the file store for the directory the JVM was started in
FileStore fileStore = Files.getFileStore(Paths.get(System.getProperty("user.dir")));
long size = fileStore.getTotalSpace();
long gb_size = size/(K*K*K);
return ImmutableTypedMap.Builder.from(existing).add(DISK_SIZE, Long.valueOf(gb_size)).build();
} catch (IOException e) {
// log?
return existing;
}
}
@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);
}
}
@Override
public Space get() throws BackgroundException {
final Path home = new DefaultHomeFinderService(session).find();
try {
final FileStore store = Files.getFileStore(session.toPath(home));
return new Space(store.getTotalSpace() - store.getUnallocatedSpace(), store.getUnallocatedSpace());
}
catch(IOException e) {
throw new LocalExceptionMappingService().map("Failure to read attributes of {0}", e, home);
}
}
public long getTotalSpace() {
long totalSpace = 0;
try {
FileStore fileStore = Files.getFileStore(Paths.get("."));
totalSpace = fileStore.getTotalSpace();
} catch (IOException ioEx) {
logger.log(Level.WARNING, "Cannot calculate free disk space", ioEx);
}
return totalSpace;
}
private Double freeSpace ()
{
try
{
final String base = System.getProperty ( SYSPROP_STORAGE_BASE );
final Path p = Paths.get ( base );
final FileStore store = Files.getFileStore ( p );
return (double)store.getUnallocatedSpace () / (double)store.getTotalSpace ();
}
catch ( final Exception e )
{
return null;
}
}
private long getTotalSpace(FileStore store) throws IOException {
long totalSpace = store.getTotalSpace();
if (totalSpace < 0) {
totalSpace = Long.MAX_VALUE;
}
return totalSpace;
}
public long getTotalSpace() {
long totalSpace = 0;
try {
FileStore fileStore = Files.getFileStore(Paths.get("."));
totalSpace = fileStore.getTotalSpace();
} catch (IOException ioEx) {
logger.log(Level.WARNING, "Cannot calculate free disk space", ioEx);
}
return totalSpace;
}
@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());
}
private String generate() throws IOException {
File reports = new File(Nukkit.DATA_PATH, "logs/bug_reports");
if (!reports.isDirectory()) {
reports.mkdirs();
}
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmSS");
String date = simpleDateFormat.format(new Date());
StringBuilder model = new StringBuilder();
long totalDiskSpace = 0;
int diskNum = 0;
for (Path root : FileSystems.getDefault().getRootDirectories()) {
try {
FileStore store = Files.getFileStore(root);
model.append("Disk ").append(diskNum++).append(":(avail=").append(getCount(store.getUsableSpace(), true))
.append(", total=").append(getCount(store.getTotalSpace(), true))
.append(") ");
totalDiskSpace += store.getTotalSpace();
} catch (IOException e) {
//
}
}
StringWriter stringWriter = new StringWriter();
throwable.printStackTrace(new PrintWriter(stringWriter));
StackTraceElement[] stackTrace = throwable.getStackTrace();
boolean pluginError = false;
if (stackTrace.length > 0) {
pluginError = !throwable.getStackTrace()[0].getClassName().startsWith("cn.nukkit");
}
File mdReport = new File(reports, date + "_" + throwable.getClass().getSimpleName() + ".md");
mdReport.createNewFile();
String content = Utils.readFile(this.getClass().getClassLoader().getResourceAsStream("report_template.md"));
String cpuType = System.getenv("PROCESSOR_IDENTIFIER");
OperatingSystemMXBean osMXBean = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
content = content.replace("${NUKKIT_VERSION}", Nukkit.VERSION);
content = content.replace("${JAVA_VERSION}", System.getProperty("java.vm.name") + " (" + System.getProperty("java.runtime.version") + ")");
content = content.replace("${HOSTOS}", osMXBean.getName() + "-" + osMXBean.getArch() + " [" + osMXBean.getVersion() + "]");
content = content.replace("${MEMORY}", getCount(osMXBean.getTotalPhysicalMemorySize(), true));
content = content.replace("${STORAGE_SIZE}", getCount(totalDiskSpace, true));
content = content.replace("${CPU_TYPE}", cpuType == null ? "UNKNOWN" : cpuType);
content = content.replace("${AVAILABLE_CORE}", String.valueOf(osMXBean.getAvailableProcessors()));
content = content.replace("${STACKTRACE}", stringWriter.toString());
content = content.replace("${PLUGIN_ERROR}", Boolean.toString(pluginError).toUpperCase());
content = content.replace("${STORAGE_TYPE}", model.toString());
Utils.writeFile(mdReport, content);
return mdReport.getAbsolutePath();
}
@Test
public void get_file_store_total_space () throws IOException {
FileStore store = Files.getFileStore(source);
long totalSpace = store.getTotalSpace();
assertTrue(totalSpace > 0);
}
@Test
public void get_used_space_nio () throws IOException {
FileStore store = Files.getFileStore(source);
long usedSpace = (store.getTotalSpace() -
store.getUnallocatedSpace()) / 1024;
assertTrue(usedSpace > 0);
}