4040 * or Collections of {@link ResourceRegion ResourceRegions}.
4141 *
4242 * @author Brian Clozel
43+ * @author Juergen Hoeller
4344 * @since 4.3
4445 */
4546public class ResourceRegionHttpMessageConverter extends AbstractGenericHttpMessageConverter <Object > {
@@ -58,7 +59,7 @@ protected MediaType getDefaultContentType(Object object) {
5859 }
5960 else {
6061 Collection <ResourceRegion > regions = (Collection <ResourceRegion >) object ;
61- if (regions .size () > 0 ) {
62+ if (! regions .isEmpty () ) {
6263 resource = regions .iterator ().next ().getResource ();
6364 }
6465 }
@@ -141,13 +142,15 @@ protected void writeInternal(Object object, Type type, HttpOutputMessage outputM
141142 protected void writeResourceRegion (ResourceRegion region , HttpOutputMessage outputMessage ) throws IOException {
142143 Assert .notNull (region , "ResourceRegion must not be null" );
143144 HttpHeaders responseHeaders = outputMessage .getHeaders ();
145+
144146 long start = region .getPosition ();
145147 long end = start + region .getCount () - 1 ;
146148 Long resourceLength = region .getResource ().contentLength ();
147149 end = Math .min (end , resourceLength - 1 );
148150 long rangeLength = end - start + 1 ;
149151 responseHeaders .add ("Content-Range" , "bytes " + start + '-' + end + '/' + resourceLength );
150152 responseHeaders .setContentLength (rangeLength );
153+
151154 InputStream in = region .getResource ().getInputStream ();
152155 try {
153156 StreamUtils .copyRange (in , outputMessage .getBody (), start , end );
@@ -167,30 +170,43 @@ private void writeResourceRegionCollection(Collection<ResourceRegion> resourceRe
167170
168171 Assert .notNull (resourceRegions , "Collection of ResourceRegion should not be null" );
169172 HttpHeaders responseHeaders = outputMessage .getHeaders ();
173+
170174 MediaType contentType = responseHeaders .getContentType ();
171175 String boundaryString = MimeTypeUtils .generateMultipartBoundaryString ();
172176 responseHeaders .set (HttpHeaders .CONTENT_TYPE , "multipart/byteranges; boundary=" + boundaryString );
173177 OutputStream out = outputMessage .getBody ();
178+
174179 for (ResourceRegion region : resourceRegions ) {
175180 long start = region .getPosition ();
176181 long end = start + region .getCount () - 1 ;
177182 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.
184185 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+ }
185207 }
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 );
193208 }
209+
194210 println (out );
195211 print (out , "--" + boundaryString + "--" );
196212 }
0 commit comments