private string GetIndexData() { // with the backward reader we can quickly locate the origin of the <indexOffset> string line = null; StreamBackwardReader s = new StreamBackwardReader(filename); // look for the closing tag of the mzXML structure bool foundSlashMzXml = false; while (!foundSlashMzXml) { line = s.ReadLine(); if (line.Contains("</mzXML>")) { foundSlashMzXml = true; } } if (!foundSlashMzXml) { throw new Exception("Invalid mz-xml File; couldn't locate the ending </mzXML> tag."); } // look for the index offset so we can read forward again // TODO this is not the most elegant way to do this. while (!line.Contains("</indexOffset>")) { line = s.ReadLine(); } while (!line.Contains("<indexOffset>")) { line += s.ReadLine(); } s.Close(); long indexOffset = Parser.Uint(regexInteger.Match(line).Groups[1].ToString()); // load the offset table StreamReader stream = new StreamReader(filename); stream.BaseStream.Seek(indexOffset, SeekOrigin.Begin); stream.DiscardBufferedData(); //_stream.BaseStream.Seek(indexOffset, SeekOrigin.Begin); //_stream.DiscardBufferedData(); StringBuilder indexData = new StringBuilder(); do { line = stream.ReadLine(); indexData.Append(line); indexData.Append("\n"); } while (!line.Contains("</index>")); stream.Close(); string index = indexData.ToString(); int indexStart = index.IndexOf("<index", StringComparison.InvariantCulture); return(index.Substring(indexStart, (index.IndexOf("</index>", StringComparison.InvariantCulture) + 8) - indexStart)); }
public void TestReadingBackwards() { var text = "these\nare\nsome\nlines"; var linesBackwards = new List <string>(); using (var memory = new MemoryStream(Encoding.UTF8.GetBytes(text))) using (var reader = new StreamBackwardReader(memory)) { linesBackwards.AddRange(reader.ReadLines()); } linesBackwards.Reverse(); var actual = string.Join("\n", linesBackwards); Assert.AreEqual(text, actual); }