Skip to content

Commit

Permalink
#44 refactoring, added array limiter into JBBPCustomFieldTypeProcesso…
Browse files Browse the repository at this point in the history
…r.java and JBBPVarFieldProcessor.java
  • Loading branch information
raydac committed Nov 3, 2024
1 parent bfc75fe commit 9054080
Show file tree
Hide file tree
Showing 17 changed files with 483 additions and 220 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import com.igormaznitsa.jbbp.compiler.JBBPNamedFieldInfo;
import com.igormaznitsa.jbbp.compiler.tokenizer.JBBPFieldTypeParameterContainer;
import com.igormaznitsa.jbbp.exceptions.JBBPReachedArraySizeLimitException;
import com.igormaznitsa.jbbp.io.JBBPArraySizeLimiter;
import com.igormaznitsa.jbbp.io.JBBPBitInputStream;
import com.igormaznitsa.jbbp.io.JBBPBitOrder;
import com.igormaznitsa.jbbp.model.JBBPAbstractField;
Expand Down Expand Up @@ -59,13 +61,20 @@ boolean isAllowed(JBBPFieldTypeParameterContainer fieldType, String fieldName, i
* @param extraData extra numeric value for the field, followed by ':', if not presented then 0
* @param readWholeStream if true then the field is array which should contain parse data for whole stream till the end
* @param arrayLength -1 if it is not array else length of the array to be read.
* @param arraySizeLimiter limiter to check number of elements during whole stream array read, must not be null
* @return parsed data as JBBP field, must not be null
* @throws IOException it can be thrown for transport errors
* @throws IOException it can be thrown for transport errors
* @throws JBBPReachedArraySizeLimitException thrown if reached limit for a whole stream array
* @since 2.1.0
* @see JBBPArraySizeLimiter#isBreakReadWholeStream(int, JBBPArraySizeLimiter)
* @see JBBPArraySizeLimiter#NO_LIMIT_FOR_ARRAY_SIZE
*/
JBBPAbstractField readCustomFieldType(JBBPBitInputStream in, JBBPBitOrder bitOrder,
int parserFlags,
JBBPFieldTypeParameterContainer customTypeFieldInfo,
JBBPNamedFieldInfo fieldName, int extraData,
boolean readWholeStream, int arrayLength)
throws IOException;
boolean readWholeStream,
int arrayLength,
JBBPArraySizeLimiter arraySizeLimiter) throws IOException;

}
109 changes: 82 additions & 27 deletions jbbp/src/main/java/com/igormaznitsa/jbbp/JBBPParser.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package com.igormaznitsa.jbbp;

import com.igormaznitsa.jbbp.compiler.JBBPNamedFieldInfo;
import com.igormaznitsa.jbbp.exceptions.JBBPReachedArraySizeLimitException;
import com.igormaznitsa.jbbp.io.JBBPArraySizeLimiter;
import com.igormaznitsa.jbbp.io.JBBPBitInputStream;
import com.igormaznitsa.jbbp.io.JBBPByteOrder;
import com.igormaznitsa.jbbp.model.JBBPAbstractArrayField;
Expand All @@ -38,15 +40,21 @@ public interface JBBPVarFieldProcessor {
* @param extraValue the extra value for the field, by default it is 0, it is the integer value after ':' char in the field type
* @param byteOrder the byte order for the field, it must not be null
* @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.
* @param arraySizeLimiter limiter to check number of elements during whole stream array read, must not be null
* @return a field array without nulls as values, it must not return null
* @throws IOException it can be thrown for transport errors or another process exceptions
* @throws IOException it can be thrown for transport errors or another process exceptions
* @throws JBBPReachedArraySizeLimitException thrown if reached limit for whole stream array
* @since 2.1.0
* @see JBBPArraySizeLimiter#isBreakReadWholeStream(int, JBBPArraySizeLimiter)
* @see JBBPArraySizeLimiter#NO_LIMIT_FOR_ARRAY_SIZE
*/
JBBPAbstractArrayField<? extends JBBPAbstractField> readVarArray(JBBPBitInputStream inStream,
int arraySize,
JBBPNamedFieldInfo fieldName,
int extraValue,
JBBPByteOrder byteOrder,
JBBPNamedNumericFieldMap numericFieldMap)
JBBPNamedNumericFieldMap numericFieldMap,
JBBPArraySizeLimiter arraySizeLimiter)
throws IOException;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ public class JBBPReachedArraySizeLimitException extends JBBPIOException {
private final int readSize;
private final int limitSize;

public JBBPReachedArraySizeLimitException(final String message, final int readSize,
final int limitSize) {
public JBBPReachedArraySizeLimitException(
final String message,
final int readSize,
final int limitSize
) {
super(message);
this.readSize = readSize;
this.limitSize = limitSize;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,46 @@
package com.igormaznitsa.jbbp.io;

import com.igormaznitsa.jbbp.exceptions.JBBPReachedArraySizeLimitException;

/**
* Interface describing an object which provides limit to read array items.
*
* @since 2.1.0
*/
@FunctionalInterface
public interface JBBPArraySizeLimiter {
/**
* Read arrays without limits.
*/
JBBPArraySizeLimiter NO_LIMIT_FOR_ARRAY_SIZE = () -> 0;

/**
* Check number of read items for whole stream array and return flag if read should be stopped or throw exception if required.
*
* @param readItems number of currently read array items
* @param limiter limiter provides number of allowed items, must not be null
* @return true if read must be stopped immediately, false otherwise
* @throws JBBPReachedArraySizeLimitException it will be thrown if reach of limit is not allowed
*/
static boolean isBreakReadWholeStream(
final int readItems,
final JBBPArraySizeLimiter limiter
) {
final int limit = limiter.getArrayItemsLimit();
if (limit == 0) {
return false;
}
if (limit > 0) {
if (readItems > limit) {
throw new JBBPReachedArraySizeLimitException("", readItems, Math.abs(limit));
} else {
return false;
}
} else {
return readItems >= Math.abs(limit);
}
}

/**
* Get allowed size of array to read.
*
Expand Down
Loading

0 comments on commit 9054080

Please sign in to comment.