From 14ef3c00526ff8c32a5c910b2e4102caa586085f Mon Sep 17 00:00:00 2001 From: Justas Dobiliauskas Date: Wed, 13 Apr 2016 18:23:08 +0300 Subject: [PATCH] WalletProtobufSerializer: Allow to specify buffer size for saving wallet file via OutputStream. --- .../bitcoinj/wallet/WalletProtobufSerializer.java | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/org/bitcoinj/wallet/WalletProtobufSerializer.java b/core/src/main/java/org/bitcoinj/wallet/WalletProtobufSerializer.java index 324863b6f..cbe9a58ce 100644 --- a/core/src/main/java/org/bitcoinj/wallet/WalletProtobufSerializer.java +++ b/core/src/main/java/org/bitcoinj/wallet/WalletProtobufSerializer.java @@ -31,6 +31,7 @@ import org.bitcoinj.wallet.Protos.Wallet.EncryptionType; import com.google.common.collect.Lists; import com.google.protobuf.ByteString; import com.google.protobuf.CodedInputStream; +import com.google.protobuf.CodedOutputStream; import com.google.protobuf.TextFormat; import com.google.protobuf.WireFormat; @@ -78,6 +79,7 @@ public class WalletProtobufSerializer { protected Map txMap; private boolean requireMandatoryExtensions = true; + private int walletWriteBufferSize = CodedOutputStream.DEFAULT_BUFFER_SIZE; public interface WalletFactory { Wallet create(NetworkParameters params, KeyChainGroup keyChainGroup); @@ -114,6 +116,14 @@ public class WalletProtobufSerializer { requireMandatoryExtensions = value; } + /** + * Change buffer size for writing wallet to output stream. Default is {@link com.google.protobuf.CodedOutputStream.DEFAULT_BUFFER_SIZE} + * @param walletWriteBufferSize - buffer size in bytes + */ + public void setWalletWriteBufferSize(int walletWriteBufferSize) { + this.walletWriteBufferSize = walletWriteBufferSize; + } + /** * Formats the given wallet (transactions and keys) to the given output stream in protocol buffer format.

* @@ -121,7 +131,9 @@ public class WalletProtobufSerializer { */ public void writeWallet(Wallet wallet, OutputStream output) throws IOException { Protos.Wallet walletProto = walletToProto(wallet); - walletProto.writeTo(output); + final CodedOutputStream codedOutput = CodedOutputStream.newInstance(output, this.walletWriteBufferSize); + walletProto.writeTo(codedOutput); + codedOutput.flush(); } /**