Created
October 26, 2011 18:38
-
-
Save andyedinborough/1317325 to your computer and use it in GitHub Desktop.
Adds the ability to peek at the next line; based on http://stackoverflow.com/questions/842465/reading-a-line-from-a-streamreader-without-consuming/842554#842554
This file contains 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.IO; | |
namespace AE.Net.Mail { | |
//http://stackoverflow.com/questions/842465/reading-a-line-from-a-streamreader-without-consuming | |
public class PeekableTextReader : TextReader { | |
private TextReader _Underlying; | |
private MemoryStream _Buffer; | |
private StreamReader _BufferReader; | |
private StreamWriter _BufferWriter; | |
private long _BufferPosition = 0; | |
public PeekableTextReader(TextReader underlying) { | |
_Underlying = underlying; | |
_Buffer = new System.IO.MemoryStream(); | |
_BufferReader = new StreamReader(_Buffer); | |
_BufferWriter = new StreamWriter(_Buffer); | |
} | |
public string PeekLine() { | |
string line = _Underlying.ReadLine(); | |
if (line == null) return null; | |
_Buffer.Seek(0, SeekOrigin.End); | |
_BufferWriter.WriteLine(line); | |
_BufferWriter.Flush(); | |
_Buffer.Seek(_BufferPosition, SeekOrigin.Begin); | |
return line; | |
} | |
public override int Peek() { | |
var read = _Underlying.Read(); | |
_Buffer.Seek(0, SeekOrigin.End); | |
_BufferWriter.Write(read); | |
_BufferWriter.Flush(); | |
_Buffer.Seek(_BufferPosition, SeekOrigin.Begin); | |
return read; | |
} | |
public override void Close() { | |
_Underlying.Close(); | |
} | |
public override System.Runtime.Remoting.ObjRef CreateObjRef(Type requestedType) { | |
return _Underlying.CreateObjRef(requestedType); | |
} | |
protected override void Dispose(bool disposing) { | |
if (disposing) { | |
TryDispose(ref _Underlying); | |
TryDispose(ref _Buffer); | |
TryDispose(ref _BufferReader); | |
TryDispose(ref _BufferWriter); | |
} | |
} | |
private void TryDispose<T>(ref T obj) where T : class, IDisposable { | |
try { | |
if (obj != null) { | |
obj.Dispose(); | |
} | |
} catch (Exception) { } | |
obj = null; | |
} | |
public override bool Equals(object obj) { | |
return _Underlying.Equals(obj); | |
} | |
public override int GetHashCode() { | |
return _Underlying.GetHashCode(); | |
} | |
public override object InitializeLifetimeService() { | |
return _Underlying.InitializeLifetimeService(); | |
} | |
public override string ReadLine() { | |
if (_Buffer.Length > _BufferPosition) { | |
var line = ReadFromBuffer(rdr => rdr.ReadLine()); | |
return line; | |
} else { | |
return _Underlying.ReadLine(); | |
} | |
} | |
public override int Read() { | |
if (_Buffer.Length > _BufferPosition) { | |
return ReadFromBuffer(rdr => rdr.Read()); | |
} else { | |
return _Underlying.Read(); | |
} | |
} | |
private T ReadFromBuffer<T>(Func<StreamReader, T> cmd) { | |
if (_BufferPosition != _Buffer.Position) { | |
_Buffer.Seek(_BufferPosition, SeekOrigin.Begin); | |
} | |
var value = cmd(_BufferReader); | |
_BufferPosition = _Buffer.Position; | |
return value; | |
} | |
public override int Read(char[] buffer, int index, int count) { | |
if (_Buffer.Length > _BufferPosition) { | |
var read = ReadFromBuffer(rdr => rdr.Read(buffer, index, count)); | |
if (count > read && _Buffer.Position == _Buffer.Length) { | |
read += _Underlying.Read(buffer, index + read, count - read); | |
} | |
return read; | |
} else { | |
return _Underlying.Read(buffer, index, count); | |
} | |
} | |
public override int ReadBlock(char[] buffer, int index, int count) { | |
if (_Buffer.Length > _BufferPosition) { | |
var read = ReadFromBuffer(rdr => rdr.ReadBlock(buffer, index, count)); | |
if (count > read && _Buffer.Position == _Buffer.Length) { | |
read += _Underlying.ReadBlock(buffer, index + read, count - read); | |
} | |
return read; | |
} else { | |
return _Underlying.ReadBlock(buffer, index, count); | |
} | |
} | |
public override string ReadToEnd() { | |
if (_Buffer.Length > _BufferPosition) { | |
return ReadFromBuffer(rdr => rdr.ReadToEnd()) + _Underlying.ReadToEnd(); | |
} else { | |
return _Underlying.ReadToEnd(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Not recommended, PeekLine() is bugged,