author | Claus Gittinger <cg@exept.de> |
Tue, 09 Dec 2014 17:27:48 +0100 | |
changeset 17204 | dc2ca6d858e8 |
parent 17203 | 625087f6df36 |
child 17245 | 4832da8d7f31 |
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 |
|
1 | 14 |
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
|
15 |
instanceVariableNames:'' |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
701
diff
changeset
|
16 |
classVariableNames:'' |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
701
diff
changeset
|
17 |
poolDictionaries:'' |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
701
diff
changeset
|
18 |
category:'Streams' |
1 | 19 |
! |
20 |
||
611 | 21 |
!ReadWriteStream class methodsFor:'documentation'! |
88 | 22 |
|
23 |
copyright |
|
24 |
" |
|
25 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
329 | 26 |
All Rights Reserved |
88 | 27 |
|
28 |
This software is furnished under a license and may be used |
|
29 |
only in accordance with the terms of that license and with the |
|
30 |
inclusion of the above copyright notice. This software may not |
|
31 |
be provided or otherwise made available to, or used by, any |
|
32 |
other person. No title to or ownership of the software is |
|
33 |
hereby transferred. |
|
34 |
" |
|
35 |
! |
|
36 |
||
37 |
documentation |
|
38 |
" |
|
39 |
ReadWriteStreams allow both read- and write-access to some collection. |
|
40 |
To allow this, they reenable some methods blocked in WriteStream. |
|
41 |
(being one of the seldom places, where multiple inheritance could be |
|
42 |
of good use in smalltalk). |
|
329 | 43 |
|
44 |
Claus: |
|
1295 | 45 |
I personally find the ReadStream - WriteStream - ReadWriteStream |
46 |
organization brain-damaged. It would be better to have an attribute |
|
47 |
(such as readOnly / writeOnly / readWrite) in an InternalStream subclass |
|
48 |
of Stream ... |
|
49 |
||
50 |
[author:] |
|
51 |
Claus Gittinger |
|
88 | 52 |
" |
5309 | 53 |
! |
54 |
||
55 |
examples |
|
56 |
" |
|
57 |
|s| |
|
58 |
||
59 |
s := ReadWriteStream with:'abcd'. |
|
60 |
s reset. |
|
61 |
s nextPut:$A. |
|
62 |
s contents |
|
63 |
" |
|
329 | 64 |
! ! |
65 |
||
345 | 66 |
!ReadWriteStream methodsFor:'access-reading'! |
1 | 67 |
|
68 |
next |
|
41 | 69 |
"return the next element; advance read position. |
70 |
If there are no more elements, nil is returned." |
|
71 |
||
72 |
|element| |
|
73 |
||
15355 | 74 |
(position >= readLimit) ifTrue:[^ self pastEndRead]. |
15352 | 75 |
element := collection at:(position+1). |
1 | 76 |
position := position + 1. |
41 | 77 |
^ element |
970
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
701
diff
changeset
|
78 |
|
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
701
diff
changeset
|
79 |
"Modified: 5.2.1996 / 21:57:40 / stefan" |
611 | 80 |
! |
81 |
||
16196 | 82 |
next:count |
83 |
"return the next count elements of the stream as aCollection, |
|
84 |
which depends on the streams type - (see #contentsSpecies)." |
|
85 |
||
86 |
|answer| |
|
87 |
||
88 |
self contentsSpecies == collection class ifTrue:[ |
|
89 |
((position + count) > readLimit) ifFalse:[ |
|
90 |
answer := collection copyFrom:position+1 to:position+count. |
|
91 |
position := position+count. |
|
92 |
^ answer |
|
93 |
]. |
|
94 |
]. |
|
95 |
^ super next:count |
|
96 |
! |
|
97 |
||
5932
6041c11b3370
added #nextByte for compatibility
Claus Gittinger <cg@exept.de>
parents:
5309
diff
changeset
|
98 |
nextByte |
6041c11b3370
added #nextByte for compatibility
Claus Gittinger <cg@exept.de>
parents:
5309
diff
changeset
|
99 |
"return the next element; advance read pointer. |
6041c11b3370
added #nextByte for compatibility
Claus Gittinger <cg@exept.de>
parents:
5309
diff
changeset
|
100 |
return nil, if there is no next element" |
6041c11b3370
added #nextByte for compatibility
Claus Gittinger <cg@exept.de>
parents:
5309
diff
changeset
|
101 |
|
8098
8d1d5cd8529b
nextByte should (at least) return integer when reading from a string-stream
Claus Gittinger <cg@exept.de>
parents:
7114
diff
changeset
|
102 |
^ self next asInteger |
5932
6041c11b3370
added #nextByte for compatibility
Claus Gittinger <cg@exept.de>
parents:
5309
diff
changeset
|
103 |
! |
6041c11b3370
added #nextByte for compatibility
Claus Gittinger <cg@exept.de>
parents:
5309
diff
changeset
|
104 |
|
611 | 105 |
peek |
106 |
"return the element to be read next without advancing read position. |
|
107 |
If there are no more elements, nil is returned." |
|
108 |
||
15355 | 109 |
(position >= readLimit) ifTrue:[^ self pastEndRead]. |
15352 | 110 |
^ 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
|
111 |
|
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
701
diff
changeset
|
112 |
"Modified: 5.2.1996 / 21:57:47 / stefan" |
1 | 113 |
! ! |
611 | 114 |
|
3081 | 115 |
!ReadWriteStream methodsFor:'accessing'! |
116 |
||
117 |
contents |
|
118 |
"return the contents as written so far; |
|
119 |
redefined to prevent confusion resulting due to |
|
120 |
my superclasses optimization. (see WriteStream contents). |
|
121 |
ST80 users if RWStream expect the contents array to remain |
|
122 |
unchanged." |
|
123 |
||
15352 | 124 |
readLimit := (readLimit ? 0) max:position. |
3081 | 125 |
^ collection copyFrom:1 to:readLimit |
126 |
||
127 |
"Created: / 30.10.1997 / 16:25:22 / cg" |
|
128 |
"Modified: / 30.10.1997 / 18:37:09 / cg" |
|
5309 | 129 |
! |
130 |
||
131 |
reset |
|
132 |
"set the read position to the beginning of the collection" |
|
133 |
||
134 |
self resetPosition |
|
135 |
||
17203 | 136 |
! |
137 |
||
138 |
resetPosition |
|
139 |
"set the read position to the beginning of the collection. |
|
140 |
Because I am a read/write stream, the readLimit is set to the current write position. |
|
141 |
Thus, the just written data can be readback." |
|
142 |
||
17204 | 143 |
readLimit := position max:readLimit. |
17203 | 144 |
super resetPosition |
3081 | 145 |
! ! |
146 |
||
677 | 147 |
!ReadWriteStream methodsFor:'converting'! |
148 |
||
149 |
readStream |
|
2927 | 150 |
"return the receiver as a readStream - thats myself" |
151 |
||
677 | 152 |
^ self |
153 |
||
154 |
"Created: 29.11.1995 / 22:04:23 / stefan" |
|
8140
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8098
diff
changeset
|
155 |
! |
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8098
diff
changeset
|
156 |
|
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8098
diff
changeset
|
157 |
readStreamOrNil |
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8098
diff
changeset
|
158 |
"return a readStream from the receiver. Since this is already |
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8098
diff
changeset
|
159 |
a readStream, return self. |
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8098
diff
changeset
|
160 |
|
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8098
diff
changeset
|
161 |
This method has been defined for protocol copmatibility with Filename" |
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 |
^ self |
677 | 164 |
! ! |
165 |
||
5309 | 166 |
!ReadWriteStream methodsFor:'initialization'! |
3081 | 167 |
|
5309 | 168 |
with:initialCollection |
11438 | 169 |
"redefined from WriteStream, to position to the beginning of the stream" |
170 |
||
5309 | 171 |
super with:initialCollection. |
15352 | 172 |
position := 0. |
3081 | 173 |
! ! |
174 |
||
611 | 175 |
!ReadWriteStream methodsFor:'queries'! |
176 |
||
177 |
isReadable |
|
2927 | 178 |
"return true if the receiver supports reading - thats true" |
179 |
||
611 | 180 |
^ true |
9004 | 181 |
! |
182 |
||
183 |
size |
|
184 |
"return the number of elements in the streamed collection." |
|
185 |
||
15352 | 186 |
^ (readLimit ? 0) max:(position ? 0). |
611 | 187 |
! ! |
188 |
||
677 | 189 |
!ReadWriteStream class methodsFor:'documentation'! |
190 |
||
191 |
version |
|
17204 | 192 |
^ '$Header: /cvs/stx/stx/libbasic/ReadWriteStream.st,v 1.35 2014-12-09 16:27:48 cg Exp $' |
15355 | 193 |
! |
194 |
||
195 |
version_CVS |
|
17204 | 196 |
^ '$Header: /cvs/stx/stx/libbasic/ReadWriteStream.st,v 1.35 2014-12-09 16:27:48 cg Exp $' |
677 | 197 |
! ! |
15352 | 198 |