File tree Expand file tree Collapse file tree
guava-tests/test/com/google/common/hash
guava/src/com/google/common/hash
guava-tests/test/com/google/common/hash
guava/src/com/google/common/hash Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -378,11 +378,13 @@ public void testBitSizeMatchesSerializationSize() throws Exception {
378378 // Header: 1 byte (strategy) + 1 byte (hash functions) + 4 bytes (array length) = 6 bytes
379379 // Data: bitSize / 8 bytes
380380 long predictedSize = bf .bitSize () / 8 + 6 ;
381+ assertThat (bf .serializedSize ()).isEqualTo (predictedSize );
381382
382383 // Serialize and measure actual size
383384 ByteArrayOutputStream out = new ByteArrayOutputStream ();
384385 bf .writeTo (out );
385386 int actualSize = out .size ();
387+ assertThat (bf .serializedSize ()).isEqualTo (actualSize );
386388
387389 assertWithMessage (
388390 "Serialization size mismatch for expectedInsertions=%s, fpp=%s" ,
Original file line number Diff line number Diff line change @@ -226,6 +226,25 @@ long bitSize() {
226226 return bits .bitSize ();
227227 }
228228
229+ /**
230+ * Returns the number of bytes required to serialize this bloom filter using the {@link
231+ * #writeTo(OutputStream)} method. This method can be useful if you need to know the serialization
232+ * size before writing to pre-allocated space (e.g., for memory-mapped files or fixed-size
233+ * records).
234+ *
235+ * @since NEXT
236+ */
237+ public long serializedSize () {
238+ // We return a long (and not an int) because the max serialized size is approximately
239+ // 17 billion bytes (2^31 * 8 bytes).
240+
241+ // The serialization header is 6 bytes:
242+ // 1 byte for the strategy
243+ // 1 byte for the number of hash functions
244+ // 4 bytes for the array length
245+ return (bits .bitSize () / 8 ) + 6 ;
246+ }
247+
229248 /**
230249 * Determines whether a given Bloom filter is compatible with this Bloom filter. For two Bloom
231250 * filters to be compatible, they must:
Original file line number Diff line number Diff line change @@ -380,11 +380,13 @@ public void testBitSizeMatchesSerializationSize() throws Exception {
380380 // Header: 1 byte (strategy) + 1 byte (hash functions) + 4 bytes (array length) = 6 bytes
381381 // Data: bitSize / 8 bytes
382382 long predictedSize = bf .bitSize () / 8 + 6 ;
383+ assertThat (bf .serializedSize ()).isEqualTo (predictedSize );
383384
384385 // Serialize and measure actual size
385386 ByteArrayOutputStream out = new ByteArrayOutputStream ();
386387 bf .writeTo (out );
387388 int actualSize = out .size ();
389+ assertThat (bf .serializedSize ()).isEqualTo (actualSize );
388390
389391 assertWithMessage (
390392 "Serialization size mismatch for expectedInsertions=%s, fpp=%s" ,
Original file line number Diff line number Diff line change @@ -238,6 +238,25 @@ long bitSize() {
238238 return bits .bitSize ();
239239 }
240240
241+ /**
242+ * Returns the number of bytes required to serialize this bloom filter using the {@link
243+ * #writeTo(OutputStream)} method. This method can be useful if you need to know the serialization
244+ * size before writing to pre-allocated space (e.g., for memory-mapped files or fixed-size
245+ * records).
246+ *
247+ * @since NEXT
248+ */
249+ public long serializedSize () {
250+ // We return a long (and not an int) because the max serialized size is approximately
251+ // 17 billion bytes (2^31 * 8 bytes).
252+
253+ // The serialization header is 6 bytes:
254+ // 1 byte for the strategy
255+ // 1 byte for the number of hash functions
256+ // 4 bytes for the array length
257+ return (bits .bitSize () / 8 ) + 6 ;
258+ }
259+
241260 /**
242261 * Determines whether a given Bloom filter is compatible with this Bloom filter. For two Bloom
243262 * filters to be compatible, they must:
You can’t perform that action at this time.
0 commit comments