--- a/Stream.st Thu Mar 03 13:10:29 2005 +0100
+++ b/Stream.st Thu Mar 03 13:15:53 2005 +0100
@@ -584,6 +584,83 @@
^ false "/ never have to wait
! !
+!Stream methodsFor:'misc functions'!
+
+copyToEndInto:outStream
+ "read from the receiver, and write all data up to the end to another stream.
+ Return the number of bytes which have been transferred"
+
+ ^ self copyToEndInto:outStream bufferSize:(128*1024)
+
+"/ data rate to USB2.0 stick (Win32):
+"/ 120 KB/s 8Kb SingleBuffer
+"/ 741 KB/s 64Kb SingleBuffer
+"/ 1345 KB/s 128Kb SingleBuffer
+"/ 2087 KB/s 256Kb SingleBuffer
+"/ 3573 KB/s 1024Kb SingleBuffer
+
+"/|t retVal|
+"/
+"/t := Time millisecondsToRun:[
+"/ retVal := self copyToEndInto:outStream bufferSize:(64*1024).
+"/].
+"/
+"/Transcript showCR:('%1 KB copied in %2s (%3 KB/s)'
+"/ bindWith:((retVal/1024)asFixedPoint:2)
+"/ with:((t/1000)asFixedPoint:2)
+"/ with:((retVal/1024/(t/1000))asFixedPoint:2)).
+"/^ retVal.
+"/
+!
+
+copyToEndInto:outStream bufferSize:bufferSize
+ "read from the receiver, and write all data up to the end to another stream.
+ Return the number of bytes which have been transferred"
+
+ |bufferSpecies buffer bytesWritten readCount writeCount count freeBuffer|
+
+ bytesWritten := 0.
+ bufferSpecies := self contentsSpecies.
+ bufferSpecies == ByteArray ifTrue:[
+ buffer:= ExternalBytes unprotectedNew:bufferSize.
+ freeBuffer := true.
+ ] ifFalse:[
+ buffer := self contentsSpecies new:bufferSize.
+ freeBuffer := false.
+ ].
+
+ [self atEnd] whileFalse:[
+ readCount := self nextAvailableBytes:bufferSize into:buffer startingAt:1.
+ readCount > 0 ifTrue:[
+ writeCount := 0.
+ [
+ count := outStream nextPutBytes:readCount-writeCount
+ from:buffer
+ startingAt:writeCount+1.
+ bytesWritten := bytesWritten + count.
+ writeCount := writeCount + count.
+ writeCount < readCount ifTrue:[
+ outStream writeWait.
+ true.
+ ] ifFalse:[
+ false
+ ].
+ ] whileTrue.
+ ].
+ ].
+ freeBuffer ifTrue:[ buffer free ].
+ ^ bytesWritten
+
+ "
+ 'hello world' readStream copyToEndInto:'/tmp/mist' asFilename writeStream.
+ 'hello world' readStream copyToEndInto:#[] writeStream.
+ ('/tmp/mist' asFilename readStream binary; yourself) copyToEndInto:#[] writeStream
+ #[1 2 3 4 5 6 7] readStream copyToEndInto:'/tmp/mist' asFilename writeStream.
+ #[1 2 3 4 5 6 7] readStream copyToEndInto:'' writeStream.
+
+ "
+! !
+
!Stream methodsFor:'non homogenous reading'!
nextAvailableBytes:numBytes into:aCollection startingAt:initialIndex
@@ -1642,183 +1719,6 @@
!Stream methodsFor:'reading'!
-copyToEndInto:outStream
- "read from the receiver, and write all data up to the end to another stream.
- Return the number of bytes which have been transferred"
-
- "/ data rate to USB2.0 stick (Win32):
- "/ 120 KB/s 8Kb SingleBuffer
- "/ 741 KB/s 64Kb SingleBuffer
- "/ 1345 KB/s 128Kb SingleBuffer
- "/ 2087 KB/s 256Kb SingleBuffer
- "/ 3573 KB/s 1024Kb SingleBuffer
- "/
- "/ 115 KB/s 8Kb DoubleBuffer
- "/ 770 KB/s 64Kb DoubleBuffer
- "/ 1325 KB/s 128Kb DoubleBuffer
- "/ 2080 KB/s 256Kb DoubleBuffer
- "/ 3536 KB/s 1024Kb DoubleBuffer
-
-|t retVal|
-
-t := Time millisecondsToRun:[
- retVal := self copyToEndInto:outStream bufferSize:(1024*1024).
-].
-
-Transcript showCR:('%1 KB copied in %2s (%3 KB/s)'
- bindWith:((retVal/1024)asFixedPoint:2)
- with:((t/1000)asFixedPoint:2)
- with:((retVal/1024/(t/1000))asFixedPoint:2)).
-^ retVal.
-
- ^ self copyToEndInto:outStream bufferSize:(64*1024)
-!
-
-copyToEndInto:outStream bufferSize:bufferSize
- "read from the receiver, and write all data up to the end to another stream.
- Return the number of bytes which have been transferred"
-
- |bufferSpecies buffer bytesWritten readCount writeCount count freeBuffer|
-
- OperatingSystem isMSWINDOWSlike ifTrue:[
- ^ self doubleBufferCopyToEndInto:outStream bufferSize:bufferSize.
- ].
-
- bytesWritten := 0.
- bufferSpecies := self contentsSpecies.
- bufferSpecies == ByteArray ifTrue:[
- buffer:= ExternalBytes unprotectedNew:bufferSize.
- freeBuffer := true.
- ] ifFalse:[
- buffer := self contentsSpecies new:bufferSize.
- freeBuffer := false.
- ].
-
- [self atEnd] whileFalse:[
- readCount := self nextAvailableBytes:bufferSize into:buffer startingAt:1.
- readCount > 0 ifTrue:[
- writeCount := 0.
- [
- count := outStream nextPutBytes:readCount-writeCount
- from:buffer
- startingAt:writeCount+1.
- bytesWritten := bytesWritten + count.
- writeCount := writeCount + count.
- writeCount < readCount ifTrue:[
- outStream writeWait.
- true.
- ] ifFalse:[
- false
- ].
- ] whileTrue.
- ].
- ].
- freeBuffer ifTrue:[ buffer free ].
- ^ bytesWritten
-
- "
- 'hello world' readStream copyToEndInto:'/tmp/mist' asFilename writeStream.
- 'hello world' readStream copyToEndInto:#[] writeStream.
- ('/tmp/mist' asFilename readStream binary; yourself) copyToEndInto:#[] writeStream
- #[1 2 3 4 5 6 7] readStream copyToEndInto:'/tmp/mist' asFilename writeStream.
- #[1 2 3 4 5 6 7] readStream copyToEndInto:'' writeStream.
-
- "
-!
-
-doubleBufferCopyToEndInto:outStream bufferSize:bufferSize
- "read from the receiver, and write all data up to the end to another stream.
- Return the number of bytes which have been transferred.
- This uses a double-buffer algorithm, which should work twice as fast on
- systems which do no write buffering but do support multiple threads (i.e. Win32).
- Unix systems (which use a write buffer) do not need that complication for speed."
-
- |bufferSpecies reader
- readBufferQueue writeBufferQueue buffer1 buffer2 bytesWritten freeBuffer|
-
- bytesWritten := 0.
-
- readBufferQueue := SharedQueue new.
- writeBufferQueue := SharedQueue new.
-
- bufferSpecies := self contentsSpecies.
- bufferSpecies == ByteArray ifTrue:[
- buffer1:= ExternalBytes unprotectedNew:bufferSize.
- buffer2:= ExternalBytes unprotectedNew:bufferSize.
- freeBuffer := true.
- ] ifFalse:[
- buffer1:= bufferSpecies new:bufferSize.
- buffer2:= bufferSpecies new:bufferSize.
- freeBuffer := false.
- ].
- readBufferQueue nextPut:buffer1.
- readBufferQueue nextPut:buffer2.
-
- reader :=
- [
- |readBuffer readCount|
-
- [self atEnd] whileFalse:[
- readBuffer := readBufferQueue next.
- readCount := self nextAvailableBytes:(readBuffer size) into:readBuffer startingAt:1.
- readCount > 0 ifTrue:[
- writeBufferQueue nextPut:(readBuffer -> readCount)
- ] ifFalse:[
- readBufferQueue nextPut:readBuffer.
- self readWait.
- ].
- ].
- writeBufferQueue nextPut:nil
- ] fork.
-
- [
- |writeBufferAndCount writeBuffer readCount writeCount|
-
- [true] whileTrue:[
- writeBufferAndCount := writeBufferQueue next.
- writeBufferAndCount isNil ifTrue:[
- ^ bytesWritten
- ].
-
- writeBuffer := writeBufferAndCount key.
- readCount := writeBufferAndCount value.
-
- writeCount := 0.
- [
- |count|
-
- count := outStream nextPutBytes:readCount-writeCount
- from:writeBuffer
- startingAt:writeCount+1.
- bytesWritten := bytesWritten + count.
- writeCount := writeCount + count.
- writeCount < readCount ifTrue:[
- outStream writeWait.
- true.
- ] ifFalse:[
- false
- ].
- ] whileTrue.
- readBufferQueue nextPut:writeBuffer
- ].
- ] ensure:[
- reader terminate.
- freeBuffer ifTrue:[
- buffer1 free.
- buffer2 free
- ].
- ]
-
- "
- 'hello world' readStream copyToEndInto:'/tmp/mist' asFilename writeStream.
- 'hello world' readStream copyToEndInto:#[] writeStream.
- ('/tmp/mist' asFilename readStream binary; yourself) copyToEndInto:#[] writeStream
- #[1 2 3 4 5 6 7] readStream copyToEndInto:'/tmp/mist' asFilename writeStream.
- #[1 2 3 4 5 6 7] readStream copyToEndInto:'' writeStream.
-
- "
-!
-
next
"return the next element of the stream
- we do not know here how to do it, it must be redefined in subclass"
@@ -2931,7 +2831,7 @@
!Stream class methodsFor:'documentation'!
version
- ^ '$Header: /cvs/stx/stx/libbasic/Stream.st,v 1.138 2005-03-03 12:10:29 cg Exp $'
+ ^ '$Header: /cvs/stx/stx/libbasic/Stream.st,v 1.139 2005-03-03 12:15:41 cg Exp $'
! !
Stream initialize!