diff --git a/src/FlatFile.Core/Base/FlatFileEngine.cs b/src/FlatFile.Core/Base/FlatFileEngine.cs index 22d18a3..a3488b9 100644 --- a/src/FlatFile.Core/Base/FlatFileEngine.cs +++ b/src/FlatFile.Core/Base/FlatFileEngine.cs @@ -57,6 +57,18 @@ protected FlatFileEngine(Func handleEntryReadError = nu public virtual IEnumerable Read(Stream stream) where TEntity : class, new() { var reader = new StreamReader(stream); + return Read(reader); + } + + /// + /// Reads the specified text reader. + /// + /// The type of the t entity. + /// The reader. + /// IEnumerable<TEntity>. + /// Impossible to parse line + public virtual IEnumerable Read(TextReader reader) where TEntity : class, new() + { string line; int lineNumber = 0; @@ -68,7 +80,7 @@ protected FlatFileEngine(Func handleEntryReadError = nu while ((line = reader.ReadLine()) != null) { if (string.IsNullOrEmpty(line) || string.IsNullOrEmpty(line.Trim())) continue; - + bool ignoreEntry = false; var entry = new TEntity(); try @@ -104,7 +116,7 @@ protected FlatFileEngine(Func handleEntryReadError = nu /// Processes the header. /// /// The reader. - protected virtual void ProcessHeader(StreamReader reader) + protected virtual void ProcessHeader(TextReader reader) { reader.ReadLine(); } @@ -147,7 +159,17 @@ protected virtual void WriteEntry(TextWriter writer, int lineNumber, TE public virtual void Write(Stream stream, IEnumerable entries) where TEntity : class, new() { TextWriter writer = new StreamWriter(stream); + Write(writer, entries); + } + /// + /// Writes to the specified text writer. + /// + /// The type of the t entity. + /// The text writer. + /// The entries. + public void Write(TextWriter writer, IEnumerable entries) where TEntity : class, new() + { this.WriteHeader(writer); int lineNumber = 0; diff --git a/src/FlatFile.Core/IFlatFileEngine.cs b/src/FlatFile.Core/IFlatFileEngine.cs index 05a6d0f..b0b8a48 100644 --- a/src/FlatFile.Core/IFlatFileEngine.cs +++ b/src/FlatFile.Core/IFlatFileEngine.cs @@ -16,6 +16,14 @@ public interface IFlatFileEngine /// IEnumerable<TEntity>. IEnumerable Read(Stream stream) where TEntity : class, new(); + /// + /// Reads from the specified text reader. + /// + /// The type of the t entity. + /// The text reader. + /// IEnumerable<TEntity>. + IEnumerable Read(TextReader reader) where TEntity : class, new(); + /// /// Writes to the specified stream. /// @@ -23,5 +31,13 @@ public interface IFlatFileEngine /// The stream. /// The entries. void Write(Stream stream, IEnumerable entries) where TEntity : class, new(); + + /// + /// Writes to the specified text writer. + /// + /// The type of the t entity. + /// The text writer. + /// The entries. + void Write(TextWriter writer, IEnumerable entries) where TEntity : class, new(); } } \ No newline at end of file diff --git a/src/FlatFile.Core/IFlatFileMultiEngine.cs b/src/FlatFile.Core/IFlatFileMultiEngine.cs index a47b36d..c33299d 100644 --- a/src/FlatFile.Core/IFlatFileMultiEngine.cs +++ b/src/FlatFile.Core/IFlatFileMultiEngine.cs @@ -13,12 +13,20 @@ public interface IFlatFileMultiEngine : IFlatFileEngine /// /// The stream. void Read(Stream stream); + + /// + /// Reads the specified text reader. + /// + /// The text reader. + void Read(TextReader reader); + /// /// Gets any records of type read by . /// /// /// IEnumerable<T>. IEnumerable GetRecords() where T : class, new(); + /// /// Gets or sets a value indicating whether this instance has a file header. /// diff --git a/src/FlatFile.Delimited/Implementation/DelimetedFileMultiEngine.cs b/src/FlatFile.Delimited/Implementation/DelimetedFileMultiEngine.cs index 215d974..e5b45db 100644 --- a/src/FlatFile.Delimited/Implementation/DelimetedFileMultiEngine.cs +++ b/src/FlatFile.Delimited/Implementation/DelimetedFileMultiEngine.cs @@ -137,7 +137,16 @@ protected override bool TryParseLine(string line, int lineNumber, ref T /// Impossible to parse line public void Read(Stream stream) { - var reader = new StreamReader(stream); + Read(new StreamReader(stream)); + } + + /// + /// Reads from the specified text reader. + /// + /// The text reader. + /// Impossible to parse line + public void Read(TextReader reader) + { string line; var lineNumber = 0; diff --git a/src/FlatFile.FixedLength/Implementation/FixedLengthFileMultiEngine.cs b/src/FlatFile.FixedLength/Implementation/FixedLengthFileMultiEngine.cs index 9226d09..499bfe5 100644 --- a/src/FlatFile.FixedLength/Implementation/FixedLengthFileMultiEngine.cs +++ b/src/FlatFile.FixedLength/Implementation/FixedLengthFileMultiEngine.cs @@ -142,22 +142,22 @@ public void Read(Stream stream) } /// - /// Reads the specified streamReader. + /// Reads from the specified text reader. /// - /// The stream reader configured as the user wants. + /// The text reader configured as the user wants. /// Impossible to parse line - public void Read(StreamReader reader) + public void Read(TextReader reader) { ReadInternal(reader); } /// - /// Internal method (private) to read from streamreader instead of stream + /// Internal method (private) to read from a text reader instead of stream /// This way the client code have a way to specify encoding. /// - /// The stream reader to read. + /// The text reader to read. /// Impossible to parse line - private void ReadInternal(StreamReader reader) + private void ReadInternal(TextReader reader) { string line; var lineNumber = 0;