Skip to content

Commit 5d1787b

Browse files
committed
Avoid expensive assertions in HttpRange
Closes gh-22742
1 parent f328bfc commit 5d1787b

File tree

1 file changed

+23
-22
lines changed

1 file changed

+23
-22
lines changed

spring-web/src/main/java/org/springframework/http/HttpRange.java

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -62,24 +62,11 @@ public ResourceRegion toResourceRegion(Resource resource) {
6262
Assert.isTrue(resource.getClass() != InputStreamResource.class,
6363
"Cannot convert an InputStreamResource to a ResourceRegion");
6464
long contentLength = getLengthFor(resource);
65-
Assert.isTrue(contentLength > 0, "Resource content length should be > 0");
6665
long start = getRangeStart(contentLength);
6766
long end = getRangeEnd(contentLength);
6867
return new ResourceRegion(resource, start, end - start + 1);
6968
}
7069

71-
private static long getLengthFor(Resource resource) {
72-
long contentLength;
73-
try {
74-
contentLength = resource.contentLength();
75-
Assert.isTrue(contentLength > 0, "Resource content length should be > 0");
76-
}
77-
catch (IOException ex) {
78-
throw new IllegalArgumentException("Failed to obtain Resource content length", ex);
79-
}
80-
return contentLength;
81-
}
82-
8370
/**
8471
* Return the start of the range given the total length of a representation.
8572
* @param length the length of the representation
@@ -131,8 +118,8 @@ public static HttpRange createSuffixRange(long suffixLength) {
131118
* <p>This method can be used to parse an {@code Range} header.
132119
* @param ranges the string to parse
133120
* @return the list of ranges
134-
* @throws IllegalArgumentException if the string cannot be parsed, or if
135-
* the number of ranges is greater than 100.
121+
* @throws IllegalArgumentException if the string cannot be parsed
122+
* or if the number of ranges is greater than 100
136123
*/
137124
public static List<HttpRange> parseRanges(String ranges) {
138125
if (!StringUtils.hasLength(ranges)) {
@@ -144,7 +131,9 @@ public static List<HttpRange> parseRanges(String ranges) {
144131
ranges = ranges.substring(BYTE_RANGE_PREFIX.length());
145132

146133
String[] tokens = StringUtils.tokenizeToStringArray(ranges, ",");
147-
Assert.isTrue(tokens.length <= MAX_RANGES, "Too many ranges " + tokens.length);
134+
if (tokens.length > MAX_RANGES) {
135+
throw new IllegalArgumentException("Too many ranges: " + tokens.length);
136+
}
148137
List<HttpRange> result = new ArrayList<HttpRange>(tokens.length);
149138
for (String token : tokens) {
150139
result.add(parseRange(token));
@@ -158,7 +147,7 @@ private static HttpRange parseRange(String range) {
158147
if (dashIdx > 0) {
159148
long firstPos = Long.parseLong(range.substring(0, dashIdx));
160149
if (dashIdx < range.length() - 1) {
161-
Long lastPos = Long.parseLong(range.substring(dashIdx + 1, range.length()));
150+
Long lastPos = Long.parseLong(range.substring(dashIdx + 1));
162151
return new ByteRange(firstPos, lastPos);
163152
}
164153
else {
@@ -180,9 +169,8 @@ else if (dashIdx == 0) {
180169
* @param ranges the list of ranges
181170
* @param resource the resource to select the regions from
182171
* @return the list of regions for the given resource
172+
* @throws IllegalArgumentException if the sum of all ranges exceeds the resource length
183173
* @since 4.3
184-
* @throws IllegalArgumentException if the sum of all ranges exceeds the
185-
* resource length.
186174
*/
187175
public static List<ResourceRegion> toResourceRegions(List<HttpRange> ranges, Resource resource) {
188176
if (CollectionUtils.isEmpty(ranges)) {
@@ -198,12 +186,25 @@ public static List<ResourceRegion> toResourceRegions(List<HttpRange> ranges, Res
198186
for (ResourceRegion region : regions) {
199187
total += region.getCount();
200188
}
201-
Assert.isTrue(total < length, "The sum of all ranges (" + total + ") " +
202-
"should be less than the resource length (" + length + ")");
189+
if (total >= length) {
190+
throw new IllegalArgumentException("The sum of all ranges (" + total +
191+
") should be less than the resource length (" + length + ")");
192+
}
203193
}
204194
return regions;
205195
}
206196

197+
private static long getLengthFor(Resource resource) {
198+
try {
199+
long contentLength = resource.contentLength();
200+
Assert.isTrue(contentLength > 0, "Resource content length should be > 0");
201+
return contentLength;
202+
}
203+
catch (IOException ex) {
204+
throw new IllegalArgumentException("Failed to obtain Resource content length", ex);
205+
}
206+
}
207+
207208
/**
208209
* Return a string representation of the given list of {@code HttpRange} objects.
209210
* <p>This method can be used to for an {@code Range} header.

0 commit comments

Comments
 (0)