Skip to content

Commit 9054080

Browse files
committed
#44 refactoring, added array limiter into JBBPCustomFieldTypeProcessor.java and JBBPVarFieldProcessor.java
1 parent bfc75fe commit 9054080

17 files changed

+483
-220
lines changed

jbbp/src/main/java/com/igormaznitsa/jbbp/JBBPCustomFieldTypeProcessor.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
import com.igormaznitsa.jbbp.compiler.JBBPNamedFieldInfo;
2020
import com.igormaznitsa.jbbp.compiler.tokenizer.JBBPFieldTypeParameterContainer;
21+
import com.igormaznitsa.jbbp.exceptions.JBBPReachedArraySizeLimitException;
22+
import com.igormaznitsa.jbbp.io.JBBPArraySizeLimiter;
2123
import com.igormaznitsa.jbbp.io.JBBPBitInputStream;
2224
import com.igormaznitsa.jbbp.io.JBBPBitOrder;
2325
import com.igormaznitsa.jbbp.model.JBBPAbstractField;
@@ -59,13 +61,20 @@ boolean isAllowed(JBBPFieldTypeParameterContainer fieldType, String fieldName, i
5961
* @param extraData extra numeric value for the field, followed by ':', if not presented then 0
6062
* @param readWholeStream if true then the field is array which should contain parse data for whole stream till the end
6163
* @param arrayLength -1 if it is not array else length of the array to be read.
64+
* @param arraySizeLimiter limiter to check number of elements during whole stream array read, must not be null
6265
* @return parsed data as JBBP field, must not be null
63-
* @throws IOException it can be thrown for transport errors
66+
* @throws IOException it can be thrown for transport errors
67+
* @throws JBBPReachedArraySizeLimitException thrown if reached limit for a whole stream array
68+
* @since 2.1.0
69+
* @see JBBPArraySizeLimiter#isBreakReadWholeStream(int, JBBPArraySizeLimiter)
70+
* @see JBBPArraySizeLimiter#NO_LIMIT_FOR_ARRAY_SIZE
6471
*/
6572
JBBPAbstractField readCustomFieldType(JBBPBitInputStream in, JBBPBitOrder bitOrder,
6673
int parserFlags,
6774
JBBPFieldTypeParameterContainer customTypeFieldInfo,
6875
JBBPNamedFieldInfo fieldName, int extraData,
69-
boolean readWholeStream, int arrayLength)
70-
throws IOException;
76+
boolean readWholeStream,
77+
int arrayLength,
78+
JBBPArraySizeLimiter arraySizeLimiter) throws IOException;
79+
7180
}

jbbp/src/main/java/com/igormaznitsa/jbbp/JBBPParser.java

Lines changed: 82 additions & 27 deletions
Large diffs are not rendered by default.

jbbp/src/main/java/com/igormaznitsa/jbbp/JBBPVarFieldProcessor.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
package com.igormaznitsa.jbbp;
1818

1919
import com.igormaznitsa.jbbp.compiler.JBBPNamedFieldInfo;
20+
import com.igormaznitsa.jbbp.exceptions.JBBPReachedArraySizeLimitException;
21+
import com.igormaznitsa.jbbp.io.JBBPArraySizeLimiter;
2022
import com.igormaznitsa.jbbp.io.JBBPBitInputStream;
2123
import com.igormaznitsa.jbbp.io.JBBPByteOrder;
2224
import com.igormaznitsa.jbbp.model.JBBPAbstractArrayField;
@@ -38,15 +40,21 @@ public interface JBBPVarFieldProcessor {
3840
* @param extraValue the extra value for the field, by default it is 0, it is the integer value after ':' char in the field type
3941
* @param byteOrder the byte order for the field, it must not be null
4042
* @param numericFieldMap the numeric field map for the session, it must not be null, it can be used for access to already read values of another numeric fields.
43+
* @param arraySizeLimiter limiter to check number of elements during whole stream array read, must not be null
4144
* @return a field array without nulls as values, it must not return null
42-
* @throws IOException it can be thrown for transport errors or another process exceptions
45+
* @throws IOException it can be thrown for transport errors or another process exceptions
46+
* @throws JBBPReachedArraySizeLimitException thrown if reached limit for whole stream array
47+
* @since 2.1.0
48+
* @see JBBPArraySizeLimiter#isBreakReadWholeStream(int, JBBPArraySizeLimiter)
49+
* @see JBBPArraySizeLimiter#NO_LIMIT_FOR_ARRAY_SIZE
4350
*/
4451
JBBPAbstractArrayField<? extends JBBPAbstractField> readVarArray(JBBPBitInputStream inStream,
4552
int arraySize,
4653
JBBPNamedFieldInfo fieldName,
4754
int extraValue,
4855
JBBPByteOrder byteOrder,
49-
JBBPNamedNumericFieldMap numericFieldMap)
56+
JBBPNamedNumericFieldMap numericFieldMap,
57+
JBBPArraySizeLimiter arraySizeLimiter)
5058
throws IOException;
5159

5260
/**

jbbp/src/main/java/com/igormaznitsa/jbbp/exceptions/JBBPReachedArraySizeLimitException.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@ public class JBBPReachedArraySizeLimitException extends JBBPIOException {
1010
private final int readSize;
1111
private final int limitSize;
1212

13-
public JBBPReachedArraySizeLimitException(final String message, final int readSize,
14-
final int limitSize) {
13+
public JBBPReachedArraySizeLimitException(
14+
final String message,
15+
final int readSize,
16+
final int limitSize
17+
) {
1518
super(message);
1619
this.readSize = readSize;
1720
this.limitSize = limitSize;

jbbp/src/main/java/com/igormaznitsa/jbbp/io/JBBPArraySizeLimiter.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,46 @@
11
package com.igormaznitsa.jbbp.io;
22

3+
import com.igormaznitsa.jbbp.exceptions.JBBPReachedArraySizeLimitException;
4+
35
/**
46
* Interface describing an object which provides limit to read array items.
57
*
68
* @since 2.1.0
79
*/
810
@FunctionalInterface
911
public interface JBBPArraySizeLimiter {
12+
/**
13+
* Read arrays without limits.
14+
*/
15+
JBBPArraySizeLimiter NO_LIMIT_FOR_ARRAY_SIZE = () -> 0;
16+
17+
/**
18+
* Check number of read items for whole stream array and return flag if read should be stopped or throw exception if required.
19+
*
20+
* @param readItems number of currently read array items
21+
* @param limiter limiter provides number of allowed items, must not be null
22+
* @return true if read must be stopped immediately, false otherwise
23+
* @throws JBBPReachedArraySizeLimitException it will be thrown if reach of limit is not allowed
24+
*/
25+
static boolean isBreakReadWholeStream(
26+
final int readItems,
27+
final JBBPArraySizeLimiter limiter
28+
) {
29+
final int limit = limiter.getArrayItemsLimit();
30+
if (limit == 0) {
31+
return false;
32+
}
33+
if (limit > 0) {
34+
if (readItems > limit) {
35+
throw new JBBPReachedArraySizeLimitException("", readItems, Math.abs(limit));
36+
} else {
37+
return false;
38+
}
39+
} else {
40+
return readItems >= Math.abs(limit);
41+
}
42+
}
43+
1044
/**
1145
* Get allowed size of array to read.
1246
*

0 commit comments

Comments
 (0)