Skip to content

Commit

Permalink
[feenkcom/gtoolkit#3323] BlRopeableCollectionFile: don't leave the re…
Browse files Browse the repository at this point in the history
…ad stream open.
  • Loading branch information
akgrant43 committed Jun 19, 2023
1 parent 76097bb commit 0327dfb
Showing 1 changed file with 38 additions and 47 deletions.
85 changes: 38 additions & 47 deletions src/Bloc/BlRopeableCollectionFile.class.st
Expand Up @@ -51,7 +51,6 @@ Class {
#instVars : [
'fileReference',
'strict',
'binaryStream',
'map',
'bufferSize',
'buffer',
Expand Down Expand Up @@ -98,6 +97,12 @@ BlRopeableCollectionFile >> at: anInteger [
^ buffer at: (anInteger - bufferStart + 1) asInteger.
]

{ #category : #enumerating }
BlRopeableCollectionFile >> binaryReadStreamDo: aBlock [

^ fileReference binaryReadStreamDo: aBlock
]

{ #category : #accessing }
BlRopeableCollectionFile >> bufferSize [
^ bufferSize
Expand All @@ -113,18 +118,14 @@ BlRopeableCollectionFile >> bufferSize: anInteger [
]

{ #category : #private }
BlRopeableCollectionFile >> characterStream [
"Answer a read stream with character decoding on the receiver's binary stream"

^ ZnCharacterReadStream
on: binaryStream
encoding: 'utf8'
]

{ #category : #'initialize-release' }
BlRopeableCollectionFile >> close [

binaryStream close
BlRopeableCollectionFile >> characterStreamDo: aBlock [
"Evaluate aBlock with a {{gtClass:ZnCharacterReadStream}} on the receiver's file."

^ self binaryReadStreamDo: [ :bStream |
aBlock value:
(ZnCharacterReadStream
on: bStream
encoding: 'utf8') ]
]

{ #category : #copying }
Expand Down Expand Up @@ -164,39 +165,38 @@ BlRopeableCollectionFile >> copyToEndFrom: startIndex [

{ #category : #enumerating }
BlRopeableCollectionFile >> do: aBlock [
| characterStream ch |

fileReference ifNil: [ ^ self ].
binaryStream position: 0.
characterStream := self characterStream.
[ characterStream atEnd ] whileFalse:
[ ch := characterStream next.
ch ifNotNil: [ aBlock value: ch ] ]
self characterStreamDo: [ :stream |
stream position: 0.
[ stream atEnd ] whileFalse:
[ stream next ifNotNil:
[ :ch | aBlock value: ch ] ] ]
]

{ #category : #private }
BlRopeableCollectionFile >> ensureBuffered: anInteger [
"Ensure the character at anInteger is buffered"
"Ensure the character at anInteger is buffered.
anInteger is the position in the underlying binary stream, not the character position."
| mapEntry rawBuffer |

| mapEntry characterStream rawBuffer |
((anInteger between: bufferStart and: bufferStart + bufferedSize - 1)
and: [ bufferStart > 0 ]) ifTrue: [ ^ self ].
(haveSize and: [ anInteger > self size ]) ifTrue: [ ^ self ].
mapEntry := self mapEntryFor: anInteger.

binaryStream position: mapEntry value.
bufferStart := mapEntry key.
bufferedSize := 0.
characterStream := self characterStream.

[ anInteger > (bufferStart + bufferedSize - 1) or: [ bufferStart = 0 ] ]
whileTrue: [ bufferStart := bufferStart + bufferedSize.
rawBuffer := self loadNext: bufferSize with: characterStream.
self characterStreamDo: [ :stream |
stream position: mapEntry value.
bufferStart := mapEntry key.
bufferedSize := 0.
[ anInteger > (bufferStart + bufferedSize - 1) or: [ bufferStart = 0 ] ] whileTrue:
[ bufferStart := bufferStart + bufferedSize.
rawBuffer := self loadNext: bufferSize with: stream.
buffer := rawBuffer.
bufferedSize := buffer size.
map add: bufferStart + bufferedSize -> binaryStream position.
haveSize := haveSize or: [ binaryStream atEnd ].
(haveSize and: [ anInteger >= self size ]) ifTrue: [ ^ self ] ]
map add: bufferStart + bufferedSize -> stream position.
haveSize := haveSize or: [ stream atEnd ].
(haveSize and: [ anInteger >= self size ]) ifTrue: [ ^ self ] ] ]
]

{ #category : #accessing }
Expand All @@ -207,9 +207,7 @@ BlRopeableCollectionFile >> fileReference [
{ #category : #accessing }
BlRopeableCollectionFile >> fileReference: aFileReference [

binaryStream ifNotNil: [ binaryStream close ].
fileReference := aFileReference asFileReference.
binaryStream := fileReference binaryReadStream
fileReference := aFileReference.
]

{ #category : #comparing }
Expand Down Expand Up @@ -244,15 +242,17 @@ BlRopeableCollectionFile >> loadNext: aNumber with: aReadStream [
If an encoding error occurs and strict is false, fall back to null encoding"
| startPosition |

startPosition := binaryStream position.
startPosition := aReadStream position.
^ [ aReadStream next: aNumber ]
on: ZnCharacterEncodingError
do: [ :ex |
strict ifTrue:
[ ex pass ]
ifFalse:
[ binaryStream position: startPosition.
self loadNext: aNumber with: self nullEncodedStream ] ]
[ aReadStream position: startPosition.
self loadNext: aNumber with: (ZnCharacterReadStream
on: aReadStream wrappedStream
encoding: 'null') ] ]
]

{ #category : #'private - testing' }
Expand Down Expand Up @@ -299,15 +299,6 @@ BlRopeableCollectionFile >> mapEntryFor: anInteger [

]

{ #category : #private }
BlRopeableCollectionFile >> nullEncodedStream [
"Answer a read stream with null (ascii) character decoding on the receiver's binary stream"

^ ZnCharacterReadStream
on: binaryStream
encoding: 'null'
]

{ #category : #accessing }
BlRopeableCollectionFile >> size [
"Answer the receiver's size.
Expand Down

0 comments on commit 0327dfb

Please sign in to comment.