Eliminate the need for Encoder#getContentLength

Issue: SPR-16892
This commit is contained in:
Rossen Stoyanchev
2018-06-04 15:46:58 -04:00
parent 124d4c833c
commit 010352163b
9 changed files with 55 additions and 72 deletions

View File

@@ -28,6 +28,7 @@ import reactor.core.publisher.Mono;
import org.springframework.core.ResolvableType;
import org.springframework.core.codec.Encoder;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ReactiveHttpOutputMessage;
@@ -98,23 +99,18 @@ public class EncoderHttpMessageWriter<T> implements HttpMessageWriter<T> {
@Nullable MediaType mediaType, ReactiveHttpOutputMessage message, Map<String, Object> hints) {
MediaType contentType = updateContentType(message, mediaType);
HttpHeaders headers = message.getHeaders();
if (headers.getContentLength() < 0 && !headers.containsKey(HttpHeaders.TRANSFER_ENCODING)) {
if (inputStream instanceof Mono) {
// This works because we don't actually commit until after the first signal...
inputStream = ((Mono<T>) inputStream).doOnNext(data -> {
Long contentLength = this.encoder.getContentLength(data, contentType);
if (contentLength != null) {
headers.setContentLength(contentLength);
}
});
}
}
Flux<DataBuffer> body = this.encoder.encode(
inputStream, message.bufferFactory(), elementType, contentType, hints);
// Response is not committed until the first signal...
if (inputStream instanceof Mono) {
HttpHeaders headers = message.getHeaders();
if (headers.getContentLength() < 0 && !headers.containsKey(HttpHeaders.TRANSFER_ENCODING)) {
body = body.doOnNext(data -> headers.setContentLength(data.readableByteCount()));
}
}
return (isStreamingMediaType(contentType) ?
message.writeAndFlushWith(body.map(Flux::just)) : message.writeWith(body));
}

View File

@@ -31,6 +31,7 @@ import org.springframework.core.ResolvableType;
import org.springframework.core.codec.ResourceDecoder;
import org.springframework.core.codec.ResourceEncoder;
import org.springframework.core.codec.ResourceRegionEncoder;
import org.springframework.core.io.InputStreamResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferFactory;
@@ -119,9 +120,9 @@ public class ResourceHttpMessageWriter implements HttpMessageWriter<Resource> {
headers.setContentType(resourceMediaType);
if (headers.getContentLength() < 0) {
Long contentLength = this.encoder.getContentLength(resource, mediaType);
if (contentLength != null) {
headers.setContentLength(contentLength);
long length = lengthOf(resource);
if (length != -1) {
headers.setContentLength(length);
}
}
@@ -141,6 +142,18 @@ public class ResourceHttpMessageWriter implements HttpMessageWriter<Resource> {
return MediaTypeFactory.getMediaType(resource).orElse(MediaType.APPLICATION_OCTET_STREAM);
}
private static long lengthOf(Resource resource) {
// Don't consume InputStream...
if (InputStreamResource.class != resource.getClass()) {
try {
return resource.contentLength();
}
catch (IOException ignored) {
}
}
return -1;
}
private static Optional<Mono<Void>> zeroCopy(Resource resource, @Nullable ResourceRegion region,
ReactiveHttpOutputMessage message) {
@@ -192,8 +205,8 @@ public class ResourceHttpMessageWriter implements HttpMessageWriter<Resource> {
if (regions.size() == 1){
ResourceRegion region = regions.get(0);
headers.setContentType(resourceMediaType);
Long contentLength = this.encoder.getContentLength(resource, mediaType);
if (contentLength != null) {
long contentLength = lengthOf(resource);
if (contentLength != -1) {
long start = region.getPosition();
long end = start + region.getCount() - 1;
end = Math.min(end, contentLength - 1);