Skip to content

Commit

Permalink
Merge pull request #17055 from Rinzwind/apply-zinc-ca180a2
Browse files Browse the repository at this point in the history
Apply addition of discardBuffer to ZnBufferedReadStream>>#readInto:startingAt:count in the case that the buffer is bypassed
  • Loading branch information
demarey authored Sep 9, 2024
2 parents e37a6e0 + 2b920d7 commit 2e4d037
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
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

0 comments on commit 2e4d037

Please sign in to comment.