PositionableStream.st
changeset 15348 f5677dc62dc0
parent 15159 b94bdee97afa
child 15353 0a60d904c4c0
equal deleted inserted replaced
15347:bf763ed0a879 15348:f5677dc62dc0
   228 !
   228 !
   229 
   229 
   230 position
   230 position
   231     "return the read position"
   231     "return the read position"
   232 
   232 
   233     ZeroPosition == 0 ifTrue:[
   233     ^ self position0Based
   234         ^ self position0Based
       
   235     ] ifFalse:[
       
   236         ^ self position1Based
       
   237     ].
       
   238 !
   234 !
   239 
   235 
   240 position0Based
   236 position0Based
   241     "return the read position 0-based"
   237     "return the read position 0-based"
   242 
   238 
   243     ^ position - ZeroPosition
   239     ^ position
   244 !
   240 !
   245 
   241 
   246 position0Based:index0Based
   242 position0Based:index0Based
   247     "set the read (or write) position"
   243     "set the read (or write) position"
   248 
   244 
   249     ((index0Based > readLimit) or:[index0Based < 0]) ifTrue: [^ self positionError:index0Based].
   245     ((index0Based > readLimit) or:[index0Based < 0]) ifTrue: [^ self positionError:index0Based].
   250     position := index0Based + ZeroPosition
   246     position := index0Based
   251 !
   247 !
   252 
   248 
   253 position1Based
   249 position1Based
   254     "return the read position 1-based"
   250     "return the read position 1-based"
   255 
   251 
   292 !
   288 !
   293 
   289 
   294 position:newPos
   290 position:newPos
   295     "set the read (or write) position"
   291     "set the read (or write) position"
   296 
   292 
   297     ZeroPosition == 0 ifTrue:[
   293     ^ self position0Based:newPos
   298         ^ self position0Based:newPos
       
   299     ] ifFalse:[
       
   300         ^ self position1Based:newPos
       
   301     ].
       
   302 
   294 
   303     "
   295     "
   304      |s|
   296      |s|
   305 
   297 
   306      s := '1234567890' readStream.
   298      s := '1234567890' readStream.
   346 !
   338 !
   347 
   339 
   348 resetPosition
   340 resetPosition
   349     "set the read position to the beginning of the collection"
   341     "set the read position to the beginning of the collection"
   350 
   342 
   351     position := ZeroPosition
   343     position := 0
   352 
   344 
   353     "
   345     "
   354      |s|
   346      |s|
   355 
   347 
   356      s := 'hello world' readStream.
   348      s := 'hello world' readStream.
   363 setToEnd
   355 setToEnd
   364     "set the read position to the end of the collection.
   356     "set the read position to the end of the collection.
   365      #next will return EOF, #nextPut: will append to the stream.
   357      #next will return EOF, #nextPut: will append to the stream.
   366      (same Behavior as FileStream."
   358      (same Behavior as FileStream."
   367 
   359 
   368     position := readLimit + ZeroPosition
   360     position := readLimit
   369 !
   361 !
   370 
   362 
   371 skip:numberToSkip
   363 skip:numberToSkip
   372     "skip the next numberToSkip elements"
   364     "skip the next numberToSkip elements"
   373 
   365 
   509     collection := aCollection.
   501     collection := aCollection.
   510     readLimit := aCollection size.
   502     readLimit := aCollection size.
   511     readLimit == 0 ifTrue:[
   503     readLimit == 0 ifTrue:[
   512         self assert:(aCollection isCollection)
   504         self assert:(aCollection isCollection)
   513     ].
   505     ].
   514     position := ZeroPosition
   506     position := 0
   515 !
   507 !
   516 
   508 
   517 on:aCollection from:first to:last
   509 on:aCollection from:first to:last
   518     "setup for streaming on aCollection from first to last"
   510     "setup for streaming on aCollection from first to last"
   519 
   511 
   520     collection := aCollection.
   512     collection := aCollection.
   521     position := first - 1 + ZeroPosition.
   513     position := first - 1.
   522     readLimit := last
   514     readLimit := last
   523 !
   515 !
   524 
   516 
   525 positionError
   517 positionError
   526     "{ Pragma: +optSpace }"
   518     "{ Pragma: +optSpace }"
   560 
   552 
   561 nextAvailable:count
   553 nextAvailable:count
   562     |end result|
   554     |end result|
   563 
   555 
   564     end := position + count.
   556     end := position + count.
   565     (end - ZeroPosition + 1) > readLimit ifTrue:[
   557     (end + 1) > readLimit ifTrue:[
   566         end := readLimit.
   558         end := readLimit.
   567     ].
   559     ].
   568 
   560 
   569     result := collection copyFrom:position+1-ZeroPosition to:end.
   561     result := collection copyFrom:position+1 to:end.
   570     position := end.
   562     position := end.
   571     ^ result.
   563     ^ result.
   572 
   564 
   573     "
   565     "
   574         'abc' readStream nextAvailable:1.
   566         'abc' readStream nextAvailable:1.
   637 !PositionableStream methodsFor:'testing'!
   629 !PositionableStream methodsFor:'testing'!
   638 
   630 
   639 atEnd
   631 atEnd
   640     "return true, if the read-position is at the end"
   632     "return true, if the read-position is at the end"
   641 
   633 
   642     ^ (position - ZeroPosition + 1) > readLimit
   634     ^ (position + 1) > readLimit
   643 !
   635 !
   644 
   636 
   645 isEmpty
   637 isEmpty
   646     "return true, if the contents of the stream is empty"
   638     "return true, if the contents of the stream is empty"
   647 
   639 
   663 ! !
   655 ! !
   664 
   656 
   665 !PositionableStream class methodsFor:'documentation'!
   657 !PositionableStream class methodsFor:'documentation'!
   666 
   658 
   667 version
   659 version
   668     ^ '$Header: /cvs/stx/stx/libbasic/PositionableStream.st,v 1.156 2013-04-25 13:09:40 stefan Exp $'
   660     ^ '$Header: /cvs/stx/stx/libbasic/PositionableStream.st,v 1.157 2013-06-03 17:48:26 cg Exp $'
   669 !
   661 !
   670 
   662 
   671 version_CVS
   663 version_CVS
   672     ^ '$Header: /cvs/stx/stx/libbasic/PositionableStream.st,v 1.156 2013-04-25 13:09:40 stefan Exp $'
   664     ^ '$Header: /cvs/stx/stx/libbasic/PositionableStream.st,v 1.157 2013-06-03 17:48:26 cg Exp $'
   673 ! !
   665 ! !
   674 
   666 
   675 
   667 
   676 PositionableStream initialize!
   668 PositionableStream initialize!