3
0
mirror of https://github.com/Qortal/altcoinj.git synced 2025-02-12 02:05:53 +00:00

Fix autosave in WalletTool on Windows

Wallet.saveToFile failed to remove original wallet file because WalletTool
left open input stream. This error happened on Windows-specific code path

Stacktrace:
```
Failed to save wallet! Old wallet should be left untouched.
java.io.IOException: Failed to delete canonical wallet file for
replacement with autosave
        at com.google.bitcoin.core.Wallet.saveToFile(Wallet.java:834)
	        at
		com.google.bitcoin.core.Wallet.saveToFile(Wallet.java:863)
		        at
			com.google.bitcoin.tools.WalletTool.saveWallet(WalletTool.java:851)
			        at
				com.google.bitcoin.tools.WalletTool.main(WalletTool.java:362)
```
This commit is contained in:
Kosta Korenkov 2014-06-24 15:16:19 +04:00 committed by Mike Hearn
parent 215ecbfa21
commit 9680911bca

View File

@ -53,10 +53,7 @@ import org.spongycastle.crypto.params.KeyParameter;
import org.spongycastle.util.encoders.Hex;
import javax.annotation.Nullable;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.*;
import java.math.BigInteger;
import java.net.InetAddress;
import java.net.UnknownHostException;
@ -301,11 +298,13 @@ public class WalletTool {
}
}
InputStream walletInputStream = null;
try {
WalletProtobufSerializer loader = new WalletProtobufSerializer();
if (options.has("ignore-mandatory-extensions"))
loader.setRequireMandatoryExtensions(false);
wallet = loader.readWallet(new BufferedInputStream(new FileInputStream(walletFile)));
walletInputStream = new BufferedInputStream(new FileInputStream(walletFile));
wallet = loader.readWallet(walletInputStream);
if (!wallet.getParams().equals(params)) {
System.err.println("Wallet does not match requested network parameters: " +
wallet.getParams().getId() + " vs " + params.getId());
@ -315,6 +314,10 @@ public class WalletTool {
System.err.println("Failed to load wallet '" + walletFile + "': " + e.getMessage());
e.printStackTrace();
return;
} finally {
if (walletInputStream != null) {
walletInputStream.close();
}
}
// What should we do?