Skip to content

Commit cd09b71

Browse files
maxime-michelsaudet
authored andcommitted
* Consider Pointer values maxBytes or maxPhysicalBytes suffixed with % as relative to Runtime.maxMemory() (pull #365)
1 parent ecf562e commit cd09b71

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

src/main/java/org/bytedeco/javacpp/Pointer.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ public static String formatBytes(long bytes) {
421421
}
422422
}
423423

424-
public static long parseBytes(String string) throws NumberFormatException {
424+
public static long parseBytes(String string, long relativeMultiple) throws NumberFormatException {
425425
int i = 0;
426426
while (i < string.length()) {
427427
if (!Character.isDigit(string.charAt(i))) {
@@ -431,6 +431,7 @@ public static long parseBytes(String string) throws NumberFormatException {
431431
}
432432
long size = Long.parseLong(string.substring(0, i));
433433
switch (string.substring(i).trim().toLowerCase()) {
434+
case "%": size = size * relativeMultiple / 100; break;
434435
case "t": case "tb": size *= 1024L; /* no break */
435436
case "g": case "gb": size *= 1024L; /* no break */
436437
case "m": case "mb": size *= 1024L; /* no break */
@@ -457,7 +458,7 @@ public static long parseBytes(String string) throws NumberFormatException {
457458
s = System.getProperty("org.bytedeco.javacpp.maxBytes", s);
458459
if (s != null && s.length() > 0) {
459460
try {
460-
m = parseBytes(s);
461+
m = parseBytes(s, m);
461462
} catch (NumberFormatException e) {
462463
throw new RuntimeException(e);
463464
}
@@ -469,7 +470,7 @@ public static long parseBytes(String string) throws NumberFormatException {
469470
s = System.getProperty("org.bytedeco.javacpp.maxPhysicalBytes", s);
470471
if (s != null && s.length() > 0) {
471472
try {
472-
m = parseBytes(s);
473+
m = parseBytes(s, m);
473474
} catch (NumberFormatException e) {
474475
throw new RuntimeException(e);
475476
}

src/test/java/org/bytedeco/javacpp/PointerTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,4 +1031,19 @@ static class TestFunction extends FunctionPointer {
10311031
}
10321032
assertTrue(floatPointer.isNull());
10331033
}
1034+
1035+
@Test public void testParseBytesWithRelativeUnits() {
1036+
System.out.println("ParseBytesWithRelativeUnits");
1037+
long arbitraryAmountOfMemory = 300000;
1038+
1039+
assertEquals(0, Pointer.parseBytes("0%", arbitraryAmountOfMemory));
1040+
assertEquals(arbitraryAmountOfMemory * 10 / 100, Pointer.parseBytes("10%", arbitraryAmountOfMemory));
1041+
try {
1042+
System.out.println("Note: NumberFormatException should get thrown here and printed below.");
1043+
Pointer.parseBytes("%", arbitraryAmountOfMemory);
1044+
fail("NumberFormatException should have been thrown.");
1045+
} catch (NumberFormatException e) {
1046+
System.out.println(e);
1047+
}
1048+
}
10341049
}

0 commit comments

Comments
 (0)