X-Git-Url: https://git.nmode.ca/signal-cli/blobdiff_plain/32818a8608f5bddc46ad5c7dc442f509c939791c..dc8abd0032b712320a66305ffc1e8429f491038a:/src/main/java/org/asamk/signal/App.java diff --git a/src/main/java/org/asamk/signal/App.java b/src/main/java/org/asamk/signal/App.java index 720a5849..03bb6fcb 100644 --- a/src/main/java/org/asamk/signal/App.java +++ b/src/main/java/org/asamk/signal/App.java @@ -32,8 +32,11 @@ import org.freedesktop.dbus.exceptions.DBusExecutionException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.io.BufferedWriter; import java.io.File; import java.io.IOException; +import java.io.OutputStreamWriter; +import java.nio.charset.Charset; import java.util.ArrayList; import java.util.List; @@ -105,9 +108,10 @@ public class App { var outputType = outputTypeInput == null ? command.getSupportedOutputTypes().stream().findFirst().orElse(null) : outputTypeInput; + var writer = new BufferedWriter(new OutputStreamWriter(System.out, Charset.defaultCharset())); var outputWriter = outputType == null ? null - : outputType == OutputType.JSON ? new JsonWriterImpl(System.out) : new PlainTextWriterImpl(System.out); + : outputType == OutputType.JSON ? new JsonWriterImpl(writer) : new PlainTextWriterImpl(writer); if (outputWriter != null && !command.getSupportedOutputTypes().contains(outputType)) { throw new UserErrorException("Command doesn't support output type " + outputType); @@ -311,6 +315,11 @@ public class App { try { manager.checkAccountState(); } catch (IOException e) { + try { + manager.close(); + } catch (IOException ie) { + logger.warn("Failed to close broken account", ie); + } throw new IOErrorException("Error while checking account " + username + ": " + e.getMessage(), e); } @@ -344,11 +353,11 @@ public class App { Command command, Signal ts, DBusConnection dBusConn, OutputWriter outputWriter ) throws CommandException { if (command instanceof LocalCommand localCommand) { - try { - localCommand.handleCommand(ns, new DbusManagerImpl(ts, dBusConn), outputWriter); + try (final var m = new DbusManagerImpl(ts, dBusConn)) { + localCommand.handleCommand(ns, m, outputWriter); } catch (UnsupportedOperationException e) { throw new UserErrorException("Command is not yet implemented via dbus", e); - } catch (DBusExecutionException e) { + } catch (IOException | DBusExecutionException e) { throw new UnexpectedErrorException(e.getMessage(), e); } } else {