Lume
Explore posts from serversRead stream twice without missing data
What I expected: ( | = cursor)
Stream: | 1, 2, 3, 4, 5, 6, 7, 8
- Read 1 byte
Stream: 1 | 2, 3, 4, 5, 6, 7, 8
- Reset to start using Seek
Stream: | 1, 2, 3, 4, 5, 6, 7, 8
- Read the whole stream
Stream: 1, 2, 3, 4, 5, 6, 7, 8 |
What actually happens:
Stream: | 1, 2, 3, 4, 5, 6, 7, 8
Internal cache:
- Read 1 byte
Stream: | 2, 3, 4, 5, 6, 7, 8
Internal cache: 1
- Reset to start using Seek
Stream: | 1, EOF + 2, 3, 4, 5, 6, 7, 8 ( cache + stream)
- Read the whole stream
Stream: 1, EOF + | 2, 3, 4, 5, 6, 7, 8
So now I have to read it again to get the rest of it. Is this kinda accurate as a mental model?
78 replies
Read stream twice without missing data
Now I thought why not just reset the "cursor" using Stream.Position = 0 to the starting position. And then if I read it using Stream.Read. The output should be the following:
Stream.Read(buffer, 0, buffer.Length) // buffer: 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0 , 0, ...
78 replies
Read stream twice without missing data
I believe there might be a misunderstanding between us. My intention is not to remove the trailing zeros from the byte array; that is perfectly acceptable. Instead, my goal is to read one byte, perform certain checks on it, and then read from the start of the original stream until it reaches a maximum of 128 bytes.
Example:
Stream: 1, 2, 3, 4, 5, 6, 7, 8, 9
var tmpBuffer = new byte[1];
Stream.Read(tmpBuffer) // tmpBuffer: 1
var buffer = new byte[128];
Stream.Read(buffer, 0, buffer.Length) // buffer: 2, 3, 4, 5, 6, 7, 8, 9 , 0, 0 , 0, ...
This is expected because the "cursor" moves one position to the right side (right?).
78 replies