author | Claus Gittinger <cg@exept.de> |
Tue, 30 Apr 1996 17:39:28 +0200 | |
changeset 1321 | 52e043fb7eaf |
parent 1295 | 83f594f05c52 |
child 1371 | 7bf89ededc78 |
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 |
|
970
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
846
diff
changeset
|
14 |
instanceVariableNames:'signalAtEnd' |
846
4117b296633d
network byte order read/write methods added
Claus Gittinger <cg@exept.de>
parents:
820
diff
changeset
|
15 |
classVariableNames:'StreamErrorSignal PositionErrorSignal ReadErrorSignal |
970
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
846
diff
changeset
|
16 |
WriteErrorSignal EndOfStreamSignal' |
743 | 17 |
poolDictionaries:'' |
18 |
category:'Streams' |
|
1 | 19 |
! |
20 |
||
88 | 21 |
!Stream class methodsFor:'documentation'! |
22 |
||
23 |
copyright |
|
24 |
" |
|
25 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
216 | 26 |
All Rights Reserved |
88 | 27 |
|
28 |
This software is furnished under a license and may be used |
|
29 |
only in accordance with the terms of that license and with the |
|
30 |
inclusion of the above copyright notice. This software may not |
|
31 |
be provided or otherwise made available to, or used by, any |
|
32 |
other person. No title to or ownership of the software is |
|
33 |
hereby transferred. |
|
34 |
" |
|
35 |
! |
|
36 |
||
37 |
documentation |
|
38 |
" |
|
39 |
An abstract class defining common behavior for all stream-like objects. |
|
40 |
See concrete subclasses for more detail. |
|
345 | 41 |
|
42 |
Subclasses should (at least) implement: |
|
1295 | 43 |
#next (if readable) |
44 |
#nextPut: (if writable) |
|
45 |
#contents |
|
46 |
#atEnd |
|
47 |
#isReadable |
|
48 |
#isWritable |
|
49 |
||
50 |
[Class variables:] |
|
51 |
StreamErrorSignal <Signal> parent of all stream errors |
|
593
19d568779cf7
moved StreamErrorSignal into Stream;
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
52 |
|
1295 | 53 |
PositionErrorSignal <Signal> position attemted on a stream |
54 |
which does not support positioning |
|
820
6d934f5d6cbc
moved readErrorSignal / writeErrorSignal up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
743
diff
changeset
|
55 |
|
1295 | 56 |
ReadErrorSignal <Signal> raised on read errors |
820
6d934f5d6cbc
moved readErrorSignal / writeErrorSignal up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
743
diff
changeset
|
57 |
|
1295 | 58 |
WriteErrorSignal <Signal> raised on write errors |
820
6d934f5d6cbc
moved readErrorSignal / writeErrorSignal up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
743
diff
changeset
|
59 |
|
1295 | 60 |
EndOfStreamSignal <Signal> raised at end of stream |
970
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
846
diff
changeset
|
61 |
|
1295 | 62 |
[author:] |
63 |
Claus Gittinger |
|
88 | 64 |
" |
65 |
! ! |
|
1 | 66 |
|
593
19d568779cf7
moved StreamErrorSignal into Stream;
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
67 |
!Stream class methodsFor:'initialization'! |
19d568779cf7
moved StreamErrorSignal into Stream;
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
68 |
|
19d568779cf7
moved StreamErrorSignal into Stream;
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
69 |
initialize |
19d568779cf7
moved StreamErrorSignal into Stream;
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
70 |
StreamErrorSignal isNil ifTrue:[ |
19d568779cf7
moved StreamErrorSignal into Stream;
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
71 |
StreamErrorSignal := ErrorSignal newSignalMayProceed:false. |
19d568779cf7
moved StreamErrorSignal into Stream;
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
72 |
StreamErrorSignal nameClass:self message:#streamErrorSignal. |
19d568779cf7
moved StreamErrorSignal into Stream;
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
73 |
StreamErrorSignal notifierString:'Stream error'. |
19d568779cf7
moved StreamErrorSignal into Stream;
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
74 |
|
19d568779cf7
moved StreamErrorSignal into Stream;
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
75 |
PositionErrorSignal := StreamErrorSignal newSignalMayProceed:true. |
19d568779cf7
moved StreamErrorSignal into Stream;
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
76 |
PositionErrorSignal nameClass:self message:#positionErrorSignal. |
19d568779cf7
moved StreamErrorSignal into Stream;
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
77 |
PositionErrorSignal notifierString:'stream as no concept of a position'. |
820
6d934f5d6cbc
moved readErrorSignal / writeErrorSignal up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
743
diff
changeset
|
78 |
|
6d934f5d6cbc
moved readErrorSignal / writeErrorSignal up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
743
diff
changeset
|
79 |
ReadErrorSignal := StreamErrorSignal newSignalMayProceed:false. |
6d934f5d6cbc
moved readErrorSignal / writeErrorSignal up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
743
diff
changeset
|
80 |
ReadErrorSignal nameClass:self message:#readErrorSignal. |
6d934f5d6cbc
moved readErrorSignal / writeErrorSignal up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
743
diff
changeset
|
81 |
ReadErrorSignal notifierString:'read error'. |
6d934f5d6cbc
moved readErrorSignal / writeErrorSignal up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
743
diff
changeset
|
82 |
|
6d934f5d6cbc
moved readErrorSignal / writeErrorSignal up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
743
diff
changeset
|
83 |
WriteErrorSignal := StreamErrorSignal newSignalMayProceed:false. |
6d934f5d6cbc
moved readErrorSignal / writeErrorSignal up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
743
diff
changeset
|
84 |
WriteErrorSignal nameClass:self message:#writeErrorSignal. |
6d934f5d6cbc
moved readErrorSignal / writeErrorSignal up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
743
diff
changeset
|
85 |
WriteErrorSignal notifierString:'write error'. |
970
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
846
diff
changeset
|
86 |
|
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
846
diff
changeset
|
87 |
EndOfStreamSignal := StreamErrorSignal newSignalMayProceed:false. |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
846
diff
changeset
|
88 |
EndOfStreamSignal nameClass:self message:#endOfStreamSignal. |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
846
diff
changeset
|
89 |
EndOfStreamSignal notifierString:'end of stream'. |
593
19d568779cf7
moved StreamErrorSignal into Stream;
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
90 |
] |
19d568779cf7
moved StreamErrorSignal into Stream;
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
91 |
! ! |
19d568779cf7
moved StreamErrorSignal into Stream;
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
92 |
|
611 | 93 |
!Stream class methodsFor:'instance creation'! |
94 |
||
95 |
new |
|
1119
956d62a5656c
compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents:
1117
diff
changeset
|
96 |
"{ Pragma: +optSpace }" |
956d62a5656c
compile seldom used methods with optSpace (is this a good idea ?)
Claus Gittinger <cg@exept.de>
parents:
1117
diff
changeset
|
97 |
|
611 | 98 |
"report an error - Streams are created using on:-messages" |
99 |
||
100 |
^ self error:'Streams cannot be created with new' |
|
101 |
! ! |
|
102 |
||
593
19d568779cf7
moved StreamErrorSignal into Stream;
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
103 |
!Stream class methodsFor:'Signal constants'! |
19d568779cf7
moved StreamErrorSignal into Stream;
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
104 |
|
970
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
846
diff
changeset
|
105 |
endOfStreamSignal |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
846
diff
changeset
|
106 |
"return the signal raised if read past end of stream is attemted" |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
846
diff
changeset
|
107 |
|
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
846
diff
changeset
|
108 |
^ EndOfStreamSignal |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
846
diff
changeset
|
109 |
! |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
846
diff
changeset
|
110 |
|
611 | 111 |
positionErrorSignal |
112 |
"return the signal raised if positioning is requested for |
|
113 |
a stream which does not support that kind of operation" |
|
114 |
||
115 |
^ PositionErrorSignal |
|
116 |
! |
|
117 |
||
820
6d934f5d6cbc
moved readErrorSignal / writeErrorSignal up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
743
diff
changeset
|
118 |
readErrorSignal |
6d934f5d6cbc
moved readErrorSignal / writeErrorSignal up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
743
diff
changeset
|
119 |
"return the signal raised on read errors" |
6d934f5d6cbc
moved readErrorSignal / writeErrorSignal up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
743
diff
changeset
|
120 |
|
6d934f5d6cbc
moved readErrorSignal / writeErrorSignal up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
743
diff
changeset
|
121 |
^ ReadErrorSignal |
6d934f5d6cbc
moved readErrorSignal / writeErrorSignal up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
743
diff
changeset
|
122 |
! |
6d934f5d6cbc
moved readErrorSignal / writeErrorSignal up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
743
diff
changeset
|
123 |
|
593
19d568779cf7
moved StreamErrorSignal into Stream;
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
124 |
streamErrorSignal |
19d568779cf7
moved StreamErrorSignal into Stream;
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
125 |
"return the parent of all stream errors; |
19d568779cf7
moved StreamErrorSignal into Stream;
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
126 |
handling this one also handles all other errors. |
19d568779cf7
moved StreamErrorSignal into Stream;
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
127 |
Also, this one may be raised for errors not related to read/write |
19d568779cf7
moved StreamErrorSignal into Stream;
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
128 |
operations, such as failed ioctls in externalStream etc." |
19d568779cf7
moved StreamErrorSignal into Stream;
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
129 |
|
19d568779cf7
moved StreamErrorSignal into Stream;
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
130 |
^ StreamErrorSignal |
820
6d934f5d6cbc
moved readErrorSignal / writeErrorSignal up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
743
diff
changeset
|
131 |
! |
6d934f5d6cbc
moved readErrorSignal / writeErrorSignal up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
743
diff
changeset
|
132 |
|
6d934f5d6cbc
moved readErrorSignal / writeErrorSignal up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
743
diff
changeset
|
133 |
writeErrorSignal |
6d934f5d6cbc
moved readErrorSignal / writeErrorSignal up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
743
diff
changeset
|
134 |
"return the signal raised on write errors" |
6d934f5d6cbc
moved readErrorSignal / writeErrorSignal up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
743
diff
changeset
|
135 |
|
6d934f5d6cbc
moved readErrorSignal / writeErrorSignal up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
743
diff
changeset
|
136 |
^ WriteErrorSignal |
1 | 137 |
! ! |
138 |
||
139 |
!Stream methodsFor:'accessing'! |
|
140 |
||
141 |
contents |
|
62 | 142 |
"return the entire contents of the stream |
1 | 143 |
- we do not know here how to do it, it must be redefined in subclass" |
144 |
||
145 |
^ self subclassResponsibility |
|
970
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
846
diff
changeset
|
146 |
! |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
846
diff
changeset
|
147 |
|
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
846
diff
changeset
|
148 |
signalAtEnd |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
846
diff
changeset
|
149 |
"return signalAtEnd" |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
846
diff
changeset
|
150 |
|
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
846
diff
changeset
|
151 |
^ signalAtEnd |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
846
diff
changeset
|
152 |
|
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
846
diff
changeset
|
153 |
"Created: 5.2.1996 / 18:24:53 / stefan" |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
846
diff
changeset
|
154 |
! |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
846
diff
changeset
|
155 |
|
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
846
diff
changeset
|
156 |
signalAtEnd:something |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
846
diff
changeset
|
157 |
"set signalAtEnd" |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
846
diff
changeset
|
158 |
|
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
846
diff
changeset
|
159 |
signalAtEnd := something. |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
846
diff
changeset
|
160 |
|
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
846
diff
changeset
|
161 |
"Created: 5.2.1996 / 18:24:53 / stefan" |
1 | 162 |
! ! |
163 |
||
611 | 164 |
!Stream methodsFor:'closing'! |
165 |
||
166 |
close |
|
167 |
"close the stream - nothing done here. |
|
168 |
Added for compatibility with external streams." |
|
169 |
||
170 |
^ self |
|
171 |
! ! |
|
172 |
||
173 |
!Stream methodsFor:'enumerating'! |
|
174 |
||
175 |
do:aBlock |
|
176 |
"evaluate the argument, aBlock for all remaining elements, |
|
177 |
up to the end of the stream" |
|
178 |
||
179 |
[self atEnd] whileFalse:[ |
|
180 |
aBlock value:(self next) |
|
181 |
] |
|
182 |
||
183 |
" |
|
184 |
|s| |
|
185 |
s := ReadStream on:#(1 2 3 4 5 6 7 8 9). |
|
186 |
s next. |
|
187 |
s next. |
|
188 |
s do:[:element | Transcript showCr:element] |
|
189 |
" |
|
190 |
! ! |
|
191 |
||
970
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
846
diff
changeset
|
192 |
!Stream methodsFor:'error handling'! |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
846
diff
changeset
|
193 |
|
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
846
diff
changeset
|
194 |
pastEnd |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
846
diff
changeset
|
195 |
"someone tried to read after the end of the stream. |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
846
diff
changeset
|
196 |
if signalAtEnd == true, raise a signal. |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
846
diff
changeset
|
197 |
Otherwise return nil." |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
846
diff
changeset
|
198 |
|
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
846
diff
changeset
|
199 |
signalAtEnd == true ifTrue:[ |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
846
diff
changeset
|
200 |
^ EndOfStreamSignal raiseIn:thisContext sender |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
846
diff
changeset
|
201 |
]. |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
846
diff
changeset
|
202 |
^ nil |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
846
diff
changeset
|
203 |
|
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
846
diff
changeset
|
204 |
"Created: 5.2.1996 / 21:56:08 / stefan" |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
846
diff
changeset
|
205 |
! ! |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
846
diff
changeset
|
206 |
|
611 | 207 |
!Stream methodsFor:'misc'! |
208 |
||
209 |
binary |
|
210 |
"switch to binary mode. ignored here, but added to make |
|
211 |
internalStreams protocol compatible with externStreams." |
|
212 |
||
213 |
! ! |
|
214 |
||
215 |
!Stream methodsFor:'non homogenous reading'! |
|
216 |
||
217 |
nextLine |
|
218 |
"for compatibility with externalStreams" |
|
219 |
||
220 |
^ self upTo:Character cr. |
|
221 |
! |
|
222 |
||
223 |
nextLongMSB:msbFlag |
|
224 |
"return a signed long (4 bytes) from the stream. |
|
225 |
The receiver must support reading of binary bytes." |
|
226 |
||
227 |
|high uval "{ Class: SmallInteger }"| |
|
228 |
||
229 |
msbFlag ifTrue:[ |
|
230 |
"most significant first" |
|
231 |
high := self next. |
|
232 |
uval := self next bitShift:16. |
|
233 |
uval := uval bitOr:(self next bitShift:8). |
|
234 |
uval := uval bitOr:(self next). |
|
235 |
uval := uval bitOr:(high bitShift:24). |
|
236 |
] ifFalse:[ |
|
237 |
"least significant first" |
|
238 |
uval := self next. |
|
239 |
uval := uval bitOr:(self next bitShift:8). |
|
240 |
uval := uval bitOr:(self next bitShift:16). |
|
241 |
uval := uval bitOr:(self next bitShift:24). |
|
242 |
]. |
|
243 |
"change from unsigned 0..FFFFFFFF to signed -80000000..7FFFFFFF" |
|
244 |
||
1117
f64e35e18e65
typed variable in nextPutNumber
Claus Gittinger <cg@exept.de>
parents:
970
diff
changeset
|
245 |
"/ sorry- for now (2.7), stc cannot compile LargeInteger constants |
f64e35e18e65
typed variable in nextPutNumber
Claus Gittinger <cg@exept.de>
parents:
970
diff
changeset
|
246 |
"/ uval >= 16r80000000 ifTrue:[ |
f64e35e18e65
typed variable in nextPutNumber
Claus Gittinger <cg@exept.de>
parents:
970
diff
changeset
|
247 |
"/ ^ uval - 16r100000000 |
f64e35e18e65
typed variable in nextPutNumber
Claus Gittinger <cg@exept.de>
parents:
970
diff
changeset
|
248 |
"/ ]. |
f64e35e18e65
typed variable in nextPutNumber
Claus Gittinger <cg@exept.de>
parents:
970
diff
changeset
|
249 |
"/ the following kludges around this |
f64e35e18e65
typed variable in nextPutNumber
Claus Gittinger <cg@exept.de>
parents:
970
diff
changeset
|
250 |
"/ well, new (2.8) can ... |
f64e35e18e65
typed variable in nextPutNumber
Claus Gittinger <cg@exept.de>
parents:
970
diff
changeset
|
251 |
|
611 | 252 |
(uval bitShift:-16) >= 16r8000 ifTrue:[ |
253 |
^ uval - (16r10000000 bitShift:8) |
|
254 |
]. |
|
255 |
^ uval |
|
256 |
! |
|
257 |
||
846
4117b296633d
network byte order read/write methods added
Claus Gittinger <cg@exept.de>
parents:
820
diff
changeset
|
258 |
nextLongNet |
4117b296633d
network byte order read/write methods added
Claus Gittinger <cg@exept.de>
parents:
820
diff
changeset
|
259 |
"return a signed long (4 bytes) in network byte order from the stream. |
4117b296633d
network byte order read/write methods added
Claus Gittinger <cg@exept.de>
parents:
820
diff
changeset
|
260 |
The receiver must support reading of binary bytes." |
4117b296633d
network byte order read/write methods added
Claus Gittinger <cg@exept.de>
parents:
820
diff
changeset
|
261 |
|
4117b296633d
network byte order read/write methods added
Claus Gittinger <cg@exept.de>
parents:
820
diff
changeset
|
262 |
^ self nextLongMSB:(Socket networkLongOrderIsMSB) |
4117b296633d
network byte order read/write methods added
Claus Gittinger <cg@exept.de>
parents:
820
diff
changeset
|
263 |
|
4117b296633d
network byte order read/write methods added
Claus Gittinger <cg@exept.de>
parents:
820
diff
changeset
|
264 |
"Created: 10.1.1996 / 19:49:28 / cg" |
4117b296633d
network byte order read/write methods added
Claus Gittinger <cg@exept.de>
parents:
820
diff
changeset
|
265 |
! |
4117b296633d
network byte order read/write methods added
Claus Gittinger <cg@exept.de>
parents:
820
diff
changeset
|
266 |
|
611 | 267 |
nextNumber:n |
268 |
"Return the next n bytes as a positive Integer; bytes are taken msb-first." |
|
269 |
||
270 |
|s |
|
271 |
val "{ Class: SmallInteger }" |
|
272 |
rep "{ Class: SmallInteger }"| |
|
273 |
||
274 |
"claus: this method is central in binaryStorage - |
|
275 |
therefore it has been tuned a bit (and needs even more tuning)" |
|
276 |
||
277 |
n == 1 ifTrue:[ |
|
278 |
^ self next |
|
279 |
]. |
|
280 |
n == 2 ifTrue:[ |
|
281 |
val := self next. |
|
282 |
val := (val bitShift:8) + self next. |
|
283 |
^ val |
|
284 |
]. |
|
285 |
n == 3 ifTrue:[ |
|
286 |
val := self next. |
|
287 |
val := (val bitShift:8) + self next. |
|
288 |
val := (val bitShift:8) + self next. |
|
289 |
^ val |
|
290 |
]. |
|
291 |
n == 4 ifTrue:[ |
|
292 |
val := self next. |
|
293 |
val <= 16r3F ifTrue:[ |
|
294 |
val := (val bitShift:8) + self next. |
|
295 |
val := (val bitShift:8) + self next. |
|
296 |
val := (val bitShift:8) + self next. |
|
297 |
^ val |
|
298 |
]. |
|
299 |
"sorry, but need a largeInteger" |
|
300 |
s := (val bitShift:8) + self next. |
|
301 |
s := (s bitShift:8) + self next. |
|
302 |
s := (s * 256) + self next. |
|
303 |
^ s |
|
304 |
]. |
|
305 |
||
306 |
" |
|
307 |
arbitrary long |
|
308 |
" |
|
309 |
s := 0. |
|
310 |
rep := n. |
|
311 |
rep timesRepeat:[ |
|
312 |
s := s * 256 + self next |
|
313 |
]. |
|
314 |
^ s truncated |
|
315 |
! |
|
316 |
||
317 |
nextShortMSB:msbFlag |
|
318 |
"return a signed short (2 bytes) from the stream. |
|
319 |
The receiver must support reading of binary bytes." |
|
320 |
||
321 |
|uval "{ Class: SmallInteger }"| |
|
322 |
||
323 |
msbFlag ifTrue:[ |
|
324 |
"most significant first" |
|
325 |
uval := self next bitShift:8. |
|
326 |
uval := uval bitOr:(self next). |
|
327 |
] ifFalse:[ |
|
328 |
"least significant first" |
|
329 |
uval := self next. |
|
330 |
uval := uval bitOr:(self next bitShift:8). |
|
331 |
]. |
|
332 |
"change from unsigned 0..FFFF to signed -8000..7FFF" |
|
333 |
uval >= 16r8000 ifTrue:[ |
|
334 |
^ uval - 16r10000 |
|
335 |
]. |
|
336 |
^ uval |
|
337 |
! |
|
338 |
||
846
4117b296633d
network byte order read/write methods added
Claus Gittinger <cg@exept.de>
parents:
820
diff
changeset
|
339 |
nextShortNet |
4117b296633d
network byte order read/write methods added
Claus Gittinger <cg@exept.de>
parents:
820
diff
changeset
|
340 |
"return a signed short (2 bytes) in network byte order from the stream. |
4117b296633d
network byte order read/write methods added
Claus Gittinger <cg@exept.de>
parents:
820
diff
changeset
|
341 |
The receiver must support reading of binary bytes." |
4117b296633d
network byte order read/write methods added
Claus Gittinger <cg@exept.de>
parents:
820
diff
changeset
|
342 |
|
4117b296633d
network byte order read/write methods added
Claus Gittinger <cg@exept.de>
parents:
820
diff
changeset
|
343 |
^ self nextShortMSB:(Socket networkShortOrderIsMSB) |
4117b296633d
network byte order read/write methods added
Claus Gittinger <cg@exept.de>
parents:
820
diff
changeset
|
344 |
|
4117b296633d
network byte order read/write methods added
Claus Gittinger <cg@exept.de>
parents:
820
diff
changeset
|
345 |
"Modified: 10.1.1996 / 19:48:50 / cg" |
4117b296633d
network byte order read/write methods added
Claus Gittinger <cg@exept.de>
parents:
820
diff
changeset
|
346 |
"Created: 10.1.1996 / 19:49:41 / cg" |
4117b296633d
network byte order read/write methods added
Claus Gittinger <cg@exept.de>
parents:
820
diff
changeset
|
347 |
! |
4117b296633d
network byte order read/write methods added
Claus Gittinger <cg@exept.de>
parents:
820
diff
changeset
|
348 |
|
611 | 349 |
nextUnsignedLongMSB:msbFlag |
350 |
"return an unsigned long (4 bytes) from the stream. |
|
351 |
The receiver must support reading of binary bytes." |
|
352 |
||
353 |
|val| |
|
354 |
||
355 |
msbFlag ifTrue:[ |
|
356 |
val := self next. |
|
357 |
val := (val bitShift:8) bitOr:(self next). |
|
358 |
val := (val bitShift:8) bitOr:(self next). |
|
359 |
val := (val * 256) + (self next). |
|
360 |
^ val |
|
361 |
]. |
|
362 |
val := self next. |
|
363 |
^ (val bitShift:24) |
|
364 |
+ (self next bitShift:16) |
|
365 |
+ (self next bitShift:8) |
|
366 |
+ self next |
|
367 |
! |
|
368 |
||
846
4117b296633d
network byte order read/write methods added
Claus Gittinger <cg@exept.de>
parents:
820
diff
changeset
|
369 |
nextUnsignedLongNet |
4117b296633d
network byte order read/write methods added
Claus Gittinger <cg@exept.de>
parents:
820
diff
changeset
|
370 |
"return an unsigned long (4 bytes) in network byte order from the stream. |
4117b296633d
network byte order read/write methods added
Claus Gittinger <cg@exept.de>
parents:
820
diff
changeset
|
371 |
The receiver must support reading of binary bytes." |
4117b296633d
network byte order read/write methods added
Claus Gittinger <cg@exept.de>
parents:
820
diff
changeset
|
372 |
|
4117b296633d
network byte order read/write methods added
Claus Gittinger <cg@exept.de>
parents:
820
diff
changeset
|
373 |
^ self nextUnsignedLongMSB:(Socket networkLongOrderIsMSB) |
4117b296633d
network byte order read/write methods added
Claus Gittinger <cg@exept.de>
parents:
820
diff
changeset
|
374 |
|
4117b296633d
network byte order read/write methods added
Claus Gittinger <cg@exept.de>
parents:
820
diff
changeset
|
375 |
"Created: 10.1.1996 / 19:49:02 / cg" |
4117b296633d
network byte order read/write methods added
Claus Gittinger <cg@exept.de>
parents:
820
diff
changeset
|
376 |
"Modified: 10.1.1996 / 19:49:50 / cg" |
4117b296633d
network byte order read/write methods added
Claus Gittinger <cg@exept.de>
parents:
820
diff
changeset
|
377 |
! |
4117b296633d
network byte order read/write methods added
Claus Gittinger <cg@exept.de>
parents:
820
diff
changeset
|
378 |
|
611 | 379 |
nextUnsignedShortMSB:msbFlag |
380 |
"return an unsigned short (2 bytes) from the stream. |
|
381 |
The receiver must support reading of binary bytes." |
|
382 |
||
383 |
msbFlag ifTrue:[ |
|
384 |
^ (self next bitShift:8) bitOr:(self next) |
|
385 |
]. |
|
386 |
^ (self next) bitOr:(self next bitShift:8) |
|
387 |
! |
|
388 |
||
846
4117b296633d
network byte order read/write methods added
Claus Gittinger <cg@exept.de>
parents:
820
diff
changeset
|
389 |
nextUnsignedShortNet |
4117b296633d
network byte order read/write methods added
Claus Gittinger <cg@exept.de>
parents:
820
diff
changeset
|
390 |
"return an unsigned short (2 bytes) in network byte order from the stream. |
4117b296633d
network byte order read/write methods added
Claus Gittinger <cg@exept.de>
parents:
820
diff
changeset
|
391 |
The receiver must support reading of binary bytes." |
4117b296633d
network byte order read/write methods added
Claus Gittinger <cg@exept.de>
parents:
820
diff
changeset
|
392 |
|
4117b296633d
network byte order read/write methods added
Claus Gittinger <cg@exept.de>
parents:
820
diff
changeset
|
393 |
^ self nextUnsignedShortMSB:(Socket networkShortOrderIsMSB) |
4117b296633d
network byte order read/write methods added
Claus Gittinger <cg@exept.de>
parents:
820
diff
changeset
|
394 |
|
4117b296633d
network byte order read/write methods added
Claus Gittinger <cg@exept.de>
parents:
820
diff
changeset
|
395 |
"Created: 10.1.1996 / 19:50:02 / cg" |
4117b296633d
network byte order read/write methods added
Claus Gittinger <cg@exept.de>
parents:
820
diff
changeset
|
396 |
! |
4117b296633d
network byte order read/write methods added
Claus Gittinger <cg@exept.de>
parents:
820
diff
changeset
|
397 |
|
611 | 398 |
nextWord |
399 |
^ self nextAlphaNumericWord |
|
400 |
! ! |
|
401 |
||
402 |
!Stream methodsFor:'non homogenous writing'! |
|
403 |
||
404 |
nextLongPut:aNumber |
|
846
4117b296633d
network byte order read/write methods added
Claus Gittinger <cg@exept.de>
parents:
820
diff
changeset
|
405 |
"for ST-80 compatibility: |
4117b296633d
network byte order read/write methods added
Claus Gittinger <cg@exept.de>
parents:
820
diff
changeset
|
406 |
Write the argument, aNumber as a long (four bytes). |
4117b296633d
network byte order read/write methods added
Claus Gittinger <cg@exept.de>
parents:
820
diff
changeset
|
407 |
The most significant byte is sent first." |
611 | 408 |
|
409 |
^ self nextPutLong:aNumber MSB:true |
|
846
4117b296633d
network byte order read/write methods added
Claus Gittinger <cg@exept.de>
parents:
820
diff
changeset
|
410 |
|
4117b296633d
network byte order read/write methods added
Claus Gittinger <cg@exept.de>
parents:
820
diff
changeset
|
411 |
"Modified: 10.1.1996 / 19:38:54 / cg" |
611 | 412 |
! |
413 |
||
414 |
nextNumber:n put:v |
|
415 |
"Append to the receiver the argument, v, which is a positive Integer, |
|
416 |
as the next n bytes. Bytes are written msb first. |
|
417 |
Possibly pad with leading zeros. |
|
418 |
The receiver must support writing of binary bytes." |
|
419 |
||
1117
f64e35e18e65
typed variable in nextPutNumber
Claus Gittinger <cg@exept.de>
parents:
970
diff
changeset
|
420 |
|vlen "{ Class: SmallInteger }" |
f64e35e18e65
typed variable in nextPutNumber
Claus Gittinger <cg@exept.de>
parents:
970
diff
changeset
|
421 |
i "{ Class: SmallInteger }"| |
611 | 422 |
|
423 |
"claus: this method is central in binaryStorage - |
|
424 |
therefore it has been tuned a bit (and needs even more tuning)" |
|
425 |
||
426 |
v class == SmallInteger ifTrue:[ "- this is a hint to stc" |
|
427 |
n == 1 ifTrue:[ |
|
428 |
(v between:0 and:16rFF) ifTrue:[ |
|
429 |
self nextPut:v. |
|
430 |
^ self |
|
431 |
]. |
|
432 |
]. |
|
433 |
n == 2 ifTrue:[ |
|
434 |
(v between:0 and:16rFFFF) ifTrue:[ |
|
435 |
self nextPut:(v bitShift:-8). |
|
436 |
self nextPut:(v bitAnd:16rFF). |
|
437 |
^ self |
|
438 |
]. |
|
439 |
]. |
|
440 |
n == 3 ifTrue:[ |
|
441 |
(v between:0 and:16rFFFFFF) ifTrue:[ |
|
442 |
self nextPut:((v bitShift:-16) bitAnd:16rFF). |
|
443 |
self nextPut:((v bitShift:-8) bitAnd:16rFF). |
|
444 |
self nextPut:(v bitAnd:16rFF). |
|
445 |
^ self |
|
446 |
]. |
|
447 |
]. |
|
448 |
n == 4 ifTrue:[ |
|
449 |
(v >= 0) ifTrue:[ |
|
450 |
self nextPut:((v bitShift:-24) bitAnd:16rFF). |
|
451 |
self nextPut:((v bitShift:-16) bitAnd:16rFF). |
|
452 |
self nextPut:((v bitShift:-8) bitAnd:16rFF). |
|
453 |
self nextPut:(v bitAnd:16rFF). |
|
454 |
^ self |
|
455 |
]. |
|
456 |
]. |
|
457 |
]. |
|
458 |
||
459 |
" |
|
460 |
arbitrary long |
|
461 |
" |
|
462 |
n < (vlen := v digitLength) ifTrue: [ |
|
463 |
" |
|
464 |
the number is too big to be repesented in n bytes |
|
465 |
" |
|
1117
f64e35e18e65
typed variable in nextPutNumber
Claus Gittinger <cg@exept.de>
parents:
970
diff
changeset
|
466 |
self error:'number too big' |
611 | 467 |
]. |
468 |
||
469 |
"pad with leading zeros" |
|
470 |
i := n. |
|
1117
f64e35e18e65
typed variable in nextPutNumber
Claus Gittinger <cg@exept.de>
parents:
970
diff
changeset
|
471 |
[i > vlen] whileTrue:[ |
f64e35e18e65
typed variable in nextPutNumber
Claus Gittinger <cg@exept.de>
parents:
970
diff
changeset
|
472 |
self nextPut:0. |
611 | 473 |
i := i - 1 |
474 |
]. |
|
1117
f64e35e18e65
typed variable in nextPutNumber
Claus Gittinger <cg@exept.de>
parents:
970
diff
changeset
|
475 |
|
f64e35e18e65
typed variable in nextPutNumber
Claus Gittinger <cg@exept.de>
parents:
970
diff
changeset
|
476 |
i == 1 ifTrue:[ |
f64e35e18e65
typed variable in nextPutNumber
Claus Gittinger <cg@exept.de>
parents:
970
diff
changeset
|
477 |
^ self nextPut:v |
611 | 478 |
]. |
1117
f64e35e18e65
typed variable in nextPutNumber
Claus Gittinger <cg@exept.de>
parents:
970
diff
changeset
|
479 |
|
f64e35e18e65
typed variable in nextPutNumber
Claus Gittinger <cg@exept.de>
parents:
970
diff
changeset
|
480 |
[i > 0] whileTrue:[ |
f64e35e18e65
typed variable in nextPutNumber
Claus Gittinger <cg@exept.de>
parents:
970
diff
changeset
|
481 |
self nextPut:(v digitAt:i). |
611 | 482 |
i := i - 1 |
483 |
] |
|
484 |
! |
|
485 |
||
486 |
nextPutLong:aNumber MSB:msbFlag |
|
487 |
"Write the argument, aNumber as a long (four bytes). If msbFlag is |
|
488 |
true, data is written most-significant byte first; otherwise least |
|
489 |
first. Returns the receiver on ok, nil on error. |
|
490 |
The receiver must support writing of binary bytes." |
|
491 |
||
492 |
msbFlag ifTrue:[ |
|
493 |
"high word first" |
|
494 |
self nextShortPut:(aNumber // 16r10000) MSB:true. |
|
495 |
^ self nextShortPut:(aNumber \\ 16r10000) MSB:true |
|
496 |
]. |
|
497 |
"low word first" |
|
498 |
self nextShortPut:(aNumber \\ 16r10000) MSB:false. |
|
499 |
^ self nextShortPut:(aNumber // 16r10000) MSB:false. |
|
500 |
! |
|
501 |
||
846
4117b296633d
network byte order read/write methods added
Claus Gittinger <cg@exept.de>
parents:
820
diff
changeset
|
502 |
nextPutLongNet:aNumber |
4117b296633d
network byte order read/write methods added
Claus Gittinger <cg@exept.de>
parents:
820
diff
changeset
|
503 |
"Write the argument, aNumber as a long (four bytes) in the network byte order. |
4117b296633d
network byte order read/write methods added
Claus Gittinger <cg@exept.de>
parents:
820
diff
changeset
|
504 |
Returns the receiver on ok, nil on error. |
4117b296633d
network byte order read/write methods added
Claus Gittinger <cg@exept.de>
parents:
820
diff
changeset
|
505 |
The receiver must support writing of binary bytes." |
4117b296633d
network byte order read/write methods added
Claus Gittinger <cg@exept.de>
parents:
820
diff
changeset
|
506 |
|
4117b296633d
network byte order read/write methods added
Claus Gittinger <cg@exept.de>
parents:
820
diff
changeset
|
507 |
^ self nextPutLong:aNumber MSB:(Socket networkLongOrderIsMSB). |
4117b296633d
network byte order read/write methods added
Claus Gittinger <cg@exept.de>
parents:
820
diff
changeset
|
508 |
|
4117b296633d
network byte order read/write methods added
Claus Gittinger <cg@exept.de>
parents:
820
diff
changeset
|
509 |
"Modified: 10.1.1996 / 19:47:10 / cg" |
4117b296633d
network byte order read/write methods added
Claus Gittinger <cg@exept.de>
parents:
820
diff
changeset
|
510 |
"Created: 10.1.1996 / 19:50:23 / cg" |
4117b296633d
network byte order read/write methods added
Claus Gittinger <cg@exept.de>
parents:
820
diff
changeset
|
511 |
! |
4117b296633d
network byte order read/write methods added
Claus Gittinger <cg@exept.de>
parents:
820
diff
changeset
|
512 |
|
611 | 513 |
nextPutShort:aNumber MSB:msbFlag |
514 |
"Write the argument, aNumber as a short (two bytes). If msbFlag is |
|
515 |
true, data is written most-significant byte first; otherwise least |
|
516 |
first. Returns the receiver on ok, nil on error. |
|
517 |
The receiver must support writing of binary bytes." |
|
518 |
||
1117
f64e35e18e65
typed variable in nextPutNumber
Claus Gittinger <cg@exept.de>
parents:
970
diff
changeset
|
519 |
|iNum "{ Class: SmallInteger }"| |
f64e35e18e65
typed variable in nextPutNumber
Claus Gittinger <cg@exept.de>
parents:
970
diff
changeset
|
520 |
|
f64e35e18e65
typed variable in nextPutNumber
Claus Gittinger <cg@exept.de>
parents:
970
diff
changeset
|
521 |
iNum := aNumber. |
f64e35e18e65
typed variable in nextPutNumber
Claus Gittinger <cg@exept.de>
parents:
970
diff
changeset
|
522 |
|
611 | 523 |
msbFlag ifTrue:[ |
524 |
"most significant first" |
|
1117
f64e35e18e65
typed variable in nextPutNumber
Claus Gittinger <cg@exept.de>
parents:
970
diff
changeset
|
525 |
self nextPut:(iNum bitShift:-8). |
f64e35e18e65
typed variable in nextPutNumber
Claus Gittinger <cg@exept.de>
parents:
970
diff
changeset
|
526 |
^ self nextPut:(iNum bitAnd:16rFF). |
611 | 527 |
]. |
528 |
"least significant first" |
|
1117
f64e35e18e65
typed variable in nextPutNumber
Claus Gittinger <cg@exept.de>
parents:
970
diff
changeset
|
529 |
self nextPut:(iNum bitAnd:16rFF). |
f64e35e18e65
typed variable in nextPutNumber
Claus Gittinger <cg@exept.de>
parents:
970
diff
changeset
|
530 |
self nextPut:(iNum bitShift:-8). |
611 | 531 |
! |
532 |
||
846
4117b296633d
network byte order read/write methods added
Claus Gittinger <cg@exept.de>
parents:
820
diff
changeset
|
533 |
nextPutShortNet:aNumber |
4117b296633d
network byte order read/write methods added
Claus Gittinger <cg@exept.de>
parents:
820
diff
changeset
|
534 |
"Write the argument, aNumber as a short (two bytes) in the network byte order. |
4117b296633d
network byte order read/write methods added
Claus Gittinger <cg@exept.de>
parents:
820
diff
changeset
|
535 |
Returns the receiver on ok, nil on error. |
4117b296633d
network byte order read/write methods added
Claus Gittinger <cg@exept.de>
parents:
820
diff
changeset
|
536 |
The receiver must support writing of binary bytes." |
4117b296633d
network byte order read/write methods added
Claus Gittinger <cg@exept.de>
parents:
820
diff
changeset
|
537 |
|
4117b296633d
network byte order read/write methods added
Claus Gittinger <cg@exept.de>
parents:
820
diff
changeset
|
538 |
^ self nextPutLong:aNumber MSB:(Socket networkShortOrderIsMSB). |
4117b296633d
network byte order read/write methods added
Claus Gittinger <cg@exept.de>
parents:
820
diff
changeset
|
539 |
|
4117b296633d
network byte order read/write methods added
Claus Gittinger <cg@exept.de>
parents:
820
diff
changeset
|
540 |
"Created: 10.1.1996 / 19:50:33 / cg" |
4117b296633d
network byte order read/write methods added
Claus Gittinger <cg@exept.de>
parents:
820
diff
changeset
|
541 |
! |
4117b296633d
network byte order read/write methods added
Claus Gittinger <cg@exept.de>
parents:
820
diff
changeset
|
542 |
|
611 | 543 |
nextPutWord:aNumber |
544 |
"write the argument, aNumber as a signed short (two bytes); |
|
545 |
write msb-first for compatibility with other smalltalks. |
|
546 |
The receiver must support writing of binary bytes. |
|
547 |
I dont know if it should be named nextPutWord: or nextWordPut:; |
|
548 |
one of them will vanish ..." |
|
549 |
||
550 |
^ self nextPutShort:aNumber MSB:true |
|
551 |
! |
|
552 |
||
553 |
nextShortPut:aNumber MSB:msbFlag |
|
554 |
"for compatibility - this will vanish" |
|
555 |
||
556 |
^ self nextPutShort:aNumber MSB:msbFlag |
|
557 |
! |
|
558 |
||
559 |
nextWordPut:aNumber |
|
846
4117b296633d
network byte order read/write methods added
Claus Gittinger <cg@exept.de>
parents:
820
diff
changeset
|
560 |
"for ST-80 compatibility: |
4117b296633d
network byte order read/write methods added
Claus Gittinger <cg@exept.de>
parents:
820
diff
changeset
|
561 |
Write the argument, aNumber as a short (two bytes). |
4117b296633d
network byte order read/write methods added
Claus Gittinger <cg@exept.de>
parents:
820
diff
changeset
|
562 |
The most significant byte is sent first." |
611 | 563 |
|
564 |
^ self nextPutShort:aNumber MSB:true |
|
846
4117b296633d
network byte order read/write methods added
Claus Gittinger <cg@exept.de>
parents:
820
diff
changeset
|
565 |
|
4117b296633d
network byte order read/write methods added
Claus Gittinger <cg@exept.de>
parents:
820
diff
changeset
|
566 |
"Modified: 10.1.1996 / 19:39:19 / cg" |
611 | 567 |
! ! |
568 |
||
569 |
!Stream methodsFor:'private'! |
|
570 |
||
571 |
contentsSpecies |
|
572 |
"this should return the class of which an instance is |
|
573 |
returned by the #contents method. It is redefinable in subclasses" |
|
574 |
||
575 |
"return Array here - since the abstract Stream has no idea |
|
576 |
of the underlying collection class" |
|
577 |
||
578 |
^ Array |
|
579 |
! ! |
|
580 |
||
581 |
!Stream methodsFor:'queries'! |
|
582 |
||
583 |
isBinary |
|
584 |
"return true, if in binary mode. Returns false here, |
|
585 |
to make internalStreams protocol compatible with externStreams." |
|
586 |
||
587 |
^ false |
|
588 |
! |
|
589 |
||
590 |
isPositionable |
|
591 |
"return true, if the stream supports positioning (some do not)" |
|
592 |
||
593 |
^ false |
|
594 |
! |
|
595 |
||
596 |
isReadable |
|
597 |
"return true, if reading is supported by the recevier. |
|
598 |
Assume yes here." |
|
599 |
||
600 |
^ self subclassResponsibility |
|
601 |
! |
|
602 |
||
603 |
isStream |
|
604 |
"return true, if the receiver is some kind of Stream." |
|
605 |
||
606 |
^ true |
|
607 |
! |
|
608 |
||
609 |
isWritable |
|
610 |
"return true, if writing is supported by the recevier. |
|
611 |
Assume yes here." |
|
612 |
||
613 |
^ self subclassResponsibility |
|
614 |
! |
|
615 |
||
616 |
lineLength |
|
617 |
"this is just a layout hint for prettyprinting functions |
|
618 |
- for compatibility with TextCollectors" |
|
619 |
||
620 |
^ 80 |
|
621 |
! ! |
|
622 |
||
62 | 623 |
!Stream methodsFor:'reading'! |
1 | 624 |
|
625 |
next |
|
626 |
"return the next element of the stream |
|
627 |
- we do not know here how to do it, it must be redefined in subclass" |
|
628 |
||
629 |
^ self subclassResponsibility |
|
630 |
! |
|
631 |
||
632 |
next:count |
|
62 | 633 |
"return the next count elements of the stream as a Collection." |
1 | 634 |
|
329 | 635 |
|answerStream |
285 | 636 |
cnt "{ Class: SmallInteger }" | |
1 | 637 |
|
329 | 638 |
answerStream := WriteStream on:(self contentsSpecies new). |
285 | 639 |
cnt := count. |
640 |
1 to:cnt do:[:index | |
|
329 | 641 |
answerStream nextPut:(self next) |
1 | 642 |
]. |
329 | 643 |
^ answerStream contents |
1 | 644 |
|
275 | 645 |
" |
646 |
(ReadStream on:#(1 2 3 4 5)) next:3 |
|
647 |
(ReadStream on:'hello') next:3 |
|
648 |
" |
|
1 | 649 |
! |
650 |
||
285 | 651 |
nextAvailable:count |
652 |
"return the next count elements of the stream as a Collection. |
|
653 |
If the stream reaches the end before count elements have been read, |
|
654 |
return what is available. (i.e. a shorter collection)" |
|
655 |
||
345 | 656 |
|answerStream |
285 | 657 |
cnt "{ Class: SmallInteger }"| |
658 |
||
329 | 659 |
answerStream := WriteStream on:(self contentsSpecies new). |
285 | 660 |
cnt := count. |
661 |
1 to:cnt do:[:index | |
|
662 |
self atEnd ifTrue:[ |
|
329 | 663 |
^ answerStream contents |
285 | 664 |
]. |
329 | 665 |
answerStream nextPut:(self next) |
285 | 666 |
]. |
329 | 667 |
^ answerStream contents |
285 | 668 |
|
669 |
" |
|
670 |
(ReadStream on:#(1 2 3 4 5)) nextAvailable:3 |
|
671 |
(ReadStream on:#(1 2 3 4 5)) nextAvailable:10 |
|
672 |
(ReadStream on:'hello') nextAvailable:3 |
|
673 |
(ReadStream on:'hello') nextAvailable:10 |
|
674 |
" |
|
675 |
! |
|
676 |
||
1 | 677 |
nextMatchFor:anObject |
62 | 678 |
"read an element from the receiver, return true if it was equal to |
679 |
the argument, anObject" |
|
680 |
||
681 |
^ (self next = anObject) |
|
682 |
||
683 |
" |
|
684 |
|s| |
|
685 |
s := ReadStream on:#(1 2 3 4 5 6 7 8). |
|
686 |
s nextMatchFor:2 |
|
687 |
" |
|
688 |
" |
|
689 |
|s| |
|
690 |
s := ReadStream on:#(1 2 3 4 5 6 7 8). |
|
691 |
s nextMatchFor:2. |
|
692 |
s nextMatchFor:2 |
|
693 |
" |
|
694 |
! |
|
695 |
||
696 |
skip:count |
|
697 |
"skip count objects, return the receiver" |
|
698 |
||
345 | 699 |
|n "{ Class: SmallInteger }"| |
700 |
||
701 |
n := count. |
|
702 |
||
62 | 703 |
"dont know how to unread ..." |
345 | 704 |
n < 0 ifTrue:[ |
216 | 705 |
^ self error:'stream is not positionable' |
62 | 706 |
]. |
345 | 707 |
n timesRepeat:self next |
62 | 708 |
|
709 |
" |
|
710 |
|s| |
|
711 |
s := ReadStream on:#(1 2 3 4 5 6 7 8). |
|
712 |
s skip:4. |
|
713 |
s next |
|
714 |
" |
|
715 |
! |
|
716 |
||
717 |
skipFor:anObject |
|
718 |
"skip all objects up-to and including anObject; return the element after anObject." |
|
719 |
||
720 |
(self skipThrough:anObject) notNil ifTrue:[ |
|
216 | 721 |
^ self next |
62 | 722 |
]. |
723 |
^ nil |
|
724 |
||
725 |
" |
|
726 |
|s| |
|
727 |
s := ReadStream on:#(1 2 3 4 5 6 7 8). |
|
728 |
s skipFor:4. |
|
729 |
" |
|
730 |
! |
|
731 |
||
732 |
skipThrough:anObject |
|
77 | 733 |
"skip all objects up-to and including anObject. Return the receiver if |
671 | 734 |
skip was successful, otherwise (i.e. if not found) return nil. |
62 | 735 |
The next read operation will return the element after anObject." |
1 | 736 |
|
737 |
|nextElement| |
|
738 |
||
739 |
[self atEnd] whileFalse:[ |
|
216 | 740 |
nextElement := self next. |
741 |
(nextElement = anObject) ifTrue: [ ^ self ] |
|
1 | 742 |
]. |
743 |
^ nil |
|
2 | 744 |
|
62 | 745 |
" |
746 |
|s| |
|
747 |
s := ReadStream on:#(1 2 3 4 5 6 7 8). |
|
748 |
s skipThrough:4. |
|
749 |
s next |
|
750 |
" |
|
77 | 751 |
! |
752 |
||
611 | 753 |
through:anObject |
754 |
"read a collection of all objects up-to anObject and return these |
|
755 |
elements, including anObject. |
|
756 |
The next read operation will return the element after anObject. |
|
757 |
Compare this with #upTo: which also reads up to some object |
|
758 |
and also positions behind it, but does not include it in the returned |
|
759 |
value." |
|
760 |
||
761 |
|answerStream element| |
|
762 |
||
763 |
answerStream := WriteStream on:(self contentsSpecies new). |
|
764 |
[self atEnd] whileFalse:[ |
|
765 |
element := self next. |
|
766 |
answerStream nextPut:element. |
|
767 |
(element = anObject) ifTrue: [ |
|
768 |
^ answerStream contents |
|
769 |
] |
|
770 |
]. |
|
771 |
^ answerStream contents |
|
772 |
||
773 |
" |
|
774 |
|s| |
|
775 |
s := ReadStream on:#(1 2 3 4 5 6 7 8). |
|
776 |
Transcript showCr:(s through:4). |
|
777 |
Transcript showCr:s next |
|
778 |
" |
|
779 |
" |
|
780 |
|s| |
|
781 |
s := ReadStream on:'hello world'. |
|
782 |
Transcript showCr:(s through:Character space). |
|
783 |
Transcript showCr:(s upToEnd) |
|
784 |
" |
|
785 |
! |
|
786 |
||
427 | 787 |
upTo:anObject |
788 |
"read a collection of all objects up-to anObject and return these |
|
789 |
elements, but excluding anObject. |
|
790 |
The next read operation will return the element after anObject. |
|
791 |
Compare this with #through: which also reads up to some object |
|
792 |
and also positions behind it, but DOES include it in the returned |
|
793 |
value." |
|
794 |
||
795 |
|answerStream element| |
|
796 |
||
797 |
answerStream := WriteStream on:(self contentsSpecies new). |
|
798 |
[self atEnd] whileFalse:[ |
|
799 |
element := self next. |
|
800 |
(element = anObject) ifTrue: [ |
|
801 |
^ answerStream contents |
|
802 |
]. |
|
1117
f64e35e18e65
typed variable in nextPutNumber
Claus Gittinger <cg@exept.de>
parents:
970
diff
changeset
|
803 |
answerStream nextPut:element. |
427 | 804 |
]. |
805 |
^ answerStream contents |
|
806 |
||
807 |
" |
|
808 |
|s| |
|
809 |
s := ReadStream on:#(1 2 3 4 5 6 7 8). |
|
810 |
Transcript showCr:(s upTo:4). |
|
811 |
Transcript showCr:s next |
|
812 |
||
813 |
|s| |
|
814 |
s := ReadStream on:'hello world'. |
|
815 |
Transcript showCr:(s upTo:Character space). |
|
816 |
Transcript showCr:(s upToEnd) |
|
817 |
||
818 |
(ReadStream on:'12345678905') upTo:$5; upTo:$5 |
|
819 |
||
820 |
(ReadStream on:'123456') upTo:$7 |
|
821 |
||
822 |
(ReadStream on:#(1 2 3 4 5 6)) upTo:4 |
|
823 |
||
824 |
(ReadStream on:'line 1 |
|
825 |
line 2') upTo:Character cr |
|
826 |
||
827 |
'Makefile' asFilename readStream upTo:Character cr;upTo:Character cr |
|
828 |
" |
|
829 |
! |
|
830 |
||
77 | 831 |
upToEnd |
832 |
"return a collection of the elements up-to the end. |
|
833 |
Return nil if the stream-end is reached before." |
|
834 |
||
835 |
|answerStream| |
|
836 |
||
837 |
answerStream := WriteStream on:(self contentsSpecies new). |
|
838 |
[self atEnd] whileFalse:[ |
|
216 | 839 |
answerStream nextPut:(self next) |
77 | 840 |
]. |
841 |
^ answerStream contents |
|
842 |
||
275 | 843 |
" |
844 |
(ReadStream on:'1234567890') upToEnd |
|
845 |
('123456' readStream) next; next; upToEnd |
|
427 | 846 |
('1 23456' readStream) upTo:Character space; upToEnd |
275 | 847 |
" |
611 | 848 |
! ! |
77 | 849 |
|
611 | 850 |
!Stream methodsFor:'testing'! |
77 | 851 |
|
611 | 852 |
atEnd |
853 |
"return true if the end of the stream has been reached; |
|
854 |
- we do not know here how to do it, it must be redefined in subclass" |
|
77 | 855 |
|
611 | 856 |
^ self subclassResponsibility |
1 | 857 |
! ! |
858 |
||
62 | 859 |
!Stream methodsFor:'writing'! |
1 | 860 |
|
339 | 861 |
commit |
862 |
"write out all unbuffered data - ignored here, but added |
|
863 |
to make internalStreams protocol compatible with externalStreams" |
|
864 |
||
865 |
! |
|
866 |
||
611 | 867 |
cr |
868 |
"append a carriage-return to the stream" |
|
869 |
||
870 |
self nextPut:(Character cr) |
|
871 |
! |
|
872 |
||
873 |
crTab |
|
874 |
"append a carriage-return followed by a tab to the stream. |
|
875 |
Same as crtab for ST/X backward compatibility" |
|
876 |
||
877 |
self obsoleteMethodWarning:'use crTab'. |
|
878 |
self crtab |
|
879 |
! |
|
880 |
||
881 |
crtab |
|
882 |
"append a carriage-return followed by a tab to the stream." |
|
1 | 883 |
|
611 | 884 |
self crtab:1 |
885 |
! |
|
886 |
||
887 |
crtab:n |
|
888 |
"append a carriage-return followed by n tabs to the stream." |
|
889 |
||
890 |
self nextPut:(Character cr). |
|
891 |
self next:n put:(Character tab) |
|
892 |
! |
|
893 |
||
894 |
endEntry |
|
895 |
"ignored here - for compatibility with Transcript" |
|
896 |
||
897 |
^ self |
|
898 |
! |
|
899 |
||
900 |
ff |
|
901 |
"append a form-feed (new-pagee) to the receiver-stream" |
|
902 |
||
903 |
self nextPut:(Character ff) |
|
1 | 904 |
! |
905 |
||
701 | 906 |
flush |
907 |
"write out all unbuffered data - for ST-80 compatibility" |
|
908 |
||
909 |
self commit |
|
910 |
||
911 |
"Created: 27.11.1995 / 12:00:35 / cg" |
|
912 |
! |
|
913 |
||
1 | 914 |
next:count put:anObject |
915 |
"put the argument, anObject count times onto the receiver" |
|
916 |
||
345 | 917 |
|n "{ Class: SmallInteger }"| |
918 |
||
919 |
n := count. |
|
920 |
n timesRepeat:[self nextPut:anObject]. |
|
1 | 921 |
^ anObject |
62 | 922 |
|
923 |
" |
|
924 |
|s| |
|
925 |
||
926 |
s := WriteStream on:#(). |
|
927 |
s nextPut:1. |
|
928 |
s next:5 put:2. |
|
929 |
s nextPut:3. |
|
930 |
s contents |
|
931 |
" |
|
1 | 932 |
! |
933 |
||
611 | 934 |
nextPut:anObject |
935 |
"put the argument, anObject onto the receiver |
|
936 |
- we do not know here how to do it, it must be redefined in subclass" |
|
937 |
||
938 |
^ self subclassResponsibility |
|
939 |
! |
|
940 |
||
1 | 941 |
nextPutAll:aCollection |
942 |
"put all elements of the argument, aCollection onto the receiver" |
|
943 |
||
944 |
aCollection do:[:element | |
|
216 | 945 |
self nextPut:element |
1 | 946 |
]. |
947 |
^ aCollection |
|
62 | 948 |
|
949 |
" |
|
950 |
|s| |
|
951 |
||
952 |
s := WriteStream on:#(). |
|
953 |
s nextPutAll:(1 to:5). |
|
954 |
s nextPutAll:#('one' 'two' 'three'). |
|
955 |
s contents |
|
956 |
" |
|
957 |
" |
|
958 |
|s| |
|
959 |
||
960 |
s := WriteStream on:(String new). |
|
961 |
s nextPutAll:($a to:$f). |
|
962 |
s nextPutAll:'one '; |
|
963 |
nextPutAll:'two '; |
|
964 |
nextPutAll:'three'. |
|
965 |
s contents |
|
966 |
" |
|
1 | 967 |
! |
968 |
||
969 |
nextPutAll:aCollection startingAt:first to:last |
|
970 |
"put the elements with index from first to last |
|
971 |
of the argument, aCollection onto the receiver" |
|
972 |
||
973 |
aCollection from:first to:last do:[:element | |
|
216 | 974 |
self nextPut:element |
1 | 975 |
]. |
976 |
^ aCollection |
|
62 | 977 |
|
978 |
" |
|
979 |
|s| |
|
980 |
||
981 |
s := WriteStream on:#(). |
|
982 |
s nextPutAll:#('one' 'two' 'three' 'four' 'five') startingAt:2 to:4. |
|
983 |
s contents |
|
984 |
" |
|
1 | 985 |
! |
986 |
||
743 | 987 |
nextPutLine:aCollection |
988 |
"put all elements of the argument, aCollection onto the receiver, |
|
989 |
and append a cr. This is only useful with character streams in textMode." |
|
990 |
||
991 |
self nextPutAll:aCollection. |
|
992 |
self cr. |
|
993 |
||
994 |
"Created: 13.12.1995 / 10:49:17 / cg" |
|
995 |
! |
|
996 |
||
611 | 997 |
print:anObject |
998 |
"append a printed representation of anObject to the receiver. |
|
999 |
Same as anObject printOn:self; For ST-80 compatibility." |
|
2 | 1000 |
|
611 | 1001 |
anObject printOn:self |
2 | 1002 |
! |
1003 |
||
1 | 1004 |
show:aString |
1005 |
"put all elements of the argument, aString onto the receiver; |
|
1006 |
this makes streams somewhat compatible to TextCollectors and |
|
1007 |
allows you to say: Smalltalk at:#Transcript put:Stdout" |
|
1008 |
||
1009 |
^ self nextPutAll:aString printString |
|
1010 |
! |
|
1011 |
||
1012 |
showCr:aString |
|
1013 |
"put all elements of the argument, aString onto the receiver; |
|
1014 |
and append a newline. |
|
62 | 1015 |
For compatibility with TextCollectors" |
1 | 1016 |
|
1017 |
self show:aString. |
|
1018 |
self cr |
|
62 | 1019 |
! |
1020 |
||
1021 |
space |
|
1022 |
"append a space character to the receiver-stream" |
|
1023 |
||
1024 |
self nextPut:(Character space) |
|
1025 |
! |
|
1026 |
||
1027 |
spaces:count |
|
1028 |
"append count space-characters to the receiver-stream" |
|
1029 |
||
1030 |
self next:count put:(Character space) |
|
1031 |
! |
|
1032 |
||
611 | 1033 |
store:anObject |
1034 |
"append a printed representation of anObject to the receiver, |
|
1035 |
from which the receiver can be reconstructed. |
|
1036 |
Same as anObject storeOn:self; For ST-80 compatibility." |
|
77 | 1037 |
|
611 | 1038 |
anObject storeOn:self |
77 | 1039 |
! |
1040 |
||
611 | 1041 |
tab |
1042 |
"append a tab-character to the stream" |
|
77 | 1043 |
|
611 | 1044 |
self nextPut:(Character tab) |
77 | 1045 |
! ! |
1046 |
||
701 | 1047 |
!Stream class methodsFor:'documentation'! |
1048 |
||
1049 |
version |
|
1295 | 1050 |
^ '$Header: /cvs/stx/stx/libbasic/Stream.st,v 1.36 1996-04-25 17:00:54 cg Exp $' |
701 | 1051 |
! ! |
611 | 1052 |
Stream initialize! |