author | Claus Gittinger <cg@exept.de> |
Tue, 20 Feb 2018 01:07:35 +0100 | |
changeset 22553 | fb78e910d191 |
parent 22152 | f19b33cfb222 |
child 22569 | 3f6ffe519140 |
permissions | -rw-r--r-- |
22553 | 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 |
||
22553 | 184 |
readStream |
185 |
"return a readstream on the current contents; |
|
186 |
that is: if more is written to the stream in the meantime, |
|
187 |
the readStream will NOT show it. |
|
188 |
To avoid copying the contents, a readStream with a readLimit |
|
189 |
is created. In the hope, that noone repositions and rewrites the |
|
190 |
underlying writeStream. |
|
191 |
If that leads to problems, you should use contents readStream." |
|
192 |
||
193 |
^ ReadStream on:collection from:1 to:self position. |
|
194 |
||
195 |
" |
|
196 |
|w r| |
|
197 |
||
198 |
w := WriteStream on:(String new). |
|
199 |
w nextPutAll:'12345'. |
|
200 |
r := w readStream. |
|
201 |
w nextPutAll:'abc'. |
|
202 |
w nextPutAll:(r upToEnd). |
|
203 |
w nextPutAll:'def'. |
|
204 |
w contents. |
|
205 |
" |
|
206 |
! |
|
207 |
||
329 | 208 |
reset |
345 | 209 |
"reset the stream; write anew. |
2410 | 210 |
See the comment in WriteStream>>contents" |
329 | 211 |
|
212 |
collection := collection species new:(collection size). |
|
19887 | 213 |
position := 0. |
2410 | 214 |
|
215 |
"Modified: 19.2.1997 / 08:57:00 / stefan" |
|
22150 | 216 |
! ! |
217 |
||
218 |
!WriteStream methodsFor:'positioning'! |
|
219 |
||
220 |
position:index0Based |
|
221 |
"redefined to allow positioning past the readLimit" |
|
222 |
||
223 |
((index0Based > collection size) or:[index0Based < 0]) ifTrue: [^ self positionError]. |
|
224 |
position := index0Based |
|
225 |
! |
|
226 |
||
227 |
skip:count |
|
228 |
"redefined to allow positioning back over already written characters" |
|
229 |
||
230 |
self position:(self position + count). |
|
22149 | 231 |
! |
232 |
||
233 |
truncateTo:givenMax |
|
234 |
"if the streamed contents is larger than maxSize, |
|
235 |
reset the write position back to this size. |
|
236 |
Otherwise do nothing." |
|
237 |
||
238 |
self size > givenMax ifTrue:[ |
|
239 |
self position:givenMax. |
|
240 |
]. |
|
241 |
||
242 |
" |
|
243 |
|s| |
|
244 |
||
245 |
s := '' writeStream. |
|
246 |
s nextPutAll:'123456'. |
|
247 |
s truncateTo:4. |
|
248 |
s nextPutAll:'abc'. |
|
249 |
s contents |
|
250 |
" |
|
251 |
||
252 |
" |
|
253 |
|s| |
|
254 |
||
255 |
s := '' writeStream. |
|
256 |
s nextPutAll:'1234'. |
|
257 |
s truncateTo:4. |
|
258 |
s nextPutAll:'abc'. |
|
259 |
s contents |
|
260 |
" |
|
261 |
||
262 |
" |
|
263 |
|s| |
|
264 |
||
265 |
s := '' writeStream. |
|
266 |
s nextPutAll:'123'. |
|
267 |
s truncateTo:4. |
|
268 |
s nextPutAll:'abc'. |
|
269 |
s contents |
|
270 |
" |
|
271 |
||
22152 | 272 |
" |
273 |
|s| |
|
274 |
||
275 |
s := 'testFile' asFilename writeStream. |
|
276 |
s nextPutAll:'123456'. |
|
277 |
s truncateTo:4. |
|
278 |
s nextPutAll:'abc'. |
|
279 |
s close. |
|
280 |
'testFile' asFilename contents |
|
281 |
" |
|
282 |
||
22149 | 283 |
"Created: / 02-08-2017 / 09:24:58 / cg" |
1 | 284 |
! ! |
285 |
||
611 | 286 |
!WriteStream methodsFor:'private'! |
287 |
||
288 |
growCollection |
|
2863
aec08a0c2c2e
no need for unsigned compare against 0
Claus Gittinger <cg@exept.de>
parents:
2410
diff
changeset
|
289 |
"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
|
290 |
|
611 | 291 |
self growCollection:10 |
2863
aec08a0c2c2e
no need for unsigned compare against 0
Claus Gittinger <cg@exept.de>
parents:
2410
diff
changeset
|
292 |
|
aec08a0c2c2e
no need for unsigned compare against 0
Claus Gittinger <cg@exept.de>
parents:
2410
diff
changeset
|
293 |
"Modified: 19.8.1997 / 17:53:28 / cg" |
611 | 294 |
! |
295 |
||
296 |
growCollection:minNewSize |
|
297 |
"grow the streamed collection to at least minNewSize" |
|
298 |
||
299 |
|oldSize newSize newColl| |
|
300 |
||
301 |
oldSize := collection size. |
|
302 |
(oldSize == 0) ifTrue:[ |
|
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
303 |
newSize := minNewSize |
611 | 304 |
] ifFalse:[ |
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
305 |
newSize := oldSize * 2. |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
306 |
(newSize < minNewSize) ifTrue:[newSize := minNewSize]. |
611 | 307 |
]. |
308 |
collection isFixedSize ifTrue:[ |
|
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
309 |
newColl := collection species new:newSize. |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
310 |
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
|
311 |
collection := newColl |
611 | 312 |
] ifFalse:[ |
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
313 |
collection grow:newSize |
611 | 314 |
]. |
2863
aec08a0c2c2e
no need for unsigned compare against 0
Claus Gittinger <cg@exept.de>
parents:
2410
diff
changeset
|
315 |
|
aec08a0c2c2e
no need for unsigned compare against 0
Claus Gittinger <cg@exept.de>
parents:
2410
diff
changeset
|
316 |
"Modified: 19.8.1997 / 17:53:11 / cg" |
611 | 317 |
! ! |
318 |
||
7260 | 319 |
!WriteStream methodsFor:'private-accessing'! |
611 | 320 |
|
11794
ad5fcc151a09
#on: - set readLimit to 0, so that setToEnd positions to end of written
Stefan Vogel <sv@exept.de>
parents:
11324
diff
changeset
|
321 |
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
|
322 |
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
|
323 |
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
|
324 |
! |
ad5fcc151a09
#on: - set readLimit to 0, so that setToEnd positions to end of written
Stefan Vogel <sv@exept.de>
parents:
11324
diff
changeset
|
325 |
|
611 | 326 |
on:aCollection from:start to:last |
327 |
"create and return a new stream for writing onto aCollection, where |
|
328 |
writing is limited to the elements in the range start to last." |
|
329 |
||
330 |
super on:aCollection from:start to:last. |
|
331 |
writeLimit := last. |
|
332 |
! ! |
|
333 |
||
334 |
!WriteStream methodsFor:'queries'! |
|
335 |
||
21132 | 336 |
endsWith:aCollection |
337 |
"Answer true, if the contents of the stream ends with aCollection. |
|
338 |
Speedup for upToAll:, throughAll etc." |
|
339 |
||
340 |
|sz start "{ Class: SmallInteger }"| |
|
341 |
||
342 |
collection isNil ifTrue:[ |
|
343 |
"only supported for internal streams" |
|
344 |
self shouldNotImplement. |
|
345 |
]. |
|
346 |
||
347 |
sz := aCollection size. |
|
348 |
start := position + 1 - sz. |
|
349 |
^ start > 0 and:[(collection copyFrom:position+1-sz to:position) endsWith:aCollection]. |
|
350 |
||
351 |
" |
|
352 |
(WriteStream with:'abcdef') endsWith:'def' |
|
353 |
(WriteStream with:'def') endsWith:'def' |
|
354 |
(WriteStream with:'abc') endsWith:'def' |
|
355 |
(WriteStream with:'ef') endsWith:'def' |
|
356 |
false " |
|
357 |
! |
|
358 |
||
3031 | 359 |
size |
360 |
"return the current size" |
|
361 |
||
15347 | 362 |
^ position |
611 | 363 |
! ! |
364 |
||
63 | 365 |
!WriteStream methodsFor:'reading'! |
1 | 366 |
|
367 |
next |
|
10 | 368 |
"catch read access to write stream - report an error" |
1 | 369 |
|
10 | 370 |
self shouldNotImplement |
1 | 371 |
! |
372 |
||
373 |
peek |
|
10 | 374 |
"catch read access to write stream - report an error" |
1 | 375 |
|
10 | 376 |
self shouldNotImplement |
63 | 377 |
! ! |
378 |
||
5556 | 379 |
!WriteStream methodsFor:'testing'! |
380 |
||
381 |
isEmpty |
|
382 |
"return true, if the contents of the stream is empty" |
|
383 |
||
15617 | 384 |
^ self position == 0 |
5556 | 385 |
|
386 |
"Created: 14.10.1997 / 20:44:37 / cg" |
|
16201 | 387 |
! |
388 |
||
18294 | 389 |
isReadable |
20432 | 390 |
"return true if the receiver supports reading - that's not true" |
16201 | 391 |
|
392 |
^ false |
|
393 |
||
394 |
"Created: / 8.11.1997 / 14:06:07 / cg" |
|
395 |
! |
|
396 |
||
397 |
isWritable |
|
19437 | 398 |
"return true, if writing is supported by the receiver. |
16201 | 399 |
Always return true here" |
400 |
||
401 |
^ true |
|
402 |
||
403 |
"Modified: 16.5.1996 / 14:44:49 / cg" |
|
5556 | 404 |
! ! |
405 |
||
611 | 406 |
!WriteStream methodsFor:'writing'! |
369 | 407 |
|
611 | 408 |
next:count put:anObject |
409 |
"append anObject count times to the receiver. |
|
410 |
Redefined to avoid count grows of the underlying collection - |
|
411 |
instead a single grow on the final size is performed." |
|
412 |
||
17181 | 413 |
|final| |
611 | 414 |
|
17062 | 415 |
count == 0 ifTrue:[^ self]. |
416 |
||
611 | 417 |
(collection isNil or:[writeLimit notNil]) ifTrue:[ |
19643 | 418 |
super next:count put:anObject. |
419 |
^ self. |
|
611 | 420 |
]. |
369 | 421 |
|
17181 | 422 |
final := position + count. |
611 | 423 |
(final > collection size) ifTrue:[ |
19643 | 424 |
self growCollection:final |
611 | 425 |
]. |
369 | 426 |
|
17181 | 427 |
collection from:position + 1 to:final put:anObject. |
428 |
position := position + count. |
|
19643 | 429 |
(position > readLimit) ifTrue:[readLimit := position]. |
12131 | 430 |
"/ ^ anObject -- return self |
16793 | 431 |
|
432 |
" |
|
433 |
'' writeStream next:10 put:$* |
|
434 |
" |
|
611 | 435 |
! |
1 | 436 |
|
437 |
nextPut:anObject |
|
1584 | 438 |
"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
|
439 |
Specially tuned for appending to String, ByteArray and Array streams." |
1 | 440 |
|
63 | 441 |
%{ /* NOCONTEXT */ |
442 |
||
8293
dcffeb2c11e0
Optimize #nextPut: on UnicodeStrings
Stefan Vogel <sv@exept.de>
parents:
7686
diff
changeset
|
443 |
#ifndef NO_PRIM_STREAM |
63 | 444 |
REGISTER int pos; |
445 |
unsigned ch; |
|
446 |
OBJ coll; |
|
2883 | 447 |
OBJ p, wL, rL; |
1584 | 448 |
int __readLimit = -1; |
63 | 449 |
|
1136 | 450 |
coll = __INST(collection); |
451 |
p = __INST(position); |
|
63 | 452 |
|
293 | 453 |
if (__isNonNilObject(coll) && __isSmallInteger(p)) { |
19643 | 454 |
pos = __intVal(p); |
455 |
/* make 1-based */ |
|
456 |
pos = pos + 1; |
|
457 |
wL = __INST(writeLimit); |
|
63 | 458 |
|
19643 | 459 |
if ((wL == nil) |
460 |
|| (__isSmallInteger(wL) && (pos <= __intVal(wL)))) { |
|
461 |
OBJ cls; |
|
252 | 462 |
|
19643 | 463 |
cls = __qClass(coll); |
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
464 |
|
19643 | 465 |
rL = __INST(readLimit); |
466 |
if (__isSmallInteger(rL)) { |
|
467 |
__readLimit = __intVal(rL); |
|
468 |
} |
|
293 | 469 |
|
19643 | 470 |
if (cls == @global(String)) { |
471 |
if (__isCharacter(anObject) |
|
472 |
&& ((ch = __intVal(__characterVal(anObject))) <= 255) /* ch is unsigned */ |
|
473 |
&& (pos <= __stringSize(coll))) { |
|
474 |
__StringInstPtr(coll)->s_element[pos-1] = ch; |
|
18294 | 475 |
advancePositionAndReturn: ; |
19643 | 476 |
if ((__readLimit >= 0) && (pos > __readLimit)) { |
477 |
__INST(readLimit) = __mkSmallInteger(pos); |
|
478 |
} |
|
479 |
__INST(position) = __mkSmallInteger(pos); |
|
480 |
RETURN ( anObject ); |
|
481 |
} |
|
482 |
} else if (cls == @global(ByteArray)) { |
|
483 |
if (__isSmallInteger(anObject) |
|
484 |
&& ((ch = __intVal(anObject)) <= 0xFF) /* ch is unsigned */ |
|
485 |
&& (pos <= __byteArraySize(coll))) { |
|
486 |
__ByteArrayInstPtr(coll)->ba_element[pos-1] = ch; |
|
487 |
goto advancePositionAndReturn; |
|
488 |
} |
|
489 |
} else if (cls == @global(Array)) { |
|
490 |
if (pos <= __arraySize(coll)) { |
|
491 |
__ArrayInstPtr(coll)->a_element[pos-1] = anObject; |
|
492 |
__STORE(coll, anObject); |
|
493 |
goto advancePositionAndReturn; |
|
494 |
} |
|
495 |
} else if (cls == @global(Unicode16String)) { |
|
496 |
if (__isCharacter(anObject) |
|
497 |
&& ((ch = __intVal(__characterVal(anObject))) <= 0xFFFF) /* ch is unsigned */ |
|
498 |
&& (pos <= __unicode16StringSize(coll))) { |
|
499 |
__Unicode16StringInstPtr(coll)->s_element[pos-1] = ch; |
|
500 |
goto advancePositionAndReturn; |
|
501 |
} |
|
502 |
} else if (cls == @global(Unicode32String)) { |
|
503 |
if (__isCharacter(anObject) |
|
504 |
&& (pos <= __unicode32StringSize(coll))) { |
|
505 |
__Unicode32StringInstPtr(coll)->s_element[pos-1] = __intVal(__characterVal(anObject)); |
|
506 |
goto advancePositionAndReturn; |
|
507 |
} |
|
508 |
} |
|
509 |
} |
|
63 | 510 |
} |
2894 | 511 |
#endif |
260 | 512 |
%}. |
293 | 513 |
(writeLimit isNil |
15347 | 514 |
or:[(position + 1) <= writeLimit]) ifTrue:[ |
19643 | 515 |
(position >= collection size) ifTrue:[self growCollection]. |
516 |
collection at:(position + 1) put:anObject. |
|
517 |
position := position + 1. |
|
518 |
(position > readLimit) ifTrue:[readLimit := position]. |
|
838 | 519 |
] ifFalse:[ |
19643 | 520 |
WriteError raiseErrorString:'write beyond writeLimit' |
293 | 521 |
]. |
1 | 522 |
^anObject |
523 |
! |
|
524 |
||
525 |
nextPutAll:aCollection |
|
63 | 526 |
"append all elements of the argument, aCollection to the stream. |
527 |
Redefined to avoid count grows of the underlying collection - |
|
528 |
instead a single grow on the final size is performed." |
|
1 | 529 |
|
17181 | 530 |
|nMore "{ Class: SmallInteger }" |
531 |
final "{ Class: SmallInteger }" | |
|
1 | 532 |
|
18294 | 533 |
(collection notNil and:[aCollection isSequenceable]) ifFalse:[ |
19643 | 534 |
"/ fallback |
535 |
super nextPutAll:aCollection. |
|
536 |
^ self. |
|
13628 | 537 |
]. |
77 | 538 |
|
1 | 539 |
nMore := aCollection size. |
9499 | 540 |
nMore == 0 ifTrue:[ |
19643 | 541 |
"/ for the programmer.. |
542 |
aCollection isCollection ifFalse:[ |
|
543 |
self error:'invalid argument (not a collection)' mayProceed:true |
|
544 |
]. |
|
9499 | 545 |
]. |
546 |
||
17181 | 547 |
final := position + nMore. |
293 | 548 |
(writeLimit notNil |
549 |
and:[final > writeLimit]) ifTrue:[ |
|
19643 | 550 |
final := writeLimit. |
551 |
nMore := final - position |
|
293 | 552 |
]. |
1 | 553 |
(final > collection size) ifTrue:[ |
19643 | 554 |
self growCollection:final |
1 | 555 |
]. |
18294 | 556 |
collection |
19643 | 557 |
replaceFrom:(position + 1) |
558 |
to:final |
|
559 |
with:aCollection |
|
560 |
startingAt:1. |
|
1 | 561 |
|
17181 | 562 |
position := position + nMore. |
19643 | 563 |
(position > readLimit) ifTrue:[readLimit := position]. |
12130 | 564 |
"/ ^ aCollection -- self |
13628 | 565 |
|
566 |
"Modified: / 04-09-2011 / 20:03:32 / cg" |
|
1539
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
567 |
! |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
568 |
|
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
569 |
nextPutAll:aCollection startingAt:pos1 to:pos2 |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
570 |
"append some elements of the argument, aCollection to the stream. |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
571 |
Redefined to avoid count grows of the underlying collection - |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
572 |
instead a single grow on the final size is performed." |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
573 |
|
17181 | 574 |
|nMore final| |
1539
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
575 |
|
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
576 |
collection isNil ifTrue:[ |
19643 | 577 |
^ super nextPutAll:aCollection startingAt:pos1 to:pos2 |
1539
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
578 |
]. |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
579 |
|
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
580 |
nMore := pos2 - pos1 + 1. |
17181 | 581 |
final := position + nMore. |
1539
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
582 |
(writeLimit notNil |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
583 |
and:[final > writeLimit]) ifTrue:[ |
19643 | 584 |
final := writeLimit. |
585 |
nMore := final - position |
|
1539
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
586 |
]. |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
587 |
(final > collection size) ifTrue:[ |
19643 | 588 |
self growCollection:final |
1539
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
589 |
]. |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
590 |
|
17181 | 591 |
collection replaceFrom:position + 1 |
19643 | 592 |
to:final |
593 |
with:aCollection |
|
594 |
startingAt:pos1. |
|
1539
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
595 |
|
17181 | 596 |
position := position + nMore. |
19643 | 597 |
(position > readLimit) ifTrue:[readLimit := position]. |
12131 | 598 |
"/ ^ aCollection -- return self |
1539
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
599 |
|
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
600 |
" |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
601 |
|s| |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
602 |
|
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
603 |
s := '' writeStream. |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
604 |
s nextPutAll:'hello '. |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
605 |
s nextPutAll:'1234world012345' startingAt:5 to:9. |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
606 |
s contents |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
607 |
" |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
608 |
|
1541 | 609 |
"Modified: 12.7.1996 / 10:31:36 / cg" |
8361 | 610 |
! |
611 |
||
11324 | 612 |
nextPutAllUnicode:aString |
613 |
"normal streams can not handle multi-byte characters, so convert them to utf8" |
|
614 |
||
15653 | 615 |
"this code is not perfect if you use both #nextPutAll: and #nextPutAllUnicode: |
18294 | 616 |
with the same stream, since 8-bit characters (with the highest bits set) |
15653 | 617 |
are not stored as UTF, so we get some inconsistent string" |
618 |
||
619 |
collection isString ifTrue:[ |
|
18294 | 620 |
collection bitsPerCharacter == 16 ifTrue:[ |
621 |
self nextPutAllUtf16:aString. |
|
622 |
] ifFalse:[ |
|
623 |
self nextPutAllUtf8:aString. |
|
624 |
]. |
|
11324 | 625 |
] ifFalse:[ |
18294 | 626 |
self nextPutAll:aString |
11324 | 627 |
]. |
13738
e11fccebb0c3
changed: #nextPutAllUnicode:
Claus Gittinger <cg@exept.de>
parents:
13628
diff
changeset
|
628 |
|
e11fccebb0c3
changed: #nextPutAllUnicode:
Claus Gittinger <cg@exept.de>
parents:
13628
diff
changeset
|
629 |
"Modified: / 28-09-2011 / 16:15:52 / cg" |
11324 | 630 |
! |
631 |
||
8615 | 632 |
nextPutByte:anObject |
633 |
"append the argument, anObject to the stream. |
|
634 |
Specially tuned for appending to String and ByteArray streams." |
|
635 |
||
636 |
%{ /* NOCONTEXT */ |
|
637 |
||
638 |
#ifndef NO_PRIM_STREAM |
|
19828 | 639 |
OBJ coll = __INST(collection); |
640 |
OBJ p = __INST(position); |
|
8615 | 641 |
|
642 |
if (__isNonNilObject(coll) && __isSmallInteger(p) && __isSmallInteger(anObject)) { |
|
19828 | 643 |
OBJ wL = __INST(writeLimit); |
644 |
INT pos = __intVal(p) + 1; /* make 1-based and usable for update below */ |
|
645 |
unsigned int ch = __intVal(anObject); |
|
8615 | 646 |
|
19828 | 647 |
if (ch <= 0xFF && /* ch is unsigned */ |
648 |
((wL == nil) || (__isSmallInteger(wL) && (pos <= __intVal(wL))))) { |
|
649 |
OBJ cls = __qClass(coll); |
|
650 |
OBJ rL = __INST(readLimit); |
|
651 |
INT __readLimit = -1; |
|
8615 | 652 |
|
19643 | 653 |
if (__isSmallInteger(rL)) { |
654 |
__readLimit = __intVal(rL); |
|
655 |
} |
|
8615 | 656 |
|
19643 | 657 |
if (cls == @global(String)) { |
19828 | 658 |
if (pos <= __stringSize(coll)) { |
19643 | 659 |
__StringInstPtr(coll)->s_element[pos-1] = ch; |
18294 | 660 |
advancePositionAndReturn: ; |
19643 | 661 |
if ((__readLimit >= 0) && (pos > __readLimit)) { |
662 |
__INST(readLimit) = __mkSmallInteger(pos); |
|
663 |
} |
|
664 |
__INST(position) = __mkSmallInteger(pos); |
|
665 |
RETURN ( anObject ); |
|
666 |
} |
|
667 |
} else if (cls == @global(ByteArray)) { |
|
19828 | 668 |
if (pos <= __byteArraySize(coll)) { |
19643 | 669 |
__ByteArrayInstPtr(coll)->ba_element[pos-1] = ch; |
670 |
goto advancePositionAndReturn; |
|
671 |
} |
|
672 |
} |
|
673 |
} |
|
8615 | 674 |
} |
675 |
#endif |
|
676 |
%}. |
|
19828 | 677 |
((writeLimit isNil or:[(position + 1) <= writeLimit]) |
678 |
and:[position >= collection size]) ifTrue:[ |
|
679 |
self growCollection. |
|
680 |
^ self nextPutByte:anObject. "try again" |
|
681 |
] ifFalse:[ |
|
682 |
^ super nextPutByte:anObject |
|
683 |
]. |
|
8615 | 684 |
! |
685 |
||
8361 | 686 |
nextPutBytes:count from:anObject startingAt:start |
687 |
"write count bytes from an object starting at index start. |
|
688 |
Return the number of bytes written. |
|
18294 | 689 |
The object must have non-pointer indexed instvars |
690 |
(i.e. be a ByteArray, String, Float- or DoubleArray). |
|
8361 | 691 |
Use with care - non object oriented i/o. |
692 |
This is provided for compatibility with externalStream; |
|
693 |
to support binary storage" |
|
694 |
||
11794
ad5fcc151a09
#on: - set readLimit to 0, so that setToEnd positions to end of written
Stefan Vogel <sv@exept.de>
parents:
11324
diff
changeset
|
695 |
anObject isByteCollection ifTrue:[ |
18294 | 696 |
self nextPutAll:anObject startingAt:start to:(start + count - 1). |
697 |
^ count. |
|
8361 | 698 |
]. |
699 |
^ super nextPutBytes:count from:anObject startingAt:start |
|
11324 | 700 |
! |
701 |
||
702 |
nextPutUnicode:aCharacter |
|
703 |
"normal streams can not handle multi-byte characters, so convert them to utf8" |
|
704 |
||
15653 | 705 |
"this code is not perfect if you use both #nextPut: and #nextPutUnicode: |
18294 | 706 |
with the same stream, since 8-bit characters (with the highest bits set) |
15653 | 707 |
are not stored as UTF, so we get some inconsistent string" |
708 |
||
709 |
collection isString ifTrue:[ |
|
18294 | 710 |
collection bitsPerCharacter == 16 ifTrue:[ |
711 |
self nextPutUtf16:aCharacter. |
|
712 |
] ifFalse:[ |
|
713 |
self nextPutUtf8:aCharacter. |
|
714 |
]. |
|
11324 | 715 |
] ifFalse:[ |
18294 | 716 |
self nextPut:aCharacter. |
11324 | 717 |
]. |
1 | 718 |
! ! |
719 |
||
1965 | 720 |
!WriteStream class methodsFor:'documentation'! |
701 | 721 |
|
722 |
version |
|
19437 | 723 |
^ '$Header$' |
12130 | 724 |
! |
725 |
||
726 |
version_CVS |
|
19437 | 727 |
^ '$Header$' |
701 | 728 |
! ! |
19437 | 729 |