int findThisStreamFirstScanLen() throws FileParsingException {
    int length = -1;
    numOpeningScanTagsFound = 0;
    XMLStreamReaderImpl reader = null;
    try {
      reader = (readerPool == null) ? new XMLStreamReaderImpl() : readerPool.borrowObject();
      reader.setInput(is, StandardCharsets.UTF_8.name());
      LogHelper.setJavolutionLogLevelFatal();

      int eventType = XMLStreamConstants.END_DOCUMENT;
      CharArray localName;
      do {
        // Read the next XML element
        try {
          eventType = reader.next();
        } catch (XMLStreamException e) {
          if (e instanceof XMLUnexpectedEndTagException) {
            continue;
          }
          if (e instanceof XMLUnexpectedEndOfDocumentException) {
            // this happens when we have nested <scan> tags and parsing not the whole MS1/MS2 child pairs
            return length;
          }
          throw new FileParsingException(e);
        }

        // Process the read event
        switch (eventType) {

          case XMLStreamConstants.START_ELEMENT:
            localName = reader.getLocalName();

            if (localName.contentEquals(TAG.SCAN.name)) {
              numOpeningScanTagsFound += 1;
              break;
            }
            break;

          case XMLStreamConstants.END_ELEMENT:
            localName = reader.getLocalName();

            if (localName.contentEquals(TAG.SCAN.name)) {
              if (numOpeningScanTagsFound == 1) {
                final XMLStreamReaderImpl.LocationImpl loc = reader.getLocation();
                length = loc.getCharacterOffset() + loc.getBomLength();
                return length;
              }
            }
            break;
        }
      } while (eventType != XMLStreamConstants.END_DOCUMENT);


    } catch (Exception e) {
      throw new FileParsingException(e);
    } finally {
      // we need to return the reaer to the pool, if we borrowed it from there
      if (readerPool != null && reader != null) {
        try {
          readerPool.returnObject(reader);
        } catch (Exception e) {
          throw new FileParsingException(e);
        }
      }
    }

    return length;
  }
int findThisStreamFirstScanLen ( ) throws FileParsingException { int length = - 1 ; numOpeningScanTagsFound = 0 ; XMLStreamReaderImpl reader = null ; try { reader = ( readerPool == null ) ? new XMLStreamReaderImpl ( ) : readerPool . borrowObject ( ) ; reader . setInput ( is , StandardCharsets . UTF_8 . name ( ) ) ; LogHelper . setJavolutionLogLevelFatal ( ) ; int eventType = XMLStreamConstants . END_DOCUMENT ; CharArray localName ; do { try { eventType = reader . next ( ) ; } catch ( XMLStreamException e ) { if ( e instanceof XMLUnexpectedEndTagException ) { continue ; } if ( e instanceof XMLUnexpectedEndOfDocumentException ) { return length ; } throw new FileParsingException ( e ) ; } switch ( eventType ) { case XMLStreamConstants . START_ELEMENT : localName = reader . getLocalName ( ) ; if ( localName . contentEquals ( TAG . SCAN . name ) ) { numOpeningScanTagsFound += 1 ; break ; } break ; case XMLStreamConstants . END_ELEMENT : localName = reader . getLocalName ( ) ; if ( localName . contentEquals ( TAG . SCAN . name ) ) { if ( numOpeningScanTagsFound == 1 ) { final XMLStreamReaderImpl . LocationImpl loc = reader . getLocation ( ) ; length = loc . getCharacterOffset ( ) + loc . getBomLength ( ) ; return length ; } } break ; } } while ( eventType != XMLStreamConstants . END_DOCUMENT ) ; } catch ( Exception e ) { throw new FileParsingException ( e ) ; } finally { if ( readerPool != null && reader != null ) { try { readerPool . returnObject ( reader ) ; } catch ( Exception e ) { throw new FileParsingException ( e ) ; } } } return length ; }
Intended use: find the length of the last scan entry in the file. MzXML might contain
chromatograms after scans, and the index only contains the offset of the last scan - there is
no easy way to figure out what the length is. If you just consider, that the length is from the
offset to the beginning of the index, then you might end up reading several hundred Mb of
chromatogram data. To use this method properly, you should create a File-based stream
(buffered), starting at the offset of the last scan and this method will read the file until it
finds the corresponding closing tag.

@return The length of the first scan entry in this stream, or, more precisely, the offset in
the stream of the end of the closing tag of the first 'scan' tag. Or -1 if no matching pair of
'scan' tags was found.