Bias pair parsing toward key in digest.txt.

In that file we have:

filename = hex_encoded_hash

The hex_encoded_hash will never contain = but the filename could
conceivably contain =, so for that file we split on the last = instead
of the first.

Everywhere else we continue to split on the first = because that makes
more sense.
This commit is contained in:
Michael Bayne
2017-02-02 19:24:16 -08:00
parent 173986c8be
commit a5ad6edf3c
4 changed files with 28 additions and 20 deletions
@@ -765,7 +765,7 @@ public class Application
File pairFile = getLocalPath(pairLocation); File pairFile = getLocalPath(pairLocation);
if (pairFile.exists()) { if (pairFile.exists()) {
try { try {
List<String[]> args = ConfigUtil.parsePairs(pairFile, false); List<String[]> args = ConfigUtil.parsePairs(pairFile, false, false);
for (String[] pair : args) { for (String[] pair : args) {
if (pair[1].length() == 0) { if (pair[1].length() == 0) {
collector.add(pair[0]); collector.add(pair[0]);
@@ -1231,7 +1231,7 @@ public class Application
try { try {
in = ConnectionUtil.open(_latest).getInputStream(); in = ConnectionUtil.open(_latest).getInputStream();
BufferedReader bin = new BufferedReader(new InputStreamReader(in)); BufferedReader bin = new BufferedReader(new InputStreamReader(in));
for (String[] pair : ConfigUtil.parsePairs(bin, false)) { for (String[] pair : ConfigUtil.parsePairs(bin, false, false)) {
if (pair[0].equals("version")) { if (pair[0].equals("version")) {
_targetVersion = Math.max(Long.parseLong(pair[1]), _targetVersion); _targetVersion = Math.max(Long.parseLong(pair[1]), _targetVersion);
if (fileVersion != -1 && _targetVersion > fileVersion) { if (fileVersion != -1 && _targetVersion > fileVersion) {
@@ -113,7 +113,9 @@ public class Digest
String filename = digestFile(version); String filename = digestFile(version);
StringBuilder data = new StringBuilder(); StringBuilder data = new StringBuilder();
File dfile = new File(appdir, filename); File dfile = new File(appdir, filename);
for (String[] pair : ConfigUtil.parsePairs(dfile, false)) { // bias = toward key: the key is the filename and could conceivably contain = signs, value
// is the hex encoded hash which will not contain =
for (String[] pair : ConfigUtil.parsePairs(dfile, false, true)) {
if (pair[0].equals(filename)) { if (pair[0].equals(filename)) {
_metaDigest = pair[1]; _metaDigest = pair[1];
break; break;
@@ -32,28 +32,32 @@ public class ConfigUtil
* *
* @param checkPlatform if true, platform qualifiers will be used to filter out pairs that do * @param checkPlatform if true, platform qualifiers will be used to filter out pairs that do
* not match the current platform; if false, all pairs will be returned. * not match the current platform; if false, all pairs will be returned.
* @param biasToKey if true the splitting on {@code =} will be key-biased meaning we split on
* the last {@code =} in the string (extra equals go in the key). If false, we bias toward the
* value: split on the first equals, put extra equals in the value.
* *
* @return a list of <code>String[]</code> instances containing the key/value pairs in the * @return a list of <code>String[]</code> instances containing the key/value pairs in the
* order they were parsed from the file. * order they were parsed from the file.
*/ */
public static List<String[]> parsePairs (File config, boolean checkPlatform) public static List<String[]> parsePairs (File config, boolean checkPlatform, boolean biasToKey)
throws IOException throws IOException
{ {
// annoyingly FileReader does not allow encoding to be specified (uses platform default) // annoyingly FileReader does not allow encoding to be specified (uses platform default)
return parsePairs( InputStreamReader input = new InputStreamReader(new FileInputStream(config), "UTF-8");
new InputStreamReader(new FileInputStream(config), "UTF-8"), checkPlatform); return parsePairs(input, checkPlatform, biasToKey);
} }
/** /**
* See {@link #parsePairs(File,boolean)}. * See {@link #parsePairs(File,boolean)}.
*/ */
public static List<String[]> parsePairs (Reader config, boolean checkPlatform) public static List<String[]> parsePairs (Reader config, boolean checkPlatform,
throws IOException boolean biasToKey) throws IOException
{ {
return parsePairs( return parsePairs(
config, config,
checkPlatform ? StringUtil.deNull(System.getProperty("os.name")).toLowerCase() : null, checkPlatform ? StringUtil.deNull(System.getProperty("os.name")).toLowerCase() : null,
checkPlatform ? StringUtil.deNull(System.getProperty("os.arch")).toLowerCase() : null); checkPlatform ? StringUtil.deNull(System.getProperty("os.arch")).toLowerCase() : null,
biasToKey);
} }
/** /**
@@ -71,7 +75,7 @@ public class ConfigUtil
// I thought that we could use HashMap<String, String[]> and put new String[] {pair[1]} for // I thought that we could use HashMap<String, String[]> and put new String[] {pair[1]} for
// the null case, but it mysteriously dies on launch, so leaving it as HashMap<String, // the null case, but it mysteriously dies on launch, so leaving it as HashMap<String,
// Object> for now // Object> for now
for (String[] pair : parsePairs(config, checkPlatform)) { for (String[] pair : parsePairs(config, checkPlatform, false)) {
Object value = data.get(pair[0]); Object value = data.get(pair[0]);
if (value == null) { if (value == null) {
data.put(pair[0], pair[1]); data.put(pair[0], pair[1]);
@@ -104,7 +108,8 @@ public class ConfigUtil
} }
/** A helper function for {@link #parsePairs(Reader,boolean}. */ /** A helper function for {@link #parsePairs(Reader,boolean}. */
protected static List<String[]> parsePairs (Reader config, String osname, String osarch) protected static List<String[]> parsePairs (Reader config, String osname, String osarch,
boolean biasToKey)
throws IOException throws IOException
{ {
List<String[]> pairs = new ArrayList<String[]>(); List<String[]> pairs = new ArrayList<String[]>();
@@ -123,7 +128,8 @@ public class ConfigUtil
// parse our key/value pair // parse our key/value pair
String[] pair = new String[2]; String[] pair = new String[2];
int eidx = line.indexOf("="); // if we're biasing toward key, put all the extra = in the key rather than the value
int eidx = biasToKey ? line.lastIndexOf("=") : line.indexOf("=");
if (eidx != -1) { if (eidx != -1) {
pair[0] = line.substring(0, eidx).trim(); pair[0] = line.substring(0, eidx).trim();
pair[1] = line.substring(eidx+1).trim(); pair[1] = line.substring(eidx+1).trim();
@@ -38,7 +38,7 @@ public class ConfigUtilTest
@Test public void testSimplePairs () throws IOException @Test public void testSimplePairs () throws IOException
{ {
List<String[]> pairs = ConfigUtil.parsePairs(toReader(SIMPLE_PAIRS), true); List<String[]> pairs = ConfigUtil.parsePairs(toReader(SIMPLE_PAIRS), true, false);
for (int ii = 0; ii < SIMPLE_PAIRS.length; ii++) { for (int ii = 0; ii < SIMPLE_PAIRS.length; ii++) {
assertEquals(SIMPLE_PAIRS[ii].key, pairs.get(ii)[0]); assertEquals(SIMPLE_PAIRS[ii].key, pairs.get(ii)[0]);
assertEquals(SIMPLE_PAIRS[ii].value, pairs.get(ii)[1]); assertEquals(SIMPLE_PAIRS[ii].value, pairs.get(ii)[1]);
@@ -57,7 +57,7 @@ public class ConfigUtilTest
Pair notWin = new Pair("fifteen", "[!windows] sixteen"); Pair notWin = new Pair("fifteen", "[!windows] sixteen");
Pair[] pairs = { linux, mac, linuxAndMac, linux64, linux64s, mac64, win64, notWin }; Pair[] pairs = { linux, mac, linuxAndMac, linux64, linux64s, mac64, win64, notWin };
List<String[]> parsed = ConfigUtil.parsePairs(toReader(pairs), "linux", "i386"); List<String[]> parsed = ConfigUtil.parsePairs(toReader(pairs), "linux", "i386", false);
assertTrue(exists(parsed, linux.key)); assertTrue(exists(parsed, linux.key));
assertTrue(!exists(parsed, mac.key)); assertTrue(!exists(parsed, mac.key));
assertTrue(exists(parsed, linuxAndMac.key)); assertTrue(exists(parsed, linuxAndMac.key));
@@ -67,7 +67,7 @@ public class ConfigUtilTest
assertTrue(!exists(parsed, win64.key)); assertTrue(!exists(parsed, win64.key));
assertTrue(exists(parsed, notWin.key)); assertTrue(exists(parsed, notWin.key));
parsed = ConfigUtil.parsePairs(toReader(pairs), "linux", "x86_64"); parsed = ConfigUtil.parsePairs(toReader(pairs), "linux", "x86_64", false);
assertTrue(exists(parsed, linux.key)); assertTrue(exists(parsed, linux.key));
assertTrue(!exists(parsed, mac.key)); assertTrue(!exists(parsed, mac.key));
assertTrue(exists(parsed, linuxAndMac.key)); assertTrue(exists(parsed, linuxAndMac.key));
@@ -77,7 +77,7 @@ public class ConfigUtilTest
assertTrue(!exists(parsed, win64.key)); assertTrue(!exists(parsed, win64.key));
assertTrue(exists(parsed, notWin.key)); assertTrue(exists(parsed, notWin.key));
parsed = ConfigUtil.parsePairs(toReader(pairs), "linux", "amd64"); parsed = ConfigUtil.parsePairs(toReader(pairs), "linux", "amd64", false);
assertTrue(exists(parsed, linux.key)); assertTrue(exists(parsed, linux.key));
assertTrue(!exists(parsed, mac.key)); assertTrue(!exists(parsed, mac.key));
assertTrue(exists(parsed, linuxAndMac.key)); assertTrue(exists(parsed, linuxAndMac.key));
@@ -87,7 +87,7 @@ public class ConfigUtilTest
assertTrue(!exists(parsed, win64.key)); assertTrue(!exists(parsed, win64.key));
assertTrue(exists(parsed, notWin.key)); assertTrue(exists(parsed, notWin.key));
parsed = ConfigUtil.parsePairs(toReader(pairs), "mac os x", "x86_64"); parsed = ConfigUtil.parsePairs(toReader(pairs), "mac os x", "x86_64", false);
assertTrue(!exists(parsed, linux.key)); assertTrue(!exists(parsed, linux.key));
assertTrue(exists(parsed, mac.key)); assertTrue(exists(parsed, mac.key));
assertTrue(exists(parsed, linuxAndMac.key)); assertTrue(exists(parsed, linuxAndMac.key));
@@ -97,7 +97,7 @@ public class ConfigUtilTest
assertTrue(!exists(parsed, win64.key)); assertTrue(!exists(parsed, win64.key));
assertTrue(exists(parsed, notWin.key)); assertTrue(exists(parsed, notWin.key));
parsed = ConfigUtil.parsePairs(toReader(pairs), "windows", "i386"); parsed = ConfigUtil.parsePairs(toReader(pairs), "windows", "i386", false);
assertTrue(!exists(parsed, linux.key)); assertTrue(!exists(parsed, linux.key));
assertTrue(!exists(parsed, mac.key)); assertTrue(!exists(parsed, mac.key));
assertTrue(!exists(parsed, linuxAndMac.key)); assertTrue(!exists(parsed, linuxAndMac.key));
@@ -107,7 +107,7 @@ public class ConfigUtilTest
assertTrue(!exists(parsed, win64.key)); assertTrue(!exists(parsed, win64.key));
assertTrue(!exists(parsed, notWin.key)); assertTrue(!exists(parsed, notWin.key));
parsed = ConfigUtil.parsePairs(toReader(pairs), "windows", "x86_64"); parsed = ConfigUtil.parsePairs(toReader(pairs), "windows", "x86_64", false);
assertTrue(!exists(parsed, linux.key)); assertTrue(!exists(parsed, linux.key));
assertTrue(!exists(parsed, mac.key)); assertTrue(!exists(parsed, mac.key));
assertTrue(!exists(parsed, linuxAndMac.key)); assertTrue(!exists(parsed, linuxAndMac.key));
@@ -117,7 +117,7 @@ public class ConfigUtilTest
assertTrue(exists(parsed, win64.key)); assertTrue(exists(parsed, win64.key));
assertTrue(!exists(parsed, notWin.key)); assertTrue(!exists(parsed, notWin.key));
parsed = ConfigUtil.parsePairs(toReader(pairs), "windows", "amd64"); parsed = ConfigUtil.parsePairs(toReader(pairs), "windows", "amd64", false);
assertTrue(!exists(parsed, linux.key)); assertTrue(!exists(parsed, linux.key));
assertTrue(!exists(parsed, mac.key)); assertTrue(!exists(parsed, mac.key));
assertTrue(!exists(parsed, linuxAndMac.key)); assertTrue(!exists(parsed, linuxAndMac.key));