Search in sources :

Example 6 with ResourceRegion

use of org.springframework.core.io.support.ResourceRegion in project spring-framework by spring-projects.

the class HttpRangeTests method toResourceRegionsValidations.

@Test
public void toResourceRegionsValidations() {
    byte[] bytes = "12345".getBytes(StandardCharsets.UTF_8);
    ByteArrayResource resource = new ByteArrayResource(bytes);
    // 1. Below length
    List<HttpRange> belowLengthRanges = HttpRange.parseRanges("bytes=0-1,2-3");
    List<ResourceRegion> regions = HttpRange.toResourceRegions(belowLengthRanges, resource);
    assertThat(regions.size()).isEqualTo(2);
    // 2. At length
    List<HttpRange> atLengthRanges = HttpRange.parseRanges("bytes=0-1,2-4");
    assertThatIllegalArgumentException().isThrownBy(() -> HttpRange.toResourceRegions(atLengthRanges, resource));
}
Also used : ResourceRegion(org.springframework.core.io.support.ResourceRegion) ByteArrayResource(org.springframework.core.io.ByteArrayResource) Test(org.junit.jupiter.api.Test)

Example 7 with ResourceRegion

use of org.springframework.core.io.support.ResourceRegion in project spring-framework by spring-projects.

the class HttpRangeTests method toResourceRegion.

@Test
public void toResourceRegion() {
    byte[] bytes = "Spring Framework".getBytes(StandardCharsets.UTF_8);
    ByteArrayResource resource = new ByteArrayResource(bytes);
    HttpRange range = HttpRange.createByteRange(0, 5);
    ResourceRegion region = range.toResourceRegion(resource);
    assertThat(region.getResource()).isEqualTo(resource);
    assertThat(region.getPosition()).isEqualTo(0L);
    assertThat(region.getCount()).isEqualTo(6L);
}
Also used : ResourceRegion(org.springframework.core.io.support.ResourceRegion) ByteArrayResource(org.springframework.core.io.ByteArrayResource) Test(org.junit.jupiter.api.Test)

Example 8 with ResourceRegion

use of org.springframework.core.io.support.ResourceRegion in project spring-framework by spring-projects.

the class ResourceRegionEncoder method writeResourceRegion.

private Flux<DataBuffer> writeResourceRegion(ResourceRegion region, DataBufferFactory bufferFactory, @Nullable Map<String, Object> hints) {
    Resource resource = region.getResource();
    long position = region.getPosition();
    long count = region.getCount();
    if (logger.isDebugEnabled() && !Hints.isLoggingSuppressed(hints)) {
        logger.debug(Hints.getLogPrefix(hints) + "Writing region " + position + "-" + (position + count) + " of [" + resource + "]");
    }
    Flux<DataBuffer> in = DataBufferUtils.read(resource, position, bufferFactory, this.bufferSize);
    if (logger.isDebugEnabled()) {
        in = in.doOnNext(buffer -> Hints.touchDataBuffer(buffer, hints, logger));
    }
    return DataBufferUtils.takeUntilByteCount(in, count);
}
Also used : StreamUtils(org.springframework.util.StreamUtils) Publisher(org.reactivestreams.Publisher) IOException(java.io.IOException) Mono(reactor.core.publisher.Mono) MimeTypeUtils(org.springframework.util.MimeTypeUtils) DataBuffer(org.springframework.core.io.buffer.DataBuffer) ResourceRegion(org.springframework.core.io.support.ResourceRegion) StandardCharsets(java.nio.charset.StandardCharsets) MimeType(org.springframework.util.MimeType) OptionalLong(java.util.OptionalLong) Flux(reactor.core.publisher.Flux) DataBufferFactory(org.springframework.core.io.buffer.DataBufferFactory) Map(java.util.Map) InputStreamResource(org.springframework.core.io.InputStreamResource) DataBufferUtils(org.springframework.core.io.buffer.DataBufferUtils) Nullable(org.springframework.lang.Nullable) ResolvableType(org.springframework.core.ResolvableType) Resource(org.springframework.core.io.Resource) Assert(org.springframework.util.Assert) InputStreamResource(org.springframework.core.io.InputStreamResource) Resource(org.springframework.core.io.Resource) DataBuffer(org.springframework.core.io.buffer.DataBuffer)

Example 9 with ResourceRegion

use of org.springframework.core.io.support.ResourceRegion in project spring-framework by spring-projects.

the class ResourceRegionEncoderTests method shouldEncodeMultipleResourceRegionsFileResource.

@Test
void shouldEncodeMultipleResourceRegionsFileResource() {
    Resource resource = new ClassPathResource("ResourceRegionEncoderTests.txt", getClass());
    Flux<ResourceRegion> regions = Flux.just(new ResourceRegion(resource, 0, 6), new ResourceRegion(resource, 7, 9), new ResourceRegion(resource, 17, 4), new ResourceRegion(resource, 22, 17));
    String boundary = MimeTypeUtils.generateMultipartBoundaryString();
    Flux<DataBuffer> result = this.encoder.encode(regions, this.bufferFactory, ResolvableType.forClass(ResourceRegion.class), MimeType.valueOf("text/plain"), Collections.singletonMap(ResourceRegionEncoder.BOUNDARY_STRING_HINT, boundary));
    StepVerifier.create(result).consumeNextWith(stringConsumer("\r\n--" + boundary + "\r\n")).consumeNextWith(stringConsumer("Content-Type: text/plain\r\n")).consumeNextWith(stringConsumer("Content-Range: bytes 0-5/39\r\n\r\n")).consumeNextWith(stringConsumer("Spring")).consumeNextWith(stringConsumer("\r\n--" + boundary + "\r\n")).consumeNextWith(stringConsumer("Content-Type: text/plain\r\n")).consumeNextWith(stringConsumer("Content-Range: bytes 7-15/39\r\n\r\n")).consumeNextWith(stringConsumer("Framework")).consumeNextWith(stringConsumer("\r\n--" + boundary + "\r\n")).consumeNextWith(stringConsumer("Content-Type: text/plain\r\n")).consumeNextWith(stringConsumer("Content-Range: bytes 17-20/39\r\n\r\n")).consumeNextWith(stringConsumer("test")).consumeNextWith(stringConsumer("\r\n--" + boundary + "\r\n")).consumeNextWith(stringConsumer("Content-Type: text/plain\r\n")).consumeNextWith(stringConsumer("Content-Range: bytes 22-38/39\r\n\r\n")).consumeNextWith(stringConsumer("resource content.")).consumeNextWith(stringConsumer("\r\n--" + boundary + "--")).expectComplete().verify();
}
Also used : ResourceRegion(org.springframework.core.io.support.ResourceRegion) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) ClassPathResource(org.springframework.core.io.ClassPathResource) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.jupiter.api.Test)

Example 10 with ResourceRegion

use of org.springframework.core.io.support.ResourceRegion in project spring-framework by spring-projects.

the class ResourceRegionEncoderTests method cancelWithoutDemandForSingleResourceRegion.

// gh-22107
@Test
void cancelWithoutDemandForSingleResourceRegion() {
    Resource resource = new ClassPathResource("ResourceRegionEncoderTests.txt", getClass());
    Mono<ResourceRegion> regions = Mono.just(new ResourceRegion(resource, 0, 6));
    String boundary = MimeTypeUtils.generateMultipartBoundaryString();
    Flux<DataBuffer> flux = this.encoder.encode(regions, this.bufferFactory, ResolvableType.forClass(ResourceRegion.class), MimeType.valueOf("text/plain"), Collections.singletonMap(ResourceRegionEncoder.BOUNDARY_STRING_HINT, boundary));
    ZeroDemandSubscriber subscriber = new ZeroDemandSubscriber();
    flux.subscribe(subscriber);
    subscriber.cancel();
}
Also used : ResourceRegion(org.springframework.core.io.support.ResourceRegion) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) ClassPathResource(org.springframework.core.io.ClassPathResource) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.jupiter.api.Test)

Aggregations

ResourceRegion (org.springframework.core.io.support.ResourceRegion)20 Test (org.junit.jupiter.api.Test)12 ClassPathResource (org.springframework.core.io.ClassPathResource)10 Resource (org.springframework.core.io.Resource)10 DataBuffer (org.springframework.core.io.buffer.DataBuffer)8 HttpHeaders (org.springframework.http.HttpHeaders)7 HttpRange (org.springframework.http.HttpRange)5 MockHttpOutputMessage (org.springframework.http.MockHttpOutputMessage)5 MediaType (org.springframework.http.MediaType)3 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 ByteArrayResource (org.springframework.core.io.ByteArrayResource)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 StandardCharsets (java.nio.charset.StandardCharsets)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 OptionalLong (java.util.OptionalLong)1