-
-
Notifications
You must be signed in to change notification settings - Fork 44
Implement our own Peek() which blocks #49
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
Conversation
JustArchi
left a comment
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.
New tests pass so LGTM
This comment has been minimized.
This comment has been minimized.
yaakov-h
left a comment
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 wonder if
| /// </summary> | ||
| internal sealed class BlockingStream : Stream | ||
| { | ||
| private static readonly byte[] TestData = Encoding.UTF8.GetBytes( |
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.
IMO this would be better as a wrapper for an existing Stream than as a weird stream with hardcoded content.
|
|
||
| private long _backingPosition; | ||
|
|
||
| internal BlockingStream(int maxReadAtOnce = 1) |
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.
If maxReadAtOnce is customisable then BlockingStream isn't guaranteed to be blocking.
It looks like it's only guaranteed to be blocking when our maxReadAtOnce is smaller than the StreamReader's bufferSize, which is also configurable.
It may also be possible that if StreamReader's bufferSize is 1 then it will never block. 🤔
| // Never read more than user-configured limit | ||
| if (count > MaxReadAtOnce) count = MaxReadAtOnce; | ||
|
|
||
| for (var i = 0; i < count; i++) buffer[offset++] = TestData[_backingPosition++]; |
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.
Maybe use Array.BlockCopy?
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.
Buffer.BlockCopy, yup.
| internal class StreamsTestCase | ||
| { | ||
| [TestCase(1)] | ||
| [TestCase(4096)] |
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 don't think 4096 gets us as blocking stream, as per the previous comment. The default buffer size is 1024.
| } | ||
|
|
||
| var text = textReader.ReadLine(); | ||
| var text = sb.ToString(); |
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.
Why this loop? Is there something wrong with textReader.ReadLine() from the old code? If its the case that we might have something in our little peekedNext field, can we not deal with that in a simpler manner?
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 wonder if it would be simpler to build a TextReader wrapper.
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 initially wanted to just use Stream, but that had its own problems.
I think ReadLine may return just for \r without being followed by \n which isn't how Valve deals with it (always looking for \n only).
|
I moved |
Fixes #48
StreamReader.Peek() just returns -1 instead of blocking, we mitigate that by using Read() which does block.