/// <summary> /// Method to create an index file if one does not exist. /// </summary> /// <param name="sourcefilename">Source filename.</param> /// <param name="sequencePointers">List of sequences in the source</param> /// <returns>IndexedSequenceProvider object</returns> public static SidecarFileProvider CreateIndexFile(string sourcefilename, IList <SequencePointer> sequencePointers) { if (sequencePointers == null) { throw new ArgumentNullException("sequencePointers"); } using (FileStream outputStream = new FileStream(sourcefilename + IndexFileExtension, FileMode.Create)) { using (BinaryWriter writer = new BinaryWriter(outputStream)) { // create a new master index file IndexFileHeader header = new IndexFileHeader(sourcefilename, sequencePointers.Count); // Spare space for writing filesize and date later on outputStream.Seek(sizeof(long) * 2, SeekOrigin.Begin); // serialize header information BinaryFormatter headerWriter = new BinaryFormatter(); headerWriter.Serialize(outputStream, header); // serialize sequence pointer objects int index; for (index = 0; index < sequencePointers.Count; index++) { WritePointer(outputStream, sequencePointers[index]); } outputStream.Seek(0, SeekOrigin.Begin); // Write to the space left at top (File size and date) writer.Write(outputStream.Length); writer.Write(DateTime.Now.Date.ToFileTime()); } } return(new SidecarFileProvider(sourcefilename)); }
/// <summary> /// Initializes a new instance of IndexedSequenceProvider class. /// </summary> /// <param name="sourceFilename">Full path to the source sequence file. /// This method will search for the index file by itself</param> private SidecarFileProvider(string sourceFilename) { try { _inputStream = new FileStream(sourceFilename + IndexFileExtension, FileMode.Open, FileAccess.Read); _offsetReader = new BinaryReader(_inputStream); _sidecarLength = _offsetReader.ReadInt64(); _sidecarLastWriteTime = _offsetReader.ReadInt64(); // read header info BinaryFormatter headerFormatter = new BinaryFormatter(); _header = headerFormatter.Deserialize(_inputStream) as IndexFileHeader; _contentsOffset = _inputStream.Position; // TOC starts just after the header } catch { // Dispose of streams if opened and later crashed Dispose(); throw; } }