import java.util.LinkedList; //导入方法依赖的package包/类
private Stream bufferedAndSortedReadAllForwards(Position positionExclusive) {
try(Stream allForwards = underlying.readAllForwards()) {
Iterator remainder = allForwards.iterator();
PeekingIterator> sortCandidates = Iterators.peekingIterator(
Iterators.transform(remainder, re -> new EventWithSortKey<>(re, sortKeyExtractor.apply(re)))
);
final LinkedList> buffer = new LinkedList<>();
while (sortCandidates.hasNext() && sortCandidates.peek().sortKey.compareTo(cutoffSortKey) < 0) {
buffer.add(sortCandidates.next());
}
if (!sortCandidates.hasNext()) {
return Stream.empty();
}
buffer.sort(Comparator.naturalOrder());
if (!positionExclusive.equals(underlying.emptyStorePosition())) {
Iterator> bufferIterator = buffer.iterator();
while (!bufferIterator.next().event.position().equals(positionExclusive)) {
bufferIterator.remove();
}
bufferIterator.remove();
}
Stream> reorderedEvents = buffer.stream().onClose(buffer::clear);
Stream> eventInTheGap = Stream.of(sortCandidates.peek());
Stream remainingEvents = stream(spliteratorUnknownSize(remainder, ORDERED), false);
return concat(concat(reorderedEvents, eventInTheGap).map(EventWithSortKey::toResolvedEvent), remainingEvents).onClose(allForwards::close);
}
}