@@ -393,24 +393,16 @@ private static void closeChannel(@Nullable Channel channel) {
393
393
public static Flux <DataBuffer > takeUntilByteCount (Publisher <DataBuffer > publisher , long maxByteCount ) {
394
394
Assert .notNull (publisher , "Publisher must not be null" );
395
395
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 );
414
406
});
415
407
}
416
408
@@ -427,27 +419,23 @@ public static Flux<DataBuffer> skipUntilByteCount(Publisher<DataBuffer> publishe
427
419
Assert .isTrue (maxByteCount >= 0 , "'maxByteCount' must be a positive number" );
428
420
AtomicLong byteCountDown = new AtomicLong (maxByteCount );
429
421
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 );
439
427
return false ;
440
428
}
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 ;
447
435
byteCountDown .set (0 );
448
- return dataBuffer .slice (skip , dataBuffer .readableByteCount () - skip );
436
+ return buffer .slice (skipCount , buffer .readableByteCount () - skipCount );
449
437
}
450
- return dataBuffer ;
438
+ return buffer ;
451
439
});
452
440
}
453
441
0 commit comments