diff --git a/spring-core/src/main/java/org/springframework/core/io/buffer/DataBuffers.java b/spring-core/src/main/java/org/springframework/core/io/buffer/DataBuffers.java index a7947b6bb0a..d18664f652a 100644 --- a/spring-core/src/main/java/org/springframework/core/io/buffer/DataBuffers.java +++ b/spring-core/src/main/java/org/springframework/core/io/buffer/DataBuffers.java @@ -175,21 +175,20 @@ private static NestedMatcher[] initMatchers(byte[][] delimiters) { public int match(DataBuffer dataBuffer) { this.longestDelimiter = NO_DELIMITER; - for (int pos = dataBuffer.readPosition(); pos < dataBuffer.writePosition(); pos++) { - byte b = dataBuffer.getByte(pos); + int matchPosition = dataBuffer.forEachByte( + dataBuffer.readPosition(), dataBuffer.readableByteCount(), b -> { + for (NestedMatcher matcher : this.matchers) { + if (matcher.match(b) && matcher.delimiter().length > this.longestDelimiter.length) { + this.longestDelimiter = matcher.delimiter(); + } + } + return (this.longestDelimiter == NO_DELIMITER); + }); - for (NestedMatcher matcher : this.matchers) { - if (matcher.match(b) && matcher.delimiter().length > this.longestDelimiter.length) { - this.longestDelimiter = matcher.delimiter(); - } - } - - if (this.longestDelimiter != NO_DELIMITER) { - reset(); - return pos; - } + if (matchPosition != -1) { + reset(); } - return -1; + return matchPosition; } @Override diff --git a/spring-core/src/test/java/org/springframework/core/io/buffer/DataBuffersTests.java b/spring-core/src/test/java/org/springframework/core/io/buffer/DataBuffersTests.java new file mode 100644 index 00000000000..843e6a1cfaf --- /dev/null +++ b/spring-core/src/test/java/org/springframework/core/io/buffer/DataBuffersTests.java @@ -0,0 +1,48 @@ +/* + * Copyright and licensing information for this source code can be found in + * the NOTICE and LICENSE files distributed with this project. + */ + +package org.springframework.core.io.buffer; + +import java.nio.charset.StandardCharsets; +import java.util.concurrent.atomic.AtomicInteger; + +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Tests for {@link DataBuffers}. + */ +class DataBuffersTests { + + @Test + void compositeMatcherUsesBulkAccess() { + DataBuffer delegate = new DefaultDataBufferFactory() + .wrap("--boundaryXline one".getBytes(StandardCharsets.US_ASCII)); + AtomicInteger getByteCalls = new AtomicInteger(); + DataBuffer buffer = new DataBufferWrapper(delegate) { + @Override + public byte getByte(int index) { + getByteCalls.incrementAndGet(); + return super.getByte(index); + } + @Override + public int forEachByte(int index, int length, ByteProcessor processor) { + return delegate.forEachByte(index, length, processor); + } + }; + + DataBufferMatcher matcher = DataBuffers.matcher( + "--boundary\r\n".getBytes(StandardCharsets.US_ASCII), + "--boundary--".getBytes(StandardCharsets.US_ASCII)); + + // Byte-by-byte access through DataBuffer.getByte is costly for some implementations + // (e.g. Netty's leak-aware buffers record an access per call), so the matcher must + // use the bulk DataBuffer.forEachByte path. + assertThat(matcher.match(buffer)).isEqualTo(-1); + assertThat(getByteCalls).hasValue(0); + } + +}