ReadWriteStream.st
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--
avoid exception in #size, when there is no position
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
     1
"
5
67342904af11 *** empty log message ***
claus
parents: 3
diff changeset
     2
 COPYRIGHT (c) 1989 by Claus Gittinger
329
claus
parents: 93
diff changeset
     3
	      All Rights Reserved
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
     4
a27a279701f8 Initial revision
claus
parents:
diff changeset
     5
 This software is furnished under a license and may be used
a27a279701f8 Initial revision
claus
parents:
diff changeset
     6
 only in accordance with the terms of that license and with the
a27a279701f8 Initial revision
claus
parents:
diff changeset
     7
 inclusion of the above copyright notice.   This software may not
a27a279701f8 Initial revision
claus
parents:
diff changeset
     8
 be provided or otherwise made available to, or used by, any
a27a279701f8 Initial revision
claus
parents:
diff changeset
     9
 other person.  No title to or ownership of the software is
a27a279701f8 Initial revision
claus
parents:
diff changeset
    10
 hereby transferred.
a27a279701f8 Initial revision
claus
parents:
diff changeset
    11
"
a27a279701f8 Initial revision
claus
parents:
diff changeset
    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
a27a279701f8 Initial revision
claus
parents:
diff changeset
    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
a27a279701f8 Initial revision
claus
parents:
diff changeset
    20
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
    21
611
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    22
!ReadWriteStream class methodsFor:'documentation'!
88
81dacba7a63a *** empty log message ***
claus
parents: 41
diff changeset
    23
81dacba7a63a *** empty log message ***
claus
parents: 41
diff changeset
    24
copyright
81dacba7a63a *** empty log message ***
claus
parents: 41
diff changeset
    25
"
81dacba7a63a *** empty log message ***
claus
parents: 41
diff changeset
    26
 COPYRIGHT (c) 1989 by Claus Gittinger
329
claus
parents: 93
diff changeset
    27
	      All Rights Reserved
88
81dacba7a63a *** empty log message ***
claus
parents: 41
diff changeset
    28
81dacba7a63a *** empty log message ***
claus
parents: 41
diff changeset
    29
 This software is furnished under a license and may be used
81dacba7a63a *** empty log message ***
claus
parents: 41
diff changeset
    30
 only in accordance with the terms of that license and with the
81dacba7a63a *** empty log message ***
claus
parents: 41
diff changeset
    31
 inclusion of the above copyright notice.   This software may not
81dacba7a63a *** empty log message ***
claus
parents: 41
diff changeset
    32
 be provided or otherwise made available to, or used by, any
81dacba7a63a *** empty log message ***
claus
parents: 41
diff changeset
    33
 other person.  No title to or ownership of the software is
81dacba7a63a *** empty log message ***
claus
parents: 41
diff changeset
    34
 hereby transferred.
81dacba7a63a *** empty log message ***
claus
parents: 41
diff changeset
    35
"
81dacba7a63a *** empty log message ***
claus
parents: 41
diff changeset
    36
!
81dacba7a63a *** empty log message ***
claus
parents: 41
diff changeset
    37
81dacba7a63a *** empty log message ***
claus
parents: 41
diff changeset
    38
documentation
81dacba7a63a *** empty log message ***
claus
parents: 41
diff changeset
    39
"
81dacba7a63a *** empty log message ***
claus
parents: 41
diff changeset
    40
    ReadWriteStreams allow both read- and write-access to some collection.
81dacba7a63a *** empty log message ***
claus
parents: 41
diff changeset
    41
    To allow this, they reenable some methods blocked in WriteStream.
81dacba7a63a *** empty log message ***
claus
parents: 41
diff changeset
    42
    (being one of the seldom places, where multiple inheritance could be
81dacba7a63a *** empty log message ***
claus
parents: 41
diff changeset
    43
     of good use in smalltalk).
329
claus
parents: 93
diff changeset
    44
claus
parents: 93
diff changeset
    45
    Claus:
1295
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 970
diff changeset
    46
        I personally find the ReadStream - WriteStream - ReadWriteStream
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 970
diff changeset
    47
        organization brain-damaged. It would be better to have an attribute
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 970
diff changeset
    48
        (such as readOnly / writeOnly / readWrite) in an InternalStream subclass 
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 970
diff changeset
    49
        of Stream ...
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 970
diff changeset
    50
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 970
diff changeset
    51
    [author:]
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 970
diff changeset
    52
        Claus Gittinger
88
81dacba7a63a *** empty log message ***
claus
parents: 41
diff changeset
    53
"
5309
fa0d92f438db compatibility bug fixes
Claus Gittinger <cg@exept.de>
parents: 3081
diff changeset
    54
!
fa0d92f438db compatibility bug fixes
Claus Gittinger <cg@exept.de>
parents: 3081
diff changeset
    55
fa0d92f438db compatibility bug fixes
Claus Gittinger <cg@exept.de>
parents: 3081
diff changeset
    56
examples
fa0d92f438db compatibility bug fixes
Claus Gittinger <cg@exept.de>
parents: 3081
diff changeset
    57
"
fa0d92f438db compatibility bug fixes
Claus Gittinger <cg@exept.de>
parents: 3081
diff changeset
    58
    |s|
fa0d92f438db compatibility bug fixes
Claus Gittinger <cg@exept.de>
parents: 3081
diff changeset
    59
fa0d92f438db compatibility bug fixes
Claus Gittinger <cg@exept.de>
parents: 3081
diff changeset
    60
    s := ReadWriteStream with:'abcd'.
fa0d92f438db compatibility bug fixes
Claus Gittinger <cg@exept.de>
parents: 3081
diff changeset
    61
    s reset.
fa0d92f438db compatibility bug fixes
Claus Gittinger <cg@exept.de>
parents: 3081
diff changeset
    62
    s nextPut:$A.
fa0d92f438db compatibility bug fixes
Claus Gittinger <cg@exept.de>
parents: 3081
diff changeset
    63
    s contents       
fa0d92f438db compatibility bug fixes
Claus Gittinger <cg@exept.de>
parents: 3081
diff changeset
    64
"
329
claus
parents: 93
diff changeset
    65
! !
claus
parents: 93
diff changeset
    66
345
claus
parents: 329
diff changeset
    67
!ReadWriteStream methodsFor:'access-reading'!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    68
a27a279701f8 Initial revision
claus
parents:
diff changeset
    69
next
41
a14247b04d03 *** empty log message ***
claus
parents: 5
diff changeset
    70
    "return the next element; advance read position.
a14247b04d03 *** empty log message ***
claus
parents: 5
diff changeset
    71
     If there are no more elements, nil is returned."
a14247b04d03 *** empty log message ***
claus
parents: 5
diff changeset
    72
a14247b04d03 *** empty log message ***
claus
parents: 5
diff changeset
    73
    |element|
a14247b04d03 *** empty log message ***
claus
parents: 5
diff changeset
    74
8614
88421fbd4ab6 pastEnd -> pastEndRead
Claus Gittinger <cg@exept.de>
parents: 8140
diff changeset
    75
    ((position-ZeroPosition+1) > readLimit) ifTrue:[^ self pastEndRead].
7114
acc13967229e position fixes
Claus Gittinger <cg@exept.de>
parents: 5932
diff changeset
    76
    element := collection at:(position-ZeroPosition+1).
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    77
    position := position + 1.
41
a14247b04d03 *** empty log message ***
claus
parents: 5
diff changeset
    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
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    81
!
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    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
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    90
peek
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    91
    "return the element to be read next without advancing read position.
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    92
     If there are no more elements, nil is returned."
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    93
8614
88421fbd4ab6 pastEnd -> pastEndRead
Claus Gittinger <cg@exept.de>
parents: 8140
diff changeset
    94
    ((position-ZeroPosition+1) > readLimit) ifTrue:[^ self pastEndRead].
7114
acc13967229e position fixes
Claus Gittinger <cg@exept.de>
parents: 5932
diff changeset
    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
a27a279701f8 Initial revision
claus
parents:
diff changeset
    98
! !
611
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    99
3081
4cd54c54ed78 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2927
diff changeset
   100
!ReadWriteStream methodsFor:'accessing'!
4cd54c54ed78 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2927
diff changeset
   101
4cd54c54ed78 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2927
diff changeset
   102
contents
4cd54c54ed78 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2927
diff changeset
   103
    "return the contents as written so far;
4cd54c54ed78 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2927
diff changeset
   104
     redefined to prevent confusion resulting due to 
4cd54c54ed78 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2927
diff changeset
   105
     my superclasses optimization. (see WriteStream contents).
4cd54c54ed78 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2927
diff changeset
   106
     ST80 users if RWStream expect the contents array to remain
4cd54c54ed78 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2927
diff changeset
   107
     unchanged."
4cd54c54ed78 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2927
diff changeset
   108
7114
acc13967229e position fixes
Claus Gittinger <cg@exept.de>
parents: 5932
diff changeset
   109
    readLimit := (readLimit ? 0) max:position-ZeroPosition.
3081
4cd54c54ed78 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2927
diff changeset
   110
    ^ collection copyFrom:1 to:readLimit
4cd54c54ed78 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2927
diff changeset
   111
4cd54c54ed78 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2927
diff changeset
   112
    "Created: / 30.10.1997 / 16:25:22 / cg"
4cd54c54ed78 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2927
diff changeset
   113
    "Modified: / 30.10.1997 / 18:37:09 / cg"
5309
fa0d92f438db compatibility bug fixes
Claus Gittinger <cg@exept.de>
parents: 3081
diff changeset
   114
!
fa0d92f438db compatibility bug fixes
Claus Gittinger <cg@exept.de>
parents: 3081
diff changeset
   115
fa0d92f438db compatibility bug fixes
Claus Gittinger <cg@exept.de>
parents: 3081
diff changeset
   116
reset
fa0d92f438db compatibility bug fixes
Claus Gittinger <cg@exept.de>
parents: 3081
diff changeset
   117
    "set the read position to the beginning of the collection"
fa0d92f438db compatibility bug fixes
Claus Gittinger <cg@exept.de>
parents: 3081
diff changeset
   118
fa0d92f438db compatibility bug fixes
Claus Gittinger <cg@exept.de>
parents: 3081
diff changeset
   119
    self resetPosition
fa0d92f438db compatibility bug fixes
Claus Gittinger <cg@exept.de>
parents: 3081
diff changeset
   120
3081
4cd54c54ed78 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2927
diff changeset
   121
! !
4cd54c54ed78 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2927
diff changeset
   122
677
9ab491d2a2b4 Need methot readStream.
Stefan Vogel <sv@exept.de>
parents: 611
diff changeset
   123
!ReadWriteStream methodsFor:'converting'!
9ab491d2a2b4 Need methot readStream.
Stefan Vogel <sv@exept.de>
parents: 611
diff changeset
   124
9ab491d2a2b4 Need methot readStream.
Stefan Vogel <sv@exept.de>
parents: 611
diff changeset
   125
readStream
2927
1434c3d7f78a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
   126
    "return the receiver as a readStream - thats myself"
1434c3d7f78a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
   127
677
9ab491d2a2b4 Need methot readStream.
Stefan Vogel <sv@exept.de>
parents: 611
diff changeset
   128
    ^ self
9ab491d2a2b4 Need methot readStream.
Stefan Vogel <sv@exept.de>
parents: 611
diff changeset
   129
9ab491d2a2b4 Need methot readStream.
Stefan Vogel <sv@exept.de>
parents: 611
diff changeset
   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
9ab491d2a2b4 Need methot readStream.
Stefan Vogel <sv@exept.de>
parents: 611
diff changeset
   140
! !
9ab491d2a2b4 Need methot readStream.
Stefan Vogel <sv@exept.de>
parents: 611
diff changeset
   141
5309
fa0d92f438db compatibility bug fixes
Claus Gittinger <cg@exept.de>
parents: 3081
diff changeset
   142
!ReadWriteStream methodsFor:'initialization'!
3081
4cd54c54ed78 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2927
diff changeset
   143
5309
fa0d92f438db compatibility bug fixes
Claus Gittinger <cg@exept.de>
parents: 3081
diff changeset
   144
on:initialCollection
fa0d92f438db compatibility bug fixes
Claus Gittinger <cg@exept.de>
parents: 3081
diff changeset
   145
    super on:initialCollection.
fa0d92f438db compatibility bug fixes
Claus Gittinger <cg@exept.de>
parents: 3081
diff changeset
   146
    readLimit := 0.
fa0d92f438db compatibility bug fixes
Claus Gittinger <cg@exept.de>
parents: 3081
diff changeset
   147
!
3081
4cd54c54ed78 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2927
diff changeset
   148
5309
fa0d92f438db compatibility bug fixes
Claus Gittinger <cg@exept.de>
parents: 3081
diff changeset
   149
with:initialCollection
fa0d92f438db compatibility bug fixes
Claus Gittinger <cg@exept.de>
parents: 3081
diff changeset
   150
    super with:initialCollection.
fa0d92f438db compatibility bug fixes
Claus Gittinger <cg@exept.de>
parents: 3081
diff changeset
   151
    readLimit := initialCollection size.
7114
acc13967229e position fixes
Claus Gittinger <cg@exept.de>
parents: 5932
diff changeset
   152
    position := ZeroPosition.
3081
4cd54c54ed78 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2927
diff changeset
   153
! !
4cd54c54ed78 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2927
diff changeset
   154
611
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   155
!ReadWriteStream methodsFor:'queries'!
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   156
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   157
isReadable 
2927
1434c3d7f78a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
   158
    "return true if the receiver supports reading - thats true"
1434c3d7f78a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
   159
611
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   160
    ^ true
9004
e24e9f8fcca1 fix #size
Stefan Vogel <sv@exept.de>
parents: 8614
diff changeset
   161
!
e24e9f8fcca1 fix #size
Stefan Vogel <sv@exept.de>
parents: 8614
diff changeset
   162
e24e9f8fcca1 fix #size
Stefan Vogel <sv@exept.de>
parents: 8614
diff changeset
   163
size
e24e9f8fcca1 fix #size
Stefan Vogel <sv@exept.de>
parents: 8614
diff changeset
   164
    "return the number of elements in the streamed collection."
e24e9f8fcca1 fix #size
Stefan Vogel <sv@exept.de>
parents: 8614
diff changeset
   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
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   167
! !
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   168
677
9ab491d2a2b4 Need methot readStream.
Stefan Vogel <sv@exept.de>
parents: 611
diff changeset
   169
!ReadWriteStream class methodsFor:'documentation'!
9ab491d2a2b4 Need methot readStream.
Stefan Vogel <sv@exept.de>
parents: 611
diff changeset
   170
9ab491d2a2b4 Need methot readStream.
Stefan Vogel <sv@exept.de>
parents: 611
diff changeset
   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
9ab491d2a2b4 Need methot readStream.
Stefan Vogel <sv@exept.de>
parents: 611
diff changeset
   173
! !