1616package com .igormaznitsa .jbbp ;
1717
1818import com .igormaznitsa .jbbp .compiler .*;
19+ import com .igormaznitsa .jbbp .compiler .tokenizer .JBBPFieldTypeParameterContainer ;
1920import com .igormaznitsa .jbbp .compiler .varlen .JBBPIntegerValueEvaluator ;
2021import com .igormaznitsa .jbbp .exceptions .JBBPParsingException ;
2122import com .igormaznitsa .jbbp .io .*;
@@ -61,21 +62,28 @@ public final class JBBPParser {
6162 private final int flags ;
6263
6364 /**
64- * The Constructor.
65+ * Custom field type processor for the parser, it can be null.
66+ */
67+ private final JBBPCustomFieldTypeProcessor customFieldTypeProcessor ;
68+
69+ /**
70+ * Constructor.
6571 *
6672 * @param source the source script to parse binary blocks and streams, must
6773 * not be null
6874 * @param bitOrder the bit order for bit reading operations, must not be null
75+ * @param customFieldTypeProcessor custom field type processor for the parser instance, it can be null
6976 * @param flags special flags for parsing process
7077 * @see #FLAG_SKIP_REMAINING_FIELDS_IF_EOF
7178 */
72- private JBBPParser (final String source , final JBBPBitOrder bitOrder , final int flags ) {
79+ private JBBPParser (final String source , final JBBPBitOrder bitOrder , final JBBPCustomFieldTypeProcessor customFieldTypeProcessor , final int flags ) {
7380 JBBPUtils .assertNotNull (source , "Script is null" );
7481 JBBPUtils .assertNotNull (bitOrder , "Bit order is null" );
82+ this .customFieldTypeProcessor = customFieldTypeProcessor ;
7583 this .bitOrder = bitOrder ;
7684 this .flags = flags ;
7785 try {
78- this .compiledBlock = JBBPCompiler .compile (source );
86+ this .compiledBlock = JBBPCompiler .compile (source , customFieldTypeProcessor );
7987 }
8088 catch (IOException ex ) {
8189 throw new RuntimeException ("Can't compile script for unexpected IOException" , ex );
@@ -231,6 +239,15 @@ private List<JBBPAbstractField> parseStruct(final JBBPBitInputStream inStream, f
231239 }
232240 }
233241 break ;
242+ case JBBPCompiler .CODE_CUSTOMTYPE : {
243+ if (resultNotIgnored ){
244+ final int extraData = JBBPUtils .unpackInt (compiled , positionAtCompiledBlock );
245+ final JBBPFieldTypeParameterContainer fieldTypeInfo = this .compiledBlock .getCustomTypeFields ()[JBBPUtils .unpackInt (compiled , positionAtCompiledBlock )];
246+ final JBBPAbstractField field = this .customFieldTypeProcessor .readCustomFieldType (inStream , this .bitOrder , this .flags , fieldTypeInfo , name , extraData , wholeStreamArray , arrayLength );
247+ JBBPUtils .assertNotNull (field , "Must not return null as read result" );
248+ structureFields .add (field );
249+ }
250+ }break ;
234251 case JBBPCompiler .CODE_BOOL : {
235252 if (resultNotIgnored ) {
236253 if (arrayLength < 0 ) {
@@ -511,7 +528,7 @@ public JBBPFieldStruct parse(final byte[] array, final JBBPVarFieldProcessor var
511528 * @see JBBPBitOrder#MSB0
512529 */
513530 public static JBBPParser prepare (final String script , final JBBPBitOrder bitOrder ) {
514- return new JBBPParser (script , bitOrder , 0 );
531+ return new JBBPParser (script , bitOrder , null , 0 );
515532 }
516533
517534 /**
@@ -529,9 +546,27 @@ public static JBBPParser prepare(final String script, final JBBPBitOrder bitOrde
529546 * @since 1.1
530547 */
531548 public static JBBPParser prepare (final String script , final JBBPBitOrder bitOrder , final int flags ) {
532- return new JBBPParser (script , bitOrder , flags );
549+ return new JBBPParser (script , bitOrder , null , flags );
533550 }
534551
552+ /**
553+ * Prepare a parser for a script with defined bit order and special flags.
554+ *
555+ * @param script a text script contains field order and types reference, it
556+ * must not be null
557+ * @param bitOrder the bit order for reading operations, it must not be null
558+ * @param customFieldTypeProcessor custom field type processor, it can be null
559+ * @param flags special flags for parsing
560+ * @return the prepared parser for the script
561+ * @see JBBPBitOrder#LSB0
562+ * @see JBBPBitOrder#MSB0
563+ * @see #FLAG_SKIP_REMAINING_FIELDS_IF_EOF
564+ *
565+ * @since 1.1.1
566+ */
567+ public static JBBPParser prepare (final String script , final JBBPBitOrder bitOrder , final JBBPCustomFieldTypeProcessor customFieldTypeProcessor , final int flags ) {
568+ return new JBBPParser (script , bitOrder , customFieldTypeProcessor , flags );
569+ }
535570
536571 /**
537572 * Prepare a parser for a script with default bit order (LSB0) use.
0 commit comments