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