Skip to content
Snippets Groups Projects

Leverage Java 8 language features.

Closed Daniel Fisher requested to merge java-streams into master
Compare and
2 files
+ 37
50
Compare changes
  • Side-by-side
  • Inline
Files
2
+ 27
29
// Requires Java 8 or newer
import java.io.*;
import java.nio.charset.*;
import java.nio.file.*;
import java.util.*;
import java.util.stream.*;
import com.google.common.hash.BloomFilter;
import com.google.common.hash.Funnels;
public class check
{
// Number of SHA1 hashes in the filter. Must match the line count of the file.
// wc -l test-sha1s.txt should equal this number
private static int cardinality = 526235978;
private static final String usage = "java -Xmx3072m -jar check.jar /path/to/filter.txt";
/**
* Read the filter into memory and return it.
**/
public static BloomFilter<byte[]> load_filter(String theFilter) throws FileNotFoundException, IOException
public static BloomFilter<String> load_filter(String theFilter) throws IOException
{
FileInputStream fis = new FileInputStream(new File(theFilter));
return BloomFilter.readFrom(fis, Funnels.byteArrayFunnel());
try (FileInputStream fis = new FileInputStream(theFilter))
{
return BloomFilter.readFrom(fis, Funnels.stringFunnel(StandardCharsets.UTF_8));
}
}
/**
@@ -32,98 +33,95 @@ public class check
if (args.length == 1)
{
try {
BloomFilter<byte[]> bf = BloomFilter.create(Funnels.byteArrayFunnel(), cardinality, 0.001);
bf = load_filter(args[0]);
BloomFilter<String> bf = load_filter(args[0]);
if( bf.mightContain("rbt".getBytes("UTF-8")) != false ) {
if( bf.mightContain("rbt") != false ) {
System.out.println("rbt failed!");
}
// Some basic sanity checks to ensure the filter is returning valid results.
// These should all pass before the filter is tagged and sent to Middleware.
if( bf.mightContain("000000005AD76BD555C1D6D771DE417A4B87E4B4".getBytes("UTF-8")) != true ) {
if( bf.mightContain("000000005AD76BD555C1D6D771DE417A4B87E4B4") != true ) {
System.out.println("000000005AD76BD555C1D6D771DE417A4B87E4B4 failed!");
}
// Invalid hash not in filter
if( bf.mightContain("w8rbt".getBytes("UTF-8")) != false ) {
if( bf.mightContain("w8rbt") != false ) {
System.out.println("w8rbt failed!");
}
// Valid hash in filter
if( bf.mightContain("FFFFFFFEE791CBAC0F6305CAF0CEE06BBE131160".getBytes("UTF-8")) != true ) {
if( bf.mightContain("FFFFFFFEE791CBAC0F6305CAF0CEE06BBE131160") != true ) {
System.out.println("FFFFFFFEE791CBAC0F6305CAF0CEE06BBE131160 failed!");
}
// Invalid hash not in filter
if( bf.mightContain("brad".getBytes("UTF-8")) != false ) {
if( bf.mightContain("brad") != false ) {
System.out.println("brad failed!");
}
// Valid hash in filter
if( bf.mightContain("B02FB8231CEB585B6B9E911DDF3B9817971AE931".getBytes("UTF-8")) != true ) {
if( bf.mightContain("B02FB8231CEB585B6B9E911DDF3B9817971AE931") != true ) {
System.out.println("B02FB8231CEB585B6B9E911DDF3B9817971AE931 failed!");
}
// Invalid hash not in filter
if( bf.mightContain("no".getBytes("UTF-8")) != false ) {
if( bf.mightContain("no") != false ) {
System.out.println("no failed!");
}
// Invalid hash not in filter
if( bf.mightContain("nope".getBytes("UTF-8")) != false ) {
if( bf.mightContain("nope") != false ) {
System.out.println("nope failed!");
}
// Invalid hash not in filter
if( bf.mightContain("not".getBytes("UTF-8")) != false ) {
if( bf.mightContain("not") != false ) {
System.out.println("not failed!");
}
// Valid hash not in filter
if( bf.mightContain("0000000000000000000000000000000000000000".getBytes("UTF-8")) != false ) {
if( bf.mightContain("0000000000000000000000000000000000000000") != false ) {
System.out.println("0000000000000000000000000000000000000000 failed!");
}
// Valid hash not in filter
if( bf.mightContain("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA".getBytes("UTF-8")) != false ) {
if( bf.mightContain("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA") != false ) {
System.out.println("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA failed!");
}
// Invalid Hash
if( bf.mightContain("KKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKK".getBytes("UTF-8")) != false ) {
if( bf.mightContain("KKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKK") != false ) {
System.out.println("KKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKK failed!");
}
// Invalid Hash
if( bf.mightContain("TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT".getBytes("UTF-8")) != false ) {
if( bf.mightContain("TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT") != false ) {
System.out.println("TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT failed!");
}
// ZZ inserted into valid hash
if( bf.mightContain("B02FB8231CEB585B6ZZE911DDF3B9817971AE931".getBytes("UTF-8")) != false ) {
if( bf.mightContain("B02FB8231CEB585B6ZZE911DDF3B9817971AE931") != false ) {
System.out.println("B02FB8231CEB585B6ZZE911DDF3B9817971AE931 failed!");
}
// Too big by 1
if( bf.mightContain("B02FB8231CEB585B6B9E911DDF3B9817971AE9311".getBytes("UTF-8")) != false ) {
if( bf.mightContain("B02FB8231CEB585B6B9E911DDF3B9817971AE9311") != false ) {
System.out.println("B02FB8231CEB585B6B9E911DDF3B9817971AE9311 failed!");
}
// Too big by 2
if( bf.mightContain("B02FB8231CEB585B6B9E911DDF3B9817971AE93112".getBytes("UTF-8")) != false ) {
if( bf.mightContain("B02FB8231CEB585B6B9E911DDF3B9817971AE93112") != false ) {
System.out.println("B02FB8231CEB585B6B9E911DDF3B9817971AE93112 failed!");
}
// Too big by 3
if( bf.mightContain("B02FB8231CEB585B6B9E911DDF3B9817971AE931123".getBytes("UTF-8")) != false ) {
if( bf.mightContain("B02FB8231CEB585B6B9E911DDF3B9817971AE931123") != false ) {
System.out.println("B02FB8231CEB585B6B9E911DDF3B9817971AE931123 failed!");
}
} catch (FileNotFoundException e) {
System.out.println(e);
} catch (IOException e) {
System.out.println(e);
} catch (IOException e) {
e.printStackTrace();
}
}