use of org.springframework.http.ZeroCopyHttpOutputMessage in project spring-framework by spring-projects.
the class ResourceRegionHttpMessageWriter method writeResourceRegion.
private Mono<Void> writeResourceRegion(ResourceRegion region, ReactiveHttpOutputMessage outputMessage) {
if (outputMessage instanceof ZeroCopyHttpOutputMessage) {
Optional<File> file = getFile(region.getResource());
if (file.isPresent()) {
ZeroCopyHttpOutputMessage zeroCopyResponse = (ZeroCopyHttpOutputMessage) outputMessage;
return zeroCopyResponse.writeWith(file.get(), region.getPosition(), region.getCount());
}
}
// non-zero copy fallback, using ResourceRegionEncoder
DataBufferFactory bufferFactory = outputMessage.bufferFactory();
MediaType contentType = outputMessage.getHeaders().getContentType();
Map<String, Object> hints = Collections.emptyMap();
Flux<DataBuffer> body = this.encoder.encode(Mono.just(region), bufferFactory, TYPE, contentType, hints);
return outputMessage.writeWith(body);
}
use of org.springframework.http.ZeroCopyHttpOutputMessage in project spring-framework by spring-projects.
the class ResourceHttpMessageWriter method zeroCopy.
private static Optional<Mono<Void>> zeroCopy(Resource resource, @Nullable ResourceRegion region, ReactiveHttpOutputMessage message, Map<String, Object> hints) {
if (message instanceof ZeroCopyHttpOutputMessage && resource.isFile()) {
try {
File file = resource.getFile();
long pos = region != null ? region.getPosition() : 0;
long count = region != null ? region.getCount() : file.length();
if (logger.isDebugEnabled()) {
String formatted = region != null ? "region " + pos + "-" + (count) + " of " : "";
logger.debug(Hints.getLogPrefix(hints) + "Zero-copy " + formatted + "[" + resource + "]");
}
return Optional.of(((ZeroCopyHttpOutputMessage) message).writeWith(file, pos, count));
} catch (IOException ex) {
// should not happen
}
}
return Optional.empty();
}
Aggregations