public long wrapRecords ( ChunkStream < byte [ ] , BytesReference > stream , boolean withCollection ) throws IOException { final AtomicInteger l = new AtomicInteger ( ) ; try { // keep reference to our record listener here, because builder will // enforce an internal record listener MarcRecordListener marcRecordListener = builder . getMarcRecordListener ( ) ; if ( withCollection ) { // write XML declaration if required if ( marcRecordListener instanceof ContentHandler ) { ( ( ContentHandler ) marcRecordListener ) . startDocument ( ) ; } marcRecordListener . beginCollection ( ) ; } if ( builder . marcGenerator == null ) { builder . marcGenerator = builder . createGenerator ( ) ; } // short-circuit: set MARC listener of the MARC generator to builder to capture records builder . marcGenerator . setMarcListener ( builder ) ; builder . marcGenerator . setCharset ( builder . getCharset ( ) ) ; builder . stream = stream ; // voila: now we can stream records builder . recordStream ( ) . forEach ( record -> { marcRecordListener . record ( record ) ; l . incrementAndGet ( ) ; } ) ; stream . close ( ) ; builder . marcGenerator . flush ( ) ; if ( withCollection ) { marcRecordListener . endCollection ( ) ; if ( marcRecordListener instanceof ContentHandler ) { ( ( ContentHandler ) marcRecordListener ) . endDocument ( ) ; } } } catch ( SAXException e ) { throw new IOException ( e ) ; } finally { // we close input stream always if ( builder . getInputStream ( ) != null ) { builder . getInputStream ( ) . close ( ) ; } } return l . get ( ) ; }
Wrap records into a collection . Can process any MARC streams not only separator streams .