title: Best way to compare IP addresses quickly
body: <p>I'm parsing two CSV files which contains IP addresses.
  The first is a source CSV, and the second is a "Blacklist".</p>
  
  <p>Because of the size of the source file, I'm trying to optimize the speed at which I find IP addresses that match the blacklist.</p>
  
  <p><strong>EDIT:</strong> The blacklist consists of IP Address "Blocks". This means that each record in the blacklist has two IP addresses: A <code>Start</code> Block (ex. 216.254.128.0) and an <code>End</code> Block. (Ex. 216.254.223.255)</p>
  
  <p>This means that direct lookups etc, will NOT work.</p>
  
  <p>I'm wondering what's the best way to approach this. The brute strength method would be:</p>
  
  <pre><code>String[] parts = sourceIP.split("\\."); // String array, each element is text between dots
  
  int hi = 255;
  int lo = 0;
  
  int mid = (hi - lo) / 2 ;
  
  if (Integer.valueOf(parts[0]) &gt; mid) {
      mid = lo;
  }
  </code></pre>
  
  <p>I could then repeat this for each <code>part</code> to decide whether or not the IP address is in the black list.</p>
  
  <p>This seems pretty aggressive and with 4k+ records, this could take a very, very long time.</p>
  
  <p>It could take 10+ iterations to decide each part and that would then have to be repeated to check the "High" part of the IP blocks in the blacklist. That's 80+ iterations per record. </p>
  
  <p>I'm hoping to get some input here to see the best method for comparing IP addresses.</p>
  
  <p>What are your thoughts?</p>
  
  <p>Would it be possible to use a quick bitwise mask to compare values rapidly by serializing <code>INetAddress</code>?</p>
  
  <p><strong>FILE STRUCTURE CLARIFICATION:</strong></p>
  
  <p>Source IP File:</p>
  
  <p>Contains a list of records from a database. (Aprox 4k). Each record contains names, addresses, emails, and IP Address.</p>
  
  <p>Blacklist:</p>
  
  <p>Contains 4.2k records. Each record is an IP Address "Block". This consists of two IP Addresses. 1. Start and 2. End.</p>
  
  <p>If the record in the Source list has an IP address that's found in the blacklist, I need to save that record and add it to a new file.</p>