author | Stefan Vogel <sv@exept.de> |
Fri, 27 Oct 2017 16:14:37 +0200 | |
branch | expecco_2_11_1_branch |
changeset 22329 | 20662662693b |
parent 20425 | a859a0719330 |
child 20578 | 39641ba8d6e0 |
child 21133 | b367d15fb612 |
permissions | -rw-r--r-- |
20425 | 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 |
|
105 |
! |
|
106 |
||
611 | 107 |
peek |
108 |
"return the element to be read next without advancing read position. |
|
109 |
If there are no more elements, nil is returned." |
|
110 |
||
15355 | 111 |
(position >= readLimit) ifTrue:[^ self pastEndRead]. |
15352 | 112 |
^ collection at:(position+1) |
970
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
701
diff
changeset
|
113 |
|
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
701
diff
changeset
|
114 |
"Modified: 5.2.1996 / 21:57:47 / stefan" |
1 | 115 |
! ! |
611 | 116 |
|
3081 | 117 |
!ReadWriteStream methodsFor:'accessing'! |
118 |
||
19883 | 119 |
clear |
120 |
"re-initialize the stream to write to the beginning and |
|
121 |
to not read any data." |
|
122 |
||
123 |
position := readLimit := 0. |
|
124 |
! |
|
125 |
||
3081 | 126 |
contents |
127 |
"return the contents as written so far; |
|
128 |
redefined to prevent confusion resulting due to |
|
129 |
my superclasses optimization. (see WriteStream contents). |
|
20098 | 130 |
ST80 users of ReadWriteStream may expect the contents array to remain |
131 |
unchanged, which we do not guarantee." |
|
3081 | 132 |
|
15352 | 133 |
readLimit := (readLimit ? 0) max:position. |
3081 | 134 |
^ collection copyFrom:1 to:readLimit |
135 |
||
136 |
"Created: / 30.10.1997 / 16:25:22 / cg" |
|
137 |
"Modified: / 30.10.1997 / 18:37:09 / cg" |
|
5309 | 138 |
! |
139 |
||
140 |
reset |
|
17203 | 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. |
|
17245 | 143 |
Thus, the just written data can be read back." |
17203 | 144 |
|
17204 | 145 |
readLimit := position max:readLimit. |
19883 | 146 |
position := 0. |
147 |
! |
|
148 |
||
149 |
resetPosition |
|
150 |
<resource: #obsolete> |
|
151 |
"set the read position to the beginning of the collection. |
|
152 |
Because I am a read/write stream, the readLimit is set to the current write position. |
|
153 |
Thus, the just written data can be read back." |
|
154 |
||
155 |
readLimit := position max:readLimit. |
|
156 |
position := 0. |
|
3081 | 157 |
! ! |
158 |
||
677 | 159 |
!ReadWriteStream methodsFor:'converting'! |
160 |
||
161 |
readStream |
|
20425 | 162 |
"return the receiver as a readStream - that's myself" |
2927 | 163 |
|
677 | 164 |
^ self |
165 |
||
166 |
"Created: 29.11.1995 / 22:04:23 / stefan" |
|
8140
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 |
|
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8098
diff
changeset
|
169 |
readStreamOrNil |
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8098
diff
changeset
|
170 |
"return a readStream from the receiver. Since this is already |
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8098
diff
changeset
|
171 |
a readStream, return self. |
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8098
diff
changeset
|
172 |
|
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8098
diff
changeset
|
173 |
This method has been defined for protocol copmatibility with Filename" |
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8098
diff
changeset
|
174 |
|
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8098
diff
changeset
|
175 |
^ self |
677 | 176 |
! ! |
177 |
||
5309 | 178 |
!ReadWriteStream methodsFor:'initialization'! |
3081 | 179 |
|
5309 | 180 |
with:initialCollection |
11438 | 181 |
"redefined from WriteStream, to position to the beginning of the stream" |
182 |
||
5309 | 183 |
super with:initialCollection. |
15352 | 184 |
position := 0. |
3081 | 185 |
! ! |
186 |
||
611 | 187 |
!ReadWriteStream methodsFor:'queries'! |
188 |
||
189 |
isReadable |
|
20425 | 190 |
"return true if the receiver supports reading - that's true" |
2927 | 191 |
|
611 | 192 |
^ true |
9004 | 193 |
! |
194 |
||
195 |
size |
|
196 |
"return the number of elements in the streamed collection." |
|
197 |
||
15352 | 198 |
^ (readLimit ? 0) max:(position ? 0). |
611 | 199 |
! ! |
200 |
||
677 | 201 |
!ReadWriteStream class methodsFor:'documentation'! |
202 |
||
203 |
version |
|
19883 | 204 |
^ '$Header$' |
15355 | 205 |
! |
206 |
||
207 |
version_CVS |
|
19883 | 208 |
^ '$Header$' |
677 | 209 |
! ! |
15352 | 210 |