ensure wallet export file streams are always closed

This commit is contained in:
Craig Raw 2021-04-30 14:03:58 +02:00
parent 45f6ae214a
commit 5ede1dd97d
5 changed files with 4 additions and 7 deletions

View file

@ -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);

View file

@ -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);

View file

@ -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);

View file

@ -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);

View file

@ -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);