Skip to content

Commit 4138ec3

Browse files
committed
Don't create an unnecessary byte[] for IPv4 lookups in an IPv6 database
1 parent 4c47826 commit 4138ec3

1 file changed

Lines changed: 10 additions & 10 deletions

File tree

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -85,27 +85,27 @@ public JsonNode get(InetAddress address) throws MaxMindDbException,
8585
long findAddressInTree(InetAddress address) throws MaxMindDbException {
8686
byte[] rawAddress = address.getAddress();
8787

88-
// XXX sort of wasteful
89-
if (rawAddress.length == 4 && this.metadata.ipVersion == 6) {
90-
byte[] newAddress = new byte[16];
91-
System.arraycopy(rawAddress, 0, newAddress, 12, rawAddress.length);
92-
rawAddress = newAddress;
93-
}
94-
9588
if (this.DEBUG) {
9689
Log.debugNewLine();
9790
Log.debug("IP address", address);
9891
Log.debug("IP address", rawAddress);
9992
Log.debugNewLine();
10093
}
10194

95+
boolean isIp4AddressInIp6Db = rawAddress.length == 4
96+
&& this.metadata.ipVersion == 6;
97+
int ipStartBit = isIp4AddressInIp6Db ? 96 : 0;
98+
10299
// The first node of the tree is always node 0, at the beginning of the
103100
// value
104101
long nodeNum = 0;
105102

106-
for (int i = 0; i < rawAddress.length * 8; i++) {
107-
int b = 0xFF & rawAddress[i / 8];
108-
int bit = 1 & (b >> 7 - (i % 8));
103+
for (int i = 0; i < rawAddress.length * 8 + ipStartBit; i++) {
104+
int bit = 0;
105+
if (i >= ipStartBit) {
106+
int b = 0xFF & rawAddress[(i - ipStartBit) / 8];
107+
bit = 1 & (b >> 7 - (i % 8));
108+
}
109109
long record = this.readNode(nodeNum, bit);
110110

111111
if (this.DEBUG) {

0 commit comments

Comments
 (0)