Here's your problem:
if (bytes < unit) return bytes + " B";
When bytes
equals -1 (which is less than unit
in either case), it returns -1 B
without ever getting to the line hr = hr.replace("-1 B", "n.a.");
.
It would be better to have one return
statement at the end, assign String hr = bytes + " B"
in the if
, and add an else
block around the next three lines. Then after that block, the hr.replace()
call will execute either way, and then return the value.