Stream.st
changeset 23892 99bbfe4d920f
parent 23738 a71468439dc7
child 23893 0273fd434361
equal deleted inserted replaced
23891:4b1801afc6ad 23892:99bbfe4d920f
   746     bufferSpecies == ByteArray ifTrue:[
   746     bufferSpecies == ByteArray ifTrue:[
   747         "an ExternalBytes buffer is faster when writing to a windows ExternalStream"
   747         "an ExternalBytes buffer is faster when writing to a windows ExternalStream"
   748         buffer:= ExternalBytes unprotectedNew:bufferSize.
   748         buffer:= ExternalBytes unprotectedNew:bufferSize.
   749         freeBuffer := true.
   749         freeBuffer := true.
   750     ] ifFalse:[
   750     ] ifFalse:[
   751         buffer := self contentsSpecies new:bufferSize.
   751         buffer := bufferSpecies new:bufferSize.
   752         freeBuffer := false.
   752         freeBuffer := false.
   753     ].
   753     ].
   754 
   754 
   755     "read loop: read required bytes"
   755     "read loop: read required bytes"
   756     [
   756     [
   781             ] whileTrue.
   781             ] whileTrue.
   782             countWritten := countWritten + writeCount.
   782             countWritten := countWritten + writeCount.
   783         ].
   783         ].
   784         "Note: atEnd will block if reading from an empty pipe or socket.
   784         "Note: atEnd will block if reading from an empty pipe or socket.
   785          avoid atEnd if possible, because it reads a single byte."
   785          avoid atEnd if possible, because it reads a single byte."
   786         bytesLeft ~~ 0 or:[self atEnd not]
   786         (bytesLeft == 0) or:[readCount == 0 and:[self atEnd]]
   787     ] whileTrue.
   787     ] whileFalse.
   788 
   788 
   789     freeBuffer ifTrue:[ buffer free ].
   789     freeBuffer ifTrue:[ buffer free ].
   790     ^ countWritten.
   790     ^ countWritten.
       
   791 
       
   792     "Modified: / 13-03-2019 / 12:00:46 / Claus Gittinger"
   791 !
   793 !
   792 
   794 
   793 copyToEndFrom:inStream
   795 copyToEndFrom:inStream
   794     "read from inStream, and write all data up to the end to the receiver.
   796     "read from inStream, and write all data up to the end to the receiver.
   795      Return the number of bytes which have been transferred.
   797      Return the number of bytes which have been transferred.
   837     bufferSpecies == ByteArray ifTrue:[
   839     bufferSpecies == ByteArray ifTrue:[
   838         "an ExternalBytes buffer is faster when writing to a windows ExternalStream"
   840         "an ExternalBytes buffer is faster when writing to a windows ExternalStream"
   839         buffer:= ExternalBytes unprotectedNew:bufferSize.
   841         buffer:= ExternalBytes unprotectedNew:bufferSize.
   840         freeBuffer := true.
   842         freeBuffer := true.
   841     ] ifFalse:[
   843     ] ifFalse:[
   842         buffer := self contentsSpecies new:bufferSize.
   844         buffer := bufferSpecies new:bufferSize.
   843         freeBuffer := false.
   845         freeBuffer := false.
   844     ].
   846     ].
   845 
   847 
   846     "read loop: read until end of stream"
   848     "read loop: read until end of stream"
   847     [
   849     [
   884       ('/tmp/mist' asFilename readStream binary; yourself) copyToEndInto:#[] writeStream
   886       ('/tmp/mist' asFilename readStream binary; yourself) copyToEndInto:#[] writeStream
   885       #[1 2 3 4 5 6 7] readStream copyToEndInto:'/tmp/mist' asFilename writeStream.
   887       #[1 2 3 4 5 6 7] readStream copyToEndInto:'/tmp/mist' asFilename writeStream.
   886       #(1 2 3 a 'b' 6.4 true) readStream next; copyToEndInto:#() writeStream inspect.
   888       #(1 2 3 a 'b' 6.4 true) readStream next; copyToEndInto:#() writeStream inspect.
   887 
   889 
   888     "
   890     "
       
   891 
       
   892     "Modified: / 13-03-2019 / 11:54:17 / Claus Gittinger"
   889 ! !
   893 ! !
   890 
   894 
   891 !Stream methodsFor:'non homogenous reading'!
   895 !Stream methodsFor:'non homogenous reading'!
   892 
   896 
   893 nextAvailableBytes:numBytes into:aCollection startingAt:initialIndex
   897 nextAvailableBytes:numBytes into:aCollection startingAt:initialIndex