Created
October 31, 2017 13:20
-
-
Save EFLFE/982c555ef68802933d7e81182ea20627 to your computer and use it in GitHub Desktop.
DuplexTextStream combined FileStream, StreamWriter and StreamReader in one class.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Diagnostics; | |
using System.IO; | |
namespace EFLFE | |
{ | |
[DebuggerDisplay("DuplexTextStream: {FileName}")] | |
public sealed class DuplexTextStream : FileStream | |
{ | |
// What? Single Responsibility Principle? I don't understand what you're talking about. | |
public readonly StreamWriter In; | |
public readonly StreamReader Out; | |
public readonly string FileName; | |
public readonly string FilePath; | |
public DuplexTextStream(string filePath) : base(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read) | |
{ | |
FileName = Path.GetFileName(filePath); | |
FilePath = filePath; | |
In = new StreamWriter(this); | |
Out = new StreamReader(this); | |
} | |
[Obsolete("Use CloseDuplex.", true)] | |
public new void Close() { } | |
public void CloseDuplex() | |
{ | |
In.Close(); | |
Out.Close(); | |
base.Close(); | |
} | |
public bool IsEmpty => Length == 0L; | |
public bool EndOfStream => Position >= Length; | |
public void SeekToEnd() => Position = Length; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment