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