diff --git a/src/Zinc-Character-Encoding-Core/ZnBufferedReadStream.class.st b/src/Zinc-Character-Encoding-Core/ZnBufferedReadStream.class.st index cc9e9a7a958..e39a2e8fcee 100644 --- a/src/Zinc-Character-Encoding-Core/ZnBufferedReadStream.class.st +++ b/src/Zinc-Character-Encoding-Core/ZnBufferedReadStream.class.st @@ -261,15 +261,14 @@ 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. @@ -277,8 +276,7 @@ ZnBufferedReadStream >> position: anInteger [ 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 ] ] @@ -301,7 +299,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." @@ -315,12 +313,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 ] diff --git a/src/Zinc-Character-Encoding-Tests/ZnBufferedReadStreamTest.class.st b/src/Zinc-Character-Encoding-Tests/ZnBufferedReadStreamTest.class.st index e1cf76b05ce..00ecb665a9c 100644 --- a/src/Zinc-Character-Encoding-Tests/ZnBufferedReadStreamTest.class.st +++ b/src/Zinc-Character-Encoding-Tests/ZnBufferedReadStreamTest.class.st @@ -33,6 +33,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 |