Skip to content

Commit 54b78b4

Browse files
author
Ilya Ganelin
committed
Simplified byteUnit class
1 parent 69e2f20 commit 54b78b4

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

network/common/src/main/java/org/apache/spark/network/util/ByteUnit.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,18 @@ private ByteUnit(long multiplier) {
2828
this.multiplier = multiplier;
2929
}
3030

31-
public long convert(long d, ByteUnit u) { return toBytes(d) / u.multiplier; }
31+
// Interpret the provided number (d) with suffix (u) as this unit type.
32+
// E.g. KiB.interpret(1, MiB) interprets 1MiB as its KiB representation = 1024k
33+
public long interpret(long d, ByteUnit u) {
34+
return u.toBytes(d) / multiplier;
35+
}
36+
37+
// Convert the provided number (d) interpreted as this unit type to unit type (u).
38+
public long convert(long d, ByteUnit u) {
39+
return toBytes(d) / u.multiplier;
40+
}
3241

33-
public long toBytes(long d) { return multiplier * d; }
42+
public long toBytes(long d) { return x(d, multiplier); }
3443
public long toKiB(long d) { return convert(d, KiB); }
3544
public long toMiB(long d) { return convert(d, MiB); }
3645
public long toGiB(long d) { return convert(d, GiB); }
@@ -46,6 +55,7 @@ private ByteUnit(long multiplier) {
4655
* This has a short name to make above code more readable.
4756
*/
4857
static long x(long d, long m) {
58+
if (d == 0) { return 0; }
4959
long over = MAX / d;
5060
if (d > over) return Long.MAX_VALUE;
5161
if (d < -over) return Long.MIN_VALUE;

network/common/src/main/java/org/apache/spark/network/util/JavaUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ private static long parseByteString(String str, ByteUnit unit) {
223223
}
224224

225225
// If suffix is valid use that, otherwise none was provided and use the default passed
226-
return unit.convert(val, suffix != null ? byteSuffixes.get(suffix) : unit);
226+
return unit.interpret(val, suffix != null ? byteSuffixes.get(suffix) : unit);
227227
} catch (NumberFormatException e) {
228228
String timeError = "Size must be specified as bytes (b), " +
229229
"kibibytes (k), mebibytes (m), gibibytes (g), tebibytes (t), or pebibytes(p). " +

0 commit comments

Comments
 (0)