-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unbuffered Stream Reader #88
Conversation
A version of StreamReader that doesn't make use of a character buffer. This results in lower performance but the prevents the underlying stream from moving further than needed.
e9abe21
to
a65f4ec
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor comments, otherwise looks good
/// </summary> | ||
/// <remarks> | ||
/// This reader is not currently at feature parity with <see cref="StreamReader"/>. Features will be developed as needed. | ||
/// See: #87 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we link that SO post in here too? Useful background info.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What SO post, the one about StreamReader position issues?
I don't think it's necessary to teach the user why position can't be calculated on a different class.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought it was the reason this class existed. Normally you'd use StreamReader
but sometimes you can't and good background reading on why that might be the case is in that SO post.
But if you don't agree, I'm a-ok with that too.
Feedback addressed |
Adds
UnbufferedStreamReader
. A stream reader that does not buffer data from the backing BaseStream.This implementation is less performant than
StreamReader
but doesn't read ahead on the underlying stream ensuring the BaseStream's position to remains lockstep with the characters read out ofUnbufferedStreamReader
.This stream is useful when reading files with mixed encoding or where the BaseStream needs to be advanced the minimum amount required.
What is the current behaviour?
StreamReader
fills a buffer of characters when responding to read requests. The characters buffer is filled by repeatedly reading blocks of itsBaseStream
and converting them into characters until the character buffer is full.This means that the
BaseStream
is advanced beyond the point that theStreamReader
has been read.What is the new behaviour?
UnbufferedStreamReader
effectively functions the same asStreamReader
with a byte and character buffer size set to 1. TheUnbufferedStreamReader
reads one byte at a time out ofBaseStream
until it has enough to resolve a full character. That one character is then evaluated by the read method to decide whether to continue reading or return.Note: The implementation does not currently cover the complete features set of
StreamReader
. New features will be developed as needed. This effort is captured in #87Why don't you just calculate actual the
BaseStream
position based onStreamReader
's unused buffer contents?TL;DR
StreamReader
is implementedI went down that path. It's not possible. This StackOverflow post covers the issue in detail. Read the linked post + all comments to understand.
If you really want to dig into this read through
StreamReader
's source: https://referencesource.microsoft.com/#mscorlib/system/io/streamreader.cs,b5fe1efcec14de32What issues does this resolve?
None, This class allows developers to continue reading a
BaseStream
beyond the lifecycle of theUnbufferedStreamReader
.What PRs does this depend on?
Does this introduce a breaking change?