#FEATURE by cg
authorClaus Gittinger <cg@exept.de>
Tue, 20 Feb 2018 01:07:35 +0100
changeset 22553 fb78e910d191
parent 22552 b14b3b47197e
child 22554 04becee61780
#FEATURE by cg class: WriteStream added: #readStream
WriteStream.st
--- a/WriteStream.st	Mon Feb 19 17:29:54 2018 +0100
+++ b/WriteStream.st	Tue Feb 20 01:07:35 2018 +0100
@@ -1,3 +1,5 @@
+"{ Encoding: utf8 }"
+
 "
  COPYRIGHT (c) 1989 by Claus Gittinger
 	      All Rights Reserved
@@ -179,6 +181,30 @@
     "
 !
 
+readStream
+    "return a readstream on the current contents;
+     that is: if more is written to the stream in the meantime,
+     the readStream will NOT show it.
+     To avoid copying the contents, a readStream with a readLimit
+     is created. In the hope, that noone repositions and rewrites the
+     underlying writeStream.
+     If that leads to problems, you should use contents readStream."
+
+    ^ ReadStream on:collection from:1 to:self position.
+
+    "
+     |w r|
+
+     w := WriteStream on:(String new).
+     w nextPutAll:'12345'.
+     r := w readStream.
+     w nextPutAll:'abc'.
+     w nextPutAll:(r upToEnd).
+     w nextPutAll:'def'.
+     w contents.
+    "
+!
+
 reset
     "reset the stream; write anew.
      See the comment in WriteStream>>contents"