#REFACTORING by stefan
authorStefan Vogel <sv@exept.de>
Wed, 13 Mar 2019 17:01:22 +0100
changeset 23895 d49a46365ed5
parent 23894 2dcb46224ee8
child 23896 c00000f683b6
#REFACTORING by stefan Refactor for common code. Better documentation class: PositionableStream removed: #copyToEndInto: #copyToEndInto:bufferSize: comment/format in: #copy:into:bufferSize: changed: #copy:into: category of: #copy:into: #copy:into:bufferSize:
PositionableStream.st
--- a/PositionableStream.st	Wed Mar 13 16:57:11 2019 +0100
+++ b/PositionableStream.st	Wed Mar 13 17:01:22 2019 +0100
@@ -1,3 +1,5 @@
+"{ Encoding: utf8 }"
+
 "
  COPYRIGHT (c) 1989 by Claus Gittinger
 	      All Rights Reserved
@@ -129,6 +131,7 @@
     ^ self == PositionableStream
 ! !
 
+
 !PositionableStream methodsFor:'Compatibility-Dolphin'!
 
 endChunk
@@ -255,107 +258,6 @@
     "Modified: / 04-06-2007 / 17:21:55 / cg"
 ! !
 
-!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.
-     Redefined here to avoid intermediate buffers/garbage."
-
-    |cnt|
-
-    collection notNil ifTrue:[
-        aWriteStream nextPutAll:collection startingAt:position+1 to:readLimit.
-        cnt := readLimit - position.
-        position := readLimit.
-        ^ cnt.
-    ].
-    ^ super copyToEndInto:aWriteStream.
-
-    "
-     |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.
-    "
-!
-
-copyToEndInto:aWriteStream 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.
-     Redefined here to avoid intermediate buffers/garbage.
-     bufferSize does not matter here."
-
-    collection notNil ifTrue:[
-        ^ self copyToEndInto:aWriteStream.
-    ].
-    ^ super copyToEndInto:aWriteStream bufferSize:bufferSize.
-
-    "
-     |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.
-    "
-! !
-
 !PositionableStream methodsFor:'non homogenous reading'!
 
 nextBytes:numBytes into:aCollection startingAt:initialIndex
@@ -1036,6 +938,69 @@
     "
 ! !
 
+!PositionableStream methodsFor:'stream-to-stream copy'!
+
+copy:numberOfElementsOrNil into:aWriteStream
+    "read from the receiver, and write numberOfElementsOrNil data to another aWriteStream.
+     If numberOfElementsOrNil is nil, copy until the end of myself.
+     Return the number of elements which have been transferred.
+     Redefined here to avoid intermediate buffers/garbage."
+
+    |endPosition cnt|
+
+    collection notNil ifTrue:[
+        numberOfElementsOrNil isNil ifTrue:[
+            endPosition := readLimit.   "/ read until end
+        ] ifFalse:[
+            endPosition := (position+numberOfElementsOrNil) min:readLimit.
+        ].
+        aWriteStream nextPutAll:collection startingAt:position+1 to:endPosition.
+        cnt := endPosition - position.
+        position := endPosition.
+        ^ cnt.
+    ].
+
+    ^ super copy:numberOfElementsOrNil 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.
+    "
+
+    "Modified (comment): / 13-03-2019 / 16:59:18 / Stefan Vogel"
+!
+
+copy:numberOfElementsOrNil into:aWriteStream bufferSize:bufferSize
+    "read from the receiver, and write numberOfElementsOrNil to another aWriteStream.
+     If numberOfElementsOrNil is nil, copy until the end of myself.
+     Return the number of elements which have been transferred.
+     Redefined here to avoid intermediate buffers/garbage
+     - bufferSize does not matter here."
+
+    collection notNil ifTrue:[
+        ^ self copy:numberOfElementsOrNil into:aWriteStream.
+    ].
+    ^ super copy:numberOfElementsOrNil into:aWriteStream bufferSize:bufferSize.
+
+    "Modified (comment): / 13-03-2019 / 17:00:11 / Stefan Vogel"
+! !
+
 !PositionableStream methodsFor:'testing'!
 
 atEnd