RFR: 8288882: JFileChooser - empty (0 bytes) file is displayed as 1 KB [v13]
Andy Goryachev
angorya at openjdk.org
Mon Aug 1 16:40:04 UTC 2022
On Thu, 28 Jul 2022 16:19:50 GMT, Abhishek Kumar <duke at openjdk.org> wrote:
>> src/java.desktop/share/classes/sun/swing/FilePane.java line 1205:
>>
>>> 1203: } else {
>>> 1204: double kbVal = formatToDoubleValue(len);
>>> 1205: len = (long)kbVal;
>>
>> This code still looks sus to me - there is no need to assign len (it goes out of scope pretty quickly) and formatToDoubleValue() should just format and return a formatted String.
>
> As the returned value has been used in comparison with 1000.0 , so returning a double value from formatToDoubleValue().
Perhaps we should mention in the comments that this code block is only relevant for ubuntu, and we'll be using base 1000 for the formatting.
The following test produces the same formatted values as ubuntu:
package goryachev.test;
import java.text.DecimalFormat;
import java.text.MessageFormat;
public class TestFileChooser {
private boolean listViewWindowsStyle;
double baseFileSize = 1000.0;
String kiloByteString = "{0} KB";
String megaByteString = "{0} MB";
String gigaByteString = "{0} GB";
private static final long MEBI = 1_000_000L;
private static final long MEGA = 1024L * 1024L;
private static final long GIBI = 1_000_000_000L;
public static void main(String[] args) {
t(0, "0 KB");
t(1, "1 KB");
t(999, "1 KB");
t(1000, "1 KB");
t(1001, "1 KB");
t(1023, "1 KB");
t(1024, "1 KB");
t(1025, "1 KB");
t(1026, "1 KB");
t(1449, "1.4 KB");
t(1450, "1.4 KB");
t(1451, "1.5 KB");
t(1500-1, "1.5 KB");
t(1500, "1.5 KB");
t(1500+1, "1.5 KB");
t(MEBI-1, "1 MB");
t(MEBI, "1 MB");
t(MEBI+1, "1 MB");
t(MEGA-1, "1 MB");
t(MEGA, "1 MB");
t(MEGA+1, "1 MB");
t(GIBI-1, "1 GB");
t(GIBI, "1 GB");
t(GIBI+1, "1 GB");
t(GIBI + 500_000_000L, "1.5 GB");
t(GIBI + 600_000_000L, "1.6 GB");
System.err.println("OK");
}
public static void t(long len, String expected) {
String res = new TestFileChooser().format(len);
if (!expected.equals(res)) {
System.err.println("expected=" + expected + " got=" + res + " for len=" + len);
}
}
public String format(long len) {
String text;
if (listViewWindowsStyle) {
if (len == 0) {
text = MessageFormat.format(kiloByteString, len);
} else {
len /= 1000L;
text = MessageFormat.format(kiloByteString, len + 1);
}
} else if (len < 1000L) {
text = MessageFormat.format(kiloByteString, (len==0 ? 0L : 1L));
} else {
double kbVal = formatToDoubleValue(len);
len = (long)kbVal;
if (kbVal < baseFileSize) {
text = MessageFormat.format(kiloByteString, kbVal);
} else {
double mbVal = formatToDoubleValue(len);
len = (long)mbVal;
if (mbVal < baseFileSize) {
text = MessageFormat.format(megaByteString, mbVal);
} else {
double gbVal = formatToDoubleValue(len);
text = MessageFormat.format(gigaByteString, gbVal);
}
}
}
return text;
}
public double formatToDoubleValue(long len) {
DecimalFormat df = new DecimalFormat("0.0");
double val = len/baseFileSize;
return Double.valueOf(df.format(val));
}
}
>> src/java.desktop/share/classes/sun/swing/FilePane.java line 1215:
>>
>>> 1213: } else {
>>> 1214: double gbVal = formatToDoubleValue(len);
>>> 1215: text = MessageFormat.format(gigaByteString, gbVal);
>>
>> should we also handle "{0} TB" cases?
>
> As discussed in meeting , Phil has suggested Files having "TB" sizes won't be handled now.
ok
-------------
PR: https://git.openjdk.org/jdk/pull/9327
More information about the client-libs-dev
mailing list