diff --git a/core/src/main/java/org/bitcoinj/core/TransactionOutputChanges.java b/core/src/main/java/org/bitcoinj/core/TransactionOutputChanges.java index 431dedcd570..0fd7a47ab0e 100644 --- a/core/src/main/java/org/bitcoinj/core/TransactionOutputChanges.java +++ b/core/src/main/java/org/bitcoinj/core/TransactionOutputChanges.java @@ -41,12 +41,12 @@ public TransactionOutputChanges(InputStream in) throws IOException { int numOutsCreated = (int) Utils.readUint32FromStream(in); txOutsCreated = new LinkedList<>(); for (int i = 0; i < numOutsCreated; i++) - txOutsCreated.add(new UTXO(in)); + txOutsCreated.add(UTXO.fromStream(in)); int numOutsSpent = (int) Utils.readUint32FromStream(in); txOutsSpent = new LinkedList<>(); for (int i = 0; i < numOutsSpent; i++) - txOutsSpent.add(new UTXO(in)); + txOutsSpent.add(UTXO.fromStream(in)); } public void serializeToStream(OutputStream bos) throws IOException { diff --git a/core/src/main/java/org/bitcoinj/core/UTXO.java b/core/src/main/java/org/bitcoinj/core/UTXO.java index e148c83f8cd..3962b60621d 100644 --- a/core/src/main/java/org/bitcoinj/core/UTXO.java +++ b/core/src/main/java/org/bitcoinj/core/UTXO.java @@ -85,10 +85,6 @@ public UTXO(Sha256Hash hash, this.address = address; } - public UTXO(InputStream in) throws IOException { - deserializeFromStream(in); - } - /** The value which this Transaction output holds. */ public Coin getValue() { return value; @@ -153,32 +149,34 @@ public void serializeToStream(OutputStream bos) throws IOException { bos.write(new byte[] { (byte)(coinbase ? 1 : 0) }); } - public void deserializeFromStream(InputStream in) throws IOException { + public static UTXO fromStream(InputStream in) throws IOException { byte[] valueBytes = new byte[8]; if (in.read(valueBytes, 0, 8) != 8) throw new EOFException(); - value = Coin.valueOf(Utils.readInt64(valueBytes, 0)); + Coin value = Coin.valueOf(Utils.readInt64(valueBytes, 0)); int scriptBytesLength = (int) Utils.readUint32FromStream(in); byte[] scriptBytes = new byte[scriptBytesLength]; if (in.read(scriptBytes) != scriptBytesLength) throw new EOFException(); - script = new Script(scriptBytes); + Script script = new Script(scriptBytes); byte[] hashBytes = new byte[32]; if (in.read(hashBytes) != 32) throw new EOFException(); - hash = Sha256Hash.wrap(hashBytes); + Sha256Hash hash = Sha256Hash.wrap(hashBytes); byte[] indexBytes = new byte[4]; if (in.read(indexBytes) != 4) throw new EOFException(); - index = Utils.readUint32(indexBytes, 0); + long index = Utils.readUint32(indexBytes, 0); - height = (int) Utils.readUint32FromStream(in); + int height = (int) Utils.readUint32FromStream(in); byte[] coinbaseByte = new byte[1]; in.read(coinbaseByte); - coinbase = coinbaseByte[0] == 1; + boolean coinbase = coinbaseByte[0] == 1; + + return new UTXO(hash, index, value, height, coinbase, script); } } diff --git a/core/src/main/java/org/bitcoinj/store/LevelDBFullPrunedBlockStore.java b/core/src/main/java/org/bitcoinj/store/LevelDBFullPrunedBlockStore.java index 814f953808d..7cfbc5c3275 100644 --- a/core/src/main/java/org/bitcoinj/store/LevelDBFullPrunedBlockStore.java +++ b/core/src/main/java/org/bitcoinj/store/LevelDBFullPrunedBlockStore.java @@ -742,7 +742,7 @@ public UTXO getTransactionOutput(Sha256Hash hash, long index) throws BlockStoreE return null; } ByteArrayInputStream bis = new ByteArrayInputStream(inbytes); - UTXO txout = new UTXO(bis); + UTXO txout = UTXO.fromStream(bis); if (instrument) endMethod("getTransactionOutput"); diff --git a/core/src/test/java/org/bitcoinj/core/UTXOTest.java b/core/src/test/java/org/bitcoinj/core/UTXOTest.java index a5ba276d1ee..324d84be031 100644 --- a/core/src/test/java/org/bitcoinj/core/UTXOTest.java +++ b/core/src/test/java/org/bitcoinj/core/UTXOTest.java @@ -35,7 +35,7 @@ public void testSerialization() throws Exception { ByteArrayOutputStream os = new ByteArrayOutputStream(); utxo.serializeToStream(os); InputStream is = new ByteArrayInputStream(os.toByteArray()); - UTXO utxoCopy = new UTXO(is); + UTXO utxoCopy = UTXO.fromStream(is); assertEquals(utxo, utxoCopy); assertEquals(utxo.getValue(), utxoCopy.getValue()); assertEquals(utxo.getHeight(), utxoCopy.getHeight());