Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reuse InputStream for ResourceRegionHttpMessageConverter #24214

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -22,7 +22,13 @@
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
import java.util.AbstractMap.SimpleEntry;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.springframework.core.io.Resource;
import org.springframework.core.io.support.ResourceRegion;
Expand Down Expand Up @@ -178,12 +184,53 @@ private void writeResourceRegionCollection(Collection<ResourceRegion> resourceRe
String boundaryString = MimeTypeUtils.generateMultipartBoundaryString();
responseHeaders.set(HttpHeaders.CONTENT_TYPE, "multipart/byteranges; boundary=" + boundaryString);
OutputStream out = outputMessage.getBody();
// Allows reuse of streams
Map<Resource, Entry<InputStream, Long>> currStreams = new HashMap<>();
List<IOException> exs = new ArrayList<>();

try {
for (ResourceRegion region : resourceRegions) {
// retrieve an existing stream or generate a new one
Entry<InputStream, Long> input = currStreams.computeIfAbsent(region.getResource(), r -> {
try {
return new SimpleEntry<>(r.getInputStream(), 0L);
} catch (IOException e) {
exs.add(e);
return null;
}
});

if (!exs.isEmpty()) {
throw exs.get(0);
}

// offset the existing stream location from the byte start so appropriate number of bytes are skipped
long start = region.getPosition() - input.getValue();

// check if range is out of order (stream pointer has advanced beyond what the range is requesting)
if (start < 0) {
// close existing
input.getKey().close();
// open new stream
input = currStreams.computeIfPresent(region.getResource(), (r, i) -> {
try {
return new SimpleEntry<>(r.getInputStream(), 0L);
} catch (IOException e) {
exs.add(e);
return null;
}
});

if (!exs.isEmpty()) {
throw exs.get(0);
}

// reset start
start = region.getPosition();
}

long end = start + region.getCount() - 1;

for (ResourceRegion region : resourceRegions) {
long start = region.getPosition();
long end = start + region.getCount() - 1;
InputStream in = region.getResource().getInputStream();
try {
// Writing MIME header.
println(out);
print(out, "--" + boundaryString);
Expand All @@ -198,16 +245,19 @@ private void writeResourceRegionCollection(Collection<ResourceRegion> resourceRe
println(out);
println(out);
// Printing content
StreamUtils.copyRange(in, out, start, end);
StreamUtils.copyRange(input.getKey(), out, start, end);

//set stream position to reuse for next time
input.setValue(end + 1);
}
finally {
} finally {
currStreams.values().stream().map(e -> e.getKey()).forEach(in -> {
try {
in.close();
}
catch (IOException ex) {
} catch (IOException ex) {
// ignore
}
}
});
}

println(out);
Expand Down