/** * Turn a {@code Resource} into a {@link ResourceRegion} using the range * information contained in the current {@code HttpRange}. * @param resource the {@code Resource} to select the region from * @return the selected region of the given {@code Resource} * @since 4.3 */ public ResourceRegion toResourceRegion(Resource resource) { // Don't try to determine contentLength on InputStreamResource - cannot be read afterwards... // Note: custom InputStreamResource subclasses could provide a pre-calculated content length! Assert.isTrue(resource.getClass() != InputStreamResource.class, "Cannot convert an InputStreamResource to a ResourceRegion"); long contentLength = getLengthFor(resource); long start = getRangeStart(contentLength); long end = getRangeEnd(contentLength); return new ResourceRegion(resource, start, end - start + 1); }
@Test(expected = IllegalArgumentException.class) public void shouldThrowExceptionForNegativePosition() { new ResourceRegion(mock(Resource.class), -1, 1); }
@Test(expected = IllegalArgumentException.class) public void shouldThrowExceptionWithNullResource() { new ResourceRegion(null, 0, 1); }
@Test(expected = IllegalArgumentException.class) public void shouldThrowExceptionForNegativeCount() { new ResourceRegion(mock(Resource.class), 0, -1); }
/** * Turn a {@code Resource} into a {@link ResourceRegion} using the range * information contained in the current {@code HttpRange}. * @param resource the {@code Resource} to select the region from * @return the selected region of the given {@code Resource} * @since 4.3 */ public ResourceRegion toResourceRegion(Resource resource) { // Don't try to determine contentLength on InputStreamResource - cannot be read afterwards... // Note: custom InputStreamResource subclasses could provide a pre-calculated content length! Assert.isTrue(resource.getClass() != InputStreamResource.class, "Cannot convert an InputStreamResource to a ResourceRegion"); long contentLength = getLengthFor(resource); long start = getRangeStart(contentLength); long end = getRangeEnd(contentLength); return new ResourceRegion(resource, start, end - start + 1); }
@Test public void shouldEncodeResourceRegionFileResource() throws Exception { ResourceRegion region = new ResourceRegion( new ClassPathResource("ResourceRegionEncoderTests.txt", getClass()), 0, 6); Flux<DataBuffer> result = this.encoder.encode(Mono.just(region), this.bufferFactory, ResolvableType.forClass(ResourceRegion.class), MimeTypeUtils.APPLICATION_OCTET_STREAM, Collections.emptyMap()); StepVerifier.create(result) .consumeNextWith(stringConsumer("Spring")) .expectComplete() .verify(); }
@Test public void nonExisting() { Resource resource = new ClassPathResource("ResourceRegionEncoderTests.txt", getClass()); Resource nonExisting = new ClassPathResource("does not exist", getClass()); Flux<ResourceRegion> regions = Flux.just( new ResourceRegion(resource, 0, 6), new ResourceRegion(nonExisting, 0, 6)); 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")) .expectError(EncodingException.class) .verify(); }
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();
/******************************************************************************************************************* * * Returns a {@link ResourceRegion} mapping the portion of bytes matching this range. * * @param resource the resource * @return the region * ******************************************************************************************************************/ @Nonnull public ResourceRegion getRegion (final @Nonnull Resource resource) { return new ResourceRegion(resource, start, length); }
/** * Turn a {@code Resource} into a {@link ResourceRegion} using the range * information contained in the current {@code HttpRange}. * @param resource the {@code Resource} to select the region from * @return the selected region of the given {@code Resource} * @since 4.3 */ public ResourceRegion toResourceRegion(Resource resource) { // Don't try to determine contentLength on InputStreamResource - cannot be read afterwards... // Note: custom InputStreamResource subclasses could provide a pre-calculated content length! Assert.isTrue(resource.getClass() != InputStreamResource.class, "Cannot convert an InputStreamResource to a ResourceRegion"); long contentLength = getLengthFor(resource); long start = getRangeStart(contentLength); long end = getRangeEnd(contentLength); return new ResourceRegion(resource, start, end - start + 1); }
/** * Turn a {@code Resource} into a {@link ResourceRegion} using the range * information contained in the current {@code HttpRange}. * @param resource the {@code Resource} to select the region from * @return the selected region of the given {@code Resource} * @since 4.3 */ public ResourceRegion toResourceRegion(Resource resource) { // Don't try to determine contentLength on InputStreamResource - cannot be read afterwards... // Note: custom InputStreamResource subclasses could provide a pre-calculated content length! Assert.isTrue(resource.getClass() != InputStreamResource.class, "Cannot convert an InputStreamResource to a ResourceRegion"); long contentLength = getLengthFor(resource); long start = getRangeStart(contentLength); long end = getRangeEnd(contentLength); return new ResourceRegion(resource, start, end - start + 1); }