Skip to content

Commit 862c224

Browse files
committed
Set ipV4Start in constructor
1 parent aeb2a3a commit 862c224

File tree

1 file changed

+5
-9
lines changed

1 file changed

+5
-9
lines changed

src/main/java/com/maxmind/db/Reader.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public final class Reader implements Closeable {
1919
(byte) 0xCD, (byte) 0xEF, 'M', 'a', 'x', 'M', 'i', 'n', 'd', '.',
2020
'c', 'o', 'm' };
2121

22-
private int ipV4Start;
22+
private final int ipV4Start;
2323
private final Metadata metadata;
2424
private final BufferHolder bufferHolder;
2525

@@ -88,6 +88,8 @@ private Reader(BufferHolder bufferHolder, String name) throws IOException {
8888

8989
Decoder metadataDecoder = new Decoder(buffer, start);
9090
this.metadata = new Metadata(metadataDecoder.decode(start).getNode());
91+
92+
this.ipV4Start = this.findIpV4StartNode(buffer);
9193
}
9294

9395
/**
@@ -137,28 +139,22 @@ private int startNode(ByteBuffer buffer, int bitLength) throws InvalidDatabaseEx
137139
// Check if we are looking up an IPv4 address in an IPv6 tree. If this
138140
// is the case, we can skip over the first 96 nodes.
139141
if (this.metadata.ipVersion == 6 && bitLength == 32) {
140-
return this.ipV4StartNode(buffer);
142+
return this.ipV4Start;
141143
}
142144
// The first node of the tree is always node 0, at the beginning of the
143145
// value
144146
return 0;
145147
}
146148

147-
private int ipV4StartNode(ByteBuffer buffer) throws InvalidDatabaseException {
148-
// This is a defensive check. There is no reason to call this when you
149-
// have an IPv4 tree.
149+
private int findIpV4StartNode(ByteBuffer buffer) throws InvalidDatabaseException {
150150
if (this.metadata.ipVersion == 4) {
151151
return 0;
152152
}
153153

154-
if (this.ipV4Start != 0) {
155-
return this.ipV4Start;
156-
}
157154
int node = 0;
158155
for (int i = 0; i < 96 && node < this.metadata.nodeCount; i++) {
159156
node = this.readNode(buffer, node, 0);
160157
}
161-
this.ipV4Start = node;
162158
return node;
163159
}
164160

0 commit comments

Comments
 (0)