Skip to content

Instantly share code, notes, and snippets.

@EFLFE
Created October 31, 2017 13:20
Show Gist options
  • Save EFLFE/982c555ef68802933d7e81182ea20627 to your computer and use it in GitHub Desktop.
Save EFLFE/982c555ef68802933d7e81182ea20627 to your computer and use it in GitHub Desktop.
DuplexTextStream combined FileStream, StreamWriter and StreamReader in one class.
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