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