FilterRecord { public int writeFullyTo(FileChannel channel) throws IOException { final long startPos = channel.position(); channel.position(startPos + HEADER_OVERHEAD); final ChecksumedBufferedOutputStream stream = new ChecksumedBufferedOutputStream( Channels.newOutputStream(channel), Configuration.channelBufferSizeForFilterPersistence()); writeBody(stream); stream.flush(); int bodyLen = (int) (channel.position() - startPos - HEADER_OVERHEAD); channel.position(startPos); final ByteBuffer headerBuffer = ByteBuffer.allocate(HEADER_OVERHEAD); headerBuffer.putInt(BODY_LENGTH_OFFSET, bodyLen); headerBuffer.put(MAGIC_OFFSET, DEFAULT_MAGIC); headerBuffer.putInt(CRC_OFFSET, (int) stream.checksum()); writeBufferTo(channel, headerBuffer); channel.position(startPos + HEADER_OVERHEAD + bodyLen); return HEADER_OVERHEAD + bodyLen; } FilterRecord(String name, F filter); String name(); F filter(); int writeFullyTo(FileChannel channel); @Override boolean equals(Object o); @Override int hashCode(); @Override String toString();  }
@Test public void testReadWriteFilterRecord() throws Exception { final GuavaBloomFilter filter = new GuavaBloomFilter( expectedInsertions, fpp, creation, expiration, validPeriodAfterAccess); final FilterRecord<GuavaBloomFilter> record = new FilterRecord<>(testingFilterName, filter); final FileChannel channel = FileChannel.open(tempFile.toPath(), StandardOpenOption.CREATE, StandardOpenOption.WRITE, StandardOpenOption.READ); record.writeFullyTo(channel); final FilterRecordInputStream<GuavaBloomFilter> stream = new FilterRecordInputStream<>(tempFile.toPath(), new GuavaBloomFilterFactory()); assertThat(stream.nextFilterRecord()).isEqualTo(new FilterRecord<>(testingFilterName, filter)); }