author | Claus Gittinger <cg@exept.de> |
Sat, 11 Nov 1995 16:28:41 +0100 | |
changeset 530 | 07d0bce293c9 |
parent 427 | 326b2c263dae |
child 593 | 19d568779cf7 |
permissions | -rw-r--r-- |
1 | 1 |
" |
5 | 2 |
COPYRIGHT (c) 1989 by Claus Gittinger |
216 | 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 |
" |
|
12 |
||
13 |
Object subclass:#Stream |
|
14 |
instanceVariableNames:'' |
|
15 |
classVariableNames:'' |
|
16 |
poolDictionaries:'' |
|
17 |
category:'Streams' |
|
18 |
! |
|
19 |
||
88 | 20 |
!Stream class methodsFor:'documentation'! |
21 |
||
22 |
copyright |
|
23 |
" |
|
24 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
216 | 25 |
All Rights Reserved |
88 | 26 |
|
27 |
This software is furnished under a license and may be used |
|
28 |
only in accordance with the terms of that license and with the |
|
29 |
inclusion of the above copyright notice. This software may not |
|
30 |
be provided or otherwise made available to, or used by, any |
|
31 |
other person. No title to or ownership of the software is |
|
32 |
hereby transferred. |
|
33 |
" |
|
34 |
! |
|
35 |
||
36 |
version |
|
530
07d0bce293c9
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
427
diff
changeset
|
37 |
^ '$Header: /cvs/stx/stx/libbasic/Stream.st,v 1.24 1995-11-11 15:27:34 cg Exp $' |
88 | 38 |
! |
39 |
||
40 |
documentation |
|
41 |
" |
|
42 |
An abstract class defining common behavior for all stream-like objects. |
|
43 |
See concrete subclasses for more detail. |
|
345 | 44 |
|
45 |
Subclasses should (at least) implement: |
|
46 |
#next (if readable) |
|
47 |
#nextPut: (if writable) |
|
48 |
#contents |
|
49 |
#atEnd |
|
50 |
#isReadable |
|
51 |
#isWritable |
|
88 | 52 |
" |
53 |
! ! |
|
1 | 54 |
|
55 |
!Stream class methodsFor:'instance creation'! |
|
56 |
||
57 |
new |
|
58 |
"report an error - Streams are created using on:-messages" |
|
59 |
||
60 |
^ self error:'Streams cannot be created with new' |
|
61 |
! ! |
|
62 |
||
63 |
!Stream methodsFor:'accessing'! |
|
64 |
||
65 |
contents |
|
62 | 66 |
"return the entire contents of the stream |
1 | 67 |
- we do not know here how to do it, it must be redefined in subclass" |
68 |
||
69 |
^ self subclassResponsibility |
|
70 |
! ! |
|
71 |
||
62 | 72 |
!Stream methodsFor:'reading'! |
1 | 73 |
|
74 |
next |
|
75 |
"return the next element of the stream |
|
76 |
- we do not know here how to do it, it must be redefined in subclass" |
|
77 |
||
78 |
^ self subclassResponsibility |
|
79 |
! |
|
80 |
||
81 |
next:count |
|
62 | 82 |
"return the next count elements of the stream as a Collection." |
1 | 83 |
|
329 | 84 |
|answerStream |
285 | 85 |
cnt "{ Class: SmallInteger }" | |
1 | 86 |
|
329 | 87 |
answerStream := WriteStream on:(self contentsSpecies new). |
285 | 88 |
cnt := count. |
89 |
1 to:cnt do:[:index | |
|
329 | 90 |
answerStream nextPut:(self next) |
1 | 91 |
]. |
329 | 92 |
^ answerStream contents |
1 | 93 |
|
275 | 94 |
" |
95 |
(ReadStream on:#(1 2 3 4 5)) next:3 |
|
96 |
(ReadStream on:'hello') next:3 |
|
97 |
" |
|
1 | 98 |
! |
99 |
||
285 | 100 |
nextAvailable:count |
101 |
"return the next count elements of the stream as a Collection. |
|
102 |
If the stream reaches the end before count elements have been read, |
|
103 |
return what is available. (i.e. a shorter collection)" |
|
104 |
||
345 | 105 |
|answerStream |
285 | 106 |
cnt "{ Class: SmallInteger }"| |
107 |
||
329 | 108 |
answerStream := WriteStream on:(self contentsSpecies new). |
285 | 109 |
cnt := count. |
110 |
1 to:cnt do:[:index | |
|
111 |
self atEnd ifTrue:[ |
|
329 | 112 |
^ answerStream contents |
285 | 113 |
]. |
329 | 114 |
answerStream nextPut:(self next) |
285 | 115 |
]. |
329 | 116 |
^ answerStream contents |
285 | 117 |
|
118 |
" |
|
119 |
(ReadStream on:#(1 2 3 4 5)) nextAvailable:3 |
|
120 |
(ReadStream on:#(1 2 3 4 5)) nextAvailable:10 |
|
121 |
(ReadStream on:'hello') nextAvailable:3 |
|
122 |
(ReadStream on:'hello') nextAvailable:10 |
|
123 |
" |
|
124 |
! |
|
125 |
||
1 | 126 |
nextMatchFor:anObject |
62 | 127 |
"read an element from the receiver, return true if it was equal to |
128 |
the argument, anObject" |
|
129 |
||
130 |
^ (self next = anObject) |
|
131 |
||
132 |
" |
|
133 |
|s| |
|
134 |
s := ReadStream on:#(1 2 3 4 5 6 7 8). |
|
135 |
s nextMatchFor:2 |
|
136 |
" |
|
137 |
" |
|
138 |
|s| |
|
139 |
s := ReadStream on:#(1 2 3 4 5 6 7 8). |
|
140 |
s nextMatchFor:2. |
|
141 |
s nextMatchFor:2 |
|
142 |
" |
|
143 |
! |
|
144 |
||
145 |
skip:count |
|
146 |
"skip count objects, return the receiver" |
|
147 |
||
345 | 148 |
|n "{ Class: SmallInteger }"| |
149 |
||
150 |
n := count. |
|
151 |
||
62 | 152 |
"dont know how to unread ..." |
345 | 153 |
n < 0 ifTrue:[ |
216 | 154 |
^ self error:'stream is not positionable' |
62 | 155 |
]. |
345 | 156 |
n timesRepeat:self next |
62 | 157 |
|
158 |
" |
|
159 |
|s| |
|
160 |
s := ReadStream on:#(1 2 3 4 5 6 7 8). |
|
161 |
s skip:4. |
|
162 |
s next |
|
163 |
" |
|
164 |
! |
|
165 |
||
166 |
skipFor:anObject |
|
167 |
"skip all objects up-to and including anObject; return the element after anObject." |
|
168 |
||
169 |
(self skipThrough:anObject) notNil ifTrue:[ |
|
216 | 170 |
^ self next |
62 | 171 |
]. |
172 |
^ nil |
|
173 |
||
174 |
" |
|
175 |
|s| |
|
176 |
s := ReadStream on:#(1 2 3 4 5 6 7 8). |
|
177 |
s skipFor:4. |
|
178 |
" |
|
179 |
! |
|
180 |
||
181 |
skipThrough:anObject |
|
77 | 182 |
"skip all objects up-to and including anObject. Return the receiver if |
183 |
skip was successfull, otherwise (i.e. if not found) return nil. |
|
62 | 184 |
The next read operation will return the element after anObject." |
1 | 185 |
|
186 |
|nextElement| |
|
187 |
||
188 |
[self atEnd] whileFalse:[ |
|
216 | 189 |
nextElement := self next. |
190 |
(nextElement = anObject) ifTrue: [ ^ self ] |
|
1 | 191 |
]. |
192 |
^ nil |
|
2 | 193 |
|
62 | 194 |
" |
195 |
|s| |
|
196 |
s := ReadStream on:#(1 2 3 4 5 6 7 8). |
|
197 |
s skipThrough:4. |
|
198 |
s next |
|
199 |
" |
|
77 | 200 |
! |
201 |
||
427 | 202 |
upTo:anObject |
203 |
"read a collection of all objects up-to anObject and return these |
|
204 |
elements, but excluding anObject. |
|
205 |
The next read operation will return the element after anObject. |
|
206 |
Compare this with #through: which also reads up to some object |
|
207 |
and also positions behind it, but DOES include it in the returned |
|
208 |
value." |
|
209 |
||
210 |
|answerStream element| |
|
211 |
||
212 |
answerStream := WriteStream on:(self contentsSpecies new). |
|
213 |
[self atEnd] whileFalse:[ |
|
214 |
element := self next. |
|
215 |
(element = anObject) ifTrue: [ |
|
216 |
^ answerStream contents |
|
217 |
]. |
|
218 |
answerStream nextPut: element. |
|
219 |
]. |
|
220 |
^ answerStream contents |
|
221 |
||
222 |
" |
|
223 |
|s| |
|
224 |
s := ReadStream on:#(1 2 3 4 5 6 7 8). |
|
225 |
Transcript showCr:(s upTo:4). |
|
226 |
Transcript showCr:s next |
|
227 |
||
228 |
|s| |
|
229 |
s := ReadStream on:'hello world'. |
|
230 |
Transcript showCr:(s upTo:Character space). |
|
231 |
Transcript showCr:(s upToEnd) |
|
232 |
||
233 |
(ReadStream on:'12345678905') upTo:$5; upTo:$5 |
|
234 |
||
235 |
(ReadStream on:'123456') upTo:$7 |
|
236 |
||
237 |
(ReadStream on:#(1 2 3 4 5 6)) upTo:4 |
|
238 |
||
239 |
(ReadStream on:'line 1 |
|
240 |
line 2') upTo:Character cr |
|
241 |
||
242 |
'Makefile' asFilename readStream upTo:Character cr;upTo:Character cr |
|
243 |
" |
|
244 |
! |
|
245 |
||
77 | 246 |
upToEnd |
247 |
"return a collection of the elements up-to the end. |
|
248 |
Return nil if the stream-end is reached before." |
|
249 |
||
250 |
|answerStream| |
|
251 |
||
252 |
answerStream := WriteStream on:(self contentsSpecies new). |
|
253 |
[self atEnd] whileFalse:[ |
|
216 | 254 |
answerStream nextPut:(self next) |
77 | 255 |
]. |
256 |
^ answerStream contents |
|
257 |
||
275 | 258 |
" |
259 |
(ReadStream on:'1234567890') upToEnd |
|
260 |
('123456' readStream) next; next; upToEnd |
|
427 | 261 |
('1 23456' readStream) upTo:Character space; upToEnd |
275 | 262 |
" |
77 | 263 |
! |
264 |
||
265 |
through:anObject |
|
427 | 266 |
"read a collection of all objects up-to anObject and return these |
267 |
elements, including anObject. |
|
268 |
The next read operation will return the element after anObject. |
|
269 |
Compare this with #upTo: which also reads up to some object |
|
270 |
and also positions behind it, but does not include it in the returned |
|
271 |
value." |
|
77 | 272 |
|
273 |
|answerStream element| |
|
274 |
||
275 |
answerStream := WriteStream on:(self contentsSpecies new). |
|
276 |
[self atEnd] whileFalse:[ |
|
216 | 277 |
element := self next. |
427 | 278 |
answerStream nextPut:element. |
216 | 279 |
(element = anObject) ifTrue: [ |
280 |
^ answerStream contents |
|
281 |
] |
|
77 | 282 |
]. |
283 |
^ answerStream contents |
|
284 |
||
285 |
" |
|
286 |
|s| |
|
287 |
s := ReadStream on:#(1 2 3 4 5 6 7 8). |
|
427 | 288 |
Transcript showCr:(s through:4). |
289 |
Transcript showCr:s next |
|
77 | 290 |
" |
291 |
" |
|
292 |
|s| |
|
293 |
s := ReadStream on:'hello world'. |
|
427 | 294 |
Transcript showCr:(s through:Character space). |
295 |
Transcript showCr:(s upToEnd) |
|
77 | 296 |
" |
1 | 297 |
! ! |
298 |
||
62 | 299 |
!Stream methodsFor:'writing'! |
1 | 300 |
|
339 | 301 |
commit |
302 |
"write out all unbuffered data - ignored here, but added |
|
303 |
to make internalStreams protocol compatible with externalStreams" |
|
304 |
||
305 |
! |
|
306 |
||
1 | 307 |
nextPut:anObject |
308 |
"put the argument, anObject onto the receiver |
|
309 |
- we do not know here how to do it, it must be redefined in subclass" |
|
310 |
||
311 |
^ self subclassResponsibility |
|
312 |
! |
|
313 |
||
314 |
next:count put:anObject |
|
315 |
"put the argument, anObject count times onto the receiver" |
|
316 |
||
345 | 317 |
|n "{ Class: SmallInteger }"| |
318 |
||
319 |
n := count. |
|
320 |
n timesRepeat:[self nextPut:anObject]. |
|
1 | 321 |
^ anObject |
62 | 322 |
|
323 |
" |
|
324 |
|s| |
|
325 |
||
326 |
s := WriteStream on:#(). |
|
327 |
s nextPut:1. |
|
328 |
s next:5 put:2. |
|
329 |
s nextPut:3. |
|
330 |
s contents |
|
331 |
" |
|
1 | 332 |
! |
333 |
||
334 |
nextPutAll:aCollection |
|
335 |
"put all elements of the argument, aCollection onto the receiver" |
|
336 |
||
337 |
aCollection do:[:element | |
|
216 | 338 |
self nextPut:element |
1 | 339 |
]. |
340 |
^ aCollection |
|
62 | 341 |
|
342 |
" |
|
343 |
|s| |
|
344 |
||
345 |
s := WriteStream on:#(). |
|
346 |
s nextPutAll:(1 to:5). |
|
347 |
s nextPutAll:#('one' 'two' 'three'). |
|
348 |
s contents |
|
349 |
" |
|
350 |
" |
|
351 |
|s| |
|
352 |
||
353 |
s := WriteStream on:(String new). |
|
354 |
s nextPutAll:($a to:$f). |
|
355 |
s nextPutAll:'one '; |
|
356 |
nextPutAll:'two '; |
|
357 |
nextPutAll:'three'. |
|
358 |
s contents |
|
359 |
" |
|
1 | 360 |
! |
361 |
||
362 |
nextPutAll:aCollection startingAt:first to:last |
|
363 |
"put the elements with index from first to last |
|
364 |
of the argument, aCollection onto the receiver" |
|
365 |
||
366 |
aCollection from:first to:last do:[:element | |
|
216 | 367 |
self nextPut:element |
1 | 368 |
]. |
369 |
^ aCollection |
|
62 | 370 |
|
371 |
" |
|
372 |
|s| |
|
373 |
||
374 |
s := WriteStream on:#(). |
|
375 |
s nextPutAll:#('one' 'two' 'three' 'four' 'five') startingAt:2 to:4. |
|
376 |
s contents |
|
377 |
" |
|
1 | 378 |
! |
379 |
||
2 | 380 |
endEntry |
62 | 381 |
"ignored here - for compatibility with Transcript" |
2 | 382 |
|
383 |
^ self |
|
384 |
! |
|
385 |
||
1 | 386 |
show:aString |
387 |
"put all elements of the argument, aString onto the receiver; |
|
388 |
this makes streams somewhat compatible to TextCollectors and |
|
389 |
allows you to say: Smalltalk at:#Transcript put:Stdout" |
|
390 |
||
391 |
^ self nextPutAll:aString printString |
|
392 |
! |
|
393 |
||
394 |
showCr:aString |
|
395 |
"put all elements of the argument, aString onto the receiver; |
|
396 |
and append a newline. |
|
62 | 397 |
For compatibility with TextCollectors" |
1 | 398 |
|
399 |
self show:aString. |
|
400 |
self cr |
|
62 | 401 |
! |
402 |
||
77 | 403 |
print:anObject |
404 |
"append a printed representation of anObject to the receiver. |
|
405 |
Same as anObject printOn:self; For ST-80 compatibility." |
|
406 |
||
407 |
anObject printOn:self |
|
408 |
! |
|
409 |
||
410 |
store:anObject |
|
411 |
"append a printed representation of anObject to the receiver, |
|
412 |
from which the receiver can be reconstructed. |
|
413 |
Same as anObject storeOn:self; For ST-80 compatibility." |
|
414 |
||
415 |
anObject storeOn:self |
|
416 |
! |
|
417 |
||
62 | 418 |
cr |
419 |
"append a carriage-return to the stream" |
|
420 |
||
421 |
self nextPut:(Character cr) |
|
422 |
! |
|
423 |
||
424 |
tab |
|
425 |
"append a tab-character to the stream" |
|
426 |
||
427 |
self nextPut:(Character tab) |
|
428 |
! |
|
429 |
||
430 |
crTab |
|
345 | 431 |
"append a carriage-return followed by a tab to the stream. |
432 |
Same as crtab for ST/X backward compatibility" |
|
433 |
||
434 |
self obsoleteMethodWarning:'use crTab'. |
|
435 |
self crtab |
|
436 |
! |
|
437 |
||
438 |
crtab:n |
|
439 |
"append a carriage-return followed by n tabs to the stream." |
|
62 | 440 |
|
441 |
self nextPut:(Character cr). |
|
345 | 442 |
self next:n put:(Character tab) |
62 | 443 |
! |
444 |
||
77 | 445 |
crtab |
345 | 446 |
"append a carriage-return followed by a tab to the stream." |
77 | 447 |
|
345 | 448 |
self crtab:1 |
77 | 449 |
! |
450 |
||
62 | 451 |
space |
452 |
"append a space character to the receiver-stream" |
|
453 |
||
454 |
self nextPut:(Character space) |
|
455 |
! |
|
456 |
||
457 |
spaces:count |
|
458 |
"append count space-characters to the receiver-stream" |
|
459 |
||
460 |
self next:count put:(Character space) |
|
461 |
! |
|
462 |
||
463 |
ff |
|
464 |
"append a form-feed (new-pagee) to the receiver-stream" |
|
465 |
||
466 |
self nextPut:(Character ff) |
|
1 | 467 |
! ! |
468 |
||
77 | 469 |
!Stream methodsFor:'non homogenous writing'! |
470 |
||
471 |
nextPutWord:aNumber |
|
472 |
"write the argument, aNumber as a signed short (two bytes); |
|
473 |
write msb-first for compatibility with other smalltalks. |
|
474 |
The receiver must support writing of binary bytes. |
|
475 |
I dont know if it should be named nextPutWord: or nextWordPut:; |
|
476 |
one of them will vanish ..." |
|
477 |
||
216 | 478 |
^ self nextPutShort:aNumber MSB:true |
77 | 479 |
! |
480 |
||
481 |
nextWordPut:aNumber |
|
308 | 482 |
"for ST-80 compatibility" |
77 | 483 |
|
484 |
^ self nextPutShort:aNumber MSB:true |
|
485 |
! |
|
486 |
||
487 |
nextShortPut:aNumber MSB:msbFlag |
|
488 |
"for compatibility - this will vanish" |
|
489 |
||
490 |
^ self nextPutShort:aNumber MSB:msbFlag |
|
491 |
! |
|
492 |
||
493 |
nextPutShort:aNumber MSB:msbFlag |
|
494 |
"Write the argument, aNumber as a short (two bytes). If msbFlag is |
|
495 |
true, data is written most-significant byte first; otherwise least |
|
496 |
first. Returns the receiver on ok, nil on error. |
|
497 |
The receiver must support writing of binary bytes." |
|
498 |
||
499 |
msbFlag ifTrue:[ |
|
216 | 500 |
"most significant first" |
275 | 501 |
self nextPut:(aNumber bitShift:-8). |
502 |
^ self nextPut:(aNumber bitAnd:16rFF). |
|
77 | 503 |
]. |
504 |
"least significant first" |
|
275 | 505 |
self nextPut:(aNumber bitAnd:16rFF). |
506 |
self nextPut:(aNumber bitShift:-8). |
|
77 | 507 |
! |
508 |
||
509 |
nextPutLong:aNumber MSB:msbFlag |
|
510 |
"Write the argument, aNumber as a long (four bytes). If msbFlag is |
|
511 |
true, data is written most-significant byte first; otherwise least |
|
512 |
first. Returns the receiver on ok, nil on error. |
|
513 |
The receiver must support writing of binary bytes." |
|
514 |
||
515 |
msbFlag ifTrue:[ |
|
216 | 516 |
"high word first" |
517 |
self nextShortPut:(aNumber // 16r10000) MSB:true. |
|
518 |
^ self nextShortPut:(aNumber \\ 16r10000) MSB:true |
|
77 | 519 |
]. |
520 |
"low word first" |
|
521 |
self nextShortPut:(aNumber \\ 16r10000) MSB:false. |
|
522 |
^ self nextShortPut:(aNumber // 16r10000) MSB:false. |
|
523 |
! |
|
524 |
||
308 | 525 |
nextLongPut:aNumber |
526 |
"for ST-80 compatibility" |
|
527 |
||
528 |
^ self nextPutLong:aNumber MSB:true |
|
529 |
! |
|
530 |
||
77 | 531 |
nextNumber:n put:v |
532 |
"Append to the receiver the argument, v, which is a positive Integer, |
|
533 |
as the next n bytes. Bytes are written msb first. |
|
534 |
Possibly pad with leading zeros. |
|
535 |
The receiver must support writing of binary bytes." |
|
536 |
||
275 | 537 |
|vlen i| |
77 | 538 |
|
539 |
"claus: this method is central in binaryStorage - |
|
540 |
therefore it has been tuned a bit (and needs even more tuning)" |
|
541 |
||
216 | 542 |
v class == SmallInteger ifTrue:[ "- this is a hint to stc" |
543 |
n == 1 ifTrue:[ |
|
544 |
(v between:0 and:16rFF) ifTrue:[ |
|
545 |
self nextPut:v. |
|
546 |
^ self |
|
547 |
]. |
|
548 |
]. |
|
549 |
n == 2 ifTrue:[ |
|
550 |
(v between:0 and:16rFFFF) ifTrue:[ |
|
551 |
self nextPut:(v bitShift:-8). |
|
552 |
self nextPut:(v bitAnd:16rFF). |
|
553 |
^ self |
|
554 |
]. |
|
555 |
]. |
|
556 |
n == 3 ifTrue:[ |
|
557 |
(v between:0 and:16rFFFFFF) ifTrue:[ |
|
558 |
self nextPut:((v bitShift:-16) bitAnd:16rFF). |
|
559 |
self nextPut:((v bitShift:-8) bitAnd:16rFF). |
|
560 |
self nextPut:(v bitAnd:16rFF). |
|
561 |
^ self |
|
562 |
]. |
|
563 |
]. |
|
564 |
n == 4 ifTrue:[ |
|
565 |
(v >= 0) ifTrue:[ |
|
566 |
self nextPut:((v bitShift:-24) bitAnd:16rFF). |
|
567 |
self nextPut:((v bitShift:-16) bitAnd:16rFF). |
|
568 |
self nextPut:((v bitShift:-8) bitAnd:16rFF). |
|
569 |
self nextPut:(v bitAnd:16rFF). |
|
570 |
^ self |
|
571 |
]. |
|
572 |
]. |
|
77 | 573 |
]. |
574 |
||
275 | 575 |
" |
576 |
arbitrary long |
|
577 |
" |
|
77 | 578 |
n < (vlen := v digitLength) ifTrue: [ |
275 | 579 |
" |
580 |
the number is too big to be repesented in n bytes |
|
581 |
" |
|
216 | 582 |
self error: 'number too big' |
77 | 583 |
]. |
584 |
||
585 |
"pad with leading zeros" |
|
586 |
i := n. |
|
587 |
[i > vlen] whileTrue: [ |
|
216 | 588 |
self nextPut: 0. |
589 |
i := i - 1 |
|
77 | 590 |
]. |
591 |
i = 1 ifTrue: [ |
|
216 | 592 |
^ self nextPut: v |
77 | 593 |
]. |
594 |
[i > 0] whileTrue: [ |
|
216 | 595 |
self nextPut: (v digitAt: i). |
596 |
i := i - 1 |
|
77 | 597 |
] |
598 |
! ! |
|
599 |
||
600 |
!Stream methodsFor:'non homogenous reading'! |
|
601 |
||
602 |
nextShortMSB:msbFlag |
|
216 | 603 |
"return a signed short (2 bytes) from the stream. |
604 |
The receiver must support reading of binary bytes." |
|
605 |
||
606 |
|uval "{ Class: SmallInteger }"| |
|
77 | 607 |
|
216 | 608 |
msbFlag ifTrue:[ |
609 |
"most significant first" |
|
610 |
uval := self next bitShift:8. |
|
611 |
uval := uval bitOr:(self next). |
|
612 |
] ifFalse:[ |
|
613 |
"least significant first" |
|
614 |
uval := self next. |
|
615 |
uval := uval bitOr:(self next bitShift:8). |
|
616 |
]. |
|
275 | 617 |
"change from unsigned 0..FFFF to signed -8000..7FFF" |
216 | 618 |
uval >= 16r8000 ifTrue:[ |
619 |
^ uval - 16r10000 |
|
620 |
]. |
|
621 |
^ uval |
|
77 | 622 |
! |
623 |
||
624 |
nextUnsignedShortMSB:msbFlag |
|
216 | 625 |
"return an unsigned short (2 bytes) from the stream. |
626 |
The receiver must support reading of binary bytes." |
|
77 | 627 |
|
628 |
msbFlag ifTrue:[ |
|
216 | 629 |
^ (self next bitShift:8) bitOr:(self next) |
77 | 630 |
]. |
631 |
^ (self next) bitOr:(self next bitShift:8) |
|
632 |
! |
|
633 |
||
634 |
nextLongMSB:msbFlag |
|
216 | 635 |
"return a signed long (4 bytes) from the stream. |
636 |
The receiver must support reading of binary bytes." |
|
637 |
||
638 |
|high uval "{ Class: SmallInteger }"| |
|
77 | 639 |
|
216 | 640 |
msbFlag ifTrue:[ |
641 |
"most significant first" |
|
642 |
high := self next. |
|
643 |
uval := self next bitShift:16. |
|
644 |
uval := uval bitOr:(self next bitShift:8). |
|
645 |
uval := uval bitOr:(self next). |
|
646 |
uval := uval bitOr:(high bitShift:24). |
|
647 |
] ifFalse:[ |
|
648 |
"least significant first" |
|
649 |
uval := self next. |
|
650 |
uval := uval bitOr:(self next bitShift:8). |
|
651 |
uval := uval bitOr:(self next bitShift:16). |
|
652 |
uval := uval bitOr:(self next bitShift:24). |
|
653 |
]. |
|
275 | 654 |
"change from unsigned 0..FFFFFFFF to signed -80000000..7FFFFFFF" |
216 | 655 |
|
656 |
"sorry- for now, stc cannot compile LargeInteger constants |
|
657 |
uval >= 16r80000000 ifTrue:[ |
|
658 |
^ uval - 16r100000000 |
|
659 |
]. |
|
660 |
the following kludges around this" |
|
661 |
(uval bitShift:-16) >= 16r8000 ifTrue:[ |
|
662 |
^ uval - (16r10000000 bitShift:8) |
|
663 |
]. |
|
664 |
^ uval |
|
77 | 665 |
! |
666 |
||
667 |
nextUnsignedLongMSB:msbFlag |
|
216 | 668 |
"return an unsigned long (4 bytes) from the stream. |
669 |
The receiver must support reading of binary bytes." |
|
77 | 670 |
|
671 |
|val| |
|
672 |
||
673 |
msbFlag ifTrue:[ |
|
216 | 674 |
val := self next. |
675 |
val := (val bitShift:8) bitOr:(self next). |
|
676 |
val := (val bitShift:8) bitOr:(self next). |
|
677 |
val := (val * 256) + (self next). |
|
678 |
^ val |
|
77 | 679 |
]. |
680 |
val := self next. |
|
216 | 681 |
^ (val bitShift:24) |
77 | 682 |
+ (self next bitShift:16) |
683 |
+ (self next bitShift:8) |
|
684 |
+ self next |
|
685 |
! |
|
686 |
||
687 |
nextNumber:n |
|
688 |
"Return the next n bytes as a positive Integer; bytes are taken msb-first." |
|
689 |
||
345 | 690 |
|s |
691 |
val "{ Class: SmallInteger }" |
|
692 |
rep "{ Class: SmallInteger }"| |
|
77 | 693 |
|
694 |
"claus: this method is central in binaryStorage - |
|
695 |
therefore it has been tuned a bit (and needs even more tuning)" |
|
696 |
||
697 |
n == 1 ifTrue:[ |
|
216 | 698 |
^ self next |
77 | 699 |
]. |
700 |
n == 2 ifTrue:[ |
|
216 | 701 |
val := self next. |
702 |
val := (val bitShift:8) + self next. |
|
703 |
^ val |
|
77 | 704 |
]. |
705 |
n == 3 ifTrue:[ |
|
216 | 706 |
val := self next. |
707 |
val := (val bitShift:8) + self next. |
|
708 |
val := (val bitShift:8) + self next. |
|
709 |
^ val |
|
77 | 710 |
]. |
711 |
n == 4 ifTrue:[ |
|
216 | 712 |
val := self next. |
713 |
val <= 16r3F ifTrue:[ |
|
714 |
val := (val bitShift:8) + self next. |
|
715 |
val := (val bitShift:8) + self next. |
|
716 |
val := (val bitShift:8) + self next. |
|
717 |
^ val |
|
718 |
]. |
|
719 |
"sorry, but need a largeInteger" |
|
275 | 720 |
s := (val bitShift:8) + self next. |
216 | 721 |
s := (s bitShift:8) + self next. |
722 |
"the multiplication below is a temporary workaround for the |
|
723 |
inability of smallintegers to handle shifts out of the 32bit range ..." |
|
724 |
s := (s * 256) + self next. |
|
725 |
^ s |
|
77 | 726 |
]. |
727 |
||
275 | 728 |
" |
729 |
arbitrary long |
|
730 |
" |
|
77 | 731 |
s := 0. |
345 | 732 |
rep := n. |
733 |
rep timesRepeat:[ |
|
216 | 734 |
s := s * 256 + self next |
77 | 735 |
]. |
736 |
^ s truncated |
|
346 | 737 |
! |
738 |
||
739 |
nextWord |
|
740 |
^ self nextAlphaNumericWord |
|
741 |
! |
|
742 |
||
743 |
nextLine |
|
744 |
"for compatibility with externalStreams" |
|
745 |
||
746 |
^ self upTo:Character cr. |
|
77 | 747 |
! ! |
748 |
||
1 | 749 |
!Stream methodsFor:'closing'! |
750 |
||
751 |
close |
|
62 | 752 |
"close the stream - nothing done here. |
753 |
Added for compatibility with external streams." |
|
1 | 754 |
|
755 |
^ self |
|
756 |
! ! |
|
757 |
||
758 |
!Stream methodsFor:'testing'! |
|
759 |
||
760 |
atEnd |
|
761 |
"return true if the end of the stream has been reached; |
|
762 |
- we do not know here how to do it, it must be redefined in subclass" |
|
763 |
||
764 |
^ self subclassResponsibility |
|
765 |
! ! |
|
766 |
||
767 |
!Stream methodsFor:'enumerating'! |
|
768 |
||
769 |
do:aBlock |
|
62 | 770 |
"evaluate the argument, aBlock for all remaining elements, |
771 |
up to the end of the stream" |
|
1 | 772 |
|
773 |
[self atEnd] whileFalse:[ |
|
216 | 774 |
aBlock value:(self next) |
1 | 775 |
] |
62 | 776 |
|
777 |
" |
|
778 |
|s| |
|
779 |
s := ReadStream on:#(1 2 3 4 5 6 7 8 9). |
|
780 |
s next. |
|
781 |
s next. |
|
782 |
s do:[:element | Transcript showCr:element] |
|
783 |
" |
|
784 |
! ! |
|
785 |
||
786 |
!Stream methodsFor:'private'! |
|
787 |
||
788 |
contentsSpecies |
|
70 | 789 |
"this should return the class of which an instance is |
790 |
returned by the #contents method. It is redefinable in subclasses" |
|
62 | 791 |
|
792 |
"return Array here - since the abstract Stream has no idea |
|
793 |
of the underlying collection class" |
|
794 |
||
795 |
^ Array |
|
1 | 796 |
! ! |
797 |
||
335 | 798 |
!Stream methodsFor:'misc'! |
799 |
||
800 |
binary |
|
801 |
"switch to binary mode. ignored here, but added to make |
|
802 |
internalStreams protocol compatible with externStreams." |
|
803 |
||
804 |
! ! |
|
805 |
||
1 | 806 |
!Stream methodsFor:'queries'! |
807 |
||
339 | 808 |
isBinary |
809 |
"return true, if in binary mode. Returns false here, |
|
810 |
to make internalStreams protocol compatible with externStreams." |
|
811 |
||
812 |
^ false |
|
813 |
! |
|
814 |
||
360 | 815 |
isPositionable |
816 |
"return true, if the stream supports positioning (some do not)" |
|
817 |
||
818 |
^ false |
|
819 |
! |
|
820 |
||
1 | 821 |
isStream |
822 |
"return true, if the receiver is some kind of Stream." |
|
823 |
||
824 |
^ true |
|
825 |
! |
|
826 |
||
345 | 827 |
isReadable |
828 |
"return true, if reading is supported by the recevier. |
|
829 |
Assume yes here." |
|
830 |
||
831 |
^ self subclassResponsibility |
|
832 |
! |
|
833 |
||
834 |
isWritable |
|
835 |
"return true, if writing is supported by the recevier. |
|
836 |
Assume yes here." |
|
837 |
||
838 |
^ self subclassResponsibility |
|
839 |
! |
|
840 |
||
1 | 841 |
lineLength |
842 |
"this is just a layout hint for prettyprinting functions |
|
843 |
- for compatibility with TextCollectors" |
|
844 |
||
845 |
^ 80 |
|
846 |
! ! |