Skip to content

Commit 542ed81

Browse files
committed
Polish
1 parent 217aa38 commit 542ed81

File tree

2 files changed

+26
-38
lines changed

2 files changed

+26
-38
lines changed

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

Lines changed: 23 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -393,24 +393,16 @@ private static void closeChannel(@Nullable Channel channel) {
393393
public static Flux<DataBuffer> takeUntilByteCount(Publisher<DataBuffer> publisher, long maxByteCount) {
394394
Assert.notNull(publisher, "Publisher must not be null");
395395
Assert.isTrue(maxByteCount >= 0, "'maxByteCount' must be a positive number");
396-
AtomicLong byteCountDown = new AtomicLong(maxByteCount);
397-
398-
return Flux.from(publisher).
399-
takeWhile(dataBuffer -> {
400-
int delta = -dataBuffer.readableByteCount();
401-
long currentCount = byteCountDown.getAndAdd(delta);
402-
return currentCount >= 0;
403-
}).
404-
map(dataBuffer -> {
405-
long currentCount = byteCountDown.get();
406-
if (currentCount >= 0) {
407-
return dataBuffer;
408-
}
409-
else {
410-
// last buffer
411-
int size = (int) (currentCount + dataBuffer.readableByteCount());
412-
return dataBuffer.slice(0, size);
413-
}
396+
AtomicLong countDown = new AtomicLong(maxByteCount);
397+
398+
return Flux.from(publisher)
399+
.takeWhile(buffer -> {
400+
int delta = -buffer.readableByteCount();
401+
return countDown.getAndAdd(delta) >= 0;
402+
})
403+
.map(buffer -> {
404+
long count = countDown.get();
405+
return count >= 0 ? buffer : buffer.slice(0, buffer.readableByteCount() + (int) count);
414406
});
415407
}
416408

@@ -427,27 +419,23 @@ public static Flux<DataBuffer> skipUntilByteCount(Publisher<DataBuffer> publishe
427419
Assert.isTrue(maxByteCount >= 0, "'maxByteCount' must be a positive number");
428420
AtomicLong byteCountDown = new AtomicLong(maxByteCount);
429421

430-
return Flux.from(publisher).
431-
skipUntil(dataBuffer -> {
432-
int delta = -dataBuffer.readableByteCount();
433-
long currentCount = byteCountDown.addAndGet(delta);
434-
if (currentCount < 0) {
435-
return true;
436-
}
437-
else {
438-
DataBufferUtils.release(dataBuffer);
422+
return Flux.from(publisher)
423+
.skipUntil(buffer -> {
424+
int delta = -buffer.readableByteCount();
425+
if (byteCountDown.addAndGet(delta) >= 0) {
426+
DataBufferUtils.release(buffer);
439427
return false;
440428
}
441-
}).
442-
map(dataBuffer -> {
443-
long currentCount = byteCountDown.get();
444-
// slice first buffer, then let others flow through
445-
if (currentCount < 0) {
446-
int skip = (int) (currentCount + dataBuffer.readableByteCount());
429+
return true;
430+
})
431+
.map(buffer -> {
432+
long count = byteCountDown.get();
433+
if (count < 0) {
434+
int skipCount = buffer.readableByteCount() + (int) count;
447435
byteCountDown.set(0);
448-
return dataBuffer.slice(skip, dataBuffer.readableByteCount() - skip);
436+
return buffer.slice(skipCount, buffer.readableByteCount() - skipCount);
449437
}
450-
return dataBuffer;
438+
return buffer;
451439
});
452440
}
453441

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ public void writeAsynchronousFileChannel() throws Exception {
225225
}
226226

227227
@Test
228-
public void takeUntilByteCount() throws Exception {
228+
public void takeUntilByteCount() {
229229
DataBuffer foo = stringBuffer("foo");
230230
DataBuffer bar = stringBuffer("bar");
231231
DataBuffer baz = stringBuffer("baz");
@@ -242,7 +242,7 @@ public void takeUntilByteCount() throws Exception {
242242
}
243243

244244
@Test
245-
public void skipUntilByteCount() throws Exception {
245+
public void skipUntilByteCount() {
246246
DataBuffer foo = stringBuffer("foo");
247247
DataBuffer bar = stringBuffer("bar");
248248
DataBuffer baz = stringBuffer("baz");
@@ -257,7 +257,7 @@ public void skipUntilByteCount() throws Exception {
257257
}
258258

259259
@Test
260-
public void skipUntilByteCountShouldSkipAll() throws Exception {
260+
public void skipUntilByteCountShouldSkipAll() {
261261
DataBuffer foo = stringBuffer("foo");
262262
DataBuffer bar = stringBuffer("bar");
263263
DataBuffer baz = stringBuffer("baz");

0 commit comments

Comments
 (0)