Skip to content

Commit e5d0cff

Browse files
authored
Merge pull request #6955 from chrisrueger/fix-bytes-macro
Macro: fix broken ${bytes} macro
2 parents 1e59536 + 760c4d7 commit e5d0cff

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

biz.aQute.bndlib/src/aQute/bnd/osgi/Macro.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2156,26 +2156,33 @@ public String _apply(String[] args) throws Exception {
21562156
}
21572157

21582158
/**
2159-
* Format bytes
2159+
* Format bytes Expects args[0] == "bytes" (ignored), args[1...] = byte
2160+
* values as strings e.g. ${bytes;1048576;10000048576} returns 1.0 Mb 9.3 Gb
21602161
*/
21612162
public String _bytes(String[] args) {
2163+
21622164
try (Formatter sb = new Formatter()) {
2163-
for (String arg : args) {
2164-
long l = Long.parseLong(arg);
2165+
for (int idx = 1; idx < args.length; idx++) {
2166+
long l = Long.parseLong(args[idx]);
21652167
bytes(sb, l, 0, new String[] {
21662168
"b", "Kb", "Mb", "Gb", "Tb", "Pb", "Eb", "Zb", "Yb", "Bb", "Geopbyte"
21672169
});
2170+
if (idx < args.length - 1) {
2171+
sb.format(" ");
2172+
}
21682173
}
21692174
return sb.toString();
2175+
} catch (NumberFormatException e) {
2176+
return "Invalid number format";
21702177
}
21712178
}
21722179

21732180
private void bytes(Formatter sb, double l, int i, String[] strings) {
2174-
if (l > 1024 && i < strings.length - 1) {
2181+
if (l >= 1024 && i < strings.length - 1) {
21752182
bytes(sb, l / 1024, i + 1, strings);
21762183
return;
21772184
}
2178-
l = Math.round(l * 10) / 10;
2185+
l = Math.round(l * 10) / 10.0;
21792186
sb.format("%s %s", l, strings[i]);
21802187
}
21812188

0 commit comments

Comments
 (0)