author | Stefan Vogel <sv@exept.de> |
Thu, 12 Mar 2015 18:12:57 +0100 | |
changeset 17605 | 0c04420ec60a |
parent 17558 | 4a044bdacd7c |
child 17613 | e3524b50162f |
permissions | -rw-r--r-- |
17605 | 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 |
||
5932
6041c11b3370
added #nextByte for compatibility
Claus Gittinger <cg@exept.de>
parents:
5309
diff
changeset
|
107 |
nextByte |
6041c11b3370
added #nextByte for compatibility
Claus Gittinger <cg@exept.de>
parents:
5309
diff
changeset
|
108 |
"return the next element; advance read pointer. |
6041c11b3370
added #nextByte for compatibility
Claus Gittinger <cg@exept.de>
parents:
5309
diff
changeset
|
109 |
return nil, if there is no next element" |
6041c11b3370
added #nextByte for compatibility
Claus Gittinger <cg@exept.de>
parents:
5309
diff
changeset
|
110 |
|
8098
8d1d5cd8529b
nextByte should (at least) return integer when reading from a string-stream
Claus Gittinger <cg@exept.de>
parents:
7114
diff
changeset
|
111 |
^ self next asInteger |
5932
6041c11b3370
added #nextByte for compatibility
Claus Gittinger <cg@exept.de>
parents:
5309
diff
changeset
|
112 |
! |
6041c11b3370
added #nextByte for compatibility
Claus Gittinger <cg@exept.de>
parents:
5309
diff
changeset
|
113 |
|
611 | 114 |
peek |
115 |
"return the element to be read next without advancing read position. |
|
116 |
If there are no more elements, nil is returned." |
|
117 |
||
15355 | 118 |
(position >= readLimit) ifTrue:[^ self pastEndRead]. |
15352 | 119 |
^ 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
|
120 |
|
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
701
diff
changeset
|
121 |
"Modified: 5.2.1996 / 21:57:47 / stefan" |
1 | 122 |
! ! |
611 | 123 |
|
3081 | 124 |
!ReadWriteStream methodsFor:'accessing'! |
125 |
||
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). |
|
130 |
ST80 users if RWStream expect the contents array to remain |
|
131 |
unchanged." |
|
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 |
|
141 |
"set the read position to the beginning of the collection" |
|
142 |
||
143 |
self resetPosition |
|
144 |
||
17203 | 145 |
! |
146 |
||
147 |
resetPosition |
|
148 |
"set the read position to the beginning of the collection. |
|
149 |
Because I am a read/write stream, the readLimit is set to the current write position. |
|
17245 | 150 |
Thus, the just written data can be read back." |
17203 | 151 |
|
17204 | 152 |
readLimit := position max:readLimit. |
17203 | 153 |
super resetPosition |
3081 | 154 |
! ! |
155 |
||
677 | 156 |
!ReadWriteStream methodsFor:'converting'! |
157 |
||
158 |
readStream |
|
2927 | 159 |
"return the receiver as a readStream - thats myself" |
160 |
||
677 | 161 |
^ self |
162 |
||
163 |
"Created: 29.11.1995 / 22:04:23 / stefan" |
|
8140
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 |
|
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8098
diff
changeset
|
166 |
readStreamOrNil |
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8098
diff
changeset
|
167 |
"return a readStream from the receiver. Since this is already |
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8098
diff
changeset
|
168 |
a readStream, return self. |
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 |
This method has been defined for protocol copmatibility with Filename" |
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8098
diff
changeset
|
171 |
|
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8098
diff
changeset
|
172 |
^ self |
677 | 173 |
! ! |
174 |
||
5309 | 175 |
!ReadWriteStream methodsFor:'initialization'! |
3081 | 176 |
|
5309 | 177 |
with:initialCollection |
11438 | 178 |
"redefined from WriteStream, to position to the beginning of the stream" |
179 |
||
5309 | 180 |
super with:initialCollection. |
15352 | 181 |
position := 0. |
3081 | 182 |
! ! |
183 |
||
17605 | 184 |
!ReadWriteStream methodsFor:'misc functions'! |
185 |
||
186 |
copyToEndInto:aWriteStream bufferSize:bufferSize |
|
187 |
"read from the receiver, and write all data up to the end to another stream. |
|
188 |
Return the number of bytes which have been transferred. |
|
189 |
Redefined here to avoid intermediate buffers/garbage. |
|
190 |
bufferSize does not matter here." |
|
191 |
||
192 |
|cnt| |
|
193 |
||
194 |
collection notNil ifTrue:[ |
|
195 |
aWriteStream nextPutAll:collection startingAt:position+1 to:readLimit. |
|
196 |
cnt := readLimit - position. |
|
197 |
position := readLimit. |
|
198 |
^ cnt. |
|
199 |
]. |
|
200 |
^ super copyToEndInto:aWriteStream bufferSize:bufferSize. |
|
201 |
||
202 |
" |
|
203 |
|rs ws| |
|
204 |
||
205 |
ws := #() writeStream. |
|
206 |
rs := #( 1 2 3 4 ) readWriteStream. |
|
207 |
rs next. |
|
208 |
rs copyToEndInto:ws bufferSize:0. |
|
209 |
ws contents. |
|
210 |
" |
|
211 |
! ! |
|
212 |
||
611 | 213 |
!ReadWriteStream methodsFor:'queries'! |
214 |
||
215 |
isReadable |
|
2927 | 216 |
"return true if the receiver supports reading - thats true" |
217 |
||
611 | 218 |
^ true |
9004 | 219 |
! |
220 |
||
221 |
size |
|
222 |
"return the number of elements in the streamed collection." |
|
223 |
||
15352 | 224 |
^ (readLimit ? 0) max:(position ? 0). |
611 | 225 |
! ! |
226 |
||
677 | 227 |
!ReadWriteStream class methodsFor:'documentation'! |
228 |
||
229 |
version |
|
17605 | 230 |
^ '$Header: /cvs/stx/stx/libbasic/ReadWriteStream.st,v 1.38 2015-03-12 17:12:57 stefan Exp $' |
15355 | 231 |
! |
232 |
||
233 |
version_CVS |
|
17605 | 234 |
^ '$Header: /cvs/stx/stx/libbasic/ReadWriteStream.st,v 1.38 2015-03-12 17:12:57 stefan Exp $' |
677 | 235 |
! ! |
15352 | 236 |