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