diff --git a/src/main/java/com/typesafe/config/Config.java b/src/main/java/com/typesafe/config/Config.java index 3ecc0578..8a69d587 100644 --- a/src/main/java/com/typesafe/config/Config.java +++ b/src/main/java/com/typesafe/config/Config.java @@ -247,8 +247,10 @@ public final class Config { * Parses a memory-size string. If no units are specified in the string, it * is assumed to be in bytes. The returned value is in bytes. The purpose of * this function is to implement the memory-size-related methods in the - * ConfigObject interface. - * + * ConfigObject interface. The units parsed are interpreted as powers of + * two, that is, the convention for memory rather than the convention for + * disk space. + * * @param input * the string to parse * @param originForException @@ -259,7 +261,7 @@ public final class Config { * @throws ConfigException * if string is invalid */ - public static long parseMemorySize(String input, + public static long parseMemorySizeInBytes(String input, ConfigOrigin originForException, String pathForException) { String s = ConfigUtil.unicodeTrim(input); String unitStringMaybePlural = getUnits(s); diff --git a/src/main/java/com/typesafe/config/ConfigObject.java b/src/main/java/com/typesafe/config/ConfigObject.java index 2f3743d5..cd9d327f 100644 --- a/src/main/java/com/typesafe/config/ConfigObject.java +++ b/src/main/java/com/typesafe/config/ConfigObject.java @@ -68,10 +68,12 @@ public interface ConfigObject extends ConfigValue, Map<String, ConfigValue> { */ ConfigValue getValue(String path); - /** Get value as a size in bytes (parses special strings like "128M") */ - // rename getSizeInBytes ? clearer. allows a megabyte version - // or just getBytes is consistent with getMilliseconds - Long getMemorySize(String path); + /** + * Get value as a size in bytes (parses special strings like "128M"). The + * size units are interpreted as for memory, not as for disk space, so they + * are in powers of two. + */ + Long getMemorySizeInBytes(String path); /** * Get value as a duration in milliseconds. If the value is already a @@ -114,7 +116,7 @@ public interface ConfigObject extends ConfigValue, Map<String, ConfigValue> { List<? extends Object> getAnyRefList(String path); - List<Long> getMemorySizeList(String path); + List<Long> getMemorySizeInBytesList(String path); List<Long> getMillisecondsList(String path); diff --git a/src/main/java/com/typesafe/config/impl/AbstractConfigObject.java b/src/main/java/com/typesafe/config/impl/AbstractConfigObject.java index a573d8e4..90d42c30 100644 --- a/src/main/java/com/typesafe/config/impl/AbstractConfigObject.java +++ b/src/main/java/com/typesafe/config/impl/AbstractConfigObject.java @@ -414,13 +414,13 @@ abstract class AbstractConfigObject extends AbstractConfigValue implements } @Override - public Long getMemorySize(String path) { + public Long getMemorySizeInBytes(String path) { Long size = null; try { size = getLong(path); } catch (ConfigException.WrongType e) { ConfigValue v = find(path, ConfigValueType.STRING, path); - size = Config.parseMemorySize((String) v.unwrapped(), v.origin(), + size = Config.parseMemorySizeInBytes((String) v.unwrapped(), v.origin(), path); } return size; @@ -534,7 +534,7 @@ abstract class AbstractConfigObject extends AbstractConfigValue implements } @Override - public List<Long> getMemorySizeList(String path) { + public List<Long> getMemorySizeInBytesList(String path) { List<Long> l = new ArrayList<Long>(); List<? extends ConfigValue> list = getList(path); for (ConfigValue v : list) { @@ -542,7 +542,7 @@ abstract class AbstractConfigObject extends AbstractConfigValue implements l.add(((Number) v.unwrapped()).longValue()); } else if (v.valueType() == ConfigValueType.STRING) { String s = (String) v.unwrapped(); - Long n = Config.parseMemorySize(s, v.origin(), path); + Long n = Config.parseMemorySizeInBytes(s, v.origin(), path); l.add(n); } else { throw new ConfigException.WrongType(v.origin(), path, diff --git a/src/test/scala/com/typesafe/config/impl/ConfigTest.scala b/src/test/scala/com/typesafe/config/impl/ConfigTest.scala index 4d917c8e..7b6100ec 100644 --- a/src/test/scala/com/typesafe/config/impl/ConfigTest.scala +++ b/src/test/scala/com/typesafe/config/impl/ConfigTest.scala @@ -465,7 +465,7 @@ class ConfigTest extends TestUtils { } intercept[ConfigException.Null] { - conf.getMemorySize("nulls.null") + conf.getMemorySizeInBytes("nulls.null") } // should throw WrongType if key is wrong type and not convertible @@ -494,7 +494,7 @@ class ConfigTest extends TestUtils { } intercept[ConfigException.WrongType] { - conf.getMemorySize("ints") + conf.getMemorySizeInBytes("ints") } // should throw BadPath on various bad paths @@ -521,7 +521,7 @@ class ConfigTest extends TestUtils { } intercept[ConfigException.BadValue] { - conf.getMemorySize("strings.a") + conf.getMemorySizeInBytes("strings.a") } } @@ -581,11 +581,11 @@ class ConfigTest extends TestUtils { assertEquals(500L, conf.getMilliseconds("durations.halfSecond")) // should get size in bytes - assertEquals(1024 * 1024L, conf.getMemorySize("memsizes.meg")) - assertEquals(1024 * 1024L, conf.getMemorySize("memsizes.megAsNumber")) + assertEquals(1024 * 1024L, conf.getMemorySizeInBytes("memsizes.meg")) + assertEquals(1024 * 1024L, conf.getMemorySizeInBytes("memsizes.megAsNumber")) assertEquals(Seq(1024 * 1024L, 1024 * 1024L, 1024L * 1024L), - conf.getMemorySizeList("memsizes.megsList").asScala) - assertEquals(512 * 1024L, conf.getMemorySize("memsizes.halfMeg")) + conf.getMemorySizeInBytesList("memsizes.megsList").asScala) + assertEquals(512 * 1024L, conf.getMemorySizeInBytes("memsizes.halfMeg")) } @Test diff --git a/src/test/scala/com/typesafe/config/impl/UnitParserTest.scala b/src/test/scala/com/typesafe/config/impl/UnitParserTest.scala index 51d0af61..8a3f43ee 100644 --- a/src/test/scala/com/typesafe/config/impl/UnitParserTest.scala +++ b/src/test/scala/com/typesafe/config/impl/UnitParserTest.scala @@ -38,7 +38,7 @@ class UnitParserTest extends TestUtils { } @Test - def parseMemorySize() { + def parseMemorySizeInBytes() { val oneMegs = List("1048576", "1048576b", "1048576bytes", "1048576byte", "1048576 b", "1048576 bytes", " 1048576 b ", " 1048576 bytes ", @@ -47,7 +47,7 @@ class UnitParserTest extends TestUtils { "1m", "1M", "1 M", "1 megabytes", "1 megabyte", "0.0009765625g", "0.0009765625G", "0.0009765625 gigabytes", "0.0009765625 gigabyte") - def parseMem(s: String) = Config.parseMemorySize(s, fakeOrigin(), "test") + def parseMem(s: String) = Config.parseMemorySizeInBytes(s, fakeOrigin(), "test") for (s <- oneMegs) { val result = parseMem(s) @@ -61,13 +61,13 @@ class UnitParserTest extends TestUtils { // bad units val e = intercept[ConfigException.BadValue] { - Config.parseMemorySize("100 dollars", fakeOrigin(), "test") + Config.parseMemorySizeInBytes("100 dollars", fakeOrigin(), "test") } assertTrue(e.getMessage().contains("size unit")) // bad number val e2 = intercept[ConfigException.BadValue] { - Config.parseMemorySize("1 00 bytes", fakeOrigin(), "test") + Config.parseMemorySizeInBytes("1 00 bytes", fakeOrigin(), "test") } assertTrue(e2.getMessage().contains("size number")) }