#FEATURE by stefan expecco_2_9_0 expecco_2_9_0_a expecco_2_9_0_win75_lx36 expecco_2_9_1
authorStefan Vogel <sv@exept.de>
Thu, 19 May 2016 16:42:03 +0200
changeset 19890 484307b07a7b
parent 19889 9d79440bb826
child 19891 5805c489e14f
#FEATURE by stefan class: PositionableStream added: #copy:into: #copy:into:bufferSize: -- required for new bridge
PositionableStream.st
--- 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.