author | Claus Gittinger <cg@exept.de> |
Thu, 27 Jun 2019 15:21:18 +0200 | |
changeset 24377 | 2fbcb2fa6c43 |
parent 22800 | 80923ef07646 |
child 24836 | caa5c31f33aa |
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 |
||
345 | 73 |
!ReadWriteStream methodsFor:'access-reading'! |
1 | 74 |
|
75 |
next |
|
41 | 76 |
"return the next element; advance read position. |
77 |
If there are no more elements, nil is returned." |
|
78 |
||
79 |
|element| |
|
80 |
||
15355 | 81 |
(position >= readLimit) ifTrue:[^ self pastEndRead]. |
15352 | 82 |
element := collection at:(position+1). |
1 | 83 |
position := position + 1. |
41 | 84 |
^ element |
970
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
701
diff
changeset
|
85 |
|
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
701
diff
changeset
|
86 |
"Modified: 5.2.1996 / 21:57:40 / stefan" |
611 | 87 |
! |
88 |
||
16196 | 89 |
next:count |
90 |
"return the next count elements of the stream as aCollection, |
|
91 |
which depends on the streams type - (see #contentsSpecies)." |
|
92 |
||
93 |
|answer| |
|
94 |
||
95 |
self contentsSpecies == collection class ifTrue:[ |
|
96 |
((position + count) > readLimit) ifFalse:[ |
|
97 |
answer := collection copyFrom:position+1 to:position+count. |
|
98 |
position := position+count. |
|
99 |
^ answer |
|
100 |
]. |
|
101 |
]. |
|
102 |
^ super next:count |
|
1 | 103 |
! ! |
611 | 104 |
|
3081 | 105 |
!ReadWriteStream methodsFor:'accessing'! |
106 |
||
19883 | 107 |
clear |
108 |
"re-initialize the stream to write to the beginning and |
|
109 |
to not read any data." |
|
110 |
||
111 |
position := readLimit := 0. |
|
112 |
! |
|
113 |
||
3081 | 114 |
contents |
115 |
"return the contents as written so far; |
|
116 |
redefined to prevent confusion resulting due to |
|
117 |
my superclasses optimization. (see WriteStream contents). |
|
20098 | 118 |
ST80 users of ReadWriteStream may expect the contents array to remain |
119 |
unchanged, which we do not guarantee." |
|
3081 | 120 |
|
15352 | 121 |
readLimit := (readLimit ? 0) max:position. |
3081 | 122 |
^ collection copyFrom:1 to:readLimit |
123 |
||
124 |
"Created: / 30.10.1997 / 16:25:22 / cg" |
|
125 |
"Modified: / 30.10.1997 / 18:37:09 / cg" |
|
5309 | 126 |
! |
127 |
||
128 |
reset |
|
17203 | 129 |
"set the read position to the beginning of the collection. |
130 |
Because I am a read/write stream, the readLimit is set to the current write position. |
|
17245 | 131 |
Thus, the just written data can be read back." |
17203 | 132 |
|
22800 | 133 |
readLimit notNil ifTrue:[ |
134 |
readLimit := position max:readLimit. |
|
135 |
]. |
|
19883 | 136 |
position := 0. |
137 |
! |
|
138 |
||
139 |
resetPosition |
|
140 |
<resource: #obsolete> |
|
141 |
"set the read position to the beginning of the collection. |
|
142 |
Because I am a read/write stream, the readLimit is set to the current write position. |
|
143 |
Thus, the just written data can be read back." |
|
144 |
||
145 |
readLimit := position max:readLimit. |
|
146 |
position := 0. |
|
3081 | 147 |
! ! |
148 |
||
677 | 149 |
!ReadWriteStream methodsFor:'converting'! |
150 |
||
151 |
readStream |
|
20425 | 152 |
"return the receiver as a readStream - that's myself" |
2927 | 153 |
|
677 | 154 |
^ self |
155 |
||
156 |
"Created: 29.11.1995 / 22:04:23 / stefan" |
|
8140
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8098
diff
changeset
|
157 |
! |
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8098
diff
changeset
|
158 |
|
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8098
diff
changeset
|
159 |
readStreamOrNil |
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8098
diff
changeset
|
160 |
"return a readStream from the receiver. Since this is already |
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8098
diff
changeset
|
161 |
a readStream, return self. |
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 |
This method has been defined for protocol copmatibility with Filename" |
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8098
diff
changeset
|
164 |
|
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8098
diff
changeset
|
165 |
^ self |
677 | 166 |
! ! |
167 |
||
5309 | 168 |
!ReadWriteStream methodsFor:'initialization'! |
3081 | 169 |
|
5309 | 170 |
with:initialCollection |
11438 | 171 |
"redefined from WriteStream, to position to the beginning of the stream" |
172 |
||
5309 | 173 |
super with:initialCollection. |
15352 | 174 |
position := 0. |
3081 | 175 |
! ! |
176 |
||
611 | 177 |
!ReadWriteStream methodsFor:'queries'! |
178 |
||
179 |
isReadable |
|
20425 | 180 |
"return true if the receiver supports reading - that's true" |
2927 | 181 |
|
611 | 182 |
^ true |
9004 | 183 |
! |
184 |
||
185 |
size |
|
186 |
"return the number of elements in the streamed collection." |
|
187 |
||
15352 | 188 |
^ (readLimit ? 0) max:(position ? 0). |
611 | 189 |
! ! |
190 |
||
21133 | 191 |
!ReadWriteStream methodsFor:'reading'! |
192 |
||
193 |
peek |
|
194 |
"return the element to be read next without advancing read position. |
|
195 |
If there are no more elements, nil is returned." |
|
196 |
||
197 |
(position >= readLimit) ifTrue:[^ self pastEndRead]. |
|
198 |
^ collection at:(position+1) |
|
199 |
||
200 |
"Modified: 5.2.1996 / 21:57:47 / stefan" |
|
201 |
! ! |
|
202 |
||
677 | 203 |
!ReadWriteStream class methodsFor:'documentation'! |
204 |
||
205 |
version |
|
19883 | 206 |
^ '$Header$' |
15355 | 207 |
! |
208 |
||
209 |
version_CVS |
|
19883 | 210 |
^ '$Header$' |
677 | 211 |
! ! |
15352 | 212 |