eXept Software AG Logo

Smalltalk/X Webserver

Documentation of class 'FilteringStream':

Home

Documentation
www.exept.de
Everywhere
for:
[back]

Class: FilteringStream


Inheritance:

   Object
   |
   +--Stream
      |
      +--PeekableStream
         |
         +--FilteringStream
            |
            +--FilteringLineStream
            |
            +--LineNumberReadStream

Package:
stx:libbasic2
Category:
Streams-Misc
Version:
rev: 1.32 date: 2024/02/09 12:53:14
user: cg
file: FilteringStream.st directory: libbasic2
module: stx stc-classLibrary: libbasic2

Description:


A FilteringStream can be connected to some input
(from which elements are read via the ReadStream protocol),
and/or to some output (to which elements are written via
the WriteStream protocol.

The FilteringStream itself performs filtering/processing
on the elements as they arrive, optionally suppressing
elements.

A FilteringStream can be operated in pull-mode, by asking
it for the next element; it will then ask its inputStream for
and element, process it and return it.

Or, in pushMode, by having someone else writing elements via
nextPut:; it will then process the element, and send it to its
output stream.

Mixing modes does not make sense, since if pulled, data will not
be written to the outputStream (unless the puller does it).

The connected streams need not be real streams; anything which
responds to the basic Stream protocol can be connected
(a Transcript, a RandomNumber generator or even a Plug will do as well).

Similar, but not quite the same as SelectingReadStream / CollectingReadStream.

[instance variables:]
    inputStream     <Stream>  the stream from which elements are read
    outputStream    <Stream>  the stream to which elements are written
    filter          <Block>   the filter block;
    unbound         <Boolean> if true, the stream is unbound.

copyright

COPYRIGHT (c) 1996 by Claus Gittinger All Rights Reserved This software is furnished under a license and may be used only in accordance with the terms of that license and with the inclusion of the above copyright notice. This software may not be provided or otherwise made available to, or used by, any other person. No title to or ownership of the software is hereby transferred.

Class protocol:

instance creation
o  new
create and return a new filteringStream.
The resulting stream must be connected to some other stream,
before being used

o  on: aStringOrStream
create and return a new filteringStream, which reads from
something (which must be convertable to a stream)

o  readingFrom: aReadStream
create and return a new filteringStream, which reads from
another stream

o  readingFrom: aReadStream writingTo: aWriteStream
create and return a new filteringStream, which reads from
aReadStream and writes to aWriteStream.

o  writingTo: aWriteStream
create and return a new filteringStream, which writes to
another stream


Instance protocol:

access - pull-reading
o  contents
(comment from inherited method)
return a collection of the elements up-to the end.
Return an empty collection, if the stream is already at the end.

o  filterUpToEnd
pull input from inputStream up to the end,
push it filtered into the outputStream.

o  next
pull input from inputStream and
push it filtered into the outputStream

o  peek
peek ahead for the next character

o  peekOrNil
peek ahead for the next character, or return nil

access - push-writing
o  nextPut: something
push something through the filter.
Answer the argument.

accessing
o  filter
return the filter

o  filter: aBlock
set the filter; a block which receives elements and can
return nil (so the element is ignored), or another element (for translation)

o  inputStream
return the inputStream

o  inputStream: something
set the inputStream

o  outputStream
return the outputStream

o  outputStream: something
set the outputStream

misc
o  clearEOF

o  close
when I am closed, close my input - if any

obsolete positioning
o  position0Based
return the receiver streams position

** This is an obsolete interface - do not use it (it may vanish in future versions) **

o  position1Based
return the receiver streams position

** This is an obsolete interface - do not use it (it may vanish in future versions) **

queries
o  atEnd
return true, if the receiver stream is at the end

o  contentsSpecies
return the kind of collection I should return when asked
for multiple elements.

o  isEmpty
(comment from inherited method)
return true, if the contents of the stream is empty

o  isEncodedStream
(comment from inherited method)
true, iff this is an encoder/decoder stream

o  isReadable
(comment from inherited method)
return true, if reading is supported by the receiver.
This has to be redefined in concrete subclasses.

o  isWritable
return true, if writing is supported by the receiver.

o  position
return the receiver streams position

o  readStream

o  size
not always correct, but probably better than 0.
Better use #isEmpty.

Usage example(s):

self error:'size of input is unknown (due to filtering)'


Examples:


pushing the contents of a stream onto another stream (here, the Transcript) without a need to read everything into a buffer or to reinvent the read-loop: (notice, a FilteringLineStream does this with less overhead, due to the byte-wise reading done here)
  |in pusher|

  in := 'Make.proto' asFilename readStream.
  pusher := FilteringStream readingFrom:in writingTo:Transcript.
  pusher filterUpToEnd
filter random numbers in [0.5 .. 0.6]:
  |in filter|

  in := Random new.

  filter := FilteringStream readingFrom:in.
  filter filter:[:num | ((num >= 0.5) and:[num <= 0.6]) ifTrue:[num] ifFalse:[nil]].

  20 timesRepeat:[
      Transcript showCR:(filter next printString).
  ]
compute the values times two
  |filter|

  filter := FilteringStream 
              readingFrom:#(1 2 3 4 5 6 7 8 9 10) readStream
              writingTo:#() writeStream.
  filter filter:[:num | num * 2].
  filter contents inspect.
filtering prime numbers (that's just a demo - not really useful, because we will end in a recursion error if many filters are stacked):
  |num generator primeFilter addFilter|

  num := 1.
  generator := Plug new.
  generator respondTo:#next
                 with:[num := num + 1. num].
  generator respondTo:#atEnd
                 with:[false].

  addFilter := [:prime | |newFilter|
      newFilter := FilteringStream basicNew.
      newFilter filter:[:num | (num \\ prime) == 0 ifTrue:[
                                  nil
                               ] ifFalse:[
                                  num
                               ]
                       ].
      newFilter inputStream:primeFilter.
      primeFilter := newFilter
  ].

  addFilter value:2.
  primeFilter inputStream:generator.

  1000 timesRepeat:[
      |nextPrime|

      nextPrime := primeFilter next.
      addFilter value:nextPrime.
      Transcript showCR:nextPrime.
  ]


ST/X 7.7.0.0; WebServer 1.702 at 20f6060372b9.unknown:8081; Mon, 18 Nov 2024 04:35:48 GMT