Skip to content
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

Apply addition of discardBuffer to ZnBufferedReadStream>>#readInto:startingAt:count in the case that the buffer is bypassed #17055

Merged
merged 1 commit into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions src/Zinc-Character-Encoding-Core/ZnBufferedReadStream.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -273,24 +273,22 @@ ZnBufferedReadStream >> peekFor: object [

{ #category : 'accessing' }
ZnBufferedReadStream >> position [

"If the buffer advanced, we need to check the original stream position, minus what we have read.
The -1 is because the buffer is base 1"

^ stream position - limit + position - 1
]

{ #category : 'accessing' }
ZnBufferedReadStream >> position: anInteger [

| bufferEnd bufferStart |
bufferEnd := stream position.
bufferStart := bufferEnd - limit.
(anInteger between: bufferStart and: bufferEnd)
ifTrue: [ position := anInteger - bufferStart + 1 ]
ifFalse: [
"We reset the buffer and update the position in the underlying stream"
limit := 0.
position := 1.
self discardBuffer.
stream position: anInteger ]
]

Expand Down Expand Up @@ -327,12 +325,13 @@ ZnBufferedReadStream >> readInto: collection startingAt: offset count: requested
| newOffset |
newOffset := offset + countRead.
(self shouldBufferReadOfCount: countYetToRead)
ifTrue: [ self nextBuffer.
ifTrue: [
self nextBuffer.
limit > 0
ifTrue:
[ countRead := countRead + (self readInto: collection startingAt: newOffset count: countYetToRead) ] ]
ifFalse:
[ countRead := countRead + (stream readInto: collection startingAt: newOffset count: countYetToRead) ] ].
ifTrue: [ countRead := countRead + (self readInto: collection startingAt: newOffset count: countYetToRead) ] ]
ifFalse: [
self discardBuffer.
countRead := countRead + (stream readInto: collection startingAt: newOffset count: countYetToRead) ] ].
^ countRead
]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,29 @@ ZnBufferedReadStreamTest >> testPeek [
self assert: stream next isNil
]

{ #category : 'tests' }
ZnBufferedReadStreamTest >> testPositioning [
| byteArray stream |

byteArray := (1 to: 255) as: ByteArray.

stream := ZnBufferedReadStream on: byteArray readStream.
stream sizeBuffer: 32.

self assert: stream position equals: 0.
self assert: stream next equals: 1.
self assert: stream position equals: 1.

self assert: (stream next: 100) equals: ((2 to: 101) as: ByteArray).
self assert: stream position equals: 101.

stream position: 99.
self assert: stream position equals: 99.

self assert: stream next equals: 100.
self assert: stream position equals: 100
]

{ #category : 'tests' }
ZnBufferedReadStreamTest >> testReadInto [
| stream buffer count |
Expand Down