#FEATURE by exept
authorClaus Gittinger <cg@exept.de>
Mon, 11 Nov 2019 20:51:07 +0100
changeset 24897 737da1130d4c
parent 24896 421293afdb60
child 24898 77318c4b1205
#FEATURE by exept class: Stream class added: #streamContents:
Stream.st
--- a/Stream.st	Sun Nov 10 03:50:58 2019 +0100
+++ b/Stream.st	Mon Nov 11 20:51:07 2019 +0100
@@ -222,6 +222,29 @@
     ^ self == Stream
 ! !
 
+!Stream class methodsFor:'utilities'!
+
+streamContents:blockWithArg
+    "create a write-stream instance of the receiver-class,
+     evaluate blockWithArg, passing that stream,
+     extract and return the streams contents.
+     Similar to collection>>streamContents:, but usable if
+     you don't know the type of collection in advance 
+     (eg: especially for CharacterWriteStream)"
+
+    |stream|
+
+    stream := self new.
+    blockWithArg value:stream.
+    ^ stream contents
+
+    "
+     CharacterWriteStream streamContents:[:s |
+        s nextPutLine:'hello'.
+        s nextPutLine:'world'
+     ]     
+    "
+! !
 
 !Stream methodsFor:'Compatibility-Dolphin'!
 
@@ -250,7 +273,6 @@
     self nextPut:(Character nl)
 ! !
 
-
 !Stream methodsFor:'accessing'!
 
 contents