Skip to content

Commit 5095ec4

Browse files
committed
takeUntilByteCount actually uses takeUntil
Issue: SPR-17188
1 parent 542ed81 commit 5095ec4

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

spring-core/src/main/java/org/springframework/core/io/buffer/DataBufferUtils.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -396,14 +396,11 @@ public static Flux<DataBuffer> takeUntilByteCount(Publisher<DataBuffer> publishe
396396
AtomicLong countDown = new AtomicLong(maxByteCount);
397397

398398
return Flux.from(publisher)
399-
.takeWhile(buffer -> {
400-
int delta = -buffer.readableByteCount();
401-
return countDown.getAndAdd(delta) >= 0;
402-
})
403399
.map(buffer -> {
404-
long count = countDown.get();
400+
long count = countDown.addAndGet(-buffer.readableByteCount());
405401
return count >= 0 ? buffer : buffer.slice(0, buffer.readableByteCount() + (int) count);
406-
});
402+
})
403+
.takeUntil(buffer -> countDown.get() <= 0);
407404
}
408405

409406
/**

spring-core/src/test/java/org/springframework/core/io/buffer/DataBufferUtilsTests.java

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -226,19 +226,32 @@ public void writeAsynchronousFileChannel() throws Exception {
226226

227227
@Test
228228
public void takeUntilByteCount() {
229-
DataBuffer foo = stringBuffer("foo");
230-
DataBuffer bar = stringBuffer("bar");
231-
DataBuffer baz = stringBuffer("baz");
232-
Flux<DataBuffer> flux = Flux.just(foo, bar, baz);
233-
Flux<DataBuffer> result = DataBufferUtils.takeUntilByteCount(flux, 5L);
229+
230+
Flux<DataBuffer> result = DataBufferUtils.takeUntilByteCount(
231+
Flux.just(stringBuffer("foo"), stringBuffer("bar")), 5L);
234232

235233
StepVerifier.create(result)
236234
.consumeNextWith(stringConsumer("foo"))
237235
.consumeNextWith(stringConsumer("ba"))
238236
.expectComplete()
239237
.verify(Duration.ofSeconds(5));
238+
}
239+
240+
@Test
241+
public void takeUntilByteCountExact() {
242+
243+
DataBuffer extraBuffer = stringBuffer("baz");
244+
245+
Flux<DataBuffer> result = DataBufferUtils.takeUntilByteCount(
246+
Flux.just(stringBuffer("foo"), stringBuffer("bar"), extraBuffer), 6L);
247+
248+
StepVerifier.create(result)
249+
.consumeNextWith(stringConsumer("foo"))
250+
.consumeNextWith(stringConsumer("bar"))
251+
.expectComplete()
252+
.verify(Duration.ofSeconds(5));
240253

241-
release(baz);
254+
release(extraBuffer);
242255
}
243256

244257
@Test

0 commit comments

Comments
 (0)