/// <summary> /// Load the provided <see cref="System.IO.Stream">stream</see> into the parser. /// </summary> /// <param name="stream">The <see cref="System.IO.Stream">stream</see> to load.</param> public override void LoadStream(System.IO.Stream stream) { if ((!Layout.OpenAsText && Layout.RecordLength < 1) || Layout.MasterFields == null || Layout.MasterFields.Length < 1) { return; } UpdateEncoders(); if (Layout.OpenAsText) { stream.GotoStart(); byte[] bytes = stream.ReadToChar(Layout.TextLineTerminator); if (Layout.HeaderRecord != null && Layout.HeaderRecord.Fields?.Length > 0) { TaskManager.StartAction(delegate { Layout.HeaderRecord.ReadRecord(bytes); }); } else { ReadRecord(bytes); } while (bytes != null) { bytes = stream.ReadToChar(Layout.TextLineTerminator, Layout.Encoder.SourceEncoding); if ((stream.Position >= stream.Length - 2 || stream.Position < 0) && Layout.FooterRecord != null && Layout.FooterRecord.Fields?.Length > 0) { TaskManager.StartAction(delegate { Layout.FooterRecord.ReadRecord(bytes); }); bytes = null; } else { ReadRecord(bytes); } } } else if (stream.Length % Layout.RecordLength != 0) { throw new InvalidOperationException( $"{IO.FileParser.Properties.Resources.ResourceManager.GetString("InvalidLength")} ({stream.Length}/{Layout.RecordLength})"); } else { stream.GotoStart(); byte[] bytes = stream.ReadNext(Layout.RecordLength); if (Layout.HeaderRecord != null && Layout.HeaderRecord.Fields.Length > 0) { TaskManager.StartAction(delegate { Layout.HeaderRecord.ReadRecord(bytes); }); } else { ReadRecord(bytes); } while (bytes != null) { bytes = stream.ReadNext(Layout.RecordLength); if ((stream.Position >= stream.Length - 2 || stream.Position < 0) && Layout.FooterRecord != null && Layout.FooterRecord.Fields.Length > 0) { TaskManager.StartAction(delegate { Layout.FooterRecord.ReadRecord(bytes); }); bytes = null; } else { ReadRecord(bytes); } } } TaskManager.AwaitAllThenClean(); }