|
Class: SplittingWriteStream
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
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:]
copyrightCOPYRIGHT (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.
instance creation
-
on: stream1 and: stream2
-
accessing
-
outStream1
-
return the value of the instance variable 'outStream1' (automatically generated)
-
outStream1: aStream
-
set the value of the instance variable 'outStream1' (automatically generated)
-
outStream2
-
return the value of the instance variable 'outStream2' (automatically generated)
-
outStream2: aStreamOrNil
-
set the value of the instance variable 'outStream2' (automatically generated)
private access
-
setOutStream1: stream1 outStream2: stream2
-
redirect messages
-
doesNotUnderstand: aMessage
-
if my superclass implements the message, it can be forwarded to both streams.
writing
-
clear
-
-
close
-
(comment from inherited method)
close the stream - nothing done here.
Added for compatibility with external streams.
-
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...
-
cr
-
(comment from inherited method)
append a carriage-return to the stream.
This is only allowed, if the receiver supports writing.
-
endEntry
-
(comment from inherited method)
for compatibility with Transcript
-
flush
-
write out all buffered data
-
nextPut: anObject
-
append something to all of my out streams.
Answer anObject
-
nextPutAll: aCollection
-
append all elements from aCollection into the underlying streams.
Answer the receiver
-
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)
-
nextPutAllUnicode: aString
-
(comment from inherited method)
normal streams can not handle multi-byte characters, so convert them to utf8
-
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.
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.
].
|
|