PositionableStream.st
changeset 17611 a3d9a43a6197
parent 17606 b30bac248329
child 18120 e3a375d5f6a8
child 19403 5accf347c2c1
equal deleted inserted replaced
17610:d0326e44edb2 17611:a3d9a43a6197
   249      s contents.
   249      s contents.
   250 
   250 
   251     "
   251     "
   252 
   252 
   253     "Modified: / 04-06-2007 / 17:21:55 / cg"
   253     "Modified: / 04-06-2007 / 17:21:55 / cg"
       
   254 ! !
       
   255 
       
   256 !PositionableStream methodsFor:'misc functions'!
       
   257 
       
   258 copyToEndInto:aWriteStream bufferSize:bufferSize
       
   259     "read from the receiver, and write all data up to the end to another stream.
       
   260      Return the number of bytes which have been transferred.
       
   261      Redefined here to avoid intermediate buffers/garbage.
       
   262      bufferSize does not matter here."
       
   263 
       
   264     |cnt|
       
   265 
       
   266     collection notNil ifTrue:[
       
   267         aWriteStream nextPutAll:collection startingAt:position+1 to:readLimit.
       
   268         cnt := readLimit - position.
       
   269         position := readLimit.
       
   270         ^ cnt.
       
   271     ].
       
   272     ^ super copyToEndInto:aWriteStream bufferSize:bufferSize.
       
   273 
       
   274     "
       
   275      |rs ws cnt|
       
   276 
       
   277      ws := #() writeStream.
       
   278      rs := #( 1 2 3 4 a nil true) readWriteStream.
       
   279      rs next.
       
   280      cnt := rs copyToEndInto:ws bufferSize:0.
       
   281      Transcript show:cnt; show:' '; showCR:ws contents.
       
   282     "
   254 ! !
   283 ! !
   255 
   284 
   256 !PositionableStream methodsFor:'positioning'!
   285 !PositionableStream methodsFor:'positioning'!
   257 
   286 
   258 backStep
   287 backStep
   647         'abc' readStream nextAvailable:2; nextAvailable:2.
   676         'abc' readStream nextAvailable:2; nextAvailable:2.
   648         'abc' readStream nextAvailable:3; nextAvailable:3.
   677         'abc' readStream nextAvailable:3; nextAvailable:3.
   649     "
   678     "
   650 !
   679 !
   651 
   680 
       
   681 nextAvailable:count into:aCollection startingAt:initialIndex
       
   682     "return the next count objects from the stream. If the end is
       
   683      reached before, only that many objects are copyied into the
       
   684      collection.
       
   685      Returns the number of objects that have been actually read."
       
   686 
       
   687     | max |
       
   688 
       
   689     collection notNil ifTrue:[
       
   690         max := (readLimit - position) min: count.
       
   691         aCollection
       
   692             replaceFrom: initialIndex
       
   693             to: initialIndex+max-1
       
   694             with: collection
       
   695             startingAt: position+1.
       
   696         position := position + max.
       
   697         ^ max.
       
   698     ].
       
   699 
       
   700     ^ super nextAvailable:count into:aCollection startingAt:initialIndex.
       
   701 
       
   702     "
       
   703      |s n buffer|
       
   704 
       
   705      buffer := ByteArray new:10.
       
   706 
       
   707      s := ReadStream on:#[1 2 3 4 5 6 7 8 9].
       
   708      s next:3.
       
   709      n := s nextBytes:9 into:buffer startingAt:1.
       
   710      Transcript showCR:('n = %1; buffer = <%2>' bindWith:n with:buffer)
       
   711     "
       
   712 !
       
   713 
       
   714 nextBytes:numBytes into:aCollection startingAt:initialIndex
       
   715     "return the next numBytes from the stream. If the end is
       
   716      reached before, only that many bytes are copyied into the
       
   717      collection.
       
   718      Returns the number of bytes that have been actually read.
       
   719      The receiver must support reading of binary bytes.
       
   720 
       
   721      Notice: this method is provided here for protocol completeness
       
   722              with externalStreams - it is normally not used with other
       
   723              streams."
       
   724 
       
   725     |max|
       
   726 
       
   727     (collection isByteCollection
       
   728      and:[aCollection isByteCollection]) ifTrue:[
       
   729         "do it the fast way"
       
   730         max := (readLimit - position) min: numBytes.
       
   731         aCollection
       
   732             replaceBytesFrom:initialIndex 
       
   733             to:(initialIndex + max - 1)
       
   734             with:collection 
       
   735             startingAt:position+1.
       
   736         position := position + max.
       
   737         ^ max
       
   738     ].
       
   739     "do it the hard way"
       
   740     ^ super nextBytes:numBytes into:aCollection startingAt:initialIndex
       
   741 
       
   742     "
       
   743      |s n buffer|
       
   744 
       
   745      buffer := ByteArray new:10.
       
   746 
       
   747      s := ReadStream on:#[1 2 3 4 5 6 7 8 9].
       
   748      s next:3.
       
   749      n := s nextBytes:9 into:buffer startingAt:1.
       
   750      Transcript showCR:('n = %1; buffer = <%2>' bindWith:n with:buffer)
       
   751     "
       
   752 !
       
   753 
   652 upToAll:aCollection
   754 upToAll:aCollection
   653     "read until a subcollection consisisting of the elements in aCollection is encountered.
   755     "read until a subcollection consisisting of the elements in aCollection is encountered.
   654      Return everything read excluding the elements in aCollection.
   756      Return everything read excluding the elements in aCollection.
   655      The position is left before the collection; i.e. the next
   757      The position is left before the collection; i.e. the next
   656      read operations will return those elements.
   758      read operations will return those elements.
   664 
   766 
   665      Note: this behavior is inconsistent with the other upTo.. methods,
   767      Note: this behavior is inconsistent with the other upTo.. methods,
   666            which position after the found item. We implement the method
   768            which position after the found item. We implement the method
   667            this way for the sake of ST80-compatibility."
   769            this way for the sake of ST80-compatibility."
   668 
   770 
   669     "/ in the future, this wil have the Squeak semantics, which is upToAllExcluding:
   771     "/ in the future, this will have the Squeak semantics, which is upToAllExcluding:
   670 
   772 
   671     ^ self upToAll_positionBefore:aCollection
   773     ^ self upToAll_positionBefore:aCollection
   672 
   774 
   673     "
   775     "
   674      |s|
   776      |s|
   778 ! !
   880 ! !
   779 
   881 
   780 !PositionableStream class methodsFor:'documentation'!
   882 !PositionableStream class methodsFor:'documentation'!
   781 
   883 
   782 version
   884 version
   783     ^ '$Header: /cvs/stx/stx/libbasic/PositionableStream.st,v 1.170 2015-03-12 17:14:00 stefan Exp $'
   885     ^ '$Header: /cvs/stx/stx/libbasic/PositionableStream.st,v 1.171 2015-03-12 19:24:30 stefan Exp $'
   784 !
   886 !
   785 
   887 
   786 version_CVS
   888 version_CVS
   787     ^ '$Header: /cvs/stx/stx/libbasic/PositionableStream.st,v 1.170 2015-03-12 17:14:00 stefan Exp $'
   889     ^ '$Header: /cvs/stx/stx/libbasic/PositionableStream.st,v 1.171 2015-03-12 19:24:30 stefan Exp $'
   788 ! !
   890 ! !
   789 
   891 
   790 
   892 
   791 PositionableStream initialize!
   893 PositionableStream initialize!