#FEATURE by stefan
class: PositionableStream
added:
#copy:into:
#copy:into:bufferSize:
-- required for new bridge
--- a/PositionableStream.st Thu May 19 16:39:01 2016 +0200
+++ b/PositionableStream.st Thu May 19 16:42:03 2016 +0200
@@ -259,6 +259,57 @@
!PositionableStream methodsFor:'misc functions'!
+copy:numberOfBytes into:aWriteStream
+ "read from the receiver, and write numberOfBytes data to another aWriteStream.
+ Return the number of bytes which have been transferred.
+ Redefined here to avoid intermediate buffers/garbage."
+
+ |endPosition cnt|
+
+ collection notNil ifTrue:[
+ endPosition := (position+numberOfBytes) min:readLimit.
+ aWriteStream nextPutAll:collection startingAt:position+1 to:endPosition.
+ cnt := endPosition - position.
+ position := endPosition.
+ ^ cnt.
+ ].
+
+ ^ super copy:numberOfBytes into:aWriteStream.
+
+
+ "
+ 'hello world' readStream copy:5 into:'/tmp/mist' asFilename writeStream.
+ 'hello world' readStream
+ copy:5 into:Transcript;
+ copy:20 into:Transcript.
+ 'hello world' readStream copy:5 into:'' writeStream inspect.
+ #[1 2 3 4 5 6 7] readStream copy:2 into:'/tmp/mist' asFilename writeStream binary.
+ #[1 2 3 4 5 6 7] readStream copy:3 into:#[] writeStream.
+ "
+
+ "
+ |rs ws cnt|
+
+ ws := #() writeStream.
+ rs := #( 1 2 3 4 a nil true) readWriteStream.
+ rs next.
+ cnt := rs copyToEndInto:ws bufferSize:0.
+ Transcript show:cnt; show:' '; showCR:ws contents.
+ "
+!
+
+copy:numberOfBytes into:aWriteStream bufferSize:bufferSize
+ "read from the receiver, and write numberOfBytes data to another aWriteStream.
+ Return the number of bytes which have been transferred.
+ Redefined here to avoid intermediate buffers/garbage.
+ bufferSize does not matter here."
+
+ collection notNil ifTrue:[
+ ^ self copy:numberOfBytes into:aWriteStream.
+ ].
+ ^ super copy:numberOfBytes into:aWriteStream bufferSize:bufferSize.
+!
+
copyToEndInto:aWriteStream
"read from the receiver, and write all data up to the end to another stream.
Return the number of bytes which have been transferred.