Skip to content

Commit 560c346

Browse files
committed
refactoring and added JBBPUtils#findMaxStaticArraySize
1 parent c9850fd commit 560c346

File tree

6 files changed

+313
-41
lines changed

6 files changed

+313
-41
lines changed

jbbp/src/main/java/com/igormaznitsa/jbbp/compiler/conversion/CompiledBlockVisitor.java

Lines changed: 75 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,8 @@ public final CompiledBlockVisitor visit() {
158158
final JBBPIntegerValueEvaluator numberOfBits =
159159
extraFieldNumAsExpr ? extraFieldValueEvaluator : new IntConstValueEvaluator(
160160
JBBPUtils.unpackInt(compiledData, positionAtCompiledBlock));
161-
visitBitField(theOffset, byteOrder, name, numberOfBits, arraySizeEvaluator);
161+
visitBitField(theOffset, byteOrder, name, readWholeStream, numberOfBits,
162+
arraySizeEvaluator);
162163
}
163164
break;
164165

@@ -175,7 +176,7 @@ public final CompiledBlockVisitor visit() {
175176
break;
176177

177178
case JBBPCompiler.CODE_STRUCT_START: {
178-
visitStructureStart(theOffset, byteOrder, name, arraySizeEvaluator);
179+
visitStructureStart(theOffset, byteOrder, readWholeStream, name, arraySizeEvaluator);
179180
}
180181
break;
181182

@@ -230,15 +231,16 @@ public void visitActionItem(int offsetInCompiledBlock, int actionType,
230231
}
231232

232233
/**
233-
* Visit field contains virtual field with VAL type.
234+
* Visit field contains virtual field defined through VAL type.
234235
*
235236
* @param offsetInCompiledBlock offset in the compiled block
236237
* @param byteOrder byteOrder
237238
* @param nameFieldInfo name of the field, must not be null
238239
* @param expression expression to calculate value
239240
* @since 1.4.0
240241
*/
241-
public void visitValField(int offsetInCompiledBlock, JBBPByteOrder byteOrder,
242+
public void visitValField(int offsetInCompiledBlock,
243+
JBBPByteOrder byteOrder,
242244
JBBPNamedFieldInfo nameFieldInfo,
243245
JBBPIntegerValueEvaluator expression) {
244246
}
@@ -262,54 +264,106 @@ public void visitValField(int offsetInCompiledBlock, JBBPByteOrder byteOrder,
262264
* @see JBBPCompiler#CODE_LONG
263265
* @see JBBPCompiler#CODE_SKIP
264266
*/
265-
public void visitPrimitiveField(int offsetInCompiledBlock, int primitiveType,
266-
JBBPNamedFieldInfo nullableNameFieldInfo, JBBPByteOrder byteOrder,
267-
boolean readWholeStreamAsArray, boolean altFieldType,
267+
public void visitPrimitiveField(int offsetInCompiledBlock,
268+
int primitiveType,
269+
JBBPNamedFieldInfo nullableNameFieldInfo,
270+
JBBPByteOrder byteOrder,
271+
boolean readWholeStreamAsArray,
272+
boolean altFieldType,
268273
JBBPIntegerValueEvaluator nullableArraySize) {
269274
}
270275

271276
/**
272277
* Visit a variable field (which is defined with var data type)
273278
*
274-
* @param offsetInCompiledBlock offset in the compiled block
275-
* @param nullableNameFieldInfo field info, null if the field is anonymous one
276-
* @param byteOrder byte order for the field, must not be null
277-
* @param readWholeStreamIntoArray true if whole stream should be read as array of var type, false otherwise
278-
* @param nullableArraySize if not null then evaluator of array size to be read from stream
279-
* @param extraDataValueEvaluator if not null then extra data evaluator for the var field
279+
* @param offsetInCompiledBlock offset in the compiled block
280+
* @param nullableNameFieldInfo field info, null if the field is anonymous one
281+
* @param byteOrder byte order for the field, must not be null
282+
* @param readWholeStream true if whole stream should be read as array of var type, false otherwise
283+
* @param nullableArraySize if not null then evaluator of array size to be read from stream
284+
* @param extraDataValue if not null then extra data evaluator for the var field
280285
*/
281-
public void visitVarField(int offsetInCompiledBlock, JBBPNamedFieldInfo nullableNameFieldInfo,
282-
JBBPByteOrder byteOrder, boolean readWholeStreamIntoArray,
286+
public void visitVarField(int offsetInCompiledBlock,
287+
JBBPNamedFieldInfo nullableNameFieldInfo,
288+
JBBPByteOrder byteOrder,
289+
boolean readWholeStream,
283290
JBBPIntegerValueEvaluator nullableArraySize,
284-
JBBPIntegerValueEvaluator extraDataValueEvaluator) {
291+
JBBPIntegerValueEvaluator extraDataValue) {
285292
}
286293

294+
/**
295+
* Visit a custom type field.
296+
*
297+
* @param offsetInCompiledBlock offset in the compiled block
298+
* @param notNullFieldType field type info, must not be null
299+
* @param nullableNameFieldInfo field info, null if the field is anonymous one
300+
* @param byteOrder byte order for the field, must not be null
301+
* @param readWholeStream true if whole stream should be read as array of var type, false otherwise
302+
* @param nullableArraySize if not null then evaluator of array size to be read from stream
303+
* @param extraDataValue if not null then extra data evaluator for the var field
304+
*/
287305
public void visitCustomField(int offsetInCompiledBlock,
288306
JBBPFieldTypeParameterContainer notNullFieldType,
289-
JBBPNamedFieldInfo nullableNameFieldInfo, JBBPByteOrder byteOrder,
307+
JBBPNamedFieldInfo nullableNameFieldInfo,
308+
JBBPByteOrder byteOrder,
290309
boolean readWholeStream,
291-
JBBPIntegerValueEvaluator nullableArraySizeEvaluator,
292-
JBBPIntegerValueEvaluator extraDataValueEvaluator) {
310+
JBBPIntegerValueEvaluator nullableArraySize,
311+
JBBPIntegerValueEvaluator extraDataValue) {
293312
}
294313

295-
public void visitBitField(int offsetInCompiledBlock, JBBPByteOrder byteOrder,
314+
/**
315+
* Visit a custom type field.
316+
*
317+
* @param offsetInCompiledBlock offset in the compiled block
318+
* @param byteOrder byte order for the field, must not be null
319+
* @param nullableNameFieldInfo field info, null if the field is anonymous one
320+
* @param readWholeStream true if whole stream should be read as array of var type, false otherwise
321+
* @param notNullFieldSize evaluator to calculate size of the field, must not be null
322+
* @param nullableArraySize if not null then evaluator of array size to be read from stream
323+
*/
324+
public void visitBitField(int offsetInCompiledBlock,
325+
JBBPByteOrder byteOrder,
296326
JBBPNamedFieldInfo nullableNameFieldInfo,
327+
boolean readWholeStream,
297328
JBBPIntegerValueEvaluator notNullFieldSize,
298329
JBBPIntegerValueEvaluator nullableArraySize) {
299330
}
300331

301-
public void visitStructureStart(int offsetInCompiledBlock, JBBPByteOrder byteOrder,
332+
/**
333+
* Visit a structure field.
334+
*
335+
* @param offsetInCompiledBlock offset in the compiled block
336+
* @param byteOrder byte order for the field, must not be null
337+
* @param readWholeStream true if whole stream should be read as array of var type, false otherwise
338+
* @param nullableNameFieldInfo field info, null if the field is anonymous one
339+
* @param nullableArraySize if not null then evaluator of array size to be read from stream
340+
*/
341+
public void visitStructureStart(int offsetInCompiledBlock,
342+
JBBPByteOrder byteOrder,
343+
boolean readWholeStream,
302344
JBBPNamedFieldInfo nullableNameFieldInfo,
303345
JBBPIntegerValueEvaluator nullableArraySize) {
304346
}
305347

348+
/**
349+
* End visit of a structure
350+
*
351+
* @param offsetInCompiledBlock offset in the compiled block
352+
* @param nullableNameFieldInfo field info, null if the field is anonymous one
353+
*/
306354
public void visitStructureEnd(int offsetInCompiledBlock,
307355
JBBPNamedFieldInfo nullableNameFieldInfo) {
308356
}
309357

358+
/**
359+
* Called before visit of each item.
360+
*/
310361
public void visitStart() {
311362
}
312363

364+
/**
365+
* Called after visit each item.
366+
*/
313367
public void visitEnd() {
314368
}
315369

jbbp/src/main/java/com/igormaznitsa/jbbp/compiler/conversion/IntConstValueEvaluator.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
*
2727
* @since 1.3.0
2828
*/
29-
final class IntConstValueEvaluator implements JBBPIntegerValueEvaluator {
29+
public final class IntConstValueEvaluator implements JBBPIntegerValueEvaluator {
3030

3131
private static final long serialVersionUID = 4640385518512384490L;
3232

@@ -40,14 +40,20 @@ final class IntConstValueEvaluator implements JBBPIntegerValueEvaluator {
4040
}
4141

4242
@Override
43-
public int eval(final JBBPBitInputStream inStream, final int currentCompiledBlockOffset,
44-
final JBBPCompiledBlock block, final JBBPNamedNumericFieldMap fieldMap) {
43+
public int eval(final JBBPBitInputStream inStream,
44+
final int currentCompiledBlockOffset,
45+
final JBBPCompiledBlock block,
46+
final JBBPNamedNumericFieldMap fieldMap
47+
) {
4548
return this.value;
4649
}
4750

4851
@Override
49-
public void visitItems(JBBPCompiledBlock block, int currentCompiledBlockOffset,
50-
ExpressionEvaluatorVisitor visitor) {
52+
public void visitItems(
53+
JBBPCompiledBlock block,
54+
int currentCompiledBlockOffset,
55+
ExpressionEvaluatorVisitor visitor
56+
) {
5157
visitor.visitStart();
5258
visitor.visitConstant(this.value);
5359
visitor.visitEnd();

jbbp/src/main/java/com/igormaznitsa/jbbp/compiler/conversion/JBBPToJavaConverter.java

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,9 @@ public void visitEnd() {
386386
}
387387

388388
@Override
389-
public void visitStructureStart(final int offsetInCompiledBlock, final JBBPByteOrder byteOrder,
389+
public void visitStructureStart(final int offsetInCompiledBlock,
390+
final JBBPByteOrder byteOrder,
391+
final boolean readWholeStream,
390392
final JBBPNamedFieldInfo nullableNameFieldInfo,
391393
final JBBPIntegerValueEvaluator nullableArraySize) {
392394
final String structName = (nullableNameFieldInfo == null ? makeAnonymousStructName() :
@@ -440,7 +442,7 @@ public void visitStructureStart(final int offsetInCompiledBlock, final JBBPByteO
440442
}
441443
processSkipRemainingFlag();
442444
processSkipRemainingFlagForWriting("this." + structName);
443-
if ("-1".equals(arraySizeIn)) {
445+
if (readWholeStream) {
444446
this.getCurrentStruct().getReadFunc().indent()
445447
.printf(
446448
"List<%3$s> __%1$s_tmplst__ = new ArrayList<%3$s>(); while (%5$s.hasAvailableData()){ __%1$s_tmplst__.add(new %3$s(%4$s).read(%5$s));} this.%1$s = __%1$s_tmplst__.toArray(new %3$s[__%1$s_tmplst__.size()]);__%1$s_tmplst__ = null;%n",
@@ -777,6 +779,7 @@ private void registerMaker(final String rawFieldType, final String fieldName,
777779
@Override
778780
public void visitBitField(final int offsetInCompiledBlock, final JBBPByteOrder byteOrder,
779781
final JBBPNamedFieldInfo nullableNameFieldInfo,
782+
final boolean readWholeStream,
780783
final JBBPIntegerValueEvaluator notNullFieldSize,
781784
final JBBPIntegerValueEvaluator nullableArraySize) {
782785
final String fieldName = nullableNameFieldInfo == null ? makeAnonymousFieldName() :
@@ -957,9 +960,9 @@ public void visitVarField(
957960
final int offsetInCompiledBlock,
958961
final JBBPNamedFieldInfo nullableNameFieldInfo,
959962
final JBBPByteOrder byteOrder,
960-
final boolean readWholeStreamIntoArray,
963+
final boolean readWholeStream,
961964
final JBBPIntegerValueEvaluator nullableArraySizeEvaluator,
962-
final JBBPIntegerValueEvaluator extraDataValueEvaluator
965+
final JBBPIntegerValueEvaluator extraDataValue
963966
) {
964967
this.flagSet.set(this.flagSet.get() | FLAG_DETECTED_VAR_FIELDS);
965968

@@ -983,7 +986,7 @@ public void visitVarField(
983986

984987
processSkipRemainingFlag();
985988
final String fieldType;
986-
if (readWholeStreamIntoArray || nullableArraySizeEvaluator != null) {
989+
if (readWholeStream || nullableArraySizeEvaluator != null) {
987990
fieldType = "JBBPAbstractArrayField<? extends JBBPAbstractField>";
988991
if (this.builder.generateFields) {
989992
printField(nullableNameFieldInfo, byteOrder, true, offsetInCompiledBlock,
@@ -996,10 +999,10 @@ public void visitVarField(
996999
this.getCurrentStruct().isRoot() ? "this" : "this." + NAME_ROOT_STRUCT,
9971000
"JBBPByteOrder." + byteOrder.name(),
9981001
nullableNameFieldInfo == null ? "null" : specialFieldName_fieldNameInfo,
999-
extraDataValueEvaluator == null ? "-1" :
1002+
extraDataValue == null ? "-1" :
10001003
evaluatorToString(NAME_INPUT_STREAM, offsetInCompiledBlock,
1001-
extraDataValueEvaluator, this.flagSet, true),
1002-
readWholeStreamIntoArray,
1004+
extraDataValue, this.flagSet, true),
1005+
readWholeStream,
10031006
nullableArraySizeEvaluator == null ? "-1" :
10041007
evaluatorToString(NAME_INPUT_STREAM, offsetInCompiledBlock,
10051008
nullableArraySizeEvaluator, this.flagSet, true)
@@ -1012,9 +1015,9 @@ public void visitVarField(
10121015
fieldName,
10131016
"JBBPByteOrder." + byteOrder.name(),
10141017
nullableNameFieldInfo == null ? "null" : specialFieldName_fieldNameInfo,
1015-
extraDataValueEvaluator == null ? "-1" :
1018+
extraDataValue == null ? "-1" :
10161019
evaluatorToString(NAME_INPUT_STREAM, offsetInCompiledBlock,
1017-
extraDataValueEvaluator, this.flagSet, true),
1020+
extraDataValue, this.flagSet, true),
10181021
nullableArraySizeEvaluator == null ? "-1" :
10191022
evaluatorToString(NAME_OUTPUT_STREAM, offsetInCompiledBlock,
10201023
nullableArraySizeEvaluator, this.flagSet, true)
@@ -1033,9 +1036,9 @@ public void visitVarField(
10331036
this.getCurrentStruct().isRoot() ? "this" : "this." + NAME_ROOT_STRUCT,
10341037
"JBBPByteOrder." + byteOrder.name(),
10351038
nullableNameFieldInfo == null ? "null" : specialFieldName_fieldNameInfo,
1036-
extraDataValueEvaluator == null ? "-1" :
1039+
extraDataValue == null ? "-1" :
10371040
evaluatorToString(NAME_INPUT_STREAM, offsetInCompiledBlock,
1038-
extraDataValueEvaluator, this.flagSet, true))
1041+
extraDataValue, this.flagSet, true))
10391042
);
10401043

10411044
this.getCurrentStruct().getWriteFunc()
@@ -1044,9 +1047,9 @@ public void visitVarField(
10441047
fieldName,
10451048
"JBBPByteOrder." + byteOrder.name(),
10461049
nullableNameFieldInfo == null ? "null" : specialFieldName_fieldNameInfo,
1047-
extraDataValueEvaluator == null ? "-1" :
1050+
extraDataValue == null ? "-1" :
10481051
evaluatorToString(NAME_OUTPUT_STREAM, offsetInCompiledBlock,
1049-
extraDataValueEvaluator, this.flagSet, true)
1052+
extraDataValue, this.flagSet, true)
10501053
);
10511054
}
10521055

0 commit comments

Comments
 (0)