Skip to content

Commit e23c263

Browse files
committed
Inlined sizeLimit test without resorting to support predicate instance
1 parent b7f52b2 commit e23c263

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

logstash-core/src/main/java/org/logstash/common/BufferedTokenizer.java

+14-11
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
import java.util.Iterator;
2222
import java.util.NoSuchElementException;
23-
import java.util.function.IntPredicate;
2423

2524
public class BufferedTokenizer {
2625

@@ -42,25 +41,29 @@ public boolean hasNext() {
4241

4342
static class DataSplitter implements Iterator<String> {
4443
private final String separator;
45-
private final IntPredicate sizeChecker;
4644
private final StringBuilder accumulator = new StringBuilder();
4745
private int currentIdx = 0;
4846
private boolean dropNextPartialFragments = false;
47+
private final int sizeLimit;
4948

49+
/**
50+
* @param separator
51+
* is the token separator string.
52+
* */
5053
DataSplitter(String separator) {
5154
this.separator = separator;
52-
this.sizeChecker = value -> false;
55+
this.sizeLimit = Integer.MIN_VALUE;
5356
}
5457

5558
/**
5659
* @param separator
5760
* is the token separator string.
58-
* @param sizeChecker
59-
* function that verifies if token size is bigger then a limit
61+
* @param sizeLimit
62+
* maximum token size length.
6063
* */
61-
DataSplitter(String separator, IntPredicate sizeChecker) {
64+
DataSplitter(String separator, int sizeLimit) {
6265
this.separator = separator;
63-
this.sizeChecker = sizeChecker;
66+
this.sizeLimit = sizeLimit;
6467
}
6568

6669
@Override
@@ -71,7 +74,7 @@ public boolean hasNext() {
7174
cleanupAccumulator();
7275
// if it has a remaining bigger than the admitted size, then it start drop other next fragments that
7376
// doesn't contain any separator
74-
if (sizeChecker.test(accumulator.length())) {
77+
if (sizeLimit != Integer.MIN_VALUE && accumulator.length() > sizeLimit) {
7578
dropNextPartialFragments = true;
7679
}
7780
return false;
@@ -90,8 +93,8 @@ public String next() {
9093
} else {
9194
String token = accumulator.substring(currentIdx, nextIdx);
9295
currentIdx = nextIdx + separator.length();
93-
if (sizeChecker.test(token.length())) {
94-
throw new IllegalStateException("input buffer full, consumed token which exceeded the sizeLimit "); // TODO + sizeLimit
96+
if (sizeLimit != Integer.MIN_VALUE && token.length() > sizeLimit) {
97+
throw new IllegalStateException("input buffer full, consumed token which exceeded the sizeLimit " + sizeLimit);
9598
}
9699
return token;
97100
}
@@ -134,7 +137,7 @@ public BufferedTokenizer(String separator, int sizeLimit) {
134137
throw new IllegalArgumentException("Size limit must be positive");
135138
}
136139

137-
this.dataSplitter = new DataSplitter(separator, tokenSize -> tokenSize > sizeLimit);
140+
this.dataSplitter = new DataSplitter(separator, sizeLimit);
138141
this.iterable = () -> dataSplitter;
139142
}
140143

0 commit comments

Comments
 (0)