eXept Software AG Logo

Smalltalk/X Webserver

Documentation of class 'SplittingWriteStream':

Home

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

Class: SplittingWriteStream


Inheritance:

   Object
   |
   +--Stream
      |
      +--SplittingWriteStream

Package:
stx:libbasic2
Category:
Streams-Misc
Version:
rev: 1.20 date: 2021/04/12 07:43:36
user: stefan
file: SplittingWriteStream.st directory: libbasic2
module: stx stc-classLibrary: libbasic2

Description:


A stream duplicator - everything written onto a splittingWriteStream
is written to two real streams.
Useful, if you have to send something to two files/destinations
simultaneously, and do not want to (or cannot) buffer it.

Especially useful, to generate a checksum, 
while sending something to a file 
(if one of the output streams is a checksummer).

Also, to duplicate some output to a Transcript.
The second stream can be closed and nilled, if no longer needed.



[instance variables:]
    outStream1      <Stream>       actual output streams
    outStream2      <Stream>

[class variables:]

copyright

COPYRIGHT (c) 1999 by eXept Software AG 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  on: stream1 and: stream2


Instance protocol:

accessing
o  outStream1
return the value of the instance variable 'outStream1' (automatically generated)

o  outStream1: aStream
set the value of the instance variable 'outStream1' (automatically generated)

o  outStream2
return the value of the instance variable 'outStream2' (automatically generated)

o  outStream2: aStreamOrNil
set the value of the instance variable 'outStream2' (automatically generated)

private access
o  setOutStream1: stream1 outStream2: stream2

redirect messages
o  doesNotUnderstand: aMessage
if my superclass implements the message, it can be forwarded to both streams.

writing
o  clear

o  close
(comment from inherited method)
close the stream - nothing done here.
Added for compatibility with external streams.

o  contents
(comment from inherited method)
return the entire contents of the stream.
For a readStream, that is the rest (i.e. upToEnd),
for a writeStream, that is the collected data. As we do not know here,
what we are, this is the responsibility of a subclass...

o  cr
(comment from inherited method)
append a carriage-return to the stream.
This is only allowed, if the receiver supports writing.

o  endEntry
(comment from inherited method)
for compatibility with Transcript

o  flush
write out all buffered data

o  nextPut: anObject
append something to all of my out streams.
Answer anObject

o  nextPutAll: aCollection
append all elements from aCollection into the underlying streams.
Answer the receiver

o  nextPutAll: aCollection startingAt: start to: stop
append the elements from first index to last index
of the argument, aCollection onto the receiver (i.e. both outstreams)

o  nextPutAllUnicode: aString
(comment from inherited method)
normal streams can not handle multi-byte characters, so convert them to utf8

o  show: anObject
(comment from inherited method)
append a printed representation of the argument to the stream.
This makes streams somewhat compatible to TextCollectors and
allows you to say:
Smalltalk at:#Transcript put:Stdout
or to use #show:/#showCR: with internal or external streams.


Examples:


writes to two files simultaneously:
  |s1 s2 splitter|

  s1 := '/tmp/foo1' asFilename writeStream.
  s2 := '/tmp/foo2' asFilename writeStream.
  splitter := SplittingWriteStream on:s1 and:s2.
  splitter nextPutAll:'hello world'.
  splitter close.
generates a hash on the fly:
  |s1 s2 splitter hash|

  s1 := '/tmp/foo1' asFilename writeStream.
  s2 := SHA1Stream new.
  splitter := SplittingWriteStream on:s1 and:s2.
  splitter nextPutAll:'hello world'.
  splitter close.
  hash := s2 hashValue. 
  self assert:(hash = (SHA1Stream hashValueOf:'hello world'))
make the Transcript write its lines to a logging file:
  |originalEntryStream fileStream splitter hash|

  originalEntryStream := Transcript entryStream.
  fileStream := 'transcript.log' asFilename writeStream.
  splitter := SplittingWriteStream on:fileStream and:originalEntryStream.
  [
      Transcript entryStream:splitter.
      1 to:10 do:[:i | Transcript nextPutLine:('line%1' bindWith:i)].
  ] ensure:[
      Transcript entryStream:originalEntryStream.
      fileStream close.
  ].


ST/X 7.7.0.0; WebServer 1.702 at 20f6060372b9.unknown:8081; Mon, 30 Dec 2024 17:56:54 GMT