On Thu, 15 Sep 2022 07:22:18 GMT, Abhishek Kumar <[email protected]> wrote:
> > So leaving aside what the native chooser does, we then just have to be
> > consistent and round up.
> > That's what the current version is trying to do isn't it ?
> > Thoughts ?
>
> Yeah, as per the current fix we are doing consistently round up for all file
> sizes.
Nearly.
It works correctly for KB:
0 0.0 KB
1 0.1 KB
99 0.1 KB
100 0.1 KB
101 0.2 KB
900 0.9 KB
901 1.0 KB
999,000 999.0 KB
999,001 999.1 KB
999,900 999.9 KB
999,901 1.0 MB
But the rounding is not right as soon as the size is larger than 1 MB:
1,000,000 1.0 MB
1,000,001 1.0 MB
1,000,999 1.0 MB
1,001,000 1.1 MB
The kilobytes are ignored. The displayed size must be 1.1 MB for all the files
but the first one.
And this is due to flooring the values by casting a double to long. The cast
should be replaced with `Math.ceil`. The following patch resolves the problem:
diff --git a/src/java.desktop/share/classes/sun/swing/FilePane.java
b/src/java.desktop/share/classes/sun/swing/FilePane.java
index 91935f0ed00..acc7dad6927 100644
--- a/src/java.desktop/share/classes/sun/swing/FilePane.java
+++ b/src/java.desktop/share/classes/sun/swing/FilePane.java
@@ -1194,7 +1194,7 @@ public class FilePane extends JPanel implements
PropertyChangeListener {
Icon icon = chooser.getIcon(file);
setIcon(icon);
- } else if (value instanceof Long len) {
+ } else if (value instanceof final Long len) {
/*
* This code block is relevant to Linux.
* File size is displayed up to 1 decimal precision.
@@ -1211,19 +1211,17 @@ public class FilePane extends JPanel implements
PropertyChangeListener {
displayedFileSize[0] = roundToOneDecimalPlace(len);
} else {
double kbVal = roundToOneDecimalPlace(len);
- len = (long) kbVal;
if (kbVal < baseFileSize) {
updateMessageFormatPattern(kiloByteString);
displayedFileSize[0] = kbVal;
} else {
- double mbVal = roundToOneDecimalPlace(len);
- len = (long) mbVal;
+ double mbVal =
roundToOneDecimalPlace(Math.ceil(kbVal));
if (mbVal < baseFileSize) {
updateMessageFormatPattern(megaByteString);
displayedFileSize[0] = mbVal;
} else {
updateMessageFormatPattern(gigaByteString);
- displayedFileSize[0] = roundToOneDecimalPlace(len);
+ displayedFileSize[0] =
roundToOneDecimalPlace(Math.ceil(mbVal));
}
}
}
@@ -1254,7 +1252,7 @@ public class FilePane extends JPanel implements
PropertyChangeListener {
* @param fileSize the file size to round to one decimal place
* @return file size rounded to one decimal place
*/
- private static double roundToOneDecimalPlace(long fileSize) {
+ private static double roundToOneDecimalPlace(double fileSize) {
return Math.ceil(fileSize / 100.0d) / 10.0d;
}
}
When the above patch is applied to the latest code in the PR, it produces the
correct result for MBs as shown above as well as for GBs:
1,000,000,000 1.0 GB
1,000,000,001 1.1 GB
-------------
PR: https://git.openjdk.org/jdk/pull/9327