Example usage for org.springframework.core.io.support ResourceRegion getCount

List of usage examples for org.springframework.core.io.support ResourceRegion getCount

Introduction

In this page you can find the example usage for org.springframework.core.io.support ResourceRegion getCount.

Prototype

public long getCount() 

Source Link

Document

Return the byte count of this region in the underlying Resource .

Usage

From source file:org.springframework.http.codec.ResourceHttpMessageWriter.java

private static Optional<Mono<Void>> zeroCopy(Resource resource, @Nullable ResourceRegion region,
        ReactiveHttpOutputMessage message, Map<String, Object> hints) {

    if (message instanceof ZeroCopyHttpOutputMessage && resource.isFile()) {
        try {/*w w w . ja v a 2 s  .  c o m*/
            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();
}

From source file:org.springframework.http.codec.ResourceHttpMessageWriter.java

@Override
@SuppressWarnings("unchecked")
public Mono<Void> write(Publisher<? extends Resource> inputStream, @Nullable ResolvableType actualType,
        ResolvableType elementType, @Nullable MediaType mediaType, ServerHttpRequest request,
        ServerHttpResponse response, Map<String, Object> hints) {

    HttpHeaders headers = response.getHeaders();
    headers.set(HttpHeaders.ACCEPT_RANGES, "bytes");

    List<HttpRange> ranges;
    try {/*from ww  w. j  av  a2s .  c om*/
        ranges = request.getHeaders().getRange();
    } catch (IllegalArgumentException ex) {
        response.setStatusCode(HttpStatus.REQUESTED_RANGE_NOT_SATISFIABLE);
        return response.setComplete();
    }

    return Mono.from(inputStream).flatMap(resource -> {

        if (ranges.isEmpty()) {
            return writeResource(resource, elementType, mediaType, response, hints);
        }

        response.setStatusCode(HttpStatus.PARTIAL_CONTENT);
        List<ResourceRegion> regions = HttpRange.toResourceRegions(ranges, resource);
        MediaType resourceMediaType = getResourceMediaType(mediaType, resource, hints);

        if (regions.size() == 1) {
            ResourceRegion region = regions.get(0);
            headers.setContentType(resourceMediaType);
            long contentLength = lengthOf(resource);
            if (contentLength != -1) {
                long start = region.getPosition();
                long end = start + region.getCount() - 1;
                end = Math.min(end, contentLength - 1);
                headers.add("Content-Range", "bytes " + start + '-' + end + '/' + contentLength);
                headers.setContentLength(end - start + 1);
            }
            return writeSingleRegion(region, response, hints);
        } else {
            String boundary = MimeTypeUtils.generateMultipartBoundaryString();
            MediaType multipartType = MediaType.parseMediaType("multipart/byteranges;boundary=" + boundary);
            headers.setContentType(multipartType);
            Map<String, Object> allHints = Hints.merge(hints, ResourceRegionEncoder.BOUNDARY_STRING_HINT,
                    boundary);
            return encodeAndWriteRegions(Flux.fromIterable(regions), resourceMediaType, response, allHints);
        }
    });
}