Search in sources :

Example 1 with ZeroCopyHttpOutputMessage

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);
}
Also used : ZeroCopyHttpOutputMessage(org.springframework.http.ZeroCopyHttpOutputMessage) MediaType(org.springframework.http.MediaType) File(java.io.File) DataBufferFactory(org.springframework.core.io.buffer.DataBufferFactory) DataBuffer(org.springframework.core.io.buffer.DataBuffer)

Example 2 with ZeroCopyHttpOutputMessage

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();
}
Also used : ZeroCopyHttpOutputMessage(org.springframework.http.ZeroCopyHttpOutputMessage) IOException(java.io.IOException) File(java.io.File)

Aggregations

File (java.io.File)2 ZeroCopyHttpOutputMessage (org.springframework.http.ZeroCopyHttpOutputMessage)2 IOException (java.io.IOException)1 DataBuffer (org.springframework.core.io.buffer.DataBuffer)1 DataBufferFactory (org.springframework.core.io.buffer.DataBufferFactory)1 MediaType (org.springframework.http.MediaType)1