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