code: public int wrapFields(String type, ChunkStream<byte[], BytesReference> stream,
                                    boolean withCollection) throws IOException {
          int count = 0;
          MarcListener marcListener = builder.getMarcListener(type);
          if (marcListener == null) {
              return count;
          }
          try {
              if (withCollection) {
                  // write XML declaration if required
                  if (marcListener instanceof ContentHandler) {
                      ((ContentHandler) marcListener).startDocument();
                  }
                  marcListener.beginCollection();
              }
              if (builder.marcGenerator == null) {
                  builder.marcGenerator = builder.createGenerator();
              }
              Chunk<byte[], BytesReference> chunk;
              while ((chunk = stream.readChunk()) != null) {
                  builder.marcGenerator.chunk(chunk);
                  count++;
              }
              stream.close();
              builder.marcGenerator.flush();
              if (withCollection) {
                  marcListener.endCollection();
                  if (marcListener instanceof ContentHandler) {
                      ((ContentHandler) marcListener).endDocument();
                  }
              }
          } catch (SAXException e) {
              throw new IOException(e);
          } finally {
              if (builder.getInputStream() != null) {
                  // essential
                  builder.getInputStream().close();
              }
          }
          return count;
      }
docs: Pass a given chunk stream to a MARC generator, chunk by chunk.
  Can process any MARC streams, not only separator streams.
  @param type the MARC record type
  @param stream a chunk stream
  @param withCollection true if stream should be wrapped into a collection element
  @return the number of chunks in the stream
  @throws IOException if chunk reading fails