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