@@ -169,21 +169,16 @@ int getIpv4Start() {
169169 */
170170 public <T > DatabaseRecord <T > getRecord (InetAddress ipAddress , Class <T > cls )
171171 throws IOException {
172- ByteBuffer buffer = this .getBufferHolder ().get ();
173172
174173 byte [] rawAddress = ipAddress .getAddress ();
175174
176- int bitLength = rawAddress .length * 8 ;
177- int record = this .startNode (bitLength );
178- int nodeCount = this .metadata .getNodeCount ();
175+ int [] traverseResult = traverseTree (rawAddress , rawAddress .length * 8 );
179176
180- int pl = 0 ;
181- for (; pl < bitLength && record < nodeCount ; pl ++) {
182- int b = 0xFF & rawAddress [pl / 8 ];
183- int bit = 1 & (b >> 7 - (pl % 8 ));
184- record = this .readNode (buffer , record , bit );
185- }
177+ int pl = traverseResult [1 ];
178+ int record = traverseResult [0 ];
186179
180+ int nodeCount = this .metadata .getNodeCount ();
181+ ByteBuffer buffer = this .getBufferHolder ().get ();
187182 T dataRecord = null ;
188183 if (record > nodeCount ) {
189184 // record is a data pointer
@@ -317,7 +312,7 @@ public <T> Networks<T> networksWithin(
317312 prefixLength += 96 ;
318313 }
319314
320- int [] traverseResult = this .traverseTree (ipBytes , 0 , prefixLength );
315+ int [] traverseResult = this .traverseTree (ipBytes , prefixLength );
321316 int node = traverseResult [0 ];
322317 int prefix = traverseResult [1 ];
323318
@@ -335,22 +330,24 @@ public <T> Networks<T> networksWithin(
335330 * @param bitCount The prefix.
336331 * @return int[]
337332 */
338- private int [] traverseTree (byte [] ip , int node , int bitCount )
333+ private int [] traverseTree (byte [] ip , int bitCount )
339334 throws ClosedDatabaseException , InvalidDatabaseException {
340- int nodeCount = this .metadata .getNodeCount ();
341- int i = 0 ;
342-
343335 ByteBuffer buffer = this .getBufferHolder ().get ();
336+ int bitLength = ip .length * 8 ;
337+ int record = this .startNode (bitLength );
338+ int nodeCount = this .metadata .getNodeCount ();
344339
345- for (; i < bitCount && node < nodeCount ; i ++) {
346- int bit = 1 & (ip [i >> 3 ] >> (7 - (i % 8 )));
340+ int i = 0 ;
341+ for (; i < bitCount && record < nodeCount ; i ++) {
342+ int b = 0xFF & ip [i / 8 ];
343+ int bit = 1 & (b >> 7 - (i % 8 ));
347344
348345 // bit:0 -> left record.
349346 // bit:1 -> right record.
350- node = this .readNode (buffer , node , bit );
347+ record = this .readNode (buffer , record , bit );
351348 }
352349
353- return new int []{node , i };
350+ return new int []{record , i };
354351 }
355352
356353 int readNode (ByteBuffer buffer , int nodeNumber , int index )
0 commit comments