Created
March 9, 2016 21:40
-
-
Save MattBroyles/f0b514d55b88ec9a4db0 to your computer and use it in GitHub Desktop.
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
// 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