Skip to content

Commit

Permalink
Merge pull request #17112 from Rinzwind/backport-zinc-ca180a2
Browse files Browse the repository at this point in the history
Backport addition of discardBuffer to ZnBufferedReadStream>>#readInto:startingAt:count in the case that the buffer is bypassed
  • Loading branch information
jecisc authored Sep 14, 2024
2 parents 5fcb57b + 5d2a3f2 commit 7d6dcff
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
19 changes: 9 additions & 10 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 All @@ -313,7 +311,7 @@ ZnBufferedReadStream >> readFromBufferInto: collection startingAt: offset count:
^ read
]

{ #category : 'accessing' }
{ #category : 'private' }
ZnBufferedReadStream >> readInto: collection startingAt: offset count: requestedCount [
"Read requestedCount elements into collection starting at offset,
answering the number of elements read, there could be fewer elements available."
Expand All @@ -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 7d6dcff

Please sign in to comment.