author | Stefan Vogel <sv@exept.de> |
Tue, 28 Apr 2020 16:22:26 +0200 | |
changeset 25375 | b784fc06a5eb |
parent 25372 | 389daab3ee10 |
permissions | -rw-r--r-- |
1 | 1 |
" |
5 | 2 |
COPYRIGHT (c) 1989 by Claus Gittinger |
329 | 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 |
" |
|
5932
6041c11b3370
added #nextByte for compatibility
Claus Gittinger <cg@exept.de>
parents:
5309
diff
changeset
|
12 |
"{ Package: 'stx:libbasic' }" |
6041c11b3370
added #nextByte for compatibility
Claus Gittinger <cg@exept.de>
parents:
5309
diff
changeset
|
13 |
|
17558 | 14 |
"{ NameSpace: Smalltalk }" |
15 |
||
1 | 16 |
WriteStream subclass:#ReadWriteStream |
970
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
701
diff
changeset
|
17 |
instanceVariableNames:'' |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
701
diff
changeset
|
18 |
classVariableNames:'' |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
701
diff
changeset
|
19 |
poolDictionaries:'' |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
701
diff
changeset
|
20 |
category:'Streams' |
1 | 21 |
! |
22 |
||
611 | 23 |
!ReadWriteStream class methodsFor:'documentation'! |
88 | 24 |
|
25 |
copyright |
|
26 |
" |
|
27 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
329 | 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 |
ReadWriteStreams allow both read- and write-access to some collection. |
|
42 |
To allow this, they reenable some methods blocked in WriteStream. |
|
43 |
(being one of the seldom places, where multiple inheritance could be |
|
44 |
of good use in smalltalk). |
|
329 | 45 |
|
17558 | 46 |
[caveat:] |
47 |
Basing capabilities like readability/writability/positionability/peekability on inheritance makes |
|
48 |
the class hierarchy ugly and leads to strange and hard to teach redefinitions (aka. NonPositionableStream |
|
49 |
below PositionableStream or ExternalReadStream under WriteStream) |
|
50 |
||
329 | 51 |
Claus: |
1295 | 52 |
I personally find the ReadStream - WriteStream - ReadWriteStream |
53 |
organization brain-damaged. It would be better to have an attribute |
|
54 |
(such as readOnly / writeOnly / readWrite) in an InternalStream subclass |
|
55 |
of Stream ... |
|
56 |
||
57 |
[author:] |
|
58 |
Claus Gittinger |
|
88 | 59 |
" |
5309 | 60 |
! |
61 |
||
62 |
examples |
|
63 |
" |
|
64 |
|s| |
|
65 |
||
66 |
s := ReadWriteStream with:'abcd'. |
|
67 |
s reset. |
|
68 |
s nextPut:$A. |
|
69 |
s contents |
|
70 |
" |
|
329 | 71 |
! ! |
72 |
||
3081 | 73 |
!ReadWriteStream methodsFor:'accessing'! |
74 |
||
19883 | 75 |
clear |
76 |
"re-initialize the stream to write to the beginning and |
|
77 |
to not read any data." |
|
78 |
||
79 |
position := readLimit := 0. |
|
80 |
! |
|
81 |
||
3081 | 82 |
contents |
83 |
"return the contents as written so far; |
|
84 |
redefined to prevent confusion resulting due to |
|
85 |
my superclasses optimization. (see WriteStream contents). |
|
20098 | 86 |
ST80 users of ReadWriteStream may expect the contents array to remain |
87 |
unchanged, which we do not guarantee." |
|
3081 | 88 |
|
15352 | 89 |
readLimit := (readLimit ? 0) max:position. |
3081 | 90 |
^ collection copyFrom:1 to:readLimit |
91 |
||
92 |
"Created: / 30.10.1997 / 16:25:22 / cg" |
|
93 |
"Modified: / 30.10.1997 / 18:37:09 / cg" |
|
5309 | 94 |
! |
95 |
||
96 |
reset |
|
17203 | 97 |
"set the read position to the beginning of the collection. |
98 |
Because I am a read/write stream, the readLimit is set to the current write position. |
|
17245 | 99 |
Thus, the just written data can be read back." |
17203 | 100 |
|
22800 | 101 |
readLimit notNil ifTrue:[ |
102 |
readLimit := position max:readLimit. |
|
103 |
]. |
|
19883 | 104 |
position := 0. |
105 |
! |
|
106 |
||
107 |
resetPosition |
|
108 |
<resource: #obsolete> |
|
109 |
"set the read position to the beginning of the collection. |
|
110 |
Because I am a read/write stream, the readLimit is set to the current write position. |
|
24838 | 111 |
Thus, the just written data can be read back. |
112 |
Obsolete - use #reset." |
|
19883 | 113 |
|
114 |
readLimit := position max:readLimit. |
|
115 |
position := 0. |
|
24838 | 116 |
|
117 |
"Modified (comment): / 15-10-2019 / 18:34:46 / Stefan Vogel" |
|
3081 | 118 |
! ! |
119 |
||
677 | 120 |
!ReadWriteStream methodsFor:'converting'! |
121 |
||
122 |
readStream |
|
20425 | 123 |
"return the receiver as a readStream - that's myself" |
2927 | 124 |
|
677 | 125 |
^ self |
126 |
||
127 |
"Created: 29.11.1995 / 22:04:23 / stefan" |
|
8140
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8098
diff
changeset
|
128 |
! |
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8098
diff
changeset
|
129 |
|
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8098
diff
changeset
|
130 |
readStreamOrNil |
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8098
diff
changeset
|
131 |
"return a readStream from the receiver. Since this is already |
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8098
diff
changeset
|
132 |
a readStream, return self. |
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8098
diff
changeset
|
133 |
|
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8098
diff
changeset
|
134 |
This method has been defined for protocol copmatibility with Filename" |
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8098
diff
changeset
|
135 |
|
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8098
diff
changeset
|
136 |
^ self |
677 | 137 |
! ! |
138 |
||
5309 | 139 |
!ReadWriteStream methodsFor:'initialization'! |
3081 | 140 |
|
5309 | 141 |
with:initialCollection |
11438 | 142 |
"redefined from WriteStream, to position to the beginning of the stream" |
143 |
||
5309 | 144 |
super with:initialCollection. |
15352 | 145 |
position := 0. |
3081 | 146 |
! ! |
147 |
||
611 | 148 |
!ReadWriteStream methodsFor:'queries'! |
149 |
||
24838 | 150 |
collectionSize |
151 |
"return the overall number of elements in the streamed collection |
|
152 |
(both already read and to be read)." |
|
153 |
||
154 |
^ readLimit ? collection size |
|
155 |
||
156 |
"Created: / 30-07-2013 / 20:55:51 / cg" |
|
157 |
"Modified: / 15-10-2019 / 18:27:55 / Stefan Vogel" |
|
158 |
! |
|
159 |
||
160 |
remainingSize |
|
161 |
"return the number of remaining elements to read in the streamed collection." |
|
162 |
||
163 |
^ (readLimit ? collection size) - position |
|
164 |
||
165 |
"Created: / 30-07-2013 / 20:18:12 / cg" |
|
166 |
"Modified (comment): / 15-10-2019 / 18:30:32 / Stefan Vogel" |
|
167 |
! |
|
168 |
||
9004 | 169 |
size |
170 |
"return the number of elements in the streamed collection." |
|
171 |
||
15352 | 172 |
^ (readLimit ? 0) max:(position ? 0). |
611 | 173 |
! ! |
174 |
||
21133 | 175 |
!ReadWriteStream methodsFor:'reading'! |
176 |
||
24854 | 177 |
next |
178 |
"return the next element; advance read position. |
|
179 |
If there are no more elements, nil is returned." |
|
180 |
||
181 |
|element| |
|
182 |
||
183 |
(position >= readLimit) ifTrue:[^ self pastEndRead]. |
|
184 |
element := collection at:(position+1). |
|
185 |
position := position + 1. |
|
186 |
^ element |
|
187 |
||
188 |
"Modified: 5.2.1996 / 21:57:40 / stefan" |
|
189 |
! |
|
190 |
||
191 |
next:count |
|
192 |
"return the next count elements of the stream as aCollection, |
|
193 |
which depends on the streams type - (see #contentsSpecies)." |
|
194 |
||
195 |
|answer| |
|
196 |
||
197 |
self contentsSpecies == collection class ifTrue:[ |
|
198 |
((position + count) > readLimit) ifFalse:[ |
|
199 |
answer := collection copyFrom:position+1 to:position+count. |
|
200 |
position := position+count. |
|
201 |
^ answer |
|
202 |
]. |
|
203 |
]. |
|
204 |
^ super next:count |
|
205 |
! |
|
206 |
||
21133 | 207 |
peek |
208 |
"return the element to be read next without advancing read position. |
|
209 |
If there are no more elements, nil is returned." |
|
210 |
||
211 |
(position >= readLimit) ifTrue:[^ self pastEndRead]. |
|
212 |
^ collection at:(position+1) |
|
213 |
||
214 |
"Modified: 5.2.1996 / 21:57:47 / stefan" |
|
215 |
! ! |
|
216 |
||
25372 | 217 |
!ReadWriteStream methodsFor:'testing'! |
218 |
||
219 |
isEmpty |
|
220 |
"redefinde from WriteStream" |
|
221 |
||
222 |
^ self size == 0 |
|
223 |
||
224 |
"Created: / 15-10-2019 / 17:15:09 / Stefan Vogel" |
|
225 |
! |
|
226 |
||
227 |
isReadable |
|
228 |
"return true if the receiver supports reading - that's true" |
|
229 |
||
230 |
^ true |
|
231 |
! ! |
|
232 |
||
677 | 233 |
!ReadWriteStream class methodsFor:'documentation'! |
234 |
||
235 |
version |
|
19883 | 236 |
^ '$Header$' |
15355 | 237 |
! |
238 |
||
239 |
version_CVS |
|
19883 | 240 |
^ '$Header$' |
677 | 241 |
! ! |
15352 | 242 |