private synchronized void updateVolumeInfo() {
// No need to waste precious cycles if status bar is not visible
if(!isVisible())
return;
final AbstractFile currentFolder = mainFrame.getActivePanel().getCurrentFolder();
// Resolve the current folder's volume and use its path as a key for the volume info cache
final String volumePath = currentFolder.exists() ?
currentFolder.getVolume().getAbsolutePath(true) : "";
Long cachedVolumeInfo[] = volumeInfoCache.get(volumePath);
if(cachedVolumeInfo!=null) {
LOGGER.debug("Cache hit!");
volumeSpaceLabel.setVolumeSpace(cachedVolumeInfo[0], cachedVolumeInfo[1]);
}
else {
// Retrieves free and total volume space.
// Perform volume info retrieval in a separate thread as this method may be called
// by the event thread and it can take a while, we want to return as soon as possible
new Thread("StatusBar.updateVolumeInfo") {
@Override
public void run() {
// Free space on current volume, -1 if this information is not available
long volumeFree;
// Total space on current volume, -1 if this information is not available
long volumeTotal;
// Folder is a local file and Java version is 1.5: call getVolumeInfo() instead of
// separate calls to getFreeSpace() and getTotalSpace() as it is twice as fast.
if(currentFolder instanceof LocalFile && JavaVersion.JAVA_1_5.isCurrentOrLower()) {
try {
long volumeInfo[] = ((LocalFile)currentFolder).getVolumeInfo();
volumeTotal = volumeInfo[0];
volumeFree = volumeInfo[1];
}
catch(IOException e) {
volumeTotal = -1;
volumeFree = -1;
}
}
// Java 1.6 and up or any other file type
else {
try { volumeFree = currentFolder.getFreeSpace(); }
catch(IOException e) { volumeFree = -1; }
try { volumeTotal = currentFolder.getTotalSpace(); }
catch(IOException e) { volumeTotal = -1; }
}
// For testing the free space indicator
//volumeFree = (long)(volumeTotal * Math.random());