Skip to content

Instantly share code, notes, and snippets.

@MattBroyles
Created March 9, 2016 21:40
Show Gist options
  • Save MattBroyles/f0b514d55b88ec9a4db0 to your computer and use it in GitHub Desktop.
Save MattBroyles/f0b514d55b88ec9a4db0 to your computer and use it in GitHub Desktop.
// stolen with love from http://stackoverflow.com/questions/842465/reading-a-line-from-a-streamreader-without-consuming
public class PeekableStreamReaderAdapter
{
private StreamReader Underlying;
private Queue<string> BufferedLines;
public PeekableStreamReaderAdapter(StreamReader underlying)
{
Underlying = underlying;
BufferedLines = new Queue<string>();
}
public string PeekLine()
{
string line = Underlying.ReadLine();
if (line == null)
return null;
BufferedLines.Enqueue(line);
return line;
}
public string ReadLine()
{
if (BufferedLines.Count > 0)
return BufferedLines.Dequeue();
return Underlying.ReadLine();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment