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.
@@ -62,24 +62,11 @@ public ResourceRegion toResourceRegion(Resource resource) {
62
62
Assert .isTrue (resource .getClass () != InputStreamResource .class ,
63
63
"Cannot convert an InputStreamResource to a ResourceRegion" );
64
64
long contentLength = getLengthFor (resource );
65
- Assert .isTrue (contentLength > 0 , "Resource content length should be > 0" );
66
65
long start = getRangeStart (contentLength );
67
66
long end = getRangeEnd (contentLength );
68
67
return new ResourceRegion (resource , start , end - start + 1 );
69
68
}
70
69
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
70
/**
84
71
* Return the start of the range given the total length of a representation.
85
72
* @param length the length of the representation
@@ -131,8 +118,8 @@ public static HttpRange createSuffixRange(long suffixLength) {
131
118
* <p>This method can be used to parse an {@code Range} header.
132
119
* @param ranges the string to parse
133
120
* @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
136
123
*/
137
124
public static List <HttpRange > parseRanges (String ranges ) {
138
125
if (!StringUtils .hasLength (ranges )) {
@@ -144,7 +131,9 @@ public static List<HttpRange> parseRanges(String ranges) {
144
131
ranges = ranges .substring (BYTE_RANGE_PREFIX .length ());
145
132
146
133
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
+ }
148
137
List <HttpRange > result = new ArrayList <HttpRange >(tokens .length );
149
138
for (String token : tokens ) {
150
139
result .add (parseRange (token ));
@@ -158,7 +147,7 @@ private static HttpRange parseRange(String range) {
158
147
if (dashIdx > 0 ) {
159
148
long firstPos = Long .parseLong (range .substring (0 , dashIdx ));
160
149
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 ));
162
151
return new ByteRange (firstPos , lastPos );
163
152
}
164
153
else {
@@ -180,9 +169,8 @@ else if (dashIdx == 0) {
180
169
* @param ranges the list of ranges
181
170
* @param resource the resource to select the regions from
182
171
* @return the list of regions for the given resource
172
+ * @throws IllegalArgumentException if the sum of all ranges exceeds the resource length
183
173
* @since 4.3
184
- * @throws IllegalArgumentException if the sum of all ranges exceeds the
185
- * resource length.
186
174
*/
187
175
public static List <ResourceRegion > toResourceRegions (List <HttpRange > ranges , Resource resource ) {
188
176
if (CollectionUtils .isEmpty (ranges )) {
@@ -198,12 +186,25 @@ public static List<ResourceRegion> toResourceRegions(List<HttpRange> ranges, Res
198
186
for (ResourceRegion region : regions ) {
199
187
total += region .getCount ();
200
188
}
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
+ }
203
193
}
204
194
return regions ;
205
195
}
206
196
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
+
207
208
/**
208
209
* Return a string representation of the given list of {@code HttpRange} objects.
209
210
* <p>This method can be used to for an {@code Range} header.
0 commit comments