FilteringStream.st
changeset 474 61cb199537b5
parent 472 33f423823933
child 477 6124ae485dbd
--- a/FilteringStream.st	Sat Jan 11 15:22:27 1997 +0100
+++ b/FilteringStream.st	Sat Jan 11 16:51:18 1997 +0100
@@ -12,10 +12,10 @@
 
 
 Stream subclass:#FilteringStream
-	instanceVariableNames:'inputStream outputStream filter unbound'
+	instanceVariableNames:'inputStream outputStream filter'
 	classVariableNames:''
 	poolDictionaries:''
-	category:'Streams'
+	category:'Streams-Misc'
 !
 
 !FilteringStream class methodsFor:'documentation'!
@@ -62,14 +62,26 @@
 
 examples
 "
+  pushing the contents of a stream onto another stream
+  (here, the Transcript) without a need to read everyting into a
+  buffer or to reinvent the read-loop:
+  (notice, a FilteringLineStream does this with less overhead)
+                                                                [exBegin]
+    |in pusher|
+
+    in := 'Makefile' asFilename readStream.
+    pusher := FilteringStream readingFrom:in writingTo:Transcript.
+    pusher filterUpToEnd
+                                                                [exEnd]
+
+
   filter random numbers:
                                                                 [exBegin]
     |in filter|
 
     in := Random new.
 
-    filter := FilteringLineStream basicNew.
-    filter inputStream:in.
+    filter := FilteringLineStream readingFrom:in.
     filter filter:[:num | ((num >= 0.5) and:[num <= 0.6]) ifTrue:[num] ifFalse:[nil]].
 
     20 timesRepeat:[
@@ -117,18 +129,63 @@
 
 ! !
 
+!FilteringStream class methodsFor:'instance creation'!
+
+new
+    "create and return a new filteringStream.
+     The resulting stream must be connected to some other stream,
+     before being used"
+
+    ^ self basicNew initialize.
+
+    "Created: 11.1.1997 / 15:31:30 / cg"
+    "Modified: 11.1.1997 / 15:33:13 / cg"
+!
+
+readingFrom:aReadStream
+    "create and return a new filteringStream, which reads from
+     another stream"
+
+    ^ self basicNew initialize inputStream:aReadStream.
+
+    "Created: 11.1.1997 / 15:32:15 / cg"
+!
+
+readingFrom:aReadStream writingTo:aWriteStream
+    "create and return a new filteringStream, which reads from
+     aReadStream and writes to aWriteStream."
+
+    |newStream|
+
+    newStream := self basicNew initialize.
+    newStream inputStream:aReadStream.
+    newStream outputStream:aWriteStream.
+    ^ newStream
+
+    "Created: 11.1.1997 / 15:32:28 / cg"
+!
+
+writingTo:aWriteStream
+    "create and return a new filteringStream, which writes to
+     another stream"
+
+    ^ self basicNew initialize outputStream:aWriteStream.
+
+    "Created: 11.1.1997 / 15:32:36 / cg"
+! !
+
 !FilteringStream methodsFor:'access - pull-reading'!
 
 filterUpToEnd
     "pull input from inputStream up to the end,
-     push it filtered into the outputStream"
+     push it filtered into the outputStream."
 
     [inputStream atEnd] whileFalse:[
         self nextPut:(inputStream next)
     ].
 
-    "Modified: 2.7.1996 / 21:02:49 / cg"
     "Created: 2.7.1996 / 21:06:42 / cg"
+    "Modified: 11.1.1997 / 16:08:35 / cg"
 !
 
 next
@@ -140,6 +197,10 @@
     [inputStream atEnd] whileFalse:[
         "/ get an element
         input := inputStream next.
+        filter isNil ifTrue:[
+            ^ input
+        ].
+
         "/ filter it - this may return nil, to eat it
         output := filter value:input.
         output notNil ifTrue:[
@@ -150,7 +211,7 @@
     ^ nil
 
     "Created: 2.7.1996 / 21:09:58 / cg"
-    "Modified: 10.1.1997 / 20:23:36 / cg"
+    "Modified: 11.1.1997 / 16:12:22 / cg"
 ! !
 
 !FilteringStream methodsFor:'access - push-writing'!
@@ -161,13 +222,16 @@
     |output|
 
     "/ filter it
-    output := filter value:something.
-    output notNil ifTrue:[
-        outputStream nextPut:output
+    filter isNil ifTrue:[
+        outputStream nextPut:something
+    ] ifFalse:[
+        output := filter value:something.
+        output notNil ifTrue:[
+            outputStream nextPut:output
+        ]
     ]
 
-    "Created: 2.7.1996 / 21:07:58 / cg"
-    "Modified: 10.1.1997 / 20:23:56 / cg"
+    "Modified: 11.1.1997 / 16:12:52 / cg"
 ! !
 
 !FilteringStream methodsFor:'accessing'!
@@ -224,24 +288,18 @@
 
     "Modified: 2.7.1996 / 21:03:52 / cg"
     "Created: 2.7.1996 / 21:06:42 / cg"
-!
+! !
 
-unbound
-    "return the unbound flag"
-
-    ^ unbound
+!FilteringStream methodsFor:'misc'!
 
-    "Created: 2.7.1996 / 21:25:40 / cg"
-    "Modified: 10.1.1997 / 20:21:53 / cg"
-!
+close
+    "when I am closed, close my input - if any"
 
-unbound:something
-    "set the unbound flag"
+    inputStream notNil ifTrue:[
+        inputStream close
+    ]
 
-    unbound := something.
-
-    "Created: 2.7.1996 / 21:25:40 / cg"
-    "Modified: 10.1.1997 / 20:21:59 / cg"
+    "Created: 11.1.1997 / 15:27:17 / cg"
 ! !
 
 !FilteringStream methodsFor:'queries'!
@@ -249,15 +307,22 @@
 atEnd
     "return true, if the receiver stream is at the end"
 
-    unbound ifTrue:[^ false].
     ^ inputStream atEnd
 
-    "Created: 2.7.1996 / 21:19:51 / cg"
-    "Modified: 10.1.1997 / 20:22:20 / cg"
+    "Modified: 11.1.1997 / 16:26:10 / cg"
+!
+
+contentsSpecies
+    "return the kind of collection I should return when asked
+     for multiple elements."
+
+    ^ inputStream contentsSpecies
+
+    "Created: 11.1.1997 / 16:23:22 / cg"
 ! !
 
 !FilteringStream class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic2/FilteringStream.st,v 1.2 1997-01-10 20:51:24 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic2/FilteringStream.st,v 1.3 1997-01-11 15:50:52 cg Exp $'
 ! !