1
1
/*
2
- * Copyright 2002-2018 the original author or authors.
2
+ * Copyright 2002-2019 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
@@ -68,18 +68,6 @@ public ResourceRegion toResourceRegion(Resource resource) {
68
68
return new ResourceRegion (resource , start , end - start + 1 );
69
69
}
70
70
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
-
83
71
/**
84
72
* Return the start of the range given the total length of a representation.
85
73
* @param length the length of the representation
@@ -131,8 +119,8 @@ public static HttpRange createSuffixRange(long suffixLength) {
131
119
* <p>This method can be used to parse an {@code Range} header.
132
120
* @param ranges the string to parse
133
121
* @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.
122
+ * @throws IllegalArgumentException if the string cannot be parsed
123
+ * or if the number of ranges is greater than 100
136
124
*/
137
125
public static List <HttpRange > parseRanges (@ Nullable String ranges ) {
138
126
if (!StringUtils .hasLength (ranges )) {
@@ -144,7 +132,9 @@ public static List<HttpRange> parseRanges(@Nullable String ranges) {
144
132
ranges = ranges .substring (BYTE_RANGE_PREFIX .length ());
145
133
146
134
String [] tokens = StringUtils .tokenizeToStringArray (ranges , "," );
147
- Assert .isTrue (tokens .length <= MAX_RANGES , () -> "Too many ranges " + tokens .length );
135
+ if (tokens .length > MAX_RANGES ) {
136
+ throw new IllegalArgumentException ("Too many ranges: " + tokens .length );
137
+ }
148
138
List <HttpRange > result = new ArrayList <>(tokens .length );
149
139
for (String token : tokens ) {
150
140
result .add (parseRange (token ));
@@ -158,7 +148,7 @@ private static HttpRange parseRange(String range) {
158
148
if (dashIdx > 0 ) {
159
149
long firstPos = Long .parseLong (range .substring (0 , dashIdx ));
160
150
if (dashIdx < range .length () - 1 ) {
161
- Long lastPos = Long .parseLong (range .substring (dashIdx + 1 , range . length () ));
151
+ Long lastPos = Long .parseLong (range .substring (dashIdx + 1 ));
162
152
return new ByteRange (firstPos , lastPos );
163
153
}
164
154
else {
@@ -195,13 +185,25 @@ public static List<ResourceRegion> toResourceRegions(List<HttpRange> ranges, Res
195
185
if (ranges .size () > 1 ) {
196
186
long length = getLengthFor (resource );
197
187
long total = regions .stream ().map (ResourceRegion ::getCount ).reduce (0L , (count , sum ) -> sum + count );
198
- Assert .isTrue (total < length ,
199
- () -> "The sum of all ranges (" + total + ") " +
200
- "should be less than the resource length (" + length + ")" );
188
+ if (total >= length ) {
189
+ throw new IllegalArgumentException ("The sum of all ranges (" + total +
190
+ ") should be less than the resource length (" + length + ")" );
191
+ }
201
192
}
202
193
return regions ;
203
194
}
204
195
196
+ private static long getLengthFor (Resource resource ) {
197
+ try {
198
+ long contentLength = resource .contentLength ();
199
+ Assert .isTrue (contentLength > 0 , "Resource content length should be > 0" );
200
+ return contentLength ;
201
+ }
202
+ catch (IOException ex ) {
203
+ throw new IllegalArgumentException ("Failed to obtain Resource content length" , ex );
204
+ }
205
+ }
206
+
205
207
/**
206
208
* Return a string representation of the given list of {@code HttpRange} objects.
207
209
* <p>This method can be used to for an {@code Range} header.
0 commit comments