From 86894f242b4b1358b3f8aea19f2b41cd286f3750 Mon Sep 17 00:00:00 2001 From: Zlatin Balevsky Date: Sat, 2 Nov 2019 14:43:24 +0000 Subject: [PATCH] support UTF-8 in persona names --- core/src/main/groovy/com/muwire/core/Core.groovy | 13 +++++++++---- .../groovy/com/muwire/core/MuWireSettings.groovy | 4 ++-- core/src/main/groovy/com/muwire/core/Name.groovy | 5 +++-- .../com/muwire/core/files/DirectoryWatcher.groovy | 4 ++-- .../com/muwire/gui/ContentPanelController.groovy | 5 +---- .../com/muwire/gui/MainFrameController.groovy | 5 +---- .../com/muwire/gui/OptionsController.groovy | 5 +---- gui/griffon-app/lifecycle/Ready.groovy | 8 ++++---- 8 files changed, 23 insertions(+), 26 deletions(-) diff --git a/core/src/main/groovy/com/muwire/core/Core.groovy b/core/src/main/groovy/com/muwire/core/Core.groovy index 3cde9374..06addcc6 100644 --- a/core/src/main/groovy/com/muwire/core/Core.groovy +++ b/core/src/main/groovy/com/muwire/core/Core.groovy @@ -191,8 +191,9 @@ public class Core { def baos = new ByteArrayOutputStream() def daos = new DataOutputStream(baos) daos.write(Constants.PERSONA_VERSION) - daos.writeShort((short)props.getNickname().length()) - daos.write(props.getNickname().getBytes(StandardCharsets.UTF_8)) + byte [] name = props.getNickname().getBytes(StandardCharsets.UTF_8) + daos.writeShort((short)name.length) + daos.write(name) destination.writeBytes(daos) daos.flush() byte [] payload = baos.toByteArray() @@ -335,8 +336,7 @@ public class Core { return } log.info("saving settings") - File f = new File(home, "MuWire.properties") - f.withOutputStream { muOptions.write(it) } + saveMuSettings() log.info("shutting down trust subscriber") trustSubscriber.stop() log.info("shutting down download manageer") @@ -357,6 +357,11 @@ public class Core { } log.info("shutdown complete") } + + public void saveMuSettings() { + File f = new File(home, "MuWire.properties") + f.withPrintWriter("UTF-8", { muOptions.write(it) }) + } static main(args) { def home = System.getProperty("user.home") + File.separator + ".MuWire" diff --git a/core/src/main/groovy/com/muwire/core/MuWireSettings.groovy b/core/src/main/groovy/com/muwire/core/MuWireSettings.groovy index f8811f35..b154f17b 100644 --- a/core/src/main/groovy/com/muwire/core/MuWireSettings.groovy +++ b/core/src/main/groovy/com/muwire/core/MuWireSettings.groovy @@ -93,7 +93,7 @@ class MuWireSettings { } - void write(OutputStream out) throws IOException { + void write(Writer out) throws IOException { Properties props = new Properties() props.setProperty("leaf", isLeaf.toString()) props.setProperty("allowUntrusted", allowUntrusted.toString()) @@ -137,7 +137,7 @@ class MuWireSettings { props.setProperty("trustSubscriptions", encoded) } - props.store(out, "") + props.store(out, "This file is UTF-8") } private static Set readEncodedSet(Properties props, String property) { diff --git a/core/src/main/groovy/com/muwire/core/Name.groovy b/core/src/main/groovy/com/muwire/core/Name.groovy index fb8e0e94..61d5c6a1 100644 --- a/core/src/main/groovy/com/muwire/core/Name.groovy +++ b/core/src/main/groovy/com/muwire/core/Name.groovy @@ -22,8 +22,9 @@ public class Name { public void write(OutputStream out) throws IOException { DataOutputStream dos = new DataOutputStream(out) - dos.writeShort(name.length()) - dos.write(name.getBytes(StandardCharsets.UTF_8)) + byte [] bytes = name.getBytes(StandardCharsets.UTF_8) + dos.writeShort(bytes.length) + dos.write(bytes) } public getName() { diff --git a/core/src/main/groovy/com/muwire/core/files/DirectoryWatcher.groovy b/core/src/main/groovy/com/muwire/core/files/DirectoryWatcher.groovy index fb4efeb3..b5d1e5db 100644 --- a/core/src/main/groovy/com/muwire/core/files/DirectoryWatcher.groovy +++ b/core/src/main/groovy/com/muwire/core/files/DirectoryWatcher.groovy @@ -86,9 +86,9 @@ class DirectoryWatcher { private void saveMuSettings() { File muSettingsFile = new File(home, "MuWire.properties") - muSettingsFile.withOutputStream { + muSettingsFile.withPrintWriter("UTF-8", { muOptions.write(it) - } + }) } private void watch() { diff --git a/gui/griffon-app/controllers/com/muwire/gui/ContentPanelController.groovy b/gui/griffon-app/controllers/com/muwire/gui/ContentPanelController.groovy index ab1f800d..2b5ff9d8 100644 --- a/gui/griffon-app/controllers/com/muwire/gui/ContentPanelController.groovy +++ b/gui/griffon-app/controllers/com/muwire/gui/ContentPanelController.groovy @@ -97,9 +97,6 @@ class ContentPanelController { } void saveMuWireSettings() { - File f = new File(core.home, "MuWire.properties") - f.withOutputStream { - core.muOptions.write(it) - } + core.saveMuSettings() } } \ No newline at end of file diff --git a/gui/griffon-app/controllers/com/muwire/gui/MainFrameController.groovy b/gui/griffon-app/controllers/com/muwire/gui/MainFrameController.groovy index b929e79a..5dd8da2f 100644 --- a/gui/griffon-app/controllers/com/muwire/gui/MainFrameController.groovy +++ b/gui/griffon-app/controllers/com/muwire/gui/MainFrameController.groovy @@ -299,10 +299,7 @@ class MainFrameController { } void saveMuWireSettings() { - File f = new File(core.home, "MuWire.properties") - f.withOutputStream { - core.muOptions.write(it) - } + core.saveMuSettings() } void mvcGroupInit(Map args) { diff --git a/gui/griffon-app/controllers/com/muwire/gui/OptionsController.groovy b/gui/griffon-app/controllers/com/muwire/gui/OptionsController.groovy index 52d2a6f0..2ac986cb 100644 --- a/gui/griffon-app/controllers/com/muwire/gui/OptionsController.groovy +++ b/gui/griffon-app/controllers/com/muwire/gui/OptionsController.groovy @@ -137,10 +137,7 @@ class OptionsController { model.trustListInterval = trustListInterval settings.trustListInterval = Integer.parseInt(trustListInterval) - File settingsFile = new File(core.home, "MuWire.properties") - settingsFile.withOutputStream { - settings.write(it) - } + core.saveMuSettings() // UI Setttings diff --git a/gui/griffon-app/lifecycle/Ready.groovy b/gui/griffon-app/lifecycle/Ready.groovy index b069ab9c..e205d6f5 100644 --- a/gui/griffon-app/lifecycle/Ready.groovy +++ b/gui/griffon-app/lifecycle/Ready.groovy @@ -41,9 +41,9 @@ class Ready extends AbstractLifecycleHandler { def propsFile = new File(home, "MuWire.properties") if (propsFile.exists()) { log.info("loading existing props file") - propsFile.withInputStream { + propsFile.withReader("UTF-8", { props.load(it) - } + }) props = new MuWireSettings(props) if (props.incompleteLocation == null) props.incompleteLocation = new File(home, "incompletes") @@ -90,9 +90,9 @@ class Ready extends AbstractLifecycleHandler { props.downloadLocation = chooser.getSelectedFile() } - propsFile.withOutputStream { + propsFile.withPrintWriter("UTF-8", { props.write(it) - } + }) } Core core