From 5ede1dd97df6aeb835a58ccba724206c15c99824 Mon Sep 17 00:00:00 2001 From: Craig Raw Date: Fri, 30 Apr 2021 14:03:58 +0200 Subject: [PATCH] ensure wallet export file streams are always closed --- .../sparrow/control/FileWalletExportPane.java | 7 ++++--- .../com/sparrowwallet/sparrow/io/ColdcardMultisig.java | 1 - src/main/java/com/sparrowwallet/sparrow/io/Electrum.java | 1 - src/main/java/com/sparrowwallet/sparrow/io/Sparrow.java | 1 - .../java/com/sparrowwallet/sparrow/io/SpecterDesktop.java | 1 - 5 files changed, 4 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/sparrowwallet/sparrow/control/FileWalletExportPane.java b/src/main/java/com/sparrowwallet/sparrow/control/FileWalletExportPane.java index 1700d68b..364560d7 100644 --- a/src/main/java/com/sparrowwallet/sparrow/control/FileWalletExportPane.java +++ b/src/main/java/com/sparrowwallet/sparrow/control/FileWalletExportPane.java @@ -127,9 +127,10 @@ public class FileWalletExportPane extends TitledDescriptionPane { private void exportWallet(File file, Wallet exportWallet) { try { if(file != null) { - OutputStream outputStream = new FileOutputStream(file); - exporter.exportWallet(exportWallet, outputStream); - EventManager.get().post(new WalletExportEvent(exportWallet)); + try(OutputStream outputStream = new FileOutputStream(file)) { + exporter.exportWallet(exportWallet, outputStream); + EventManager.get().post(new WalletExportEvent(exportWallet)); + } } else { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); exporter.exportWallet(exportWallet, outputStream); diff --git a/src/main/java/com/sparrowwallet/sparrow/io/ColdcardMultisig.java b/src/main/java/com/sparrowwallet/sparrow/io/ColdcardMultisig.java index 6d425b1a..b993dba4 100644 --- a/src/main/java/com/sparrowwallet/sparrow/io/ColdcardMultisig.java +++ b/src/main/java/com/sparrowwallet/sparrow/io/ColdcardMultisig.java @@ -202,7 +202,6 @@ public class ColdcardMultisig implements WalletImport, KeystoreFileImport, Walle } writer.flush(); - writer.close(); } catch(Exception e) { log.error("Error exporting Coldcard multisig wallet", e); throw new ExportException("Error exporting Coldcard multisig wallet", e); diff --git a/src/main/java/com/sparrowwallet/sparrow/io/Electrum.java b/src/main/java/com/sparrowwallet/sparrow/io/Electrum.java index 10004f22..1110abc8 100644 --- a/src/main/java/com/sparrowwallet/sparrow/io/Electrum.java +++ b/src/main/java/com/sparrowwallet/sparrow/io/Electrum.java @@ -367,7 +367,6 @@ public class Electrum implements KeystoreFileImport, WalletImport, WalletExport String json = gson.toJson(eJson); outputStream.write(json.getBytes(StandardCharsets.UTF_8)); outputStream.flush(); - outputStream.close(); } catch (Exception e) { log.error("Error exporting Electrum Wallet", e); throw new ExportException("Error exporting Electrum Wallet", e); diff --git a/src/main/java/com/sparrowwallet/sparrow/io/Sparrow.java b/src/main/java/com/sparrowwallet/sparrow/io/Sparrow.java index ce7576ae..84ec0e59 100644 --- a/src/main/java/com/sparrowwallet/sparrow/io/Sparrow.java +++ b/src/main/java/com/sparrowwallet/sparrow/io/Sparrow.java @@ -28,7 +28,6 @@ public class Sparrow implements WalletExport { Storage storage = AppServices.get().getOpenWallets().get(wallet); Files.copy(storage.getWalletFile().toPath(), outputStream); outputStream.flush(); - outputStream.close(); } catch(Exception e) { log.error("Error exporting Sparrow wallet file", e); throw new ExportException("Error exporting Sparrow wallet file", e); diff --git a/src/main/java/com/sparrowwallet/sparrow/io/SpecterDesktop.java b/src/main/java/com/sparrowwallet/sparrow/io/SpecterDesktop.java index 6d83ae55..76032800 100644 --- a/src/main/java/com/sparrowwallet/sparrow/io/SpecterDesktop.java +++ b/src/main/java/com/sparrowwallet/sparrow/io/SpecterDesktop.java @@ -31,7 +31,6 @@ public class SpecterDesktop implements WalletImport, WalletExport { String json = gson.toJson(specterWallet); outputStream.write(json.getBytes(StandardCharsets.UTF_8)); outputStream.flush(); - outputStream.close(); } catch(Exception e) { log.error("Error exporting Specter Desktop wallet", e); throw new ExportException("Error exporting Specter Desktop wallet", e);