From 38e741c2f984435af5a018c20c055674fc745184 Mon Sep 17 00:00:00 2001 From: Hyunsik Kang Date: Sun, 22 Nov 2026 13:26:56 +0900 Subject: [PATCH] Use bulk access in multiple-delimiter matching Prior to this commit, CompositeMatcher.match indexed the buffer byte by byte through DataBuffer.getByte. Some DataBuffer implementations record an access for each call (for example, Netty's leak-aware buffers capture a stack trace when paranoid leak detection is enabled), so matching a multipart body made parsing orders of magnitude slower and multipart requests timed out. This commit uses DataBuffer.forEachByte instead, which delegates to the underlying implementation's bulk path, as the previous NestedMatcher implementation did. Signed-off-by: Hyunsik Kang --- .../core/io/buffer/DataBuffers.java | 25 +++++----- .../core/io/buffer/DataBuffersTests.java | 48 +++++++++++++++++++ 2 files changed, 60 insertions(+), 13 deletions(-) create mode 100644 spring-core/src/test/java/org/springframework/core/io/buffer/DataBuffersTests.java 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); + } + +}