5154
|
1 |
"{ Encoding: utf8 }"
|
|
2 |
|
4275
|
3 |
"
|
|
4 |
COPYRIGHT (c) 1999 by eXept Software AG
|
|
5 |
All Rights Reserved
|
3532
|
6 |
|
4275
|
7 |
This software is furnished under a license and may be used
|
|
8 |
only in accordance with the terms of that license and with the
|
|
9 |
inclusion of the above copyright notice. This software may not
|
|
10 |
be provided or otherwise made available to, or used by, any
|
|
11 |
other person. No title to or ownership of the software is
|
|
12 |
hereby transferred.
|
|
13 |
"
|
912
|
14 |
"{ Package: 'stx:libbasic2' }"
|
|
15 |
|
3470
|
16 |
"{ NameSpace: Smalltalk }"
|
|
17 |
|
833
|
18 |
Stream subclass:#SplittingWriteStream
|
|
19 |
instanceVariableNames:'outStream1 outStream2'
|
|
20 |
classVariableNames:''
|
|
21 |
poolDictionaries:''
|
|
22 |
category:'Streams-Misc'
|
|
23 |
!
|
|
24 |
|
|
25 |
!SplittingWriteStream class methodsFor:'documentation'!
|
|
26 |
|
4275
|
27 |
copyright
|
|
28 |
"
|
|
29 |
COPYRIGHT (c) 1999 by eXept Software AG
|
|
30 |
All Rights Reserved
|
|
31 |
|
|
32 |
This software is furnished under a license and may be used
|
|
33 |
only in accordance with the terms of that license and with the
|
|
34 |
inclusion of the above copyright notice. This software may not
|
|
35 |
be provided or otherwise made available to, or used by, any
|
|
36 |
other person. No title to or ownership of the software is
|
|
37 |
hereby transferred.
|
|
38 |
|
|
39 |
"
|
|
40 |
!
|
|
41 |
|
833
|
42 |
documentation
|
|
43 |
"
|
|
44 |
A stream duplicator - everything written onto a splittingWriteStream
|
|
45 |
is written to two real streams.
|
|
46 |
Useful, if you have to send something to two files/destinations
|
|
47 |
simultaneously, and do not want to (or cannot) buffer it.
|
4833
|
48 |
|
833
|
49 |
Especially useful, to generate a checksum,
|
|
50 |
while sending something to a file
|
|
51 |
(if one of the output streams is a checksummer).
|
4833
|
52 |
|
4760
|
53 |
Also, to duplicate some output to a Transcript.
|
|
54 |
The second stream can be closed and nilled, if no longer needed.
|
|
55 |
|
833
|
56 |
[author:]
|
|
57 |
Claus Gittinger (cg@exept)
|
|
58 |
|
|
59 |
[see also:]
|
|
60 |
WriteStream
|
|
61 |
|
|
62 |
[instance variables:]
|
|
63 |
outStream1 <Stream> actual output streams
|
|
64 |
outStream2 <Stream>
|
|
65 |
|
|
66 |
[class variables:]
|
|
67 |
"
|
|
68 |
!
|
|
69 |
|
|
70 |
examples
|
|
71 |
"
|
4458
|
72 |
writes to two files simultaneously:
|
|
73 |
|
833
|
74 |
[exBegin]
|
|
75 |
|s1 s2 splitter|
|
|
76 |
|
|
77 |
s1 := '/tmp/foo1' asFilename writeStream.
|
|
78 |
s2 := '/tmp/foo2' asFilename writeStream.
|
|
79 |
splitter := SplittingWriteStream on:s1 and:s2.
|
|
80 |
splitter nextPutAll:'hello world'.
|
|
81 |
splitter close.
|
|
82 |
[exEnd]
|
4458
|
83 |
|
|
84 |
generates a hash on the fly:
|
|
85 |
[exBegin]
|
|
86 |
|s1 s2 splitter hash|
|
|
87 |
|
|
88 |
s1 := '/tmp/foo1' asFilename writeStream.
|
|
89 |
s2 := SHA1Stream new.
|
|
90 |
splitter := SplittingWriteStream on:s1 and:s2.
|
|
91 |
splitter nextPutAll:'hello world'.
|
|
92 |
splitter close.
|
|
93 |
hash := s2 hashValue.
|
|
94 |
self assert:(hash = (SHA1Stream hashValueOf:'hello world'))
|
|
95 |
[exEnd]
|
5230
|
96 |
|
|
97 |
make the Transcript write its lines to a logging file:
|
|
98 |
[exBegin]
|
|
99 |
|originalEntryStream fileStream splitter hash|
|
|
100 |
|
|
101 |
originalEntryStream := Transcript entryStream.
|
|
102 |
fileStream := 'transcript.log' asFilename writeStream.
|
5231
|
103 |
splitter := SplittingWriteStream on:fileStream and:originalEntryStream.
|
5230
|
104 |
[
|
|
105 |
Transcript entryStream:splitter.
|
|
106 |
1 to:10 do:[:i | Transcript nextPutLine:('line%1' bindWith:i)].
|
|
107 |
] ensure:[
|
|
108 |
Transcript entryStream:originalEntryStream.
|
|
109 |
fileStream close.
|
|
110 |
].
|
|
111 |
[exEnd]
|
833
|
112 |
"
|
|
113 |
! !
|
|
114 |
|
|
115 |
!SplittingWriteStream class methodsFor:'instance creation'!
|
|
116 |
|
|
117 |
on:stream1 and:stream2
|
|
118 |
^ self basicNew setOutStream1:stream1 outStream2:stream2
|
|
119 |
! !
|
|
120 |
|
912
|
121 |
!SplittingWriteStream methodsFor:'accessing'!
|
|
122 |
|
|
123 |
outStream1
|
|
124 |
"return the value of the instance variable 'outStream1' (automatically generated)"
|
|
125 |
|
2993
|
126 |
^ outStream1
|
|
127 |
!
|
912
|
128 |
|
4760
|
129 |
outStream1:aStream
|
912
|
130 |
"set the value of the instance variable 'outStream1' (automatically generated)"
|
|
131 |
|
4760
|
132 |
outStream1 := aStream.
|
|
133 |
|
|
134 |
"Modified (format): / 04-11-2018 / 21:53:51 / Claus Gittinger"
|
2993
|
135 |
!
|
912
|
136 |
|
|
137 |
outStream2
|
|
138 |
"return the value of the instance variable 'outStream2' (automatically generated)"
|
|
139 |
|
2993
|
140 |
^ outStream2
|
|
141 |
!
|
912
|
142 |
|
4760
|
143 |
outStream2:aStreamOrNil
|
912
|
144 |
"set the value of the instance variable 'outStream2' (automatically generated)"
|
|
145 |
|
4760
|
146 |
outStream2 := aStreamOrNil.
|
|
147 |
|
|
148 |
"Modified (format): / 04-11-2018 / 21:54:02 / Claus Gittinger"
|
2993
|
149 |
! !
|
912
|
150 |
|
833
|
151 |
!SplittingWriteStream methodsFor:'private access'!
|
|
152 |
|
|
153 |
setOutStream1:stream1 outStream2:stream2
|
|
154 |
outStream1 := stream1.
|
|
155 |
outStream2 := stream2.
|
|
156 |
|
|
157 |
! !
|
|
158 |
|
3532
|
159 |
!SplittingWriteStream methodsFor:'redirect messages'!
|
|
160 |
|
|
161 |
doesNotUnderstand:aMessage
|
|
162 |
"if my superclass implements the message, it can be forwarded to both streams."
|
|
163 |
|
4760
|
164 |
|retVal|
|
|
165 |
|
|
166 |
(outStream1 class canUnderstand:aMessage selector) ifTrue:[
|
|
167 |
retVal := aMessage sendTo:outStream1.
|
|
168 |
outStream2 notNil ifTrue:[
|
|
169 |
aMessage sendTo:outStream2.
|
|
170 |
].
|
|
171 |
^ retVal
|
3532
|
172 |
].
|
|
173 |
^ super doesNotUnderstand:aMessage.
|
|
174 |
|
|
175 |
"
|
|
176 |
|sp s1 s2|
|
|
177 |
|
|
178 |
s1 := TextStream on:''.
|
|
179 |
s2 := TextStream on:''.
|
|
180 |
|
|
181 |
sp := SplittingWriteStream on:s1 and:s2.
|
|
182 |
|
|
183 |
sp nextPutAllText:('ABC' allBold); closeRun.
|
|
184 |
s2 contents inspect.
|
|
185 |
"
|
4760
|
186 |
|
|
187 |
"Modified: / 04-11-2018 / 21:52:43 / Claus Gittinger"
|
3532
|
188 |
! !
|
|
189 |
|
833
|
190 |
!SplittingWriteStream methodsFor:'writing'!
|
|
191 |
|
2993
|
192 |
clear
|
|
193 |
outStream1 isStream ifFalse:[ outStream1 clear ].
|
4760
|
194 |
outStream2 notNil ifTrue:[
|
|
195 |
outStream2 isStream ifFalse:[ outStream2 clear ].
|
|
196 |
].
|
|
197 |
|
|
198 |
"Modified: / 04-11-2018 / 21:51:20 / Claus Gittinger"
|
2993
|
199 |
!
|
|
200 |
|
833
|
201 |
close
|
|
202 |
outStream1 close.
|
4760
|
203 |
outStream2 notNil ifTrue:[
|
|
204 |
outStream2 close.
|
|
205 |
]
|
833
|
206 |
|
4760
|
207 |
"Modified: / 04-11-2018 / 21:51:29 / Claus Gittinger"
|
833
|
208 |
!
|
|
209 |
|
2993
|
210 |
contents
|
|
211 |
^ outStream1 contents
|
|
212 |
!
|
|
213 |
|
4763
|
214 |
cr
|
|
215 |
outStream1 cr.
|
|
216 |
outStream2 notNil ifTrue:[
|
|
217 |
outStream2 cr.
|
|
218 |
]
|
|
219 |
|
|
220 |
"Created: / 12-11-2018 / 14:35:52 / Claus Gittinger"
|
|
221 |
!
|
|
222 |
|
833
|
223 |
endEntry
|
|
224 |
outStream1 endEntry.
|
4760
|
225 |
outStream2 notNil ifTrue:[
|
|
226 |
outStream2 endEntry.
|
|
227 |
]
|
833
|
228 |
|
4760
|
229 |
"Modified: / 04-11-2018 / 21:52:55 / Claus Gittinger"
|
833
|
230 |
!
|
|
231 |
|
|
232 |
flush
|
5462
|
233 |
"write out all buffered data"
|
|
234 |
|
833
|
235 |
outStream1 flush.
|
4760
|
236 |
outStream2 notNil ifTrue:[
|
|
237 |
outStream2 flush.
|
|
238 |
]
|
833
|
239 |
|
4760
|
240 |
"Modified: / 04-11-2018 / 21:53:09 / Claus Gittinger"
|
833
|
241 |
!
|
|
242 |
|
5185
|
243 |
nextPut:anObject
|
|
244 |
"append something to all of my out streams.
|
|
245 |
Answer anObject"
|
|
246 |
|
|
247 |
outStream1 nextPut:anObject.
|
4760
|
248 |
outStream2 notNil ifTrue:[
|
5185
|
249 |
outStream2 nextPut:anObject.
|
5164
|
250 |
].
|
5185
|
251 |
^ anObject
|
833
|
252 |
|
4760
|
253 |
"Modified: / 04-11-2018 / 21:53:16 / Claus Gittinger"
|
833
|
254 |
!
|
|
255 |
|
5154
|
256 |
nextPutAll:aCollection
|
|
257 |
"append all elements from aCollection into the underlying streams.
|
|
258 |
Answer the receiver"
|
|
259 |
|
|
260 |
outStream1 nextPutAll:aCollection.
|
4760
|
261 |
outStream2 notNil ifTrue:[
|
5154
|
262 |
outStream2 nextPutAll:aCollection.
|
4760
|
263 |
]
|
833
|
264 |
|
4760
|
265 |
"Modified: / 04-11-2018 / 21:53:25 / Claus Gittinger"
|
3470
|
266 |
!
|
|
267 |
|
|
268 |
nextPutAll:aCollection startingAt:start to:stop
|
4931
|
269 |
"append the elements from first index to last index
|
|
270 |
of the argument, aCollection onto the receiver (i.e. both outstreams)"
|
|
271 |
|
3470
|
272 |
outStream1 nextPutAll:aCollection startingAt:start to:stop.
|
4760
|
273 |
outStream2 notNil ifTrue:[
|
|
274 |
outStream2 nextPutAll:aCollection startingAt:start to:stop.
|
|
275 |
]
|
|
276 |
|
|
277 |
"Modified: / 04-11-2018 / 21:53:33 / Claus Gittinger"
|
4931
|
278 |
"Modified (comment): / 01-04-2019 / 17:12:42 / Claus Gittinger"
|
4518
|
279 |
!
|
|
280 |
|
|
281 |
nextPutAllUnicode:aString
|
|
282 |
outStream1 nextPutAllUnicode:aString.
|
4760
|
283 |
outStream2 notNil ifTrue:[
|
|
284 |
outStream2 nextPutAllUnicode:aString.
|
|
285 |
]
|
4518
|
286 |
|
|
287 |
"Created: / 21-09-2017 / 11:58:13 / cg"
|
4760
|
288 |
"Modified: / 04-11-2018 / 21:53:40 / Claus Gittinger"
|
4763
|
289 |
!
|
|
290 |
|
|
291 |
show:something
|
|
292 |
outStream1 show:something.
|
|
293 |
outStream2 notNil ifTrue:[
|
|
294 |
outStream2 show:something.
|
|
295 |
]
|
|
296 |
|
|
297 |
"Created: / 12-11-2018 / 14:35:45 / Claus Gittinger"
|
833
|
298 |
! !
|
|
299 |
|
|
300 |
!SplittingWriteStream class methodsFor:'documentation'!
|
|
301 |
|
|
302 |
version
|
4275
|
303 |
^ '$Header$'
|
5231
|
304 |
!
|
|
305 |
|
|
306 |
version_CVS
|
|
307 |
^ '$Header$'
|
833
|
308 |
! !
|
2993
|
309 |
|