Skip to content

Commit

Permalink
Reuse InputStream for ResourceRegionHttpMessageConverter
Browse files Browse the repository at this point in the history
Dynamically generated input streams cannot be closed and reopened
multiple times. This fix reuses the same InputStream across multiple
ResourceRegion writes.

Closes spring-projects#24210
  • Loading branch information
randomnicode committed Dec 14, 2019
1 parent 5f91780 commit 7fff0b9
Showing 1 changed file with 6 additions and 13 deletions.
Expand Up @@ -179,11 +179,10 @@ private void writeResourceRegionCollection(Collection<ResourceRegion> resourceRe
responseHeaders.set(HttpHeaders.CONTENT_TYPE, "multipart/byteranges; boundary=" + boundaryString);
OutputStream out = outputMessage.getBody();

for (ResourceRegion region : resourceRegions) {
long start = region.getPosition();
long end = start + region.getCount() - 1;
InputStream in = region.getResource().getInputStream();
try {
try (InputStream in = region.getResource().getInputStream()) {
for (ResourceRegion region : resourceRegions) {
long start = region.getPosition();
long end = start + region.getCount() - 1;
// Writing MIME header.
println(out);
print(out, "--" + boundaryString);
Expand All @@ -200,14 +199,8 @@ private void writeResourceRegionCollection(Collection<ResourceRegion> resourceRe
// Printing content
StreamUtils.copyRange(in, out, start, end);
}
finally {
try {
in.close();
}
catch (IOException ex) {
// ignore
}
}
} catch (IOException ex) {
// ignore
}

println(out);
Expand Down

0 comments on commit 7fff0b9

Please sign in to comment.