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