Skip to content

Commit

Permalink
* Consider Pointer values maxBytes or maxPhysicalBytes suffixe…
Browse files Browse the repository at this point in the history
…d with `%` as relative to `Runtime.maxMemory()` (pull #365)
  • Loading branch information
maxime-michel authored and saudet committed Nov 29, 2019
1 parent ecf562e commit cd09b71
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/main/java/org/bytedeco/javacpp/Pointer.java
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ public static String formatBytes(long bytes) {
}
}

public static long parseBytes(String string) throws NumberFormatException {
public static long parseBytes(String string, long relativeMultiple) throws NumberFormatException {
int i = 0;
while (i < string.length()) {
if (!Character.isDigit(string.charAt(i))) {
Expand All @@ -431,6 +431,7 @@ public static long parseBytes(String string) throws NumberFormatException {
}
long size = Long.parseLong(string.substring(0, i));
switch (string.substring(i).trim().toLowerCase()) {
case "%": size = size * relativeMultiple / 100; break;
case "t": case "tb": size *= 1024L; /* no break */
case "g": case "gb": size *= 1024L; /* no break */
case "m": case "mb": size *= 1024L; /* no break */
Expand All @@ -457,7 +458,7 @@ public static long parseBytes(String string) throws NumberFormatException {
s = System.getProperty("org.bytedeco.javacpp.maxBytes", s);
if (s != null && s.length() > 0) {
try {
m = parseBytes(s);
m = parseBytes(s, m);
} catch (NumberFormatException e) {
throw new RuntimeException(e);
}
Expand All @@ -469,7 +470,7 @@ public static long parseBytes(String string) throws NumberFormatException {
s = System.getProperty("org.bytedeco.javacpp.maxPhysicalBytes", s);
if (s != null && s.length() > 0) {
try {
m = parseBytes(s);
m = parseBytes(s, m);
} catch (NumberFormatException e) {
throw new RuntimeException(e);
}
Expand Down
15 changes: 15 additions & 0 deletions src/test/java/org/bytedeco/javacpp/PointerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1031,4 +1031,19 @@ static class TestFunction extends FunctionPointer {
}
assertTrue(floatPointer.isNull());
}

@Test public void testParseBytesWithRelativeUnits() {
System.out.println("ParseBytesWithRelativeUnits");
long arbitraryAmountOfMemory = 300000;

assertEquals(0, Pointer.parseBytes("0%", arbitraryAmountOfMemory));
assertEquals(arbitraryAmountOfMemory * 10 / 100, Pointer.parseBytes("10%", arbitraryAmountOfMemory));
try {
System.out.println("Note: NumberFormatException should get thrown here and printed below.");
Pointer.parseBytes("%", arbitraryAmountOfMemory);
fail("NumberFormatException should have been thrown.");
} catch (NumberFormatException e) {
System.out.println(e);
}
}
}

0 comments on commit cd09b71

Please sign in to comment.