author | Claus Gittinger <cg@exept.de> |
Mon, 08 Jun 2015 21:22:21 +0200 | |
changeset 18465 | e9e4bb62235f |
parent 17613 | e3524b50162f |
child 18120 | e3a375d5f6a8 |
child 19883 | 9d05c218e05c |
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 |
|
103 |
! |
|
104 |
||
5932
6041c11b3370
added #nextByte for compatibility
Claus Gittinger <cg@exept.de>
parents:
5309
diff
changeset
|
105 |
nextByte |
6041c11b3370
added #nextByte for compatibility
Claus Gittinger <cg@exept.de>
parents:
5309
diff
changeset
|
106 |
"return the next element; advance read pointer. |
6041c11b3370
added #nextByte for compatibility
Claus Gittinger <cg@exept.de>
parents:
5309
diff
changeset
|
107 |
return nil, if there is no next element" |
6041c11b3370
added #nextByte for compatibility
Claus Gittinger <cg@exept.de>
parents:
5309
diff
changeset
|
108 |
|
8098
8d1d5cd8529b
nextByte should (at least) return integer when reading from a string-stream
Claus Gittinger <cg@exept.de>
parents:
7114
diff
changeset
|
109 |
^ self next asInteger |
5932
6041c11b3370
added #nextByte for compatibility
Claus Gittinger <cg@exept.de>
parents:
5309
diff
changeset
|
110 |
! |
6041c11b3370
added #nextByte for compatibility
Claus Gittinger <cg@exept.de>
parents:
5309
diff
changeset
|
111 |
|
611 | 112 |
peek |
113 |
"return the element to be read next without advancing read position. |
|
114 |
If there are no more elements, nil is returned." |
|
115 |
||
15355 | 116 |
(position >= readLimit) ifTrue:[^ self pastEndRead]. |
15352 | 117 |
^ 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
|
118 |
|
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
701
diff
changeset
|
119 |
"Modified: 5.2.1996 / 21:57:47 / stefan" |
1 | 120 |
! ! |
611 | 121 |
|
3081 | 122 |
!ReadWriteStream methodsFor:'accessing'! |
123 |
||
124 |
contents |
|
125 |
"return the contents as written so far; |
|
126 |
redefined to prevent confusion resulting due to |
|
127 |
my superclasses optimization. (see WriteStream contents). |
|
128 |
ST80 users if RWStream expect the contents array to remain |
|
129 |
unchanged." |
|
130 |
||
15352 | 131 |
readLimit := (readLimit ? 0) max:position. |
3081 | 132 |
^ collection copyFrom:1 to:readLimit |
133 |
||
134 |
"Created: / 30.10.1997 / 16:25:22 / cg" |
|
135 |
"Modified: / 30.10.1997 / 18:37:09 / cg" |
|
5309 | 136 |
! |
137 |
||
138 |
reset |
|
139 |
"set the read position to the beginning of the collection" |
|
140 |
||
141 |
self resetPosition |
|
142 |
||
17203 | 143 |
! |
144 |
||
145 |
resetPosition |
|
146 |
"set the read position to the beginning of the collection. |
|
147 |
Because I am a read/write stream, the readLimit is set to the current write position. |
|
17245 | 148 |
Thus, the just written data can be read back." |
17203 | 149 |
|
17204 | 150 |
readLimit := position max:readLimit. |
17203 | 151 |
super resetPosition |
3081 | 152 |
! ! |
153 |
||
677 | 154 |
!ReadWriteStream methodsFor:'converting'! |
155 |
||
156 |
readStream |
|
2927 | 157 |
"return the receiver as a readStream - thats myself" |
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 |
||
184 |
isReadable |
|
2927 | 185 |
"return true if the receiver supports reading - thats true" |
186 |
||
611 | 187 |
^ true |
9004 | 188 |
! |
189 |
||
190 |
size |
|
191 |
"return the number of elements in the streamed collection." |
|
192 |
||
15352 | 193 |
^ (readLimit ? 0) max:(position ? 0). |
611 | 194 |
! ! |
195 |
||
677 | 196 |
!ReadWriteStream class methodsFor:'documentation'! |
197 |
||
198 |
version |
|
17613 | 199 |
^ '$Header: /cvs/stx/stx/libbasic/ReadWriteStream.st,v 1.39 2015-03-12 19:27:01 stefan Exp $' |
15355 | 200 |
! |
201 |
||
202 |
version_CVS |
|
17613 | 203 |
^ '$Header: /cvs/stx/stx/libbasic/ReadWriteStream.st,v 1.39 2015-03-12 19:27:01 stefan Exp $' |
677 | 204 |
! ! |
15352 | 205 |