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