Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}

}