author | Jan Vrany <jan.vrany@fit.cvut.cz> |
Thu, 29 Sep 2011 16:44:37 +0100 | |
branch | jv |
changeset 17869 | 9610c6c94e71 |
parent 17865 | 598963c6ff8e |
child 17892 | d86c8bd5ece3 |
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 |
||
8614 | 74 |
((position-ZeroPosition+1) > readLimit) ifTrue:[^ self pastEndRead]. |
7114 | 75 |
element := collection at:(position-ZeroPosition+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 |
||
5932
6041c11b3370
added #nextByte for compatibility
Claus Gittinger <cg@exept.de>
parents:
5309
diff
changeset
|
82 |
nextByte |
6041c11b3370
added #nextByte for compatibility
Claus Gittinger <cg@exept.de>
parents:
5309
diff
changeset
|
83 |
"return the next element; advance read pointer. |
6041c11b3370
added #nextByte for compatibility
Claus Gittinger <cg@exept.de>
parents:
5309
diff
changeset
|
84 |
return nil, if there is no next element" |
6041c11b3370
added #nextByte for compatibility
Claus Gittinger <cg@exept.de>
parents:
5309
diff
changeset
|
85 |
|
8098
8d1d5cd8529b
nextByte should (at least) return integer when reading from a string-stream
Claus Gittinger <cg@exept.de>
parents:
7114
diff
changeset
|
86 |
^ self next asInteger |
5932
6041c11b3370
added #nextByte for compatibility
Claus Gittinger <cg@exept.de>
parents:
5309
diff
changeset
|
87 |
! |
6041c11b3370
added #nextByte for compatibility
Claus Gittinger <cg@exept.de>
parents:
5309
diff
changeset
|
88 |
|
611 | 89 |
peek |
90 |
"return the element to be read next without advancing read position. |
|
91 |
If there are no more elements, nil is returned." |
|
92 |
||
8614 | 93 |
((position-ZeroPosition+1) > readLimit) ifTrue:[^ self pastEndRead]. |
7114 | 94 |
^ 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
|
95 |
|
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
701
diff
changeset
|
96 |
"Modified: 5.2.1996 / 21:57:47 / stefan" |
1 | 97 |
! ! |
611 | 98 |
|
3081 | 99 |
!ReadWriteStream methodsFor:'accessing'! |
100 |
||
101 |
contents |
|
102 |
"return the contents as written so far; |
|
103 |
redefined to prevent confusion resulting due to |
|
104 |
my superclasses optimization. (see WriteStream contents). |
|
105 |
ST80 users if RWStream expect the contents array to remain |
|
106 |
unchanged." |
|
107 |
||
7114 | 108 |
readLimit := (readLimit ? 0) max:position-ZeroPosition. |
3081 | 109 |
^ collection copyFrom:1 to:readLimit |
110 |
||
111 |
"Created: / 30.10.1997 / 16:25:22 / cg" |
|
112 |
"Modified: / 30.10.1997 / 18:37:09 / cg" |
|
5309 | 113 |
! |
114 |
||
115 |
reset |
|
116 |
"set the read position to the beginning of the collection" |
|
117 |
||
118 |
self resetPosition |
|
119 |
||
3081 | 120 |
! ! |
121 |
||
677 | 122 |
!ReadWriteStream methodsFor:'converting'! |
123 |
||
124 |
readStream |
|
2927 | 125 |
"return the receiver as a readStream - thats myself" |
126 |
||
677 | 127 |
^ self |
128 |
||
129 |
"Created: 29.11.1995 / 22:04:23 / stefan" |
|
8140
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8098
diff
changeset
|
130 |
! |
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 |
readStreamOrNil |
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8098
diff
changeset
|
133 |
"return a readStream from the receiver. Since this is already |
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8098
diff
changeset
|
134 |
a readStream, return self. |
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8098
diff
changeset
|
135 |
|
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8098
diff
changeset
|
136 |
This method has been defined for protocol copmatibility with Filename" |
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8098
diff
changeset
|
137 |
|
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8098
diff
changeset
|
138 |
^ self |
677 | 139 |
! ! |
140 |
||
5309 | 141 |
!ReadWriteStream methodsFor:'initialization'! |
3081 | 142 |
|
5309 | 143 |
with:initialCollection |
11438 | 144 |
"redefined from WriteStream, to position to the beginning of the stream" |
145 |
||
5309 | 146 |
super with:initialCollection. |
7114 | 147 |
position := ZeroPosition. |
3081 | 148 |
! ! |
149 |
||
611 | 150 |
!ReadWriteStream methodsFor:'queries'! |
151 |
||
152 |
isReadable |
|
2927 | 153 |
"return true if the receiver supports reading - thats true" |
154 |
||
611 | 155 |
^ true |
9004 | 156 |
! |
157 |
||
158 |
size |
|
159 |
"return the number of elements in the streamed collection." |
|
160 |
||
9303
ec12016ead14
size computation w.r.t. ZeroPosition
Stefan Vogel <sv@exept.de>
parents:
9285
diff
changeset
|
161 |
^ (readLimit ? 0) max:(position ? ZeroPosition)-ZeroPosition. |
611 | 162 |
! ! |
163 |
||
677 | 164 |
!ReadWriteStream class methodsFor:'documentation'! |
165 |
||
166 |
version |
|
17869 | 167 |
^ '$Id: ReadWriteStream.st 10700 2011-09-29 15:44:37Z vranyj1 $' |
17865 | 168 |
! |
169 |
||
170 |
version_SVN |
|
17869 | 171 |
^ '$Id: ReadWriteStream.st 10700 2011-09-29 15:44:37Z vranyj1 $' |
17846 | 172 |
! ! |