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); return DataBufferUtils.takeUntilByteCount(in, count); }
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); return DataBufferUtils.takeUntilByteCount(in, count); }
/** * Consume up to the specified number of bytes from the response body and * cancel if any more data arrives. * <p>Internally delegates to {@link DataBufferUtils#takeUntilByteCount}. * @param maxByteCount the limit as number of bytes * @return the filter to limit the response size with * @since 5.1 */ public static ExchangeFilterFunction limitResponseSize(long maxByteCount) { return (request, next) -> next.exchange(request).map(response -> { Flux<DataBuffer> body = response.body(BodyExtractors.toDataBuffers()); body = DataBufferUtils.takeUntilByteCount(body, maxByteCount); return ClientResponse.from(response).body(body).build(); }); }
@Test public void readResourcePositionAndTakeUntil() throws Exception { Resource resource = new ClassPathResource("DataBufferUtilsTests.txt", getClass()); Flux<DataBuffer> flux = DataBufferUtils.read(resource, 3, this.bufferFactory, 3); flux = DataBufferUtils.takeUntilByteCount(flux, 5); StepVerifier.create(flux) .consumeNextWith(stringConsumer("bar")) .consumeNextWith(stringConsumer("ba")) .expectComplete() .verify(Duration.ofSeconds(5)); }
@Test public void takeUntilByteCountError() { Flux<DataBuffer> source = Flux.concat( Mono.defer(() -> Mono.just(stringBuffer("foo"))), Mono.error(new RuntimeException()) ); Flux<DataBuffer> result = DataBufferUtils.takeUntilByteCount(source, 5L); StepVerifier.create(result) .consumeNextWith(stringConsumer("foo")) .expectError(RuntimeException.class) .verify(Duration.ofSeconds(5)); }
@Test public void readByteArrayResourcePositionAndTakeUntil() throws Exception { Resource resource = new ByteArrayResource("foobarbazqux" .getBytes()); Flux<DataBuffer> flux = DataBufferUtils.read(resource, 3, this.bufferFactory, 3); flux = DataBufferUtils.takeUntilByteCount(flux, 5); StepVerifier.create(flux) .consumeNextWith(stringConsumer("bar")) .consumeNextWith(stringConsumer("ba")) .expectComplete() .verify(Duration.ofSeconds(5)); }
@Test public void takeUntilByteCountCanceled() { Flux<DataBuffer> source = Flux.concat( deferStringBuffer("foo"), deferStringBuffer("bar") ); Flux<DataBuffer> result = DataBufferUtils.takeUntilByteCount( source, 5L); StepVerifier.create(result) .consumeNextWith(stringConsumer("foo")) .thenCancel() .verify(Duration.ofSeconds(5)); }
@Test public void takeUntilByteCount() { Flux<DataBuffer> result = DataBufferUtils.takeUntilByteCount( Flux.just(stringBuffer("foo"), stringBuffer("bar")), 5L); StepVerifier.create(result) .consumeNextWith(stringConsumer("foo")) .consumeNextWith(stringConsumer("ba")) .expectComplete() .verify(Duration.ofSeconds(5)); }
@Test public void takeUntilByteCountExact() { Flux<DataBuffer> source = Flux.concat( deferStringBuffer("foo"), deferStringBuffer("bar"), deferStringBuffer("baz") ); Flux<DataBuffer> result = DataBufferUtils.takeUntilByteCount(source, 6L); StepVerifier.create(result) .consumeNextWith(stringConsumer("foo")) .consumeNextWith(stringConsumer("bar")) .expectComplete() .verify(Duration.ofSeconds(5)); }
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); return DataBufferUtils.takeUntilByteCount(in, count); }
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); return DataBufferUtils.takeUntilByteCount(in, count); }