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