class SnapshotTracker implements AutoCloseable {

    boolean addChunk(final int chunkIndex, final byte[] chunk, final OptionalInt maybeLastChunkHashCode)
            throws InvalidChunkException, IOException {
        log.debug("addChunk: chunkIndex={}, lastChunkIndex={}, collectedChunks.size={}, lastChunkHashCode={}",
                chunkIndex, lastChunkIndex, count, this.lastChunkHashCode);

        if (sealed) {
            throw new InvalidChunkException("Invalid chunk received with chunkIndex " + chunkIndex
                    + " all chunks already received");
        }

        if (lastChunkIndex + 1 != chunkIndex) {
            throw new InvalidChunkException("Expected chunkIndex " + (lastChunkIndex + 1) + " got " + chunkIndex);
        }

        if (maybeLastChunkHashCode.isPresent() && maybeLastChunkHashCode.getAsInt() != this.lastChunkHashCode) {
            throw new InvalidChunkException("The hash code of the recorded last chunk does not match "
                    + "the senders hash code, expected " + this.lastChunkHashCode + " was "
                    + maybeLastChunkHashCode.getAsInt());
        }

        bufferedStream.write(chunk);

        count += chunk.length;
        sealed = chunkIndex == totalChunks;
        lastChunkIndex = chunkIndex;
        this.lastChunkHashCode = Arrays.hashCode(chunk);
        return sealed;
    }

      SnapshotTracker(final Logger log, final int totalChunks, final String leaderId, final RaftActorContext context);

}