Skip to content

Commit f62259b

Browse files
msgilliganschildbach
authored andcommitted
BitcoinSerializer: store packetMagic in instance field
1 parent 369445d commit f62259b

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

core/src/main/java/org/bitcoinj/core/BitcoinSerializer.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public class BitcoinSerializer extends MessageSerializer {
5050
private static final int COMMAND_LEN = 12;
5151

5252
private final NetworkParameters params;
53+
private final int packetMagic;
5354
private final int protocolVersion;
5455

5556
private static final Map<Class<? extends Message>, String> names = new HashMap<>();
@@ -96,6 +97,7 @@ public BitcoinSerializer(NetworkParameters params) {
9697
*/
9798
public BitcoinSerializer(NetworkParameters params, int protocolVersion) {
9899
this.params = params;
100+
this.packetMagic = params.getPacketMagic();
99101
this.protocolVersion = protocolVersion;
100102
}
101103

@@ -116,7 +118,7 @@ public int getProtocolVersion() {
116118
@Override
117119
public void serialize(String name, byte[] message, OutputStream out) throws IOException {
118120
byte[] header = new byte[4 + COMMAND_LEN + 4 + 4 /* checksum */];
119-
ByteUtils.writeInt32BE(params.getPacketMagic(), header, 0);
121+
ByteUtils.writeInt32BE(packetMagic, header, 0);
120122

121123
// The header array is initialized to zero by Java so we don't have to worry about
122124
// NULL terminating the string here.
@@ -344,7 +346,7 @@ public void seekPastMagicBytes(ByteBuffer in) throws BufferUnderflowException {
344346
byte b = in.get();
345347
// We're looking for a run of bytes that is the same as the packet magic but we want to ignore partial
346348
// magics that aren't complete. So we keep track of where we're up to with magicCursor.
347-
byte expectedByte = (byte)(0xFF & params.getPacketMagic() >>> (magicCursor * 8));
349+
byte expectedByte = (byte)(0xFF & packetMagic >>> (magicCursor * 8));
348350
if (b == expectedByte) {
349351
magicCursor--;
350352
if (magicCursor < 0) {

0 commit comments

Comments
 (0)