Skip to content

Commit f3b8c44

Browse files
committed
#44 refactoring and fixed read of struct in JBBPParser
1 parent 2bc5394 commit f3b8c44

File tree

4 files changed

+45
-27
lines changed

4 files changed

+45
-27
lines changed

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ private List<JBBPAbstractField> parseStruct(final JBBPBitInputStream inStream,
283283
boolean endStructureNotMet = true;
284284

285285
while (endStructureNotMet && positionAtCompiledBlock.get() < compiled.length) {
286-
if (inStream.isArrayLimitDetected() ||
286+
if (inStream.isDetectedArrayLimit() ||
287287
(!inStream.hasAvailableData() && (flags & FLAG_SKIP_REMAINING_FIELDS_IF_EOF) != 0)) {
288288
// Break reading because the ignore flag for EOF has been set or reached limit for whole stream array read
289289
break;
@@ -628,6 +628,7 @@ private List<JBBPAbstractField> parseStruct(final JBBPBitInputStream inStream,
628628

629629
if (JBBPArraySizeLimiter.isBreakReadWholeStream(list.size(),
630630
arraySizeLimiter)) {
631+
inStream.setDetectedArrayLimit(true);
631632
break;
632633
}
633634

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

+37-24
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,11 @@ public class JBBPBitInputStream extends FilterInputStream implements JBBPCountab
7171
private long markedByteCounter;
7272

7373
/**
74-
* Internal flag shows that read stopped for reach of array limiter.
74+
* Internal flag shows that read stopped for whole stream array read limit reach.
75+
*
76+
* @since 2.1.0
7577
*/
76-
private boolean arrayLimitDetected;
78+
private boolean detectedArrayLimit;
7779

7880
/**
7981
* Flag shows that read of array was stopped for array limiter restrictions.
@@ -82,8 +84,19 @@ public class JBBPBitInputStream extends FilterInputStream implements JBBPCountab
8284
* @see JBBPArraySizeLimiter
8385
* @since 2.1.0
8486
*/
85-
public boolean isArrayLimitDetected() {
86-
return this.arrayLimitDetected;
87+
public boolean isDetectedArrayLimit() {
88+
return this.detectedArrayLimit;
89+
}
90+
91+
/**
92+
* Set value for array limit detected flag. It is important to set the flag for correct processing of parsing.
93+
*
94+
* @param value true or false.
95+
* @see com.igormaznitsa.jbbp.JBBPParser
96+
* @since 2.1.0
97+
*/
98+
public void setDetectedArrayLimit(final boolean value) {
99+
this.detectedArrayLimit = value;
87100
}
88101

89102
/**
@@ -135,7 +148,7 @@ public boolean[] readBoolArray(final int items) throws IOException {
135148
public boolean[] readBoolArray(final int items,
136149
final JBBPArraySizeLimiter arraySizeLimiter)
137150
throws IOException {
138-
this.arrayLimitDetected = false;
151+
this.setDetectedArrayLimit(false);
139152
int pos = 0;
140153
byte[] buffer;
141154
if (items < 0) {
@@ -149,7 +162,7 @@ public boolean[] readBoolArray(final int items,
149162
pos += read;
150163

151164
if (isBreakReadWholeStream(pos, arraySizeLimiter)) {
152-
this.arrayLimitDetected = true;
165+
this.setDetectedArrayLimit(true);
153166
final int limit = arraySizeLimiter.getArrayItemsLimit();
154167
if (limit < 0) {
155168
pos = Math.min(pos, Math.abs(limit));
@@ -346,7 +359,7 @@ private byte[] internalReadArray(
346359
final JBBPArraySizeLimiter streamLimiter
347360
) throws IOException {
348361
final boolean readByteArray = bitNumber == null;
349-
this.arrayLimitDetected = false;
362+
this.setDetectedArrayLimit(false);
350363
int pos = 0;
351364
if (items < 0) {
352365
byte[] buffer = new byte[INITIAL_ARRAY_BUFFER_SIZE];
@@ -363,7 +376,7 @@ private byte[] internalReadArray(
363376
}
364377
buffer[pos++] = (byte) next;
365378
if (isBreakReadWholeStream(pos, streamLimiter)) {
366-
this.arrayLimitDetected = true;
379+
this.setDetectedArrayLimit(true);
367380
break;
368381
}
369382
}
@@ -429,7 +442,7 @@ public short[] readShortArray(final int items, final JBBPByteOrder byteOrder) th
429442
public short[] readShortArray(final int items, final JBBPByteOrder byteOrder,
430443
final JBBPArraySizeLimiter arraySizeLimiter)
431444
throws IOException {
432-
this.arrayLimitDetected = false;
445+
this.setDetectedArrayLimit(false);
433446
int pos = 0;
434447
if (items < 0) {
435448
short[] buffer = new short[INITIAL_ARRAY_BUFFER_SIZE];
@@ -443,7 +456,7 @@ public short[] readShortArray(final int items, final JBBPByteOrder byteOrder,
443456
}
444457
buffer[pos++] = (short) next;
445458
if (isBreakReadWholeStream(pos, arraySizeLimiter)) {
446-
this.arrayLimitDetected = true;
459+
this.setDetectedArrayLimit(true);
447460
break;
448461
}
449462
}
@@ -500,7 +513,7 @@ public long[] readUIntArray(
500513
final JBBPByteOrder byteOrder,
501514
final JBBPArraySizeLimiter arraySizeLimiter
502515
) throws IOException {
503-
this.arrayLimitDetected = false;
516+
this.setDetectedArrayLimit(false);
504517
int pos = 0;
505518
if (items < 0) {
506519
long[] buffer = new long[INITIAL_ARRAY_BUFFER_SIZE];
@@ -514,7 +527,7 @@ public long[] readUIntArray(
514527
}
515528
buffer[pos++] = next;
516529
if (isBreakReadWholeStream(pos, arraySizeLimiter)) {
517-
this.arrayLimitDetected = true;
530+
this.setDetectedArrayLimit(true);
518531
break;
519532
}
520533
}
@@ -569,7 +582,7 @@ public char[] readUShortArray(final int items, final JBBPByteOrder byteOrder) th
569582
public char[] readUShortArray(final int items, final JBBPByteOrder byteOrder,
570583
final JBBPArraySizeLimiter arraySizeLimiter)
571584
throws IOException {
572-
this.arrayLimitDetected = false;
585+
this.setDetectedArrayLimit(false);
573586
int pos = 0;
574587
if (items < 0) {
575588
char[] buffer = new char[INITIAL_ARRAY_BUFFER_SIZE];
@@ -583,7 +596,7 @@ public char[] readUShortArray(final int items, final JBBPByteOrder byteOrder,
583596
}
584597
buffer[pos++] = (char) next;
585598
if (isBreakReadWholeStream(pos, arraySizeLimiter)) {
586-
this.arrayLimitDetected = true;
599+
this.setDetectedArrayLimit(true);
587600
break;
588601
}
589602
}
@@ -635,7 +648,7 @@ public int[] readIntArray(final int items, final JBBPByteOrder byteOrder) throws
635648
*/
636649
public int[] readIntArray(final int items, final JBBPByteOrder byteOrder,
637650
final JBBPArraySizeLimiter arraySizeLimiter) throws IOException {
638-
this.arrayLimitDetected = false;
651+
this.setDetectedArrayLimit(false);
639652
int pos = 0;
640653
if (items < 0) {
641654
int[] buffer = new int[INITIAL_ARRAY_BUFFER_SIZE];
@@ -649,7 +662,7 @@ public int[] readIntArray(final int items, final JBBPByteOrder byteOrder,
649662
}
650663
buffer[pos++] = next;
651664
if (isBreakReadWholeStream(pos, arraySizeLimiter)) {
652-
this.arrayLimitDetected = true;
665+
this.setDetectedArrayLimit(true);
653666
break;
654667
}
655668
}
@@ -706,7 +719,7 @@ public float[] readFloatArray(
706719
final JBBPByteOrder byteOrder,
707720
final JBBPArraySizeLimiter arraySizeLimiter
708721
) throws IOException {
709-
this.arrayLimitDetected = false;
722+
this.setDetectedArrayLimit(false);
710723
int pos = 0;
711724
if (items < 0) {
712725
float[] buffer = new float[INITIAL_ARRAY_BUFFER_SIZE];
@@ -720,7 +733,7 @@ public float[] readFloatArray(
720733
}
721734
buffer[pos++] = next;
722735
if (isBreakReadWholeStream(pos, arraySizeLimiter)) {
723-
this.arrayLimitDetected = true;
736+
this.setDetectedArrayLimit(true);
724737
break;
725738
}
726739
}
@@ -1072,7 +1085,7 @@ public long[] readLongArray(
10721085
final JBBPByteOrder byteOrder,
10731086
final JBBPArraySizeLimiter arraySizeLimiter
10741087
) throws IOException {
1075-
this.arrayLimitDetected = false;
1088+
this.setDetectedArrayLimit(false);
10761089
int pos = 0;
10771090
if (items < 0) {
10781091
long[] buffer = new long[INITIAL_ARRAY_BUFFER_SIZE];
@@ -1086,7 +1099,7 @@ public long[] readLongArray(
10861099
}
10871100
buffer[pos++] = next;
10881101
if (isBreakReadWholeStream(pos, arraySizeLimiter)) {
1089-
this.arrayLimitDetected = true;
1102+
this.setDetectedArrayLimit(true);
10901103
break;
10911104
}
10921105
}
@@ -1205,7 +1218,7 @@ public double[] readDoubleArray(
12051218
final JBBPByteOrder byteOrder,
12061219
final JBBPArraySizeLimiter arraySizeLimiter
12071220
) throws IOException {
1208-
this.arrayLimitDetected = false;
1221+
this.setDetectedArrayLimit(false);
12091222
int pos = 0;
12101223
if (items < 0) {
12111224
double[] buffer = new double[INITIAL_ARRAY_BUFFER_SIZE];
@@ -1219,7 +1232,7 @@ public double[] readDoubleArray(
12191232
}
12201233
buffer[pos++] = Double.longBitsToDouble(next);
12211234
if (isBreakReadWholeStream(pos, arraySizeLimiter)) {
1222-
this.arrayLimitDetected = true;
1235+
this.setDetectedArrayLimit(true);
12231236
break;
12241237
}
12251238
}
@@ -1416,7 +1429,7 @@ public String[] readStringArray(
14161429
final JBBPByteOrder byteOrder,
14171430
final JBBPArraySizeLimiter arraySizeLimiter
14181431
) throws IOException {
1419-
this.arrayLimitDetected = false;
1432+
this.setDetectedArrayLimit(false);
14201433
int pos = 0;
14211434
if (items < 0) {
14221435
String[] buffer = new String[INITIAL_ARRAY_BUFFER_SIZE];
@@ -1430,7 +1443,7 @@ public String[] readStringArray(
14301443
}
14311444
buffer[pos++] = next;
14321445
if (isBreakReadWholeStream(pos, arraySizeLimiter)) {
1433-
this.arrayLimitDetected = true;
1446+
this.setDetectedArrayLimit(true);
14341447
break;
14351448
}
14361449
}

jbbp/src/test/java/com/igormaznitsa/jbbp/JBBPParserTest.java

+4
Original file line numberDiff line numberDiff line change
@@ -2550,6 +2550,10 @@ public void testLimitedWholeStreamArrayRead() throws Exception {
25502550
testArrayLimiter_2elementsLimit(TestUtils.getRandomBytes(128), "long [_] test;", null, null);
25512551
testArrayLimiter_2elementsLimit(TestUtils.getRandomBytes(128), "floatj [_] test;", null, null);
25522552
testArrayLimiter_2elementsLimit(TestUtils.getRandomBytes(128), "doublej [_] test;", null, null);
2553+
testArrayLimiter_2elementsLimit(TestUtils.getRandomBytes(128), "doublej [_] test;", null, null);
2554+
testArrayLimiter_2elementsLimit(TestUtils.getRandomBytes(128),
2555+
"test [_] { byte a; byte b; byte c; byte d;}", null, null);
2556+
25532557
testArrayLimiter_2elementsLimit(
25542558
TestUtils.makeStringArray(JBBPByteOrder.BIG_ENDIAN, "hello", "world", "one", "two", "three",
25552559
"four"), "stringj [_] test;", null, null);

jbbp/src/test/java/com/igormaznitsa/jbbp/io/JBBPBitInputStreamTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -993,13 +993,13 @@ private void testWholeStreamArrayRead(
993993
) throws Exception {
994994
final Pair<JBBPBitInputStream, Integer> readWholeData = readWhole.getData();
995995
assertEquals(expectedReadWholeSize, readWholeData.getRight());
996-
assertFalse(readWholeData.getLeft().isArrayLimitDetected());
996+
assertFalse(readWholeData.getLeft().isDetectedArrayLimit());
997997

998998
assertThrows(JBBPReachedArraySizeLimitException.class, readWholeWithException::getData);
999999

10001000
final Pair<JBBPBitInputStream, Integer> readWholeLimitedData = readWholeLimited.getData();
10011001
assertEquals(expectedReadLimitedSize, readWholeLimitedData.getRight());
1002-
assertTrue(readWholeLimitedData.getLeft().isArrayLimitDetected());
1002+
assertTrue(readWholeLimitedData.getLeft().isDetectedArrayLimit());
10031003
}
10041004

10051005
@Test

0 commit comments

Comments
 (0)