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));
}
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);
}
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);
}
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();
}
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();
}
Aggregations