diff --git a/src/resources/server_info/ConfigData.java b/src/resources/server_info/ConfigData.java index 6a2cc2fce..c699c2e1c 100644 --- a/src/resources/server_info/ConfigData.java +++ b/src/resources/server_info/ConfigData.java @@ -55,15 +55,21 @@ class ConfigData { } public boolean containsKey(String key) { - return data.containsKey(key); + synchronized (data) { + return data.containsKey(key); + } } public String get(String key) { - return data.get(key); + synchronized (data) { + return data.get(key); + } } public String put(String key, String value) { - return data.put(key, value); + synchronized (data) { + return data.put(key, value); + } } /** @@ -74,54 +80,57 @@ class ConfigData { Map delta = new HashMap<>(); BufferedReader reader = null; - delta.putAll(data); // Copy the current data - - try { - reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8)); - String line = reader.readLine(); - while (line != null) { - loadLine(line); - line = reader.readLine(); - } - } catch (IOException e) { - e.printStackTrace(); - return null; - } finally { - if (reader != null) { - try { - reader.close(); - } catch (IOException e) { - return null; + synchronized (data) { + delta.putAll(data); // Copy the current data + + try { + reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8)); + String line = reader.readLine(); + while (line != null) { + loadLine(line); + line = reader.readLine(); + } + } catch (IOException e) { + e.printStackTrace(); + return null; + } finally { + if (reader != null) { + try { + reader.close(); + } catch (IOException e) { + return null; + } } } + for(Entry entry : data.entrySet()) + delta.remove(entry.getKey(), entry.getValue()); } - for(Entry entry : data.entrySet()) - delta.remove(entry.getKey(), entry.getValue()); - return delta; } public boolean save() { BufferedWriter writer = null; - try { - writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8)); - writer.write("# "+FORMAT.format(System.currentTimeMillis())); - writer.newLine(); - for (Entry e : data.entrySet()) { - writer.write(e.getKey() + "=" + e.getValue()); + synchronized (data) { + try { + writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8)); + writer.write("# "+FORMAT.format(System.currentTimeMillis())); writer.newLine(); - } - return true; - } catch (IOException e) { - e.printStackTrace(); - return false; - } finally { - if (writer != null) { - try { - writer.close(); - } catch (IOException e) { - + for (Entry e : data.entrySet()) { + writer.write(e.getKey() + "=" + e.getValue()); + writer.newLine(); + } + return true; + } catch (IOException e) { + e.printStackTrace(); + return false; + } finally { + if (writer != null) { + try { + writer.close(); + } catch (IOException e) { + + } } } } @@ -135,7 +144,9 @@ class ConfigData { return; String key = beforeComment.substring(0, beforeComment.indexOf('=')); String val = beforeComment.substring(key.length()+1); - data.put(key, val); + synchronized (data) { + data.put(key, val); + } } }