Search code examples
javabean-io

Comments require reader.markSupported() to return true beanio


Xml File

<beanio xmlns="http://www.beanio.org/2012/03"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://www.beanio.org/2012/03 http://www.beanio.org/2012/03/mapping.xsd">

        <stream name="stream" format="delimited" ignoreUnidentifiedRecords="true">
            <parser>
                <property name="delimiter" value="|"/>
                <property name="comments" value="Response Codes:,|File Trailer|,File Header|"/>
            </parser>

            <group name="HeaderRecord">
                <record>
                .
                .
                .
                </record>
            </group>
            <group name="GroupHeader">
                <record>
                .
                .
                .
                .
                </record>
            </group>
        </stream>
</beanio>

File that contains records looks like this

1   File Header|
.   HeaderRecord text.....
.   GroupHeader text......
19  Response Codes:
20                 |A - StackOverflow           |                
21                 |B - Facebook                |               
22                 |C - Google                  |                
23  |File Trailer|



Error 
java.lang.IllegalArgumentException: Comments require reader.markSupported() to return true
    at org.beanio.stream.util.CommentReader.<init>(CommentReader.java:61)
    at org.beanio.stream.delimited.DelimitedReader.<init>(DelimitedReader.java:149)
    at org.beanio.stream.delimited.DelimitedRecordParserFactory.createReader(DelimitedRecordParserFactory.java:56)
    at org.beanio.internal.parser.StreamFormatSupport.createRecordReader(StreamFormatSupport.java:61)
    at org.beanio.internal.parser.Stream.createBeanReader(Stream.java:88)

I want to ignore File Header, Trailer & Response Codes. Response codes starts in next line. Various blogs suggested for comments in property. Can anyone explain why it is not working?


Solution

  • The problem is with the Reader implementation that you used to read the data from as indicated by the error message:

    java.lang.IllegalArgumentException: Comments require reader.markSupported() to return true

    You need to use a Reader that supports buffering, i.e. a Reader that implements the mark() and reset() methods. See section 4.1.2 of the documentation and the description and explanation for the comments property name, which states:

    A comma separated list of values for identifying commented lines. If a line read from an input stream begins with any of the configured values, the line is ignored. A backslash may be used to escape a comma and itself. All whitespace is preserved.

    Enabling comments require the input reader passed to StreamFactory to support marking. Among others, Java's BufferedReader and StringReader support marking.

    For comments to work you need to read the data with either a BufferedReader or a StringReader

    InputStream inputStream = ... // obtain some input stream to the source of your data 
    Reader reader = new BufferedReader(new InputStreamReader(inputStream));
    BeanReader beanReader = factory.createReader("streamName", reader);