40
40
* or Collections of {@link ResourceRegion ResourceRegions}.
41
41
*
42
42
* @author Brian Clozel
43
+ * @author Juergen Hoeller
43
44
* @since 4.3
44
45
*/
45
46
public class ResourceRegionHttpMessageConverter extends AbstractGenericHttpMessageConverter <Object > {
@@ -58,7 +59,7 @@ protected MediaType getDefaultContentType(Object object) {
58
59
}
59
60
else {
60
61
Collection <ResourceRegion > regions = (Collection <ResourceRegion >) object ;
61
- if (regions .size () > 0 ) {
62
+ if (! regions .isEmpty () ) {
62
63
resource = regions .iterator ().next ().getResource ();
63
64
}
64
65
}
@@ -141,13 +142,15 @@ protected void writeInternal(Object object, Type type, HttpOutputMessage outputM
141
142
protected void writeResourceRegion (ResourceRegion region , HttpOutputMessage outputMessage ) throws IOException {
142
143
Assert .notNull (region , "ResourceRegion must not be null" );
143
144
HttpHeaders responseHeaders = outputMessage .getHeaders ();
145
+
144
146
long start = region .getPosition ();
145
147
long end = start + region .getCount () - 1 ;
146
148
Long resourceLength = region .getResource ().contentLength ();
147
149
end = Math .min (end , resourceLength - 1 );
148
150
long rangeLength = end - start + 1 ;
149
151
responseHeaders .add ("Content-Range" , "bytes " + start + '-' + end + '/' + resourceLength );
150
152
responseHeaders .setContentLength (rangeLength );
153
+
151
154
InputStream in = region .getResource ().getInputStream ();
152
155
try {
153
156
StreamUtils .copyRange (in , outputMessage .getBody (), start , end );
@@ -167,30 +170,43 @@ private void writeResourceRegionCollection(Collection<ResourceRegion> resourceRe
167
170
168
171
Assert .notNull (resourceRegions , "Collection of ResourceRegion should not be null" );
169
172
HttpHeaders responseHeaders = outputMessage .getHeaders ();
173
+
170
174
MediaType contentType = responseHeaders .getContentType ();
171
175
String boundaryString = MimeTypeUtils .generateMultipartBoundaryString ();
172
176
responseHeaders .set (HttpHeaders .CONTENT_TYPE , "multipart/byteranges; boundary=" + boundaryString );
173
177
OutputStream out = outputMessage .getBody ();
178
+
174
179
for (ResourceRegion region : resourceRegions ) {
175
180
long start = region .getPosition ();
176
181
long end = start + region .getCount () - 1 ;
177
182
InputStream in = region .getResource ().getInputStream ();
178
- // Writing MIME header.
179
- println (out );
180
- print (out , "--" + boundaryString );
181
- println (out );
182
- if (contentType != null ) {
183
- print (out , "Content-Type: " + contentType .toString ());
183
+ try {
184
+ // Writing MIME header.
184
185
println (out );
186
+ print (out , "--" + boundaryString );
187
+ println (out );
188
+ if (contentType != null ) {
189
+ print (out , "Content-Type: " + contentType .toString ());
190
+ println (out );
191
+ }
192
+ Long resourceLength = region .getResource ().contentLength ();
193
+ end = Math .min (end , resourceLength - 1 );
194
+ print (out , "Content-Range: bytes " + start + '-' + end + '/' + resourceLength );
195
+ println (out );
196
+ println (out );
197
+ // Printing content
198
+ StreamUtils .copyRange (in , out , start , end );
199
+ }
200
+ finally {
201
+ try {
202
+ in .close ();
203
+ }
204
+ catch (IOException ex ) {
205
+ // ignore
206
+ }
185
207
}
186
- Long resourceLength = region .getResource ().contentLength ();
187
- end = Math .min (end , resourceLength - 1 );
188
- print (out , "Content-Range: bytes " + start + '-' + end + '/' + resourceLength );
189
- println (out );
190
- println (out );
191
- // Printing content
192
- StreamUtils .copyRange (in , out , start , end );
193
208
}
209
+
194
210
println (out );
195
211
print (out , "--" + boundaryString + "--" );
196
212
}
0 commit comments