From 2f8cdef0580fcc45f5440fdb98157cdf9e3ffe89 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Mon, 8 Apr 2019 18:39:55 +0200 Subject: [PATCH] Avoid expensive Stream API usage in HttpRange See gh-22742 --- .../src/main/java/org/springframework/http/HttpRange.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/http/HttpRange.java b/spring-web/src/main/java/org/springframework/http/HttpRange.java index f1a7d33f513..3e0fbe3c123 100644 --- a/spring-web/src/main/java/org/springframework/http/HttpRange.java +++ b/spring-web/src/main/java/org/springframework/http/HttpRange.java @@ -170,8 +170,7 @@ public abstract class HttpRange { * @param ranges the list of ranges * @param resource the resource to select the regions from * @return the list of regions for the given resource - * @throws IllegalArgumentException if the sum of all ranges exceeds the - * resource length. + * @throws IllegalArgumentException if the sum of all ranges exceeds the resource length * @since 4.3 */ public static List toResourceRegions(List ranges, Resource resource) { @@ -184,7 +183,10 @@ public abstract class HttpRange { } if (ranges.size() > 1) { long length = getLengthFor(resource); - long total = regions.stream().map(ResourceRegion::getCount).reduce(0L, (count, sum) -> sum + count); + long total = 0; + for (ResourceRegion region : regions) { + total += region.getCount(); + } if (total >= length) { throw new IllegalArgumentException("The sum of all ranges (" + total + ") should be less than the resource length (" + length + ")");