diff --git a/src/main/java/org/bytedeco/javacpp/Pointer.java b/src/main/java/org/bytedeco/javacpp/Pointer.java index 5efcaf8a0..8e2fc68fe 100644 --- a/src/main/java/org/bytedeco/javacpp/Pointer.java +++ b/src/main/java/org/bytedeco/javacpp/Pointer.java @@ -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))) { @@ -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 */ @@ -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); } @@ -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); } diff --git a/src/test/java/org/bytedeco/javacpp/PointerTest.java b/src/test/java/org/bytedeco/javacpp/PointerTest.java index ad489069c..90f9037ac 100644 --- a/src/test/java/org/bytedeco/javacpp/PointerTest.java +++ b/src/test/java/org/bytedeco/javacpp/PointerTest.java @@ -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); + } + } }