author | Jan Vrany <jan.vrany@fit.cvut.cz> |
Mon, 03 Oct 2016 12:44:41 +0100 | |
branch | jv |
changeset 20578 | 39641ba8d6e0 |
parent 20342 | 219a5a47e8b1 |
parent 20432 | e7056f247cd9 |
child 21242 | 19fabe339f8b |
permissions | -rw-r--r-- |
20432 | 1 |
"{ Encoding: utf8 }" |
2 |
||
1 | 3 |
" |
5 | 4 |
COPYRIGHT (c) 1989 by Claus Gittinger |
252 | 5 |
All Rights Reserved |
1 | 6 |
|
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 |
" |
|
5556 | 14 |
"{ Package: 'stx:libbasic' }" |
15 |
||
17305 | 16 |
"{ NameSpace: Smalltalk }" |
17 |
||
1 | 18 |
PositionableStream subclass:#WriteStream |
1295 | 19 |
instanceVariableNames:'' |
20 |
classVariableNames:'' |
|
21 |
poolDictionaries:'' |
|
22 |
category:'Streams' |
|
1 | 23 |
! |
24 |
||
1965 | 25 |
!WriteStream class methodsFor:'documentation'! |
1 | 26 |
|
88 | 27 |
copyright |
28 |
" |
|
29 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
252 | 30 |
All Rights Reserved |
88 | 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 |
||
68 | 41 |
documentation |
42 |
" |
|
2883 | 43 |
Streams for writing into. |
44 |
WriteStreams are especially useful, if big strings are to be constructed |
|
16201 | 45 |
from pieces - create a writeStream, add the pieces (with #nextPut: or |
46 |
#nextPutAll:) and finally fetch the concatenated string via #contents. |
|
2883 | 47 |
This is much better than constructing the big string by concatenating via |
48 |
the comma (,) operator, since less intermediate garbage objects are created. |
|
49 |
||
18294 | 50 |
This implementation currently DOES change the |
20271 | 51 |
identity of the streamed-upon collection IF it cannot grow easily. |
261 | 52 |
Collections which cannot grow easily are for example: Array, ByteArray and String. |
18294 | 53 |
Thus it is slightly incompatible to ST-80 since 'aStream contents' does |
88 | 54 |
not always return the original collection. This may change. |
1295 | 55 |
|
17557 | 56 |
[caveat:] |
20271 | 57 |
Basing capabilities like readability/writability/positionability/peekability on inheritance makes |
58 |
the class hierarchy ugly and leads to strange and hard to teach redefinitions (aka. NonPositionableStream |
|
59 |
below PositionableStream or ExternalReadStream under WriteStream) |
|
17557 | 60 |
|
1295 | 61 |
[author:] |
20271 | 62 |
Claus Gittinger |
10281 | 63 |
|
64 |
[see also:] |
|
20271 | 65 |
CharacterWriteStream (if streaming for a unicode string) |
68 | 66 |
" |
2883 | 67 |
! |
68 |
||
69 |
examples |
|
70 |
" |
|
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
71 |
[exBegin] |
2883 | 72 |
|s| |
73 |
||
74 |
s := WriteStream on:''. |
|
75 |
s nextPutAll:'hello'; |
|
76 |
space; |
|
77 |
nextPutAll:'world'. |
|
78 |
||
79 |
s contents inspect |
|
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
80 |
[exEnd] |
2883 | 81 |
|
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
82 |
[exBegin] |
2883 | 83 |
|s| |
84 |
||
85 |
s := WriteStream on:''. |
|
86 |
s nextPutAll:'hello'; |
|
87 |
space; |
|
88 |
nextPutAll:'world'. |
|
89 |
||
90 |
Transcript nextPutLine:(s contents) |
|
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
91 |
[exEnd] |
2883 | 92 |
|
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
93 |
[exBegin] |
2883 | 94 |
|s| |
95 |
||
96 |
s := '' writeStream. |
|
97 |
s nextPutAll:'hello'; |
|
98 |
space; |
|
99 |
nextPutAll:'world'. |
|
100 |
||
101 |
Transcript nextPutLine:(s contents) |
|
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
102 |
[exEnd] |
2883 | 103 |
" |
104 |
||
345 | 105 |
! ! |
106 |
||
1 | 107 |
!WriteStream methodsFor:'accessing'! |
108 |
||
15179 | 109 |
clear |
110 |
"for compatibility with Transcript" |
|
111 |
||
112 |
self reset |
|
113 |
! |
|
114 |
||
1 | 115 |
contents |
345 | 116 |
"return the current contents (a collection) of the stream. |
117 |
Currently, this returns the actual collection if possible |
|
118 |
(and reset is implemented to create a new one) in contrast to |
|
119 |
ST80, where contents returns a copy and reset only sets the writePointer. |
|
120 |
The ST/X behavior creates less temporary garbage in the normal case |
|
2410 | 121 |
(where things are written for the contents only) but may be incompatible |
345 | 122 |
with some applications. Time will show, if this is to be changed." |
1 | 123 |
|
260 | 124 |
|lastIndex| |
125 |
||
15347 | 126 |
lastIndex := position. |
260 | 127 |
collection size == lastIndex ifFalse:[ |
18294 | 128 |
collection isFixedSize ifTrue:[ |
129 |
" |
|
130 |
grow is expensive - return a copy. |
|
131 |
(is this what users of writeStream expect ?) |
|
132 |
" |
|
133 |
collection := collection copyFrom:1 to:lastIndex |
|
134 |
] ifFalse:[ |
|
135 |
collection grow:lastIndex |
|
136 |
] |
|
1 | 137 |
]. |
138 |
^ collection |
|
2410 | 139 |
|
3087 | 140 |
"Modified: / 19.2.1997 / 08:57:28 / stefan" |
141 |
"Modified: / 30.10.1997 / 16:21:23 / cg" |
|
1 | 142 |
! |
143 |
||
5888 | 144 |
last |
145 |
"return the last element - report an error if the stream is empty" |
|
146 |
||
16201 | 147 |
^ collection at:position. |
5888 | 148 |
|
149 |
" |
|
150 |
|s| |
|
151 |
||
152 |
s := '' writeStream. |
|
153 |
s nextPut:$a. |
|
18294 | 154 |
s last. |
5888 | 155 |
s nextPut:$b. |
18294 | 156 |
s last. |
5888 | 157 |
s nextPut:$c. |
18294 | 158 |
s last. |
5888 | 159 |
" |
160 |
! |
|
161 |
||
162 |
last:n |
|
163 |
"return the last n elements as species of the underlying collection; |
|
164 |
Report an error if the stream is empty" |
|
165 |
||
17181 | 166 |
^ collection copyFrom:(position - n + 1) to:position. |
5888 | 167 |
|
168 |
" |
|
169 |
|s| |
|
170 |
||
171 |
s := '' writeStream. |
|
172 |
s nextPut:$a. |
|
18294 | 173 |
s last:1. |
5888 | 174 |
s nextPut:$b. |
18294 | 175 |
s last:1. |
176 |
s last:2. |
|
5888 | 177 |
s nextPut:$c. |
18294 | 178 |
s last:1. |
179 |
s last:2. |
|
180 |
s last:3. |
|
5888 | 181 |
" |
182 |
! |
|
183 |
||
329 | 184 |
reset |
345 | 185 |
"reset the stream; write anew. |
2410 | 186 |
See the comment in WriteStream>>contents" |
329 | 187 |
|
188 |
collection := collection species new:(collection size). |
|
19887 | 189 |
position := 0. |
2410 | 190 |
|
191 |
"Modified: 19.2.1997 / 08:57:00 / stefan" |
|
1 | 192 |
! ! |
193 |
||
611 | 194 |
!WriteStream methodsFor:'positioning'! |
195 |
||
15617 | 196 |
position:index0Based |
611 | 197 |
"redefined to allow positioning past the readLimit" |
198 |
||
7052
207cc9c62982
prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents:
7051
diff
changeset
|
199 |
((index0Based > collection size) or:[index0Based < 0]) ifTrue: [^ self positionError]. |
15347 | 200 |
position := index0Based |
17688 | 201 |
! |
202 |
||
203 |
skip:count |
|
204 |
"redefined to allow positioning back over already written characters" |
|
205 |
||
206 |
self position:(self position + count). |
|
611 | 207 |
! ! |
208 |
||
209 |
!WriteStream methodsFor:'private'! |
|
210 |
||
211 |
growCollection |
|
2863
aec08a0c2c2e
no need for unsigned compare against 0
Claus Gittinger <cg@exept.de>
parents:
2410
diff
changeset
|
212 |
"grow the streamed collection to at least 10 elements" |
aec08a0c2c2e
no need for unsigned compare against 0
Claus Gittinger <cg@exept.de>
parents:
2410
diff
changeset
|
213 |
|
611 | 214 |
self growCollection:10 |
2863
aec08a0c2c2e
no need for unsigned compare against 0
Claus Gittinger <cg@exept.de>
parents:
2410
diff
changeset
|
215 |
|
aec08a0c2c2e
no need for unsigned compare against 0
Claus Gittinger <cg@exept.de>
parents:
2410
diff
changeset
|
216 |
"Modified: 19.8.1997 / 17:53:28 / cg" |
611 | 217 |
! |
218 |
||
219 |
growCollection:minNewSize |
|
220 |
"grow the streamed collection to at least minNewSize" |
|
221 |
||
222 |
|oldSize newSize newColl| |
|
223 |
||
224 |
oldSize := collection size. |
|
225 |
(oldSize == 0) ifTrue:[ |
|
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
226 |
newSize := minNewSize |
611 | 227 |
] ifFalse:[ |
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
228 |
newSize := oldSize * 2. |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
229 |
(newSize < minNewSize) ifTrue:[newSize := minNewSize]. |
611 | 230 |
]. |
231 |
collection isFixedSize ifTrue:[ |
|
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
232 |
newColl := collection species new:newSize. |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
233 |
newColl replaceFrom:1 to:oldSize with:collection startingAt:1. |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
234 |
collection := newColl |
611 | 235 |
] ifFalse:[ |
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
236 |
collection grow:newSize |
611 | 237 |
]. |
2863
aec08a0c2c2e
no need for unsigned compare against 0
Claus Gittinger <cg@exept.de>
parents:
2410
diff
changeset
|
238 |
|
aec08a0c2c2e
no need for unsigned compare against 0
Claus Gittinger <cg@exept.de>
parents:
2410
diff
changeset
|
239 |
"Modified: 19.8.1997 / 17:53:11 / cg" |
611 | 240 |
! ! |
241 |
||
7260 | 242 |
!WriteStream methodsFor:'private-accessing'! |
611 | 243 |
|
11794
ad5fcc151a09
#on: - set readLimit to 0, so that setToEnd positions to end of written
Stefan Vogel <sv@exept.de>
parents:
11324
diff
changeset
|
244 |
on:aCollection |
ad5fcc151a09
#on: - set readLimit to 0, so that setToEnd positions to end of written
Stefan Vogel <sv@exept.de>
parents:
11324
diff
changeset
|
245 |
super on:aCollection. |
ad5fcc151a09
#on: - set readLimit to 0, so that setToEnd positions to end of written
Stefan Vogel <sv@exept.de>
parents:
11324
diff
changeset
|
246 |
readLimit := 0. |
ad5fcc151a09
#on: - set readLimit to 0, so that setToEnd positions to end of written
Stefan Vogel <sv@exept.de>
parents:
11324
diff
changeset
|
247 |
! |
ad5fcc151a09
#on: - set readLimit to 0, so that setToEnd positions to end of written
Stefan Vogel <sv@exept.de>
parents:
11324
diff
changeset
|
248 |
|
611 | 249 |
on:aCollection from:start to:last |
250 |
"create and return a new stream for writing onto aCollection, where |
|
251 |
writing is limited to the elements in the range start to last." |
|
252 |
||
253 |
super on:aCollection from:start to:last. |
|
254 |
writeLimit := last. |
|
255 |
! ! |
|
256 |
||
257 |
!WriteStream methodsFor:'queries'! |
|
258 |
||
3031 | 259 |
size |
260 |
"return the current size" |
|
261 |
||
15347 | 262 |
^ position |
611 | 263 |
! ! |
264 |
||
63 | 265 |
!WriteStream methodsFor:'reading'! |
1 | 266 |
|
267 |
next |
|
10 | 268 |
"catch read access to write stream - report an error" |
1 | 269 |
|
10 | 270 |
self shouldNotImplement |
1 | 271 |
! |
272 |
||
273 |
peek |
|
10 | 274 |
"catch read access to write stream - report an error" |
1 | 275 |
|
10 | 276 |
self shouldNotImplement |
63 | 277 |
! ! |
278 |
||
5556 | 279 |
!WriteStream methodsFor:'testing'! |
280 |
||
281 |
isEmpty |
|
282 |
"return true, if the contents of the stream is empty" |
|
283 |
||
15617 | 284 |
^ self position == 0 |
5556 | 285 |
|
286 |
"Created: 14.10.1997 / 20:44:37 / cg" |
|
16201 | 287 |
! |
288 |
||
18294 | 289 |
isReadable |
20432 | 290 |
"return true if the receiver supports reading - that's not true" |
16201 | 291 |
|
292 |
^ false |
|
293 |
||
294 |
"Created: / 8.11.1997 / 14:06:07 / cg" |
|
295 |
! |
|
296 |
||
297 |
isWritable |
|
19437 | 298 |
"return true, if writing is supported by the receiver. |
16201 | 299 |
Always return true here" |
300 |
||
301 |
^ true |
|
302 |
||
303 |
"Modified: 16.5.1996 / 14:44:49 / cg" |
|
5556 | 304 |
! ! |
305 |
||
611 | 306 |
!WriteStream methodsFor:'writing'! |
369 | 307 |
|
611 | 308 |
next:count put:anObject |
309 |
"append anObject count times to the receiver. |
|
310 |
Redefined to avoid count grows of the underlying collection - |
|
311 |
instead a single grow on the final size is performed." |
|
312 |
||
17181 | 313 |
|final| |
611 | 314 |
|
17062 | 315 |
count == 0 ifTrue:[^ self]. |
316 |
||
611 | 317 |
(collection isNil or:[writeLimit notNil]) ifTrue:[ |
19643 | 318 |
super next:count put:anObject. |
319 |
^ self. |
|
611 | 320 |
]. |
369 | 321 |
|
17181 | 322 |
final := position + count. |
611 | 323 |
(final > collection size) ifTrue:[ |
19643 | 324 |
self growCollection:final |
611 | 325 |
]. |
369 | 326 |
|
17181 | 327 |
collection from:position + 1 to:final put:anObject. |
328 |
position := position + count. |
|
19643 | 329 |
(position > readLimit) ifTrue:[readLimit := position]. |
12131 | 330 |
"/ ^ anObject -- return self |
16793 | 331 |
|
332 |
" |
|
333 |
'' writeStream next:10 put:$* |
|
334 |
" |
|
611 | 335 |
! |
1 | 336 |
|
5198
ca2c2f7ec69c
added #next:putAll:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4406
diff
changeset
|
337 |
next:n putAll:aCollection startingAt:pos1 |
ca2c2f7ec69c
added #next:putAll:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4406
diff
changeset
|
338 |
"append some elements of the argument, aCollection to the stream." |
ca2c2f7ec69c
added #next:putAll:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4406
diff
changeset
|
339 |
|
ca2c2f7ec69c
added #next:putAll:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4406
diff
changeset
|
340 |
^ self nextPutAll:aCollection startingAt:pos1 to:pos1+n-1 |
ca2c2f7ec69c
added #next:putAll:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4406
diff
changeset
|
341 |
|
ca2c2f7ec69c
added #next:putAll:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4406
diff
changeset
|
342 |
" |
ca2c2f7ec69c
added #next:putAll:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4406
diff
changeset
|
343 |
|s| |
ca2c2f7ec69c
added #next:putAll:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4406
diff
changeset
|
344 |
|
ca2c2f7ec69c
added #next:putAll:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4406
diff
changeset
|
345 |
s := '' writeStream. |
ca2c2f7ec69c
added #next:putAll:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4406
diff
changeset
|
346 |
s nextPutAll:'hello '. |
ca2c2f7ec69c
added #next:putAll:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4406
diff
changeset
|
347 |
s next:5 putAll:'1234world012345' startingAt:5. |
18294 | 348 |
s contents |
5198
ca2c2f7ec69c
added #next:putAll:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4406
diff
changeset
|
349 |
" |
ca2c2f7ec69c
added #next:putAll:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4406
diff
changeset
|
350 |
|
ca2c2f7ec69c
added #next:putAll:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4406
diff
changeset
|
351 |
"Modified: 12.7.1996 / 10:31:36 / cg" |
ca2c2f7ec69c
added #next:putAll:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4406
diff
changeset
|
352 |
! |
ca2c2f7ec69c
added #next:putAll:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4406
diff
changeset
|
353 |
|
1 | 354 |
nextPut:anObject |
1584 | 355 |
"append the argument, anObject to the stream. |
2863
aec08a0c2c2e
no need for unsigned compare against 0
Claus Gittinger <cg@exept.de>
parents:
2410
diff
changeset
|
356 |
Specially tuned for appending to String, ByteArray and Array streams." |
1 | 357 |
|
63 | 358 |
%{ /* NOCONTEXT */ |
359 |
||
8293
dcffeb2c11e0
Optimize #nextPut: on UnicodeStrings
Stefan Vogel <sv@exept.de>
parents:
7686
diff
changeset
|
360 |
#ifndef NO_PRIM_STREAM |
63 | 361 |
REGISTER int pos; |
362 |
unsigned ch; |
|
363 |
OBJ coll; |
|
2883 | 364 |
OBJ p, wL, rL; |
1584 | 365 |
int __readLimit = -1; |
63 | 366 |
|
1136 | 367 |
coll = __INST(collection); |
368 |
p = __INST(position); |
|
63 | 369 |
|
293 | 370 |
if (__isNonNilObject(coll) && __isSmallInteger(p)) { |
19643 | 371 |
pos = __intVal(p); |
372 |
/* make 1-based */ |
|
373 |
pos = pos + 1; |
|
374 |
wL = __INST(writeLimit); |
|
63 | 375 |
|
19643 | 376 |
if ((wL == nil) |
377 |
|| (__isSmallInteger(wL) && (pos <= __intVal(wL)))) { |
|
378 |
OBJ cls; |
|
252 | 379 |
|
19643 | 380 |
cls = __qClass(coll); |
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
381 |
|
19643 | 382 |
rL = __INST(readLimit); |
383 |
if (__isSmallInteger(rL)) { |
|
384 |
__readLimit = __intVal(rL); |
|
385 |
} |
|
293 | 386 |
|
19643 | 387 |
if (cls == @global(String)) { |
388 |
if (__isCharacter(anObject) |
|
389 |
&& ((ch = __intVal(__characterVal(anObject))) <= 255) /* ch is unsigned */ |
|
390 |
&& (pos <= __stringSize(coll))) { |
|
391 |
__StringInstPtr(coll)->s_element[pos-1] = ch; |
|
18294 | 392 |
advancePositionAndReturn: ; |
19643 | 393 |
if ((__readLimit >= 0) && (pos > __readLimit)) { |
394 |
__INST(readLimit) = __mkSmallInteger(pos); |
|
395 |
} |
|
396 |
__INST(position) = __mkSmallInteger(pos); |
|
397 |
RETURN ( anObject ); |
|
398 |
} |
|
399 |
} else if (cls == @global(ByteArray)) { |
|
400 |
if (__isSmallInteger(anObject) |
|
401 |
&& ((ch = __intVal(anObject)) <= 0xFF) /* ch is unsigned */ |
|
402 |
&& (pos <= __byteArraySize(coll))) { |
|
403 |
__ByteArrayInstPtr(coll)->ba_element[pos-1] = ch; |
|
404 |
goto advancePositionAndReturn; |
|
405 |
} |
|
406 |
} else if (cls == @global(Array)) { |
|
407 |
if (pos <= __arraySize(coll)) { |
|
408 |
__ArrayInstPtr(coll)->a_element[pos-1] = anObject; |
|
409 |
__STORE(coll, anObject); |
|
410 |
goto advancePositionAndReturn; |
|
411 |
} |
|
412 |
} else if (cls == @global(Unicode16String)) { |
|
413 |
if (__isCharacter(anObject) |
|
414 |
&& ((ch = __intVal(__characterVal(anObject))) <= 0xFFFF) /* ch is unsigned */ |
|
415 |
&& (pos <= __unicode16StringSize(coll))) { |
|
416 |
__Unicode16StringInstPtr(coll)->s_element[pos-1] = ch; |
|
417 |
goto advancePositionAndReturn; |
|
418 |
} |
|
419 |
} else if (cls == @global(Unicode32String)) { |
|
420 |
if (__isCharacter(anObject) |
|
421 |
&& (pos <= __unicode32StringSize(coll))) { |
|
422 |
__Unicode32StringInstPtr(coll)->s_element[pos-1] = __intVal(__characterVal(anObject)); |
|
423 |
goto advancePositionAndReturn; |
|
424 |
} |
|
425 |
} |
|
426 |
} |
|
63 | 427 |
} |
2894 | 428 |
#endif |
260 | 429 |
%}. |
293 | 430 |
(writeLimit isNil |
15347 | 431 |
or:[(position + 1) <= writeLimit]) ifTrue:[ |
19643 | 432 |
(position >= collection size) ifTrue:[self growCollection]. |
433 |
collection at:(position + 1) put:anObject. |
|
434 |
position := position + 1. |
|
435 |
(position > readLimit) ifTrue:[readLimit := position]. |
|
838 | 436 |
] ifFalse:[ |
19643 | 437 |
WriteError raiseErrorString:'write beyond writeLimit' |
293 | 438 |
]. |
1 | 439 |
^anObject |
440 |
! |
|
441 |
||
442 |
nextPutAll:aCollection |
|
63 | 443 |
"append all elements of the argument, aCollection to the stream. |
444 |
Redefined to avoid count grows of the underlying collection - |
|
445 |
instead a single grow on the final size is performed." |
|
1 | 446 |
|
17181 | 447 |
|nMore "{ Class: SmallInteger }" |
448 |
final "{ Class: SmallInteger }" | |
|
1 | 449 |
|
18294 | 450 |
(collection notNil and:[aCollection isSequenceable]) ifFalse:[ |
19643 | 451 |
"/ fallback |
452 |
super nextPutAll:aCollection. |
|
453 |
^ self. |
|
13628 | 454 |
]. |
77 | 455 |
|
1 | 456 |
nMore := aCollection size. |
9499 | 457 |
nMore == 0 ifTrue:[ |
19643 | 458 |
"/ for the programmer.. |
459 |
aCollection isCollection ifFalse:[ |
|
460 |
self error:'invalid argument (not a collection)' mayProceed:true |
|
461 |
]. |
|
9499 | 462 |
]. |
463 |
||
17181 | 464 |
final := position + nMore. |
293 | 465 |
(writeLimit notNil |
466 |
and:[final > writeLimit]) ifTrue:[ |
|
19643 | 467 |
final := writeLimit. |
468 |
nMore := final - position |
|
293 | 469 |
]. |
1 | 470 |
(final > collection size) ifTrue:[ |
19643 | 471 |
self growCollection:final |
1 | 472 |
]. |
18294 | 473 |
collection |
19643 | 474 |
replaceFrom:(position + 1) |
475 |
to:final |
|
476 |
with:aCollection |
|
477 |
startingAt:1. |
|
1 | 478 |
|
17181 | 479 |
position := position + nMore. |
19643 | 480 |
(position > readLimit) ifTrue:[readLimit := position]. |
12130 | 481 |
"/ ^ aCollection -- self |
13628 | 482 |
|
483 |
"Modified: / 04-09-2011 / 20:03:32 / cg" |
|
1539
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
484 |
! |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
485 |
|
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
486 |
nextPutAll:aCollection startingAt:pos1 to:pos2 |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
487 |
"append some elements of the argument, aCollection to the stream. |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
488 |
Redefined to avoid count grows of the underlying collection - |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
489 |
instead a single grow on the final size is performed." |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
490 |
|
17181 | 491 |
|nMore final| |
1539
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
492 |
|
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
493 |
collection isNil ifTrue:[ |
19643 | 494 |
^ super nextPutAll:aCollection startingAt:pos1 to:pos2 |
1539
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
495 |
]. |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
496 |
|
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
497 |
nMore := pos2 - pos1 + 1. |
17181 | 498 |
final := position + nMore. |
1539
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
499 |
(writeLimit notNil |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
500 |
and:[final > writeLimit]) ifTrue:[ |
19643 | 501 |
final := writeLimit. |
502 |
nMore := final - position |
|
1539
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
503 |
]. |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
504 |
(final > collection size) ifTrue:[ |
19643 | 505 |
self growCollection:final |
1539
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
506 |
]. |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
507 |
|
17181 | 508 |
collection replaceFrom:position + 1 |
19643 | 509 |
to:final |
510 |
with:aCollection |
|
511 |
startingAt:pos1. |
|
1539
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
512 |
|
17181 | 513 |
position := position + nMore. |
19643 | 514 |
(position > readLimit) ifTrue:[readLimit := position]. |
12131 | 515 |
"/ ^ aCollection -- return self |
1539
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
516 |
|
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
517 |
" |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
518 |
|s| |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
519 |
|
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
520 |
s := '' writeStream. |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
521 |
s nextPutAll:'hello '. |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
522 |
s nextPutAll:'1234world012345' startingAt:5 to:9. |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
523 |
s contents |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
524 |
" |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
525 |
|
1541 | 526 |
"Modified: 12.7.1996 / 10:31:36 / cg" |
8361 | 527 |
! |
528 |
||
11324 | 529 |
nextPutAllUnicode:aString |
530 |
"normal streams can not handle multi-byte characters, so convert them to utf8" |
|
531 |
||
15653 | 532 |
"this code is not perfect if you use both #nextPutAll: and #nextPutAllUnicode: |
18294 | 533 |
with the same stream, since 8-bit characters (with the highest bits set) |
15653 | 534 |
are not stored as UTF, so we get some inconsistent string" |
535 |
||
536 |
collection isString ifTrue:[ |
|
18294 | 537 |
collection bitsPerCharacter == 16 ifTrue:[ |
538 |
self nextPutAllUtf16:aString. |
|
539 |
] ifFalse:[ |
|
540 |
self nextPutAllUtf8:aString. |
|
541 |
]. |
|
11324 | 542 |
] ifFalse:[ |
18294 | 543 |
self nextPutAll:aString |
11324 | 544 |
]. |
13738
e11fccebb0c3
changed: #nextPutAllUnicode:
Claus Gittinger <cg@exept.de>
parents:
13628
diff
changeset
|
545 |
|
e11fccebb0c3
changed: #nextPutAllUnicode:
Claus Gittinger <cg@exept.de>
parents:
13628
diff
changeset
|
546 |
"Modified: / 28-09-2011 / 16:15:52 / cg" |
11324 | 547 |
! |
548 |
||
8615 | 549 |
nextPutByte:anObject |
550 |
"append the argument, anObject to the stream. |
|
551 |
Specially tuned for appending to String and ByteArray streams." |
|
552 |
||
553 |
%{ /* NOCONTEXT */ |
|
554 |
||
555 |
#ifndef NO_PRIM_STREAM |
|
19828 | 556 |
OBJ coll = __INST(collection); |
557 |
OBJ p = __INST(position); |
|
8615 | 558 |
|
559 |
if (__isNonNilObject(coll) && __isSmallInteger(p) && __isSmallInteger(anObject)) { |
|
19828 | 560 |
OBJ wL = __INST(writeLimit); |
561 |
INT pos = __intVal(p) + 1; /* make 1-based and usable for update below */ |
|
562 |
unsigned int ch = __intVal(anObject); |
|
8615 | 563 |
|
19828 | 564 |
if (ch <= 0xFF && /* ch is unsigned */ |
565 |
((wL == nil) || (__isSmallInteger(wL) && (pos <= __intVal(wL))))) { |
|
566 |
OBJ cls = __qClass(coll); |
|
567 |
OBJ rL = __INST(readLimit); |
|
568 |
INT __readLimit = -1; |
|
8615 | 569 |
|
19643 | 570 |
if (__isSmallInteger(rL)) { |
571 |
__readLimit = __intVal(rL); |
|
572 |
} |
|
8615 | 573 |
|
19643 | 574 |
if (cls == @global(String)) { |
19828 | 575 |
if (pos <= __stringSize(coll)) { |
19643 | 576 |
__StringInstPtr(coll)->s_element[pos-1] = ch; |
18294 | 577 |
advancePositionAndReturn: ; |
19643 | 578 |
if ((__readLimit >= 0) && (pos > __readLimit)) { |
579 |
__INST(readLimit) = __mkSmallInteger(pos); |
|
580 |
} |
|
581 |
__INST(position) = __mkSmallInteger(pos); |
|
582 |
RETURN ( anObject ); |
|
583 |
} |
|
584 |
} else if (cls == @global(ByteArray)) { |
|
19828 | 585 |
if (pos <= __byteArraySize(coll)) { |
19643 | 586 |
__ByteArrayInstPtr(coll)->ba_element[pos-1] = ch; |
587 |
goto advancePositionAndReturn; |
|
588 |
} |
|
589 |
} |
|
590 |
} |
|
8615 | 591 |
} |
592 |
#endif |
|
593 |
%}. |
|
19828 | 594 |
((writeLimit isNil or:[(position + 1) <= writeLimit]) |
595 |
and:[position >= collection size]) ifTrue:[ |
|
596 |
self growCollection. |
|
597 |
^ self nextPutByte:anObject. "try again" |
|
598 |
] ifFalse:[ |
|
599 |
^ super nextPutByte:anObject |
|
600 |
]. |
|
8615 | 601 |
! |
602 |
||
8361 | 603 |
nextPutBytes:count from:anObject startingAt:start |
604 |
"write count bytes from an object starting at index start. |
|
605 |
Return the number of bytes written. |
|
18294 | 606 |
The object must have non-pointer indexed instvars |
607 |
(i.e. be a ByteArray, String, Float- or DoubleArray). |
|
8361 | 608 |
Use with care - non object oriented i/o. |
609 |
This is provided for compatibility with externalStream; |
|
610 |
to support binary storage" |
|
611 |
||
11794
ad5fcc151a09
#on: - set readLimit to 0, so that setToEnd positions to end of written
Stefan Vogel <sv@exept.de>
parents:
11324
diff
changeset
|
612 |
anObject isByteCollection ifTrue:[ |
18294 | 613 |
self nextPutAll:anObject startingAt:start to:(start + count - 1). |
614 |
^ count. |
|
8361 | 615 |
]. |
616 |
^ super nextPutBytes:count from:anObject startingAt:start |
|
11324 | 617 |
! |
618 |
||
619 |
nextPutUnicode:aCharacter |
|
620 |
"normal streams can not handle multi-byte characters, so convert them to utf8" |
|
621 |
||
15653 | 622 |
"this code is not perfect if you use both #nextPut: and #nextPutUnicode: |
18294 | 623 |
with the same stream, since 8-bit characters (with the highest bits set) |
15653 | 624 |
are not stored as UTF, so we get some inconsistent string" |
625 |
||
626 |
collection isString ifTrue:[ |
|
18294 | 627 |
collection bitsPerCharacter == 16 ifTrue:[ |
628 |
self nextPutUtf16:aCharacter. |
|
629 |
] ifFalse:[ |
|
630 |
self nextPutUtf8:aCharacter. |
|
631 |
]. |
|
11324 | 632 |
] ifFalse:[ |
18294 | 633 |
self nextPut:aCharacter. |
11324 | 634 |
]. |
1 | 635 |
! ! |
636 |
||
1965 | 637 |
!WriteStream class methodsFor:'documentation'! |
701 | 638 |
|
639 |
version |
|
19437 | 640 |
^ '$Header$' |
12130 | 641 |
! |
642 |
||
643 |
version_CVS |
|
19437 | 644 |
^ '$Header$' |
701 | 645 |
! ! |
19437 | 646 |