class BloomFilter extends Object
The filter has 256 bytes (m = 2048 bits) and two hash functions (k = 2) derived from the first four bytes of the SHA-1 of the inserted value, as specified in BEP 33. In I2P the inserted values are the 32-byte destination hashes of the peers rather than the IP addresses of the specification, as that is how peers are identified on the I2P network; both the filter builder and the estimator insert the same hashes, so the resulting filters stay compatible between clients.
The number of inserted values can be estimated from the count of
zero bits, see estimateSize(). The estimate is only reliable up
to a few thousand inserted values; the filter saturates around 8000.
| Modifier and Type | Field and Description |
|---|---|
(package private) static int | SIZESize of the filter in bytes, as specified in BEP 33 |
| Constructor and Description |
|---|
BloomFilter()Create a new empty filter. |
BloomFilter(byte[] data)Create a filter from data received in a get_peers response. |
| Modifier and Type | Method and Description |
|---|---|
(package private) boolean | contains(byte[] data)Test whether a value is probably contained in the filter. |
(package private) int | countZeroBits()Count the number of bits in the filter that are still zero. |
(package private) double | estimateSize()Estimate the number of values inserted into the filter. |
(package private) byte[] | getData()The filter data. |
(package private) void | insert(byte[] data)Insert a value into the filter, setting the two bits at indices
derived from the SHA-1 of the value. |
static final int SIZE
BloomFilter()
BloomFilter(byte[] data)
data - 256 bytes of filter dataIllegalArgumentException - if the data is not 256 bytesboolean contains(byte[] data)
data - the value to testint countZeroBits()
double estimateSize()
Uses the equation from BEP 33, log(c/m) / (k * log(1 - 1/m)), where c is the count of zero bits. The estimate is only meaningful while the filter is less than about half full; it breaks down as the filter approaches 8000 inserted values.
byte[] getData()
void insert(byte[] data)
data - the value to insert, e.g. a 32-byte destination hash