Reads a byte stream, which was written by plain #writeTo(OutputStream), into a BloomFilter . The Funnel to be used is not encoded in the stream, so it must be provided here. Warning: the funnel provided must behave identically to the one used to populate the o
(InputStream in, Funnel<T> funnel)
| 511 | |
| 512 | |
| 513 | public static <T> BloomFilter<T> readFrom(InputStream in, Funnel<T> funnel) |
| 514 | throws IOException { |
| 515 | checkNotNull(in, "InputStream"); |
| 516 | checkNotNull(funnel, "Funnel"); |
| 517 | int strategyOrdinal = -1; |
| 518 | int numHashFunctions = -1; |
| 519 | int dataLength = -1; |
| 520 | try { |
| 521 | DataInputStream din = new DataInputStream(in); |
| 522 | // currently this assumes there is no negative ordinal; will have to be updated if we |
| 523 | // add non-stateless strategies (for which we've reserved negative ordinals; see |
| 524 | // Strategy.ordinal()). |
| 525 | strategyOrdinal = din.readByte(); |
| 526 | numHashFunctions = UnsignedBytes.toInt(din.readByte()); |
| 527 | dataLength = din.readInt(); |
| 528 | Strategy strategy = BloomFilterStrategies.values() [strategyOrdinal]; |
| 529 | long[] data = new long[dataLength]; |
| 530 | for (int i = 0; i < data.length; i++) { |
| 531 | data[i] = din.readLong(); |
| 532 | } |
| 533 | return new BloomFilter<T>(new BitArray(data), numHashFunctions, funnel, strategy); |
| 534 | } catch (RuntimeException e) { |
| 535 | IOException ioException = new IOException("Unable to deserialize BloomFilter from InputStream." + " strategyOrdinal: " |
| 536 | + strategyOrdinal |
| 537 | + " numHashFunctions: " |
| 538 | + numHashFunctions + " dataLength: " + dataLength); |
| 539 | ioException.initCause(e); |
| 540 | throw ioException; |
| 541 | } |
| 542 | } |
| 543 | } |