PositionableStream.st
author Claus Gittinger <cg@exept.de>
Tue, 09 Jul 2019 20:55:17 +0200
changeset 24417 03b083548da2
parent 23895 d49a46365ed5
child 24744 2d27b0eb2d4a
permissions -rw-r--r--
#REFACTORING by exept class: Smalltalk class changed: #recursiveInstallAutoloadedClassesFrom:rememberIn:maxLevels:noAutoload:packageTop:showSplashInLevels: Transcript showCR:(... bindWith:...) -> Transcript showCR:... with:...
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
23895
d49a46365ed5 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22514
diff changeset
     1
"{ Encoding: utf8 }"
d49a46365ed5 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22514
diff changeset
     2
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
     3
"
5
67342904af11 *** empty log message ***
claus
parents: 3
diff changeset
     4
 COPYRIGHT (c) 1989 by Claus Gittinger
159
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
     5
	      All Rights Reserved
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
     6
a27a279701f8 Initial revision
claus
parents:
diff changeset
     7
 This software is furnished under a license and may be used
a27a279701f8 Initial revision
claus
parents:
diff changeset
     8
 only in accordance with the terms of that license and with the
a27a279701f8 Initial revision
claus
parents:
diff changeset
     9
 inclusion of the above copyright notice.   This software may not
a27a279701f8 Initial revision
claus
parents:
diff changeset
    10
 be provided or otherwise made available to, or used by, any
a27a279701f8 Initial revision
claus
parents:
diff changeset
    11
 other person.  No title to or ownership of the software is
a27a279701f8 Initial revision
claus
parents:
diff changeset
    12
 hereby transferred.
a27a279701f8 Initial revision
claus
parents:
diff changeset
    13
"
5393
7e2230bd4bad moved some stuff to superclass
Claus Gittinger <cg@exept.de>
parents: 5308
diff changeset
    14
"{ Package: 'stx:libbasic' }"
7e2230bd4bad moved some stuff to superclass
Claus Gittinger <cg@exept.de>
parents: 5308
diff changeset
    15
17559
010577abda05 class: PositionableStream
Claus Gittinger <cg@exept.de>
parents: 16876
diff changeset
    16
"{ NameSpace: Smalltalk }"
010577abda05 class: PositionableStream
Claus Gittinger <cg@exept.de>
parents: 16876
diff changeset
    17
57
db9677479d35 *** empty log message ***
claus
parents: 44
diff changeset
    18
PeekableStream subclass:#PositionableStream
973
5bb2473bf1ef checkin from browser
Claus Gittinger <cg@exept.de>
parents: 875
diff changeset
    19
	instanceVariableNames:'collection position readLimit writeLimit'
8443
7bc4348c059e fileIn stuff moved to peekableStream
Claus Gittinger <cg@exept.de>
parents: 8363
diff changeset
    20
	classVariableNames:'ZeroPosition InvalidPositionErrorSignal'
973
5bb2473bf1ef checkin from browser
Claus Gittinger <cg@exept.de>
parents: 875
diff changeset
    21
	poolDictionaries:''
5bb2473bf1ef checkin from browser
Claus Gittinger <cg@exept.de>
parents: 875
diff changeset
    22
	category:'Streams'
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    23
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
    24
1897
22d1ce9b62a4 fixes for packageQuery
Claus Gittinger <cg@exept.de>
parents: 1489
diff changeset
    25
!PositionableStream class methodsFor:'documentation'!
88
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    26
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    27
copyright
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    28
"
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    29
 COPYRIGHT (c) 1989 by Claus Gittinger
159
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
    30
	      All Rights Reserved
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    31
88
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    32
 This software is furnished under a license and may be used
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    33
 only in accordance with the terms of that license and with the
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    34
 inclusion of the above copyright notice.   This software may not
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    35
 be provided or otherwise made available to, or used by, any
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    36
 other person.  No title to or ownership of the software is
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    37
 hereby transferred.
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    38
"
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    39
!
10
claus
parents: 5
diff changeset
    40
88
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    41
documentation
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    42
"
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    43
    Instances of PositionableStream allow positioning the read pointer.
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    44
    The PositionableStream class also adds methods for source-chunk reading
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    45
    and writing, and for filing-in/out of source code.
3249
8401c3e42165 checkin to force re-checkout.
Claus Gittinger <cg@exept.de>
parents: 3184
diff changeset
    46
88
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    47
    This is an abstract class.
1295
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1086
diff changeset
    48
4405
247058206089 added ZeroPosition;
Claus Gittinger <cg@exept.de>
parents: 4325
diff changeset
    49
    Compatibility Notice:
9434
8700cca5c249 comment
Claus Gittinger <cg@exept.de>
parents: 9284
diff changeset
    50
        In previous versions of ST/X, streams started with a 1-position (i.e. as in collections),
8700cca5c249 comment
Claus Gittinger <cg@exept.de>
parents: 9284
diff changeset
    51
        while ST-80 has always been using 0-based postions for streams and 1-based positions for collections.
4405
247058206089 added ZeroPosition;
Claus Gittinger <cg@exept.de>
parents: 4325
diff changeset
    52
17559
010577abda05 class: PositionableStream
Claus Gittinger <cg@exept.de>
parents: 16876
diff changeset
    53
        THIS CERTAINLY IS BAD.
010577abda05 class: PositionableStream
Claus Gittinger <cg@exept.de>
parents: 16876
diff changeset
    54
010577abda05 class: PositionableStream
Claus Gittinger <cg@exept.de>
parents: 16876
diff changeset
    55
        Although this is confusing ST/X has been changed to now also uses 0-based stream positioning.
9434
8700cca5c249 comment
Claus Gittinger <cg@exept.de>
parents: 9284
diff changeset
    56
17559
010577abda05 class: PositionableStream
Claus Gittinger <cg@exept.de>
parents: 16876
diff changeset
    57
    [caveat:]
010577abda05 class: PositionableStream
Claus Gittinger <cg@exept.de>
parents: 16876
diff changeset
    58
        Basing capabilities like readability/writability/positionability/peekability on inheritance makes
010577abda05 class: PositionableStream
Claus Gittinger <cg@exept.de>
parents: 16876
diff changeset
    59
        the class hierarchy ugly and leads to strange and hard to teach redefinitions (aka. NonPositionableStream
010577abda05 class: PositionableStream
Claus Gittinger <cg@exept.de>
parents: 16876
diff changeset
    60
        below PositionableStream or ExternalReadStream under WriteStream)
4405
247058206089 added ZeroPosition;
Claus Gittinger <cg@exept.de>
parents: 4325
diff changeset
    61
1295
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1086
diff changeset
    62
    [author:]
9434
8700cca5c249 comment
Claus Gittinger <cg@exept.de>
parents: 9284
diff changeset
    63
        Claus Gittinger
88
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    64
"
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    65
! !
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    66
1897
22d1ce9b62a4 fixes for packageQuery
Claus Gittinger <cg@exept.de>
parents: 1489
diff changeset
    67
!PositionableStream class methodsFor:'initialization'!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    68
10
claus
parents: 5
diff changeset
    69
initialize
15353
0a60d904c4c0 class: PositionableStream
Claus Gittinger <cg@exept.de>
parents: 15348
diff changeset
    70
    ZeroPosition := 0.        "/ changed with stx rel5.1; 
0a60d904c4c0 class: PositionableStream
Claus Gittinger <cg@exept.de>
parents: 15348
diff changeset
    71
                              "/ rel6.2: no longer used (i.e. 0 has now ben hardcoded)
0a60d904c4c0 class: PositionableStream
Claus Gittinger <cg@exept.de>
parents: 15348
diff changeset
    72
                              "/ class variable to be eliminated.
7363
721c2a72449a stream position now 0-based
Claus Gittinger <cg@exept.de>
parents: 7261
diff changeset
    73
8443
7bc4348c059e fileIn stuff moved to peekableStream
Claus Gittinger <cg@exept.de>
parents: 8363
diff changeset
    74
    InvalidPositionErrorSignal isNil ifTrue:[
7092
630807cd320f Convert Object>>errorSignal -> Error
Stefan Vogel <sv@exept.de>
parents: 7054
diff changeset
    75
        InvalidPositionErrorSignal := PositionOutOfBoundsError.
630807cd320f Convert Object>>errorSignal -> Error
Stefan Vogel <sv@exept.de>
parents: 7054
diff changeset
    76
        InvalidPositionErrorSignal notifierString:'position out of bounds: '.
10
claus
parents: 5
diff changeset
    77
    ]
claus
parents: 5
diff changeset
    78
! !
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    79
1897
22d1ce9b62a4 fixes for packageQuery
Claus Gittinger <cg@exept.de>
parents: 1489
diff changeset
    80
!PositionableStream class methodsFor:'instance creation'!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    81
a27a279701f8 Initial revision
claus
parents:
diff changeset
    82
on:aCollection
a27a279701f8 Initial revision
claus
parents:
diff changeset
    83
    "return a new PositionableStream streaming on aCollection"
a27a279701f8 Initial revision
claus
parents:
diff changeset
    84
a27a279701f8 Initial revision
claus
parents:
diff changeset
    85
    ^ (self basicNew) on:aCollection
a27a279701f8 Initial revision
claus
parents:
diff changeset
    86
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
    87
a27a279701f8 Initial revision
claus
parents:
diff changeset
    88
on:aCollection from:first to:last
a27a279701f8 Initial revision
claus
parents:
diff changeset
    89
    "return a new PositionableStream streaming on aCollection
a27a279701f8 Initial revision
claus
parents:
diff changeset
    90
     from first to last"
a27a279701f8 Initial revision
claus
parents:
diff changeset
    91
369
claus
parents: 362
diff changeset
    92
    ^ (self basicNew) on:aCollection from:first to:last
57
db9677479d35 *** empty log message ***
claus
parents: 44
diff changeset
    93
!
db9677479d35 *** empty log message ***
claus
parents: 44
diff changeset
    94
db9677479d35 *** empty log message ***
claus
parents: 44
diff changeset
    95
with:aCollection
db9677479d35 *** empty log message ***
claus
parents: 44
diff changeset
    96
    "return a new PositionableStream streaming on aCollection,
db9677479d35 *** empty log message ***
claus
parents: 44
diff changeset
    97
     the stream is positioned to the end of the collection."
db9677479d35 *** empty log message ***
claus
parents: 44
diff changeset
    98
362
claus
parents: 360
diff changeset
    99
    ^ (self basicNew) with:aCollection
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   100
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
   101
3032
d267a0b1b48d handle empty chunks when filing in
Claus Gittinger <cg@exept.de>
parents: 2966
diff changeset
   102
!PositionableStream class methodsFor:'Signal constants'!
d267a0b1b48d handle empty chunks when filing in
Claus Gittinger <cg@exept.de>
parents: 2966
diff changeset
   103
d267a0b1b48d handle empty chunks when filing in
Claus Gittinger <cg@exept.de>
parents: 2966
diff changeset
   104
invalidPositionErrorSignal
d267a0b1b48d handle empty chunks when filing in
Claus Gittinger <cg@exept.de>
parents: 2966
diff changeset
   105
    "return the signal raised if positioning is attempted to an
d267a0b1b48d handle empty chunks when filing in
Claus Gittinger <cg@exept.de>
parents: 2966
diff changeset
   106
     invalid position (i.e. before the begin of the stream or after
d267a0b1b48d handle empty chunks when filing in
Claus Gittinger <cg@exept.de>
parents: 2966
diff changeset
   107
     the end)"
d267a0b1b48d handle empty chunks when filing in
Claus Gittinger <cg@exept.de>
parents: 2966
diff changeset
   108
8320
229adaeaf183 Use exception classes instead of class vars
Stefan Vogel <sv@exept.de>
parents: 8318
diff changeset
   109
    ^ PositionOutOfBoundsError
3032
d267a0b1b48d handle empty chunks when filing in
Claus Gittinger <cg@exept.de>
parents: 2966
diff changeset
   110
! !
d267a0b1b48d handle empty chunks when filing in
Claus Gittinger <cg@exept.de>
parents: 2966
diff changeset
   111
1897
22d1ce9b62a4 fixes for packageQuery
Claus Gittinger <cg@exept.de>
parents: 1489
diff changeset
   112
!PositionableStream class methodsFor:'constants'!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   113
4405
247058206089 added ZeroPosition;
Claus Gittinger <cg@exept.de>
parents: 4325
diff changeset
   114
zeroPosition
247058206089 added ZeroPosition;
Claus Gittinger <cg@exept.de>
parents: 4325
diff changeset
   115
    "return the number, which marks the initial position.
247058206089 added ZeroPosition;
Claus Gittinger <cg@exept.de>
parents: 4325
diff changeset
   116
     Compatibility Notice:
9434
8700cca5c249 comment
Claus Gittinger <cg@exept.de>
parents: 9284
diff changeset
   117
        In previous versions of ST/X, streams started with a 1-position (i.e. as in collections),
8700cca5c249 comment
Claus Gittinger <cg@exept.de>
parents: 9284
diff changeset
   118
        while ST-80 has always been using 0-based postions for streams and 1-based positions for collections.
4405
247058206089 added ZeroPosition;
Claus Gittinger <cg@exept.de>
parents: 4325
diff changeset
   119
9434
8700cca5c249 comment
Claus Gittinger <cg@exept.de>
parents: 9284
diff changeset
   120
     Although this is confusing ST/X has been changed to now also use 0-based stream positioning.
8700cca5c249 comment
Claus Gittinger <cg@exept.de>
parents: 9284
diff changeset
   121
    "
4405
247058206089 added ZeroPosition;
Claus Gittinger <cg@exept.de>
parents: 4325
diff changeset
   122
15353
0a60d904c4c0 class: PositionableStream
Claus Gittinger <cg@exept.de>
parents: 15348
diff changeset
   123
    ^ 0
9434
8700cca5c249 comment
Claus Gittinger <cg@exept.de>
parents: 9284
diff changeset
   124
8700cca5c249 comment
Claus Gittinger <cg@exept.de>
parents: 9284
diff changeset
   125
    "Modified: / 13-07-2006 / 20:36:54 / cg"
360
claus
parents: 329
diff changeset
   126
! !
claus
parents: 329
diff changeset
   127
21034
a5700813414f #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 20691
diff changeset
   128
!PositionableStream class methodsFor:'testing'!
a5700813414f #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 20691
diff changeset
   129
a5700813414f #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 20691
diff changeset
   130
isAbstract
a5700813414f #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 20691
diff changeset
   131
    ^ self == PositionableStream
a5700813414f #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 20691
diff changeset
   132
! !
a5700813414f #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 20691
diff changeset
   133
23895
d49a46365ed5 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22514
diff changeset
   134
7261
f35fc9cee675 method category rename
Claus Gittinger <cg@exept.de>
parents: 7226
diff changeset
   135
!PositionableStream methodsFor:'Compatibility-Dolphin'!
6413
dc8d492a39ff dolphin compatibility
Claus Gittinger <cg@exept.de>
parents: 6361
diff changeset
   136
dc8d492a39ff dolphin compatibility
Claus Gittinger <cg@exept.de>
parents: 6361
diff changeset
   137
endChunk
dc8d492a39ff dolphin compatibility
Claus Gittinger <cg@exept.de>
parents: 6361
diff changeset
   138
    self nextPutChunkSeparator
dc8d492a39ff dolphin compatibility
Claus Gittinger <cg@exept.de>
parents: 6361
diff changeset
   139
! !
dc8d492a39ff dolphin compatibility
Claus Gittinger <cg@exept.de>
parents: 6361
diff changeset
   140
9284
6afd161ee43c +skipTo:
Claus Gittinger <cg@exept.de>
parents: 9283
diff changeset
   141
!PositionableStream methodsFor:'Compatibility-ST/V'!
6afd161ee43c +skipTo:
Claus Gittinger <cg@exept.de>
parents: 9283
diff changeset
   142
6afd161ee43c +skipTo:
Claus Gittinger <cg@exept.de>
parents: 9283
diff changeset
   143
skipTo:anElement
6afd161ee43c +skipTo:
Claus Gittinger <cg@exept.de>
parents: 9283
diff changeset
   144
    "ST/V compatibility:
6afd161ee43c +skipTo:
Claus Gittinger <cg@exept.de>
parents: 9283
diff changeset
   145
     skip for the element given by the argument, anElement;
6afd161ee43c +skipTo:
Claus Gittinger <cg@exept.de>
parents: 9283
diff changeset
   146
     return nil if not found, self otherwise. 
6afd161ee43c +skipTo:
Claus Gittinger <cg@exept.de>
parents: 9283
diff changeset
   147
     On a successful match, the next read will return the element after anElement."
6afd161ee43c +skipTo:
Claus Gittinger <cg@exept.de>
parents: 9283
diff changeset
   148
6afd161ee43c +skipTo:
Claus Gittinger <cg@exept.de>
parents: 9283
diff changeset
   149
    ^ self skipThrough:anElement
6afd161ee43c +skipTo:
Claus Gittinger <cg@exept.de>
parents: 9283
diff changeset
   150
6afd161ee43c +skipTo:
Claus Gittinger <cg@exept.de>
parents: 9283
diff changeset
   151
    "
6afd161ee43c +skipTo:
Claus Gittinger <cg@exept.de>
parents: 9283
diff changeset
   152
     |s|
6afd161ee43c +skipTo:
Claus Gittinger <cg@exept.de>
parents: 9283
diff changeset
   153
     s := ReadStream on:'12345678901234567890'.
6afd161ee43c +skipTo:
Claus Gittinger <cg@exept.de>
parents: 9283
diff changeset
   154
     s skipTo:$5.
6afd161ee43c +skipTo:
Claus Gittinger <cg@exept.de>
parents: 9283
diff changeset
   155
     s copyFrom:1 to:(s position).    
6afd161ee43c +skipTo:
Claus Gittinger <cg@exept.de>
parents: 9283
diff changeset
   156
     s upToEnd      
6afd161ee43c +skipTo:
Claus Gittinger <cg@exept.de>
parents: 9283
diff changeset
   157
    "
6afd161ee43c +skipTo:
Claus Gittinger <cg@exept.de>
parents: 9283
diff changeset
   158
! !
6afd161ee43c +skipTo:
Claus Gittinger <cg@exept.de>
parents: 9283
diff changeset
   159
16876
7e3ad01ca413 Added: #back / #peekBack for Squeak compatibility.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 16840
diff changeset
   160
!PositionableStream methodsFor:'Compatibility-Squeak'!
7e3ad01ca413 Added: #back / #peekBack for Squeak compatibility.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 16840
diff changeset
   161
7e3ad01ca413 Added: #back / #peekBack for Squeak compatibility.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 16840
diff changeset
   162
back
7e3ad01ca413 Added: #back / #peekBack for Squeak compatibility.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 16840
diff changeset
   163
        "Go back one element and return it."
7e3ad01ca413 Added: #back / #peekBack for Squeak compatibility.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 16840
diff changeset
   164
7e3ad01ca413 Added: #back / #peekBack for Squeak compatibility.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 16840
diff changeset
   165
        self position = 0 ifTrue: [self positionError].
7e3ad01ca413 Added: #back / #peekBack for Squeak compatibility.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 16840
diff changeset
   166
        self skip: -1.
7e3ad01ca413 Added: #back / #peekBack for Squeak compatibility.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 16840
diff changeset
   167
        ^ self peek
7e3ad01ca413 Added: #back / #peekBack for Squeak compatibility.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 16840
diff changeset
   168
7e3ad01ca413 Added: #back / #peekBack for Squeak compatibility.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 16840
diff changeset
   169
    "Created: / 03-10-2014 / 03:06:31 / Jan Vrany <jan.vrany@fit.cvut.cz>"
7e3ad01ca413 Added: #back / #peekBack for Squeak compatibility.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 16840
diff changeset
   170
!
7e3ad01ca413 Added: #back / #peekBack for Squeak compatibility.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 16840
diff changeset
   171
7e3ad01ca413 Added: #back / #peekBack for Squeak compatibility.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 16840
diff changeset
   172
peekBack
7e3ad01ca413 Added: #back / #peekBack for Squeak compatibility.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 16840
diff changeset
   173
        "Return the element at the previous position, without changing position.  Use indirect messages in case self is a StandardFileStream."
7e3ad01ca413 Added: #back / #peekBack for Squeak compatibility.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 16840
diff changeset
   174
7e3ad01ca413 Added: #back / #peekBack for Squeak compatibility.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 16840
diff changeset
   175
        | element |
7e3ad01ca413 Added: #back / #peekBack for Squeak compatibility.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 16840
diff changeset
   176
        element := self back.
7e3ad01ca413 Added: #back / #peekBack for Squeak compatibility.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 16840
diff changeset
   177
        self skip: 1.
7e3ad01ca413 Added: #back / #peekBack for Squeak compatibility.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 16840
diff changeset
   178
        ^ element
7e3ad01ca413 Added: #back / #peekBack for Squeak compatibility.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 16840
diff changeset
   179
7e3ad01ca413 Added: #back / #peekBack for Squeak compatibility.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 16840
diff changeset
   180
    "Created: / 03-10-2014 / 03:06:26 / Jan Vrany <jan.vrany@fit.cvut.cz>"
7e3ad01ca413 Added: #back / #peekBack for Squeak compatibility.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 16840
diff changeset
   181
! !
7e3ad01ca413 Added: #back / #peekBack for Squeak compatibility.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 16840
diff changeset
   182
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   183
!PositionableStream methodsFor:'accessing'!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   184
7686
290cda6075ac String new -> uninitializedNew: / basicNew:
Claus Gittinger <cg@exept.de>
parents: 7619
diff changeset
   185
collection
19591
185835d2d0e5 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 19403
diff changeset
   186
    "return the underlying collection buffer.
185835d2d0e5 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 19403
diff changeset
   187
     Notice, that this buffer may become invalid after being retrieved,
185835d2d0e5 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 19403
diff changeset
   188
     if more data is written to the stream (because a bigger buffer might be
185835d2d0e5 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 19403
diff changeset
   189
     allocated). Therefore, it should be only used in special situations,
22404
698e01ed8d59 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 22235
diff changeset
   190
     where an already filled buffer needs to be backpatched later
19591
185835d2d0e5 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 19403
diff changeset
   191
     (eg. before being sent out to some external stream or socket)"
185835d2d0e5 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 19403
diff changeset
   192
    
7686
290cda6075ac String new -> uninitializedNew: / basicNew:
Claus Gittinger <cg@exept.de>
parents: 7619
diff changeset
   193
    ^ collection
22404
698e01ed8d59 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 22235
diff changeset
   194
698e01ed8d59 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 22235
diff changeset
   195
    "Modified (comment): / 02-01-2018 / 19:54:37 / stefan"
7686
290cda6075ac String new -> uninitializedNew: / basicNew:
Claus Gittinger <cg@exept.de>
parents: 7619
diff changeset
   196
!
290cda6075ac String new -> uninitializedNew: / basicNew:
Claus Gittinger <cg@exept.de>
parents: 7619
diff changeset
   197
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   198
contents
a27a279701f8 Initial revision
claus
parents:
diff changeset
   199
    "return the entire contents of the stream"
a27a279701f8 Initial revision
claus
parents:
diff changeset
   200
a27a279701f8 Initial revision
claus
parents:
diff changeset
   201
    ^ collection
a27a279701f8 Initial revision
claus
parents:
diff changeset
   202
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   203
2432
5338f9b60a66 added #peekForAll:
Claus Gittinger <cg@exept.de>
parents: 2343
diff changeset
   204
peekForAll:aCollection
5338f9b60a66 added #peekForAll:
Claus Gittinger <cg@exept.de>
parents: 2343
diff changeset
   205
    "return true and advance if the next elements are the same
5338f9b60a66 added #peekForAll:
Claus Gittinger <cg@exept.de>
parents: 2343
diff changeset
   206
     as aCollection. 
5338f9b60a66 added #peekForAll:
Claus Gittinger <cg@exept.de>
parents: 2343
diff changeset
   207
     otherwise stay and let the position unchanged"
5338f9b60a66 added #peekForAll:
Claus Gittinger <cg@exept.de>
parents: 2343
diff changeset
   208
5338f9b60a66 added #peekForAll:
Claus Gittinger <cg@exept.de>
parents: 2343
diff changeset
   209
    |oldPos|
5338f9b60a66 added #peekForAll:
Claus Gittinger <cg@exept.de>
parents: 2343
diff changeset
   210
5338f9b60a66 added #peekForAll:
Claus Gittinger <cg@exept.de>
parents: 2343
diff changeset
   211
    oldPos := self position.
5338f9b60a66 added #peekForAll:
Claus Gittinger <cg@exept.de>
parents: 2343
diff changeset
   212
    (self next:(aCollection size)) = aCollection ifTrue:[
7051
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6924
diff changeset
   213
	^ true
2432
5338f9b60a66 added #peekForAll:
Claus Gittinger <cg@exept.de>
parents: 2343
diff changeset
   214
    ].
5338f9b60a66 added #peekForAll:
Claus Gittinger <cg@exept.de>
parents: 2343
diff changeset
   215
    self position:oldPos.
5338f9b60a66 added #peekForAll:
Claus Gittinger <cg@exept.de>
parents: 2343
diff changeset
   216
    ^ false
5338f9b60a66 added #peekForAll:
Claus Gittinger <cg@exept.de>
parents: 2343
diff changeset
   217
5338f9b60a66 added #peekForAll:
Claus Gittinger <cg@exept.de>
parents: 2343
diff changeset
   218
    "Created: 1.3.1997 / 15:11:25 / cg"
5338f9b60a66 added #peekForAll:
Claus Gittinger <cg@exept.de>
parents: 2343
diff changeset
   219
!
5338f9b60a66 added #peekForAll:
Claus Gittinger <cg@exept.de>
parents: 2343
diff changeset
   220
3905
682fc38fd339 added read-access to readLimit.
Claus Gittinger <cg@exept.de>
parents: 3604
diff changeset
   221
readLimit
20423
fefb552a4ca5 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 20377
diff changeset
   222
    "return the read-limit; that's the position at which EOF is returned"
3905
682fc38fd339 added read-access to readLimit.
Claus Gittinger <cg@exept.de>
parents: 3604
diff changeset
   223
682fc38fd339 added read-access to readLimit.
Claus Gittinger <cg@exept.de>
parents: 3604
diff changeset
   224
    ^ readLimit
682fc38fd339 added read-access to readLimit.
Claus Gittinger <cg@exept.de>
parents: 3604
diff changeset
   225
682fc38fd339 added read-access to readLimit.
Claus Gittinger <cg@exept.de>
parents: 3604
diff changeset
   226
    "Created: / 30.10.1998 / 16:47:04 / cg"
682fc38fd339 added read-access to readLimit.
Claus Gittinger <cg@exept.de>
parents: 3604
diff changeset
   227
!
682fc38fd339 added read-access to readLimit.
Claus Gittinger <cg@exept.de>
parents: 3604
diff changeset
   228
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   229
readLimit:aNumber
20423
fefb552a4ca5 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 20377
diff changeset
   230
    "set the read-limit; that's the position at which EOF is returned"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   231
a27a279701f8 Initial revision
claus
parents:
diff changeset
   232
    readLimit := aNumber
369
claus
parents: 362
diff changeset
   233
!
claus
parents: 362
diff changeset
   234
14934
5e273cddded7 setCollection: moved
Claus Gittinger <cg@exept.de>
parents: 13379
diff changeset
   235
setCollection:newCollection
5e273cddded7 setCollection: moved
Claus Gittinger <cg@exept.de>
parents: 13379
diff changeset
   236
    collection := newCollection
5e273cddded7 setCollection: moved
Claus Gittinger <cg@exept.de>
parents: 13379
diff changeset
   237
!
5e273cddded7 setCollection: moved
Claus Gittinger <cg@exept.de>
parents: 13379
diff changeset
   238
369
claus
parents: 362
diff changeset
   239
writeLimit:aNumber
20423
fefb552a4ca5 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 20377
diff changeset
   240
    "set the writeLimit; that's the position after which writing is prohibited"
369
claus
parents: 362
diff changeset
   241
claus
parents: 362
diff changeset
   242
    writeLimit := aNumber
10592
3957c03de6a1 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10577
diff changeset
   243
3957c03de6a1 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10577
diff changeset
   244
    "
3957c03de6a1 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10577
diff changeset
   245
     |s|
3957c03de6a1 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10577
diff changeset
   246
     s := WriteStream on:String new.
3957c03de6a1 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10577
diff changeset
   247
     s nextPutAll:'hello world'.
3957c03de6a1 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10577
diff changeset
   248
     s contents.
3957c03de6a1 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10577
diff changeset
   249
3957c03de6a1 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10577
diff changeset
   250
     |s|
3957c03de6a1 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10577
diff changeset
   251
     s := WriteStream on:String new.
3957c03de6a1 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10577
diff changeset
   252
     s writeLimit:5.
3957c03de6a1 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10577
diff changeset
   253
     s nextPutAll:'hello world'.
3957c03de6a1 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10577
diff changeset
   254
     s contents.
3957c03de6a1 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10577
diff changeset
   255
3957c03de6a1 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10577
diff changeset
   256
    "
3957c03de6a1 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10577
diff changeset
   257
3957c03de6a1 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10577
diff changeset
   258
    "Modified: / 04-06-2007 / 17:21:55 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   259
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
   260
21136
fc36f8c599ce #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21034
diff changeset
   261
!PositionableStream methodsFor:'non homogenous reading'!
fc36f8c599ce #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21034
diff changeset
   262
fc36f8c599ce #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21034
diff changeset
   263
nextBytes:numBytes into:aCollection startingAt:initialIndex
fc36f8c599ce #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21034
diff changeset
   264
    "return the next numBytes from the stream. If the end is
22514
e7e21b6d5ea6 #OTHER by mawalch
mawalch
parents: 22410
diff changeset
   265
     reached before, only that many bytes are copied into the
21136
fc36f8c599ce #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21034
diff changeset
   266
     collection.
fc36f8c599ce #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21034
diff changeset
   267
     Returns the number of bytes that have been actually read.
fc36f8c599ce #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21034
diff changeset
   268
     The receiver must support reading of binary bytes.
fc36f8c599ce #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21034
diff changeset
   269
fc36f8c599ce #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21034
diff changeset
   270
     Notice: this method is provided here for protocol completeness
fc36f8c599ce #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21034
diff changeset
   271
             with externalStreams - it is normally not used with other
fc36f8c599ce #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21034
diff changeset
   272
             streams."
fc36f8c599ce #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21034
diff changeset
   273
fc36f8c599ce #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21034
diff changeset
   274
    |max|
fc36f8c599ce #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21034
diff changeset
   275
22514
e7e21b6d5ea6 #OTHER by mawalch
mawalch
parents: 22410
diff changeset
   276
    (collection isSingleByteCollection
22235
d595aa55dc44 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 22204
diff changeset
   277
     and:[aCollection class == collection class or:[aCollection isSingleByteCollection]]
d595aa55dc44 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 22204
diff changeset
   278
    ) ifTrue:[
21136
fc36f8c599ce #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21034
diff changeset
   279
        "do it the fast way"
fc36f8c599ce #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21034
diff changeset
   280
        max := (readLimit - position) min: numBytes.
fc36f8c599ce #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21034
diff changeset
   281
        aCollection
22514
e7e21b6d5ea6 #OTHER by mawalch
mawalch
parents: 22410
diff changeset
   282
            replaceBytesFrom:initialIndex
21136
fc36f8c599ce #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21034
diff changeset
   283
            to:(initialIndex + max - 1)
22514
e7e21b6d5ea6 #OTHER by mawalch
mawalch
parents: 22410
diff changeset
   284
            with:collection
21136
fc36f8c599ce #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21034
diff changeset
   285
            startingAt:position+1.
fc36f8c599ce #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21034
diff changeset
   286
        position := position + max.
fc36f8c599ce #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21034
diff changeset
   287
        ^ max
fc36f8c599ce #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21034
diff changeset
   288
    ].
fc36f8c599ce #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21034
diff changeset
   289
    "do it the hard way"
fc36f8c599ce #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21034
diff changeset
   290
    ^ super nextBytes:numBytes into:aCollection startingAt:initialIndex
fc36f8c599ce #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21034
diff changeset
   291
fc36f8c599ce #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21034
diff changeset
   292
    "
fc36f8c599ce #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21034
diff changeset
   293
     |s n buffer|
fc36f8c599ce #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21034
diff changeset
   294
fc36f8c599ce #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21034
diff changeset
   295
     buffer := ByteArray new:10.
fc36f8c599ce #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21034
diff changeset
   296
fc36f8c599ce #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21034
diff changeset
   297
     s := ReadStream on:#[1 2 3 4 5 6 7 8 9].
fc36f8c599ce #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21034
diff changeset
   298
     s next:3.
fc36f8c599ce #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21034
diff changeset
   299
     n := s nextBytes:9 into:buffer startingAt:1.
fc36f8c599ce #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21034
diff changeset
   300
     Transcript showCR:('n = %1; buffer = <%2>' bindWith:n with:buffer)
fc36f8c599ce #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21034
diff changeset
   301
    "
22514
e7e21b6d5ea6 #OTHER by mawalch
mawalch
parents: 22410
diff changeset
   302
e7e21b6d5ea6 #OTHER by mawalch
mawalch
parents: 22410
diff changeset
   303
    "Modified (comment): / 29-01-2018 / 14:44:20 / mawalch"
21136
fc36f8c599ce #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21034
diff changeset
   304
! !
fc36f8c599ce #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21034
diff changeset
   305
611
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   306
!PositionableStream methodsFor:'positioning'!
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   307
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   308
backStep
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   309
    "move backward read position by one"
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   310
4405
247058206089 added ZeroPosition;
Claus Gittinger <cg@exept.de>
parents: 4325
diff changeset
   311
    self position:(self position - 1)
611
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   312
!
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   313
16188
4df8b9b15db4 class: PositionableStream
Claus Gittinger <cg@exept.de>
parents: 16186
diff changeset
   314
match: subCollection
4df8b9b15db4 class: PositionableStream
Claus Gittinger <cg@exept.de>
parents: 16186
diff changeset
   315
    "Set the access position of the receiver to be past the next occurrence of the subCollection. 
4df8b9b15db4 class: PositionableStream
Claus Gittinger <cg@exept.de>
parents: 16186
diff changeset
   316
     Answer whether subCollection is found.  
4df8b9b15db4 class: PositionableStream
Claus Gittinger <cg@exept.de>
parents: 16186
diff changeset
   317
     No wildcards, and case does matter."
4df8b9b15db4 class: PositionableStream
Claus Gittinger <cg@exept.de>
parents: 16186
diff changeset
   318
4df8b9b15db4 class: PositionableStream
Claus Gittinger <cg@exept.de>
parents: 16186
diff changeset
   319
    | pattern startMatch |
4df8b9b15db4 class: PositionableStream
Claus Gittinger <cg@exept.de>
parents: 16186
diff changeset
   320
4df8b9b15db4 class: PositionableStream
Claus Gittinger <cg@exept.de>
parents: 16186
diff changeset
   321
    pattern := ReadStream on: subCollection.
4df8b9b15db4 class: PositionableStream
Claus Gittinger <cg@exept.de>
parents: 16186
diff changeset
   322
    startMatch := nil.
4df8b9b15db4 class: PositionableStream
Claus Gittinger <cg@exept.de>
parents: 16186
diff changeset
   323
    [pattern atEnd] whileFalse: [
4df8b9b15db4 class: PositionableStream
Claus Gittinger <cg@exept.de>
parents: 16186
diff changeset
   324
        self atEnd ifTrue: [^ false].
4df8b9b15db4 class: PositionableStream
Claus Gittinger <cg@exept.de>
parents: 16186
diff changeset
   325
        (self next) = (pattern next) 
4df8b9b15db4 class: PositionableStream
Claus Gittinger <cg@exept.de>
parents: 16186
diff changeset
   326
            ifTrue: [pattern position = 1 ifTrue: [startMatch := self position]]
4df8b9b15db4 class: PositionableStream
Claus Gittinger <cg@exept.de>
parents: 16186
diff changeset
   327
            ifFalse: [
4df8b9b15db4 class: PositionableStream
Claus Gittinger <cg@exept.de>
parents: 16186
diff changeset
   328
                pattern position: 0.
4df8b9b15db4 class: PositionableStream
Claus Gittinger <cg@exept.de>
parents: 16186
diff changeset
   329
                startMatch ifNotNil: [
4df8b9b15db4 class: PositionableStream
Claus Gittinger <cg@exept.de>
parents: 16186
diff changeset
   330
                    self position: startMatch.
4df8b9b15db4 class: PositionableStream
Claus Gittinger <cg@exept.de>
parents: 16186
diff changeset
   331
                    startMatch := nil
4df8b9b15db4 class: PositionableStream
Claus Gittinger <cg@exept.de>
parents: 16186
diff changeset
   332
                ]
4df8b9b15db4 class: PositionableStream
Claus Gittinger <cg@exept.de>
parents: 16186
diff changeset
   333
            ]
4df8b9b15db4 class: PositionableStream
Claus Gittinger <cg@exept.de>
parents: 16186
diff changeset
   334
    ].
4df8b9b15db4 class: PositionableStream
Claus Gittinger <cg@exept.de>
parents: 16186
diff changeset
   335
    ^ true
4df8b9b15db4 class: PositionableStream
Claus Gittinger <cg@exept.de>
parents: 16186
diff changeset
   336
4df8b9b15db4 class: PositionableStream
Claus Gittinger <cg@exept.de>
parents: 16186
diff changeset
   337
    "
4df8b9b15db4 class: PositionableStream
Claus Gittinger <cg@exept.de>
parents: 16186
diff changeset
   338
     'abc def ghi' readStream match:'def'; upToEnd
4df8b9b15db4 class: PositionableStream
Claus Gittinger <cg@exept.de>
parents: 16186
diff changeset
   339
    "
4df8b9b15db4 class: PositionableStream
Claus Gittinger <cg@exept.de>
parents: 16186
diff changeset
   340
!
4df8b9b15db4 class: PositionableStream
Claus Gittinger <cg@exept.de>
parents: 16186
diff changeset
   341
611
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   342
position
15615
f67e9f5cd22a class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 15357
diff changeset
   343
    "return the read position (0-based)"
7052
207cc9c62982 prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents: 7051
diff changeset
   344
15348
f5677dc62dc0 class: PositionableStream
Claus Gittinger <cg@exept.de>
parents: 15159
diff changeset
   345
    ^ position
7052
207cc9c62982 prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents: 7051
diff changeset
   346
!
207cc9c62982 prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents: 7051
diff changeset
   347
15615
f67e9f5cd22a class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 15357
diff changeset
   348
position0Based
f67e9f5cd22a class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 15357
diff changeset
   349
    <resource: #obsolete>
f67e9f5cd22a class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 15357
diff changeset
   350
    "return the read position 0-based"
f67e9f5cd22a class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 15357
diff changeset
   351
f67e9f5cd22a class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 15357
diff changeset
   352
    ^ self position
f67e9f5cd22a class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 15357
diff changeset
   353
!
f67e9f5cd22a class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 15357
diff changeset
   354
7052
207cc9c62982 prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents: 7051
diff changeset
   355
position0Based:index0Based
15615
f67e9f5cd22a class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 15357
diff changeset
   356
    <resource: #obsolete>
4405
247058206089 added ZeroPosition;
Claus Gittinger <cg@exept.de>
parents: 4325
diff changeset
   357
    "set the read (or write) position"
611
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   358
15615
f67e9f5cd22a class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 15357
diff changeset
   359
    self position:index0Based
7052
207cc9c62982 prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents: 7051
diff changeset
   360
!
207cc9c62982 prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents: 7051
diff changeset
   361
207cc9c62982 prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents: 7051
diff changeset
   362
position1Based
15615
f67e9f5cd22a class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 15357
diff changeset
   363
    <resource: #obsolete>
7052
207cc9c62982 prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents: 7051
diff changeset
   364
    "return the read position 1-based"
611
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   365
15615
f67e9f5cd22a class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 15357
diff changeset
   366
    ^ self position + 1
7052
207cc9c62982 prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents: 7051
diff changeset
   367
!
207cc9c62982 prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents: 7051
diff changeset
   368
207cc9c62982 prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents: 7051
diff changeset
   369
position1Based:index1Based
15615
f67e9f5cd22a class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 15357
diff changeset
   370
    <resource: #obsolete>
7052
207cc9c62982 prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents: 7051
diff changeset
   371
    "set the read (or write) position"
207cc9c62982 prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents: 7051
diff changeset
   372
15615
f67e9f5cd22a class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 15357
diff changeset
   373
    self position:(index1Based - 1)
4405
247058206089 added ZeroPosition;
Claus Gittinger <cg@exept.de>
parents: 4325
diff changeset
   374
247058206089 added ZeroPosition;
Claus Gittinger <cg@exept.de>
parents: 4325
diff changeset
   375
    "
247058206089 added ZeroPosition;
Claus Gittinger <cg@exept.de>
parents: 4325
diff changeset
   376
     |s|
247058206089 added ZeroPosition;
Claus Gittinger <cg@exept.de>
parents: 4325
diff changeset
   377
247058206089 added ZeroPosition;
Claus Gittinger <cg@exept.de>
parents: 4325
diff changeset
   378
     s := '1234567890' readStream.
247058206089 added ZeroPosition;
Claus Gittinger <cg@exept.de>
parents: 4325
diff changeset
   379
     s next:5.
247058206089 added ZeroPosition;
Claus Gittinger <cg@exept.de>
parents: 4325
diff changeset
   380
     s position:1.
247058206089 added ZeroPosition;
Claus Gittinger <cg@exept.de>
parents: 4325
diff changeset
   381
     s next:7.       
247058206089 added ZeroPosition;
Claus Gittinger <cg@exept.de>
parents: 4325
diff changeset
   382
    "
247058206089 added ZeroPosition;
Claus Gittinger <cg@exept.de>
parents: 4325
diff changeset
   383
247058206089 added ZeroPosition;
Claus Gittinger <cg@exept.de>
parents: 4325
diff changeset
   384
    "
247058206089 added ZeroPosition;
Claus Gittinger <cg@exept.de>
parents: 4325
diff changeset
   385
     |s|
247058206089 added ZeroPosition;
Claus Gittinger <cg@exept.de>
parents: 4325
diff changeset
   386
247058206089 added ZeroPosition;
Claus Gittinger <cg@exept.de>
parents: 4325
diff changeset
   387
     s := '' writeStream.
247058206089 added ZeroPosition;
Claus Gittinger <cg@exept.de>
parents: 4325
diff changeset
   388
     s nextPutAll:'1234567890'.
247058206089 added ZeroPosition;
Claus Gittinger <cg@exept.de>
parents: 4325
diff changeset
   389
     s position:5.
247058206089 added ZeroPosition;
Claus Gittinger <cg@exept.de>
parents: 4325
diff changeset
   390
     s nextPutAll:'abcdefg'.
247058206089 added ZeroPosition;
Claus Gittinger <cg@exept.de>
parents: 4325
diff changeset
   391
     s contents 
247058206089 added ZeroPosition;
Claus Gittinger <cg@exept.de>
parents: 4325
diff changeset
   392
    "
247058206089 added ZeroPosition;
Claus Gittinger <cg@exept.de>
parents: 4325
diff changeset
   393
247058206089 added ZeroPosition;
Claus Gittinger <cg@exept.de>
parents: 4325
diff changeset
   394
    "
247058206089 added ZeroPosition;
Claus Gittinger <cg@exept.de>
parents: 4325
diff changeset
   395
     |s|
247058206089 added ZeroPosition;
Claus Gittinger <cg@exept.de>
parents: 4325
diff changeset
   396
247058206089 added ZeroPosition;
Claus Gittinger <cg@exept.de>
parents: 4325
diff changeset
   397
     s := '' writeStream.
247058206089 added ZeroPosition;
Claus Gittinger <cg@exept.de>
parents: 4325
diff changeset
   398
     s nextPutAll:'1234567890'.
247058206089 added ZeroPosition;
Claus Gittinger <cg@exept.de>
parents: 4325
diff changeset
   399
     s position:0.
247058206089 added ZeroPosition;
Claus Gittinger <cg@exept.de>
parents: 4325
diff changeset
   400
     s nextPutAll:'abcdefg'.
247058206089 added ZeroPosition;
Claus Gittinger <cg@exept.de>
parents: 4325
diff changeset
   401
     s contents 
247058206089 added ZeroPosition;
Claus Gittinger <cg@exept.de>
parents: 4325
diff changeset
   402
    "
611
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   403
!
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   404
15615
f67e9f5cd22a class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 15357
diff changeset
   405
position:index0Based
7052
207cc9c62982 prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents: 7051
diff changeset
   406
    "set the read (or write) position"
207cc9c62982 prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents: 7051
diff changeset
   407
15615
f67e9f5cd22a class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 15357
diff changeset
   408
    ((index0Based > readLimit) or:[index0Based < 0]) ifTrue: [^ self positionError:index0Based].
f67e9f5cd22a class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 15357
diff changeset
   409
    position := index0Based
6626
f7b08d6bee3f #positionStartingAt0 and #positionStartingAt1
Stefan Vogel <sv@exept.de>
parents: 6538
diff changeset
   410
!
f7b08d6bee3f #positionStartingAt0 and #positionStartingAt1
Stefan Vogel <sv@exept.de>
parents: 6538
diff changeset
   411
611
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   412
reset
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   413
    "set the read position to the beginning of the collection"
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   414
19885
7a8a85b3fa8d #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 19591
diff changeset
   415
    position := 0.
5308
8e8b5d2b3b8e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 5302
diff changeset
   416
8e8b5d2b3b8e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 5302
diff changeset
   417
    "
8e8b5d2b3b8e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 5302
diff changeset
   418
     |s|
8e8b5d2b3b8e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 5302
diff changeset
   419
8e8b5d2b3b8e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 5302
diff changeset
   420
     s := 'hello world' readStream.
8e8b5d2b3b8e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 5302
diff changeset
   421
     Transcript showCR:(s next:5).
8e8b5d2b3b8e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 5302
diff changeset
   422
     s reset.
8e8b5d2b3b8e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 5302
diff changeset
   423
     Transcript showCR:(s next:10).
8e8b5d2b3b8e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 5302
diff changeset
   424
    "
8e8b5d2b3b8e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 5302
diff changeset
   425
!
8e8b5d2b3b8e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 5302
diff changeset
   426
8e8b5d2b3b8e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 5302
diff changeset
   427
resetPosition
19885
7a8a85b3fa8d #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 19591
diff changeset
   428
    <resource: #obsolete>
5308
8e8b5d2b3b8e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 5302
diff changeset
   429
    "set the read position to the beginning of the collection"
8e8b5d2b3b8e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 5302
diff changeset
   430
15348
f5677dc62dc0 class: PositionableStream
Claus Gittinger <cg@exept.de>
parents: 15159
diff changeset
   431
    position := 0
4405
247058206089 added ZeroPosition;
Claus Gittinger <cg@exept.de>
parents: 4325
diff changeset
   432
247058206089 added ZeroPosition;
Claus Gittinger <cg@exept.de>
parents: 4325
diff changeset
   433
    "
247058206089 added ZeroPosition;
Claus Gittinger <cg@exept.de>
parents: 4325
diff changeset
   434
     |s|
247058206089 added ZeroPosition;
Claus Gittinger <cg@exept.de>
parents: 4325
diff changeset
   435
247058206089 added ZeroPosition;
Claus Gittinger <cg@exept.de>
parents: 4325
diff changeset
   436
     s := 'hello world' readStream.
247058206089 added ZeroPosition;
Claus Gittinger <cg@exept.de>
parents: 4325
diff changeset
   437
     Transcript showCR:(s next:5).
247058206089 added ZeroPosition;
Claus Gittinger <cg@exept.de>
parents: 4325
diff changeset
   438
     s reset.
247058206089 added ZeroPosition;
Claus Gittinger <cg@exept.de>
parents: 4325
diff changeset
   439
     Transcript showCR:(s next:10).
247058206089 added ZeroPosition;
Claus Gittinger <cg@exept.de>
parents: 4325
diff changeset
   440
    "
611
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   441
!
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   442
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   443
setToEnd
11433
bf4acfe61ece changed #setToEnd
Stefan Vogel <sv@exept.de>
parents: 10592
diff changeset
   444
    "set the read position to the end of the collection.
bf4acfe61ece changed #setToEnd
Stefan Vogel <sv@exept.de>
parents: 10592
diff changeset
   445
     #next will return EOF, #nextPut: will append to the stream.
bf4acfe61ece changed #setToEnd
Stefan Vogel <sv@exept.de>
parents: 10592
diff changeset
   446
     (same Behavior as FileStream."
611
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   447
15348
f5677dc62dc0 class: PositionableStream
Claus Gittinger <cg@exept.de>
parents: 15159
diff changeset
   448
    position := readLimit
611
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   449
!
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   450
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   451
skip:numberToSkip
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   452
    "skip the next numberToSkip elements"
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   453
5774
dafe65be735b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 5748
diff changeset
   454
    numberToSkip ~~ 0 ifTrue:[
7051
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6924
diff changeset
   455
	self position:(self position + numberToSkip)
5774
dafe65be735b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 5748
diff changeset
   456
    ]
1489
9e3b166483fb moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents: 1420
diff changeset
   457
!
9e3b166483fb moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents: 1420
diff changeset
   458
9e3b166483fb moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents: 1420
diff changeset
   459
skipThroughAll:aCollection
9e3b166483fb moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents: 1420
diff changeset
   460
    "skip for and through the sequence given by the argument, aCollection;
9e3b166483fb moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents: 1420
diff changeset
   461
     return nil if not found, self otherwise. 
9e3b166483fb moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents: 1420
diff changeset
   462
     On a successful match, the next read will return elements after aCollection;
2153
244e36cbbd9b skipThroughAll: does not need the old position
Claus Gittinger <cg@exept.de>
parents: 2130
diff changeset
   463
     if no match was found, the receiver will be positioned at the end.
244e36cbbd9b skipThroughAll: does not need the old position
Claus Gittinger <cg@exept.de>
parents: 2130
diff changeset
   464
     This is redefined here, to make use of positioning."
1489
9e3b166483fb moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents: 1420
diff changeset
   465
7054
685d359f9847 code rewritten to be independent of stream zero-base
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   466
    |buffer len first idx|
1489
9e3b166483fb moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents: 1420
diff changeset
   467
16186
dae8e43ccc46 class: PositionableStream
Claus Gittinger <cg@exept.de>
parents: 15640
diff changeset
   468
    self isPositionable ifFalse:[
dae8e43ccc46 class: PositionableStream
Claus Gittinger <cg@exept.de>
parents: 15640
diff changeset
   469
        "/ for non-positionable subclasses
dae8e43ccc46 class: PositionableStream
Claus Gittinger <cg@exept.de>
parents: 15640
diff changeset
   470
        ^ super skipThroughAll:aCollection
dae8e43ccc46 class: PositionableStream
Claus Gittinger <cg@exept.de>
parents: 15640
diff changeset
   471
    ].
dae8e43ccc46 class: PositionableStream
Claus Gittinger <cg@exept.de>
parents: 15640
diff changeset
   472
7054
685d359f9847 code rewritten to be independent of stream zero-base
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   473
    len := aCollection size.
1489
9e3b166483fb moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents: 1420
diff changeset
   474
    first := aCollection at:1.
9e3b166483fb moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents: 1420
diff changeset
   475
    [self atEnd] whileFalse:[
7054
685d359f9847 code rewritten to be independent of stream zero-base
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   476
        buffer := self nextAvailable:len.
10357
66543e405aa4 Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents: 10001
diff changeset
   477
        buffer size == len ifTrue:[
16209
f2d1d7b2d649 class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 16195
diff changeset
   478
            buffer = aCollection ifTrue:[
f2d1d7b2d649 class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 16195
diff changeset
   479
                ^ self
f2d1d7b2d649 class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 16195
diff changeset
   480
            ].
10357
66543e405aa4 Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents: 10001
diff changeset
   481
            "expect more input"
66543e405aa4 Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents: 10001
diff changeset
   482
            idx := buffer indexOf:first startingAt:2.
16209
f2d1d7b2d649 class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 16195
diff changeset
   483
            idx ~~ 0 ifTrue:[
f2d1d7b2d649 class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 16195
diff changeset
   484
                self skip:(idx - len - 1)
10357
66543e405aa4 Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents: 10001
diff changeset
   485
            ].
66543e405aa4 Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents: 10001
diff changeset
   486
        ].
1489
9e3b166483fb moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents: 1420
diff changeset
   487
    ].
9e3b166483fb moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents: 1420
diff changeset
   488
    ^ nil
9e3b166483fb moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents: 1420
diff changeset
   489
9e3b166483fb moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents: 1420
diff changeset
   490
    "
9e3b166483fb moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents: 1420
diff changeset
   491
     |s|
10357
66543e405aa4 Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents: 10001
diff changeset
   492
     s := ReadStream on:'12345678901234567890a'.
66543e405aa4 Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents: 10001
diff changeset
   493
     s skipThroughAll:'90ab'.
1489
9e3b166483fb moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents: 1420
diff changeset
   494
     s upToEnd  
9e3b166483fb moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents: 1420
diff changeset
   495
    "
2153
244e36cbbd9b skipThroughAll: does not need the old position
Claus Gittinger <cg@exept.de>
parents: 2130
diff changeset
   496
    "
244e36cbbd9b skipThroughAll: does not need the old position
Claus Gittinger <cg@exept.de>
parents: 2130
diff changeset
   497
     |s|
244e36cbbd9b skipThroughAll: does not need the old position
Claus Gittinger <cg@exept.de>
parents: 2130
diff changeset
   498
     s := ReadStream on:'12345678901234567890'.
244e36cbbd9b skipThroughAll: does not need the old position
Claus Gittinger <cg@exept.de>
parents: 2130
diff changeset
   499
     s skipThroughAll:'1234'.
244e36cbbd9b skipThroughAll: does not need the old position
Claus Gittinger <cg@exept.de>
parents: 2130
diff changeset
   500
     s upToEnd  
244e36cbbd9b skipThroughAll: does not need the old position
Claus Gittinger <cg@exept.de>
parents: 2130
diff changeset
   501
    "
244e36cbbd9b skipThroughAll: does not need the old position
Claus Gittinger <cg@exept.de>
parents: 2130
diff changeset
   502
    "
244e36cbbd9b skipThroughAll: does not need the old position
Claus Gittinger <cg@exept.de>
parents: 2130
diff changeset
   503
     |s|
244e36cbbd9b skipThroughAll: does not need the old position
Claus Gittinger <cg@exept.de>
parents: 2130
diff changeset
   504
     s := ReadStream on:'12345678901234567890'.
244e36cbbd9b skipThroughAll: does not need the old position
Claus Gittinger <cg@exept.de>
parents: 2130
diff changeset
   505
     s skipThroughAll:'999'.
244e36cbbd9b skipThroughAll: does not need the old position
Claus Gittinger <cg@exept.de>
parents: 2130
diff changeset
   506
     s atEnd  
244e36cbbd9b skipThroughAll: does not need the old position
Claus Gittinger <cg@exept.de>
parents: 2130
diff changeset
   507
    "
1489
9e3b166483fb moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents: 1420
diff changeset
   508
9e3b166483fb moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents: 1420
diff changeset
   509
    "Created: 26.6.1996 / 09:35:35 / cg"
2154
89e96599e1e2 skipThroughAll: does not need the old position
Claus Gittinger <cg@exept.de>
parents: 2153
diff changeset
   510
    "Modified: 11.1.1997 / 19:16:38 / cg"
1489
9e3b166483fb moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents: 1420
diff changeset
   511
!
9e3b166483fb moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents: 1420
diff changeset
   512
9e3b166483fb moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents: 1420
diff changeset
   513
skipToAll:aCollection
9e3b166483fb moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents: 1420
diff changeset
   514
    "skip for the sequence given by the argument, aCollection;
9e3b166483fb moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents: 1420
diff changeset
   515
     return nil if not found, self otherwise. 
9e3b166483fb moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents: 1420
diff changeset
   516
     On a successful match, the next read will return elements of aCollection."
9e3b166483fb moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents: 1420
diff changeset
   517
7054
685d359f9847 code rewritten to be independent of stream zero-base
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   518
    |oldPos buffer len first idx|
1489
9e3b166483fb moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents: 1420
diff changeset
   519
9e3b166483fb moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents: 1420
diff changeset
   520
    oldPos := self position.
7054
685d359f9847 code rewritten to be independent of stream zero-base
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   521
    len := aCollection size.
1489
9e3b166483fb moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents: 1420
diff changeset
   522
    first := aCollection at:1.
9e3b166483fb moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents: 1420
diff changeset
   523
    [self atEnd] whileFalse:[
10357
66543e405aa4 Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents: 10001
diff changeset
   524
        buffer := self nextAvailable:len.
7054
685d359f9847 code rewritten to be independent of stream zero-base
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   525
        buffer = aCollection ifTrue:[
685d359f9847 code rewritten to be independent of stream zero-base
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   526
            self position:(self position - len).
685d359f9847 code rewritten to be independent of stream zero-base
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   527
            ^ self
685d359f9847 code rewritten to be independent of stream zero-base
Claus Gittinger <cg@exept.de>
parents: 7053
diff changeset
   528
        ].
10357
66543e405aa4 Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents: 10001
diff changeset
   529
        buffer size == len ifTrue:[
66543e405aa4 Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents: 10001
diff changeset
   530
            "more input can be expected"
66543e405aa4 Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents: 10001
diff changeset
   531
            idx := buffer indexOf:first startingAt:2.
66543e405aa4 Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents: 10001
diff changeset
   532
            idx == 0 ifFalse:[
66543e405aa4 Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents: 10001
diff changeset
   533
                self position:(self position - len + idx - 1)
66543e405aa4 Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents: 10001
diff changeset
   534
            ].
66543e405aa4 Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents: 10001
diff changeset
   535
        ].
1489
9e3b166483fb moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents: 1420
diff changeset
   536
    ].
9e3b166483fb moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents: 1420
diff changeset
   537
    self position:oldPos.
9e3b166483fb moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents: 1420
diff changeset
   538
    ^ nil
9e3b166483fb moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents: 1420
diff changeset
   539
9e3b166483fb moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents: 1420
diff changeset
   540
    "
9e3b166483fb moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents: 1420
diff changeset
   541
     |s|
9e3b166483fb moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents: 1420
diff changeset
   542
     s := ReadStream on:'12345678901234567890'.
9e3b166483fb moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents: 1420
diff changeset
   543
     s skipToAll:'901'.
9e3b166483fb moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents: 1420
diff changeset
   544
     s upToEnd  
9e3b166483fb moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents: 1420
diff changeset
   545
    "
9e3b166483fb moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents: 1420
diff changeset
   546
10357
66543e405aa4 Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents: 10001
diff changeset
   547
    "
66543e405aa4 Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents: 10001
diff changeset
   548
     |s|
66543e405aa4 Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents: 10001
diff changeset
   549
     s := ReadStream on:'1234567890'.
66543e405aa4 Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents: 10001
diff changeset
   550
     s skipToAll:'901'.
66543e405aa4 Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents: 10001
diff changeset
   551
     s upToEnd  
66543e405aa4 Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents: 10001
diff changeset
   552
    "
66543e405aa4 Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents: 10001
diff changeset
   553
66543e405aa4 Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents: 10001
diff changeset
   554
    "
66543e405aa4 Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents: 10001
diff changeset
   555
     |s|
66543e405aa4 Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents: 10001
diff changeset
   556
     s := 'Makefile' asFilename readStream.
66543e405aa4 Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents: 10001
diff changeset
   557
     [
66543e405aa4 Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents: 10001
diff changeset
   558
         (s skipToAll:'EDIT') notNil ifTrue:[
66543e405aa4 Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents: 10001
diff changeset
   559
            s next:100.
66543e405aa4 Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents: 10001
diff changeset
   560
         ].
66543e405aa4 Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents: 10001
diff changeset
   561
     ] ensure:[
66543e405aa4 Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents: 10001
diff changeset
   562
        s close.
66543e405aa4 Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents: 10001
diff changeset
   563
     ]
66543e405aa4 Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents: 10001
diff changeset
   564
    "
66543e405aa4 Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents: 10001
diff changeset
   565
1489
9e3b166483fb moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents: 1420
diff changeset
   566
    "Modified: 26.6.1996 / 09:28:27 / cg"
9e3b166483fb moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents: 1420
diff changeset
   567
    "Created: 26.6.1996 / 09:35:06 / cg"
44
b262907c93ea *** empty log message ***
claus
parents: 42
diff changeset
   568
! !
611
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   569
10357
66543e405aa4 Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents: 10001
diff changeset
   570
!PositionableStream methodsFor:'printing & storing'!
66543e405aa4 Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents: 10001
diff changeset
   571
66543e405aa4 Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents: 10001
diff changeset
   572
printOn:aStream
66543e405aa4 Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents: 10001
diff changeset
   573
    aStream nextPutAll:self className; nextPutAll:'(on:'; nextPutAll:collection classNameWithArticle; nextPut:$)
66543e405aa4 Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents: 10001
diff changeset
   574
66543e405aa4 Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents: 10001
diff changeset
   575
    "
66543e405aa4 Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents: 10001
diff changeset
   576
      '' readStream printString
66543e405aa4 Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents: 10001
diff changeset
   577
      '' writeStream printString
66543e405aa4 Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents: 10001
diff changeset
   578
    "
66543e405aa4 Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents: 10001
diff changeset
   579
! !
66543e405aa4 Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents: 10001
diff changeset
   580
611
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   581
!PositionableStream methodsFor:'private'!
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   582
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   583
contentsSpecies
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   584
    "return a class of which instances will be returned, when
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   585
     parts of the collection are asked for. 
15640
d5a8c24894e2 class: PositionableStream
Claus Gittinger <cg@exept.de>
parents: 15615
diff changeset
   586
     (see upTo-kind of methods in Stream)"
611
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   587
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   588
    ^ collection species
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   589
!
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   590
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   591
on:aCollection
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   592
    "setup for streaming on aCollection"
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   593
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   594
    collection := aCollection.
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   595
    readLimit := aCollection size.
13379
a4644dee3177 changed: #on:
sr
parents: 11434
diff changeset
   596
    readLimit == 0 ifTrue:[
a4644dee3177 changed: #on:
sr
parents: 11434
diff changeset
   597
        self assert:(aCollection isCollection)
a4644dee3177 changed: #on:
sr
parents: 11434
diff changeset
   598
    ].
15348
f5677dc62dc0 class: PositionableStream
Claus Gittinger <cg@exept.de>
parents: 15159
diff changeset
   599
    position := 0
611
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   600
!
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   601
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   602
on:aCollection from:first to:last
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   603
    "setup for streaming on aCollection from first to last"
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   604
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   605
    collection := aCollection.
15348
f5677dc62dc0 class: PositionableStream
Claus Gittinger <cg@exept.de>
parents: 15159
diff changeset
   606
    position := first - 1.
611
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   607
    readLimit := last
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   608
!
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   609
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   610
positionError
1998
9749a5f87238 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1929
diff changeset
   611
    "{ Pragma: +optSpace }"
9749a5f87238 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1929
diff changeset
   612
2966
856dfc8a294a lots of VMS changes
Claus Gittinger <cg@exept.de>
parents: 2893
diff changeset
   613
    "report an error when positioning past the end
856dfc8a294a lots of VMS changes
Claus Gittinger <cg@exept.de>
parents: 2893
diff changeset
   614
     or before the beginning."
611
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   615
8320
229adaeaf183 Use exception classes instead of class vars
Stefan Vogel <sv@exept.de>
parents: 8318
diff changeset
   616
    ^ PositionOutOfBoundsError raiseRequestWith:nil
4452
3d050f505a0a Raise proceedable signals proceedable.
Stefan Vogel <sv@exept.de>
parents: 4416
diff changeset
   617
3d050f505a0a Raise proceedable signals proceedable.
Stefan Vogel <sv@exept.de>
parents: 4416
diff changeset
   618
    "Modified: / 26.7.1999 / 10:59:13 / stefan"
611
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   619
!
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   620
6314
e1aad938d629 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 6230
diff changeset
   621
positionError:badPostition
e1aad938d629 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 6230
diff changeset
   622
    "{ Pragma: +optSpace }"
e1aad938d629 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 6230
diff changeset
   623
e1aad938d629 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 6230
diff changeset
   624
    "report an error when positioning past the end
e1aad938d629 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 6230
diff changeset
   625
     or before the beginning."
e1aad938d629 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 6230
diff changeset
   626
8320
229adaeaf183 Use exception classes instead of class vars
Stefan Vogel <sv@exept.de>
parents: 8318
diff changeset
   627
    ^ PositionOutOfBoundsError raiseRequestWith:badPostition
6314
e1aad938d629 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 6230
diff changeset
   628
!
e1aad938d629 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 6230
diff changeset
   629
611
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   630
with:aCollection
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   631
    "setup for streaming to the end of aCollection"
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   632
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   633
    collection := aCollection.
11434
525e70af011a changed #with:
Stefan Vogel <sv@exept.de>
parents: 11433
diff changeset
   634
    readLimit := aCollection size.
611
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   635
    self setToEnd
11434
525e70af011a changed #with:
Stefan Vogel <sv@exept.de>
parents: 11433
diff changeset
   636
525e70af011a changed #with:
Stefan Vogel <sv@exept.de>
parents: 11433
diff changeset
   637
    "
525e70af011a changed #with:
Stefan Vogel <sv@exept.de>
parents: 11433
diff changeset
   638
      (WriteStream with:#(1 2 3 4 5)) 
525e70af011a changed #with:
Stefan Vogel <sv@exept.de>
parents: 11433
diff changeset
   639
            nextPut:6;
525e70af011a changed #with:
Stefan Vogel <sv@exept.de>
parents: 11433
diff changeset
   640
            contents
525e70af011a changed #with:
Stefan Vogel <sv@exept.de>
parents: 11433
diff changeset
   641
    "
611
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   642
! !
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   643
16195
2bed37c6f731 class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 16189
diff changeset
   644
!PositionableStream methodsFor:'queries'!
2bed37c6f731 class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 16189
diff changeset
   645
16209
f2d1d7b2d649 class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 16195
diff changeset
   646
endsBeforePositionWith:aSequenceableCollection
16195
2bed37c6f731 class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 16189
diff changeset
   647
    "answer true, if the elements in aSequenceableCollection
16209
f2d1d7b2d649 class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 16195
diff changeset
   648
     are at the current end of the stream up to position."
16195
2bed37c6f731 class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 16189
diff changeset
   649
16209
f2d1d7b2d649 class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 16195
diff changeset
   650
    |sz pos|
16195
2bed37c6f731 class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 16189
diff changeset
   651
16209
f2d1d7b2d649 class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 16195
diff changeset
   652
    sz := aSequenceableCollection size.
f2d1d7b2d649 class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 16195
diff changeset
   653
    pos := self position.
f2d1d7b2d649 class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 16195
diff changeset
   654
    pos < sz ifTrue:[
f2d1d7b2d649 class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 16195
diff changeset
   655
        ^ false.
16195
2bed37c6f731 class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 16189
diff changeset
   656
    ].
16209
f2d1d7b2d649 class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 16195
diff changeset
   657
    self contentsSpecies == collection class ifTrue:[
f2d1d7b2d649 class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 16195
diff changeset
   658
        ^ collection sameContentsFrom:pos+1-sz to:pos as:aSequenceableCollection startingAt:1.
f2d1d7b2d649 class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 16195
diff changeset
   659
    ].
f2d1d7b2d649 class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 16195
diff changeset
   660
    self position:pos-sz.
f2d1d7b2d649 class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 16195
diff changeset
   661
    ^ (self next:sz) = aSequenceableCollection.
16195
2bed37c6f731 class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 16189
diff changeset
   662
2bed37c6f731 class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 16189
diff changeset
   663
    "
16209
f2d1d7b2d649 class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 16195
diff changeset
   664
        ('' writeStream nextPutAll:'Hello World') endsBeforePositionWith:'World'
f2d1d7b2d649 class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 16195
diff changeset
   665
        ('' writeStream nextPutAll:'Hello World') endsBeforePositionWith:'Hello World'
f2d1d7b2d649 class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 16195
diff changeset
   666
        ('' writeStream nextPutAll:'Hello World') endsBeforePositionWith:'Hello Worldx'
f2d1d7b2d649 class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 16195
diff changeset
   667
        ('' writeStream nextPutAll:'Hello World') endsBeforePositionWith:'Bla'
f2d1d7b2d649 class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 16195
diff changeset
   668
        ('' writeStream) endsBeforePositionWith:'Bla'
f2d1d7b2d649 class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 16195
diff changeset
   669
        ('' writeStream) endsBeforePositionWith:''
16195
2bed37c6f731 class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 16189
diff changeset
   670
        ''  endsWith:''
2bed37c6f731 class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 16189
diff changeset
   671
    "
2bed37c6f731 class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 16189
diff changeset
   672
! !
2bed37c6f731 class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 16189
diff changeset
   673
3170
7b299d83c45a moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents: 3032
diff changeset
   674
!PositionableStream methodsFor:'reading'!
7b299d83c45a moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents: 3032
diff changeset
   675
9003
baa2a9409c9d speed up #nextAvailable
Stefan Vogel <sv@exept.de>
parents: 8443
diff changeset
   676
nextAvailable:count
22204
189b58c6ed87 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 22003
diff changeset
   677
    "return the next count elements of the stream as aCollection.
189b58c6ed87 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 22003
diff changeset
   678
     If the stream reaches the end before count elements have been read,
189b58c6ed87 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 22003
diff changeset
   679
     return what is available. (i.e. a shorter collection)."
189b58c6ed87 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 22003
diff changeset
   680
17606
b30bac248329 class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 17559
diff changeset
   681
    collection notNil ifTrue:[
b30bac248329 class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 17559
diff changeset
   682
        |end result|
9003
baa2a9409c9d speed up #nextAvailable
Stefan Vogel <sv@exept.de>
parents: 8443
diff changeset
   683
17606
b30bac248329 class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 17559
diff changeset
   684
        end := position + count.
b30bac248329 class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 17559
diff changeset
   685
        end >= readLimit ifTrue:[
b30bac248329 class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 17559
diff changeset
   686
            end := readLimit.
b30bac248329 class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 17559
diff changeset
   687
        ].
b30bac248329 class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 17559
diff changeset
   688
b30bac248329 class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 17559
diff changeset
   689
        result := collection copyFrom:position+1 to:end.
b30bac248329 class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 17559
diff changeset
   690
        position := end.
b30bac248329 class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 17559
diff changeset
   691
        ^ result.
9003
baa2a9409c9d speed up #nextAvailable
Stefan Vogel <sv@exept.de>
parents: 8443
diff changeset
   692
    ].
baa2a9409c9d speed up #nextAvailable
Stefan Vogel <sv@exept.de>
parents: 8443
diff changeset
   693
17606
b30bac248329 class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 17559
diff changeset
   694
    ^ super nextAvailable:count.
9003
baa2a9409c9d speed up #nextAvailable
Stefan Vogel <sv@exept.de>
parents: 8443
diff changeset
   695
baa2a9409c9d speed up #nextAvailable
Stefan Vogel <sv@exept.de>
parents: 8443
diff changeset
   696
    "
baa2a9409c9d speed up #nextAvailable
Stefan Vogel <sv@exept.de>
parents: 8443
diff changeset
   697
        'abc' readStream nextAvailable:1.
baa2a9409c9d speed up #nextAvailable
Stefan Vogel <sv@exept.de>
parents: 8443
diff changeset
   698
        'abc' readStream nextAvailable:2.
baa2a9409c9d speed up #nextAvailable
Stefan Vogel <sv@exept.de>
parents: 8443
diff changeset
   699
        'abc' readStream nextAvailable:3.
baa2a9409c9d speed up #nextAvailable
Stefan Vogel <sv@exept.de>
parents: 8443
diff changeset
   700
        'abc' readStream nextAvailable:4.
baa2a9409c9d speed up #nextAvailable
Stefan Vogel <sv@exept.de>
parents: 8443
diff changeset
   701
baa2a9409c9d speed up #nextAvailable
Stefan Vogel <sv@exept.de>
parents: 8443
diff changeset
   702
        'abc' readStream nextAvailable:2; nextAvailable:2.
baa2a9409c9d speed up #nextAvailable
Stefan Vogel <sv@exept.de>
parents: 8443
diff changeset
   703
        'abc' readStream nextAvailable:3; nextAvailable:3.
baa2a9409c9d speed up #nextAvailable
Stefan Vogel <sv@exept.de>
parents: 8443
diff changeset
   704
    "
22204
189b58c6ed87 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 22003
diff changeset
   705
189b58c6ed87 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 22003
diff changeset
   706
    "Modified (format): / 22-08-2017 / 20:25:54 / cg"
9003
baa2a9409c9d speed up #nextAvailable
Stefan Vogel <sv@exept.de>
parents: 8443
diff changeset
   707
!
baa2a9409c9d speed up #nextAvailable
Stefan Vogel <sv@exept.de>
parents: 8443
diff changeset
   708
17611
a3d9a43a6197 class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 17606
diff changeset
   709
nextAvailable:count into:aCollection startingAt:initialIndex
a3d9a43a6197 class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 17606
diff changeset
   710
    "return the next count objects from the stream. If the end is
22514
e7e21b6d5ea6 #OTHER by mawalch
mawalch
parents: 22410
diff changeset
   711
     reached before, only that many objects are copied into the
17611
a3d9a43a6197 class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 17606
diff changeset
   712
     collection.
a3d9a43a6197 class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 17606
diff changeset
   713
     Returns the number of objects that have been actually read."
a3d9a43a6197 class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 17606
diff changeset
   714
a3d9a43a6197 class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 17606
diff changeset
   715
    | max |
a3d9a43a6197 class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 17606
diff changeset
   716
a3d9a43a6197 class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 17606
diff changeset
   717
    collection notNil ifTrue:[
a3d9a43a6197 class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 17606
diff changeset
   718
        max := (readLimit - position) min: count.
a3d9a43a6197 class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 17606
diff changeset
   719
        aCollection
a3d9a43a6197 class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 17606
diff changeset
   720
            replaceFrom: initialIndex
a3d9a43a6197 class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 17606
diff changeset
   721
            to: initialIndex+max-1
a3d9a43a6197 class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 17606
diff changeset
   722
            with: collection
a3d9a43a6197 class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 17606
diff changeset
   723
            startingAt: position+1.
a3d9a43a6197 class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 17606
diff changeset
   724
        position := position + max.
a3d9a43a6197 class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 17606
diff changeset
   725
        ^ max.
a3d9a43a6197 class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 17606
diff changeset
   726
    ].
a3d9a43a6197 class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 17606
diff changeset
   727
a3d9a43a6197 class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 17606
diff changeset
   728
    ^ super nextAvailable:count into:aCollection startingAt:initialIndex.
a3d9a43a6197 class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 17606
diff changeset
   729
a3d9a43a6197 class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 17606
diff changeset
   730
    "
a3d9a43a6197 class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 17606
diff changeset
   731
     |s n buffer|
a3d9a43a6197 class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 17606
diff changeset
   732
a3d9a43a6197 class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 17606
diff changeset
   733
     buffer := ByteArray new:10.
a3d9a43a6197 class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 17606
diff changeset
   734
a3d9a43a6197 class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 17606
diff changeset
   735
     s := ReadStream on:#[1 2 3 4 5 6 7 8 9].
a3d9a43a6197 class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 17606
diff changeset
   736
     s next:3.
a3d9a43a6197 class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 17606
diff changeset
   737
     n := s nextBytes:9 into:buffer startingAt:1.
a3d9a43a6197 class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 17606
diff changeset
   738
     Transcript showCR:('n = %1; buffer = <%2>' bindWith:n with:buffer)
a3d9a43a6197 class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 17606
diff changeset
   739
    "
22514
e7e21b6d5ea6 #OTHER by mawalch
mawalch
parents: 22410
diff changeset
   740
e7e21b6d5ea6 #OTHER by mawalch
mawalch
parents: 22410
diff changeset
   741
    "Modified (comment): / 29-01-2018 / 14:43:56 / mawalch"
17611
a3d9a43a6197 class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 17606
diff changeset
   742
!
a3d9a43a6197 class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 17606
diff changeset
   743
21136
fc36f8c599ce #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21034
diff changeset
   744
peek
fc36f8c599ce #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21034
diff changeset
   745
    "look ahead for and return the next element"
17611
a3d9a43a6197 class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 17606
diff changeset
   746
21136
fc36f8c599ce #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21034
diff changeset
   747
    |peekObject|
20691
1d196c7aa50e #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 20423
diff changeset
   748
21136
fc36f8c599ce #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21034
diff changeset
   749
    peekObject := self next.
fc36f8c599ce #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21034
diff changeset
   750
    self backStep.
fc36f8c599ce #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21034
diff changeset
   751
    ^ peekObject
20691
1d196c7aa50e #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 20423
diff changeset
   752
!
1d196c7aa50e #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 20423
diff changeset
   753
22003
1a4affe1e22c #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21136
diff changeset
   754
peek:n
1a4affe1e22c #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21136
diff changeset
   755
    "look ahead n elements and return them after positioning pack to the 
1a4affe1e22c #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21136
diff changeset
   756
     position we had before"
1a4affe1e22c #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21136
diff changeset
   757
1a4affe1e22c #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21136
diff changeset
   758
    |posBefore peekObjects|
1a4affe1e22c #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21136
diff changeset
   759
1a4affe1e22c #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21136
diff changeset
   760
    posBefore := self position.
1a4affe1e22c #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21136
diff changeset
   761
    peekObjects := self next:n.
1a4affe1e22c #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21136
diff changeset
   762
    self position:posBefore.
1a4affe1e22c #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21136
diff changeset
   763
    ^ peekObjects
1a4affe1e22c #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21136
diff changeset
   764
1a4affe1e22c #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21136
diff changeset
   765
    "
1a4affe1e22c #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21136
diff changeset
   766
     |s|
1a4affe1e22c #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21136
diff changeset
   767
1a4affe1e22c #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21136
diff changeset
   768
     s := '1234567890' readStream.
1a4affe1e22c #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21136
diff changeset
   769
     s peek:4.
1a4affe1e22c #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21136
diff changeset
   770
     s next:5.
1a4affe1e22c #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21136
diff changeset
   771
    "
1a4affe1e22c #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21136
diff changeset
   772
1a4affe1e22c #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21136
diff changeset
   773
    "Created: / 13-07-2017 / 12:32:03 / cg"
1a4affe1e22c #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21136
diff changeset
   774
!
1a4affe1e22c #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21136
diff changeset
   775
3170
7b299d83c45a moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents: 3032
diff changeset
   776
upToAll:aCollection
16186
dae8e43ccc46 class: PositionableStream
Claus Gittinger <cg@exept.de>
parents: 15640
diff changeset
   777
    "read until a subcollection consisisting of the elements in aCollection is encountered.
3170
7b299d83c45a moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents: 3032
diff changeset
   778
     Return everything read excluding the elements in aCollection.
7b299d83c45a moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents: 3032
diff changeset
   779
     The position is left before the collection; i.e. the next
7b299d83c45a moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents: 3032
diff changeset
   780
     read operations will return those elements.
7b299d83c45a moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents: 3032
diff changeset
   781
     If no such subcollection is encountered, all elements up to the end 
7b299d83c45a moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents: 3032
diff changeset
   782
     are read and returned.
6494
5b3d8667c22a comment
Claus Gittinger <cg@exept.de>
parents: 6421
diff changeset
   783
     See also #throughAll: which also reads up to some objects
3170
7b299d83c45a moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents: 3032
diff changeset
   784
     but positions behind it and DOES include it in the returned
3184
d99302322139 Ok, #upToAll: positions after the collection.
Stefan Vogel <sv@exept.de>
parents: 3170
diff changeset
   785
     collection.
6494
5b3d8667c22a comment
Claus Gittinger <cg@exept.de>
parents: 6421
diff changeset
   786
     See also #upToAllExcluding:, which returns the same, but leaves the
5b3d8667c22a comment
Claus Gittinger <cg@exept.de>
parents: 6421
diff changeset
   787
     read pointer after the matched subcollection.
3184
d99302322139 Ok, #upToAll: positions after the collection.
Stefan Vogel <sv@exept.de>
parents: 3170
diff changeset
   788
d99302322139 Ok, #upToAll: positions after the collection.
Stefan Vogel <sv@exept.de>
parents: 3170
diff changeset
   789
     Note: this behavior is inconsistent with the other upTo.. methods,
15159
b94bdee97afa Changed usage of deprecated #copyWithoutLast: to #copyButLast:
Stefan Vogel <sv@exept.de>
parents: 14934
diff changeset
   790
           which position after the found item. We implement the method
b94bdee97afa Changed usage of deprecated #copyWithoutLast: to #copyButLast:
Stefan Vogel <sv@exept.de>
parents: 14934
diff changeset
   791
           this way for the sake of ST80-compatibility."
3170
7b299d83c45a moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents: 3032
diff changeset
   792
17611
a3d9a43a6197 class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 17606
diff changeset
   793
    "/ in the future, this will have the Squeak semantics, which is upToAllExcluding:
16189
d7b9978c07bb class: PositionableStream
Claus Gittinger <cg@exept.de>
parents: 16188
diff changeset
   794
d7b9978c07bb class: PositionableStream
Claus Gittinger <cg@exept.de>
parents: 16188
diff changeset
   795
    ^ self upToAll_positionBefore:aCollection
d7b9978c07bb class: PositionableStream
Claus Gittinger <cg@exept.de>
parents: 16188
diff changeset
   796
d7b9978c07bb class: PositionableStream
Claus Gittinger <cg@exept.de>
parents: 16188
diff changeset
   797
    "
d7b9978c07bb class: PositionableStream
Claus Gittinger <cg@exept.de>
parents: 16188
diff changeset
   798
     |s|
d7b9978c07bb class: PositionableStream
Claus Gittinger <cg@exept.de>
parents: 16188
diff changeset
   799
     s := ReadStream on:'hello world'.
d7b9978c07bb class: PositionableStream
Claus Gittinger <cg@exept.de>
parents: 16188
diff changeset
   800
     Transcript show:'<'; show:(s upToAll:'wo'); showCR:'>'. 
d7b9978c07bb class: PositionableStream
Claus Gittinger <cg@exept.de>
parents: 16188
diff changeset
   801
     Transcript showCR:s atEnd.
d7b9978c07bb class: PositionableStream
Claus Gittinger <cg@exept.de>
parents: 16188
diff changeset
   802
     Transcript show:'<'; show:(s upToEnd); showCR:'>'. 
d7b9978c07bb class: PositionableStream
Claus Gittinger <cg@exept.de>
parents: 16188
diff changeset
   803
    "
d7b9978c07bb class: PositionableStream
Claus Gittinger <cg@exept.de>
parents: 16188
diff changeset
   804
    "
d7b9978c07bb class: PositionableStream
Claus Gittinger <cg@exept.de>
parents: 16188
diff changeset
   805
     |s|
d7b9978c07bb class: PositionableStream
Claus Gittinger <cg@exept.de>
parents: 16188
diff changeset
   806
     s := ReadStream on:'hello world'.
d7b9978c07bb class: PositionableStream
Claus Gittinger <cg@exept.de>
parents: 16188
diff changeset
   807
     Transcript show:'<'; show:(s upToAll:'wo'); showCR:'>'. 
d7b9978c07bb class: PositionableStream
Claus Gittinger <cg@exept.de>
parents: 16188
diff changeset
   808
     Transcript showCR:s atEnd.
d7b9978c07bb class: PositionableStream
Claus Gittinger <cg@exept.de>
parents: 16188
diff changeset
   809
     Transcript show:'<'; show:(s upToAll:'wo'); showCR:'>'. 
d7b9978c07bb class: PositionableStream
Claus Gittinger <cg@exept.de>
parents: 16188
diff changeset
   810
    "
d7b9978c07bb class: PositionableStream
Claus Gittinger <cg@exept.de>
parents: 16188
diff changeset
   811
    "
d7b9978c07bb class: PositionableStream
Claus Gittinger <cg@exept.de>
parents: 16188
diff changeset
   812
     |s|
d7b9978c07bb class: PositionableStream
Claus Gittinger <cg@exept.de>
parents: 16188
diff changeset
   813
     s := ReadStream on:'hello world'.
d7b9978c07bb class: PositionableStream
Claus Gittinger <cg@exept.de>
parents: 16188
diff changeset
   814
     Transcript show:'<'; show:(s upToAll:'xx'); showCR:'>'. 
d7b9978c07bb class: PositionableStream
Claus Gittinger <cg@exept.de>
parents: 16188
diff changeset
   815
     Transcript showCR:s atEnd.
d7b9978c07bb class: PositionableStream
Claus Gittinger <cg@exept.de>
parents: 16188
diff changeset
   816
     Transcript show:'<'; show:(s upToEnd); showCR:'>'. 
d7b9978c07bb class: PositionableStream
Claus Gittinger <cg@exept.de>
parents: 16188
diff changeset
   817
    "
d7b9978c07bb class: PositionableStream
Claus Gittinger <cg@exept.de>
parents: 16188
diff changeset
   818
d7b9978c07bb class: PositionableStream
Claus Gittinger <cg@exept.de>
parents: 16188
diff changeset
   819
    "Modified: / 12.1.1998 / 22:06:42 / cg"
d7b9978c07bb class: PositionableStream
Claus Gittinger <cg@exept.de>
parents: 16188
diff changeset
   820
    "Created: / 12.1.1998 / 22:07:01 / cg"
d7b9978c07bb class: PositionableStream
Claus Gittinger <cg@exept.de>
parents: 16188
diff changeset
   821
!
d7b9978c07bb class: PositionableStream
Claus Gittinger <cg@exept.de>
parents: 16188
diff changeset
   822
d7b9978c07bb class: PositionableStream
Claus Gittinger <cg@exept.de>
parents: 16188
diff changeset
   823
upToAll_positionBefore:aCollection
d7b9978c07bb class: PositionableStream
Claus Gittinger <cg@exept.de>
parents: 16188
diff changeset
   824
    "read until a subcollection consisisting of the elements in aCollection is encountered.
d7b9978c07bb class: PositionableStream
Claus Gittinger <cg@exept.de>
parents: 16188
diff changeset
   825
     Return everything read excluding the elements in aCollection.
d7b9978c07bb class: PositionableStream
Claus Gittinger <cg@exept.de>
parents: 16188
diff changeset
   826
     The position is left before the collection; i.e. the next
d7b9978c07bb class: PositionableStream
Claus Gittinger <cg@exept.de>
parents: 16188
diff changeset
   827
     read operations will return those elements.
d7b9978c07bb class: PositionableStream
Claus Gittinger <cg@exept.de>
parents: 16188
diff changeset
   828
     If no such subcollection is encountered, all elements up to the end 
d7b9978c07bb class: PositionableStream
Claus Gittinger <cg@exept.de>
parents: 16188
diff changeset
   829
     are read and returned.
d7b9978c07bb class: PositionableStream
Claus Gittinger <cg@exept.de>
parents: 16188
diff changeset
   830
     See also #throughAll: which also reads up to some objects
d7b9978c07bb class: PositionableStream
Claus Gittinger <cg@exept.de>
parents: 16188
diff changeset
   831
     but positions behind it and DOES include it in the returned
16840
48075991f9d9 class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 16209
diff changeset
   832
     collection."
16189
d7b9978c07bb class: PositionableStream
Claus Gittinger <cg@exept.de>
parents: 16188
diff changeset
   833
16209
f2d1d7b2d649 class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 16195
diff changeset
   834
    |answerStream element last|
3170
7b299d83c45a moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents: 3032
diff changeset
   835
7b299d83c45a moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents: 3032
diff changeset
   836
    last := aCollection last.
22410
60be3e20e9f0 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 22404
diff changeset
   837
    answerStream := self contentsSpecies writeStream.
16209
f2d1d7b2d649 class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 16195
diff changeset
   838
    [(element := self nextOrNil) notNil] whileTrue:[
15159
b94bdee97afa Changed usage of deprecated #copyWithoutLast: to #copyButLast:
Stefan Vogel <sv@exept.de>
parents: 14934
diff changeset
   839
        answerStream nextPut:element.
16209
f2d1d7b2d649 class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 16195
diff changeset
   840
        (element = last and:[answerStream endsBeforePositionWith:aCollection]) ifTrue:[
f2d1d7b2d649 class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 16195
diff changeset
   841
            |backStep|
f2d1d7b2d649 class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 16195
diff changeset
   842
            backStep := aCollection size negated.
f2d1d7b2d649 class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 16195
diff changeset
   843
            self skip:backStep.
f2d1d7b2d649 class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 16195
diff changeset
   844
            answerStream skip:backStep.
f2d1d7b2d649 class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 16195
diff changeset
   845
            ^ answerStream contents
15159
b94bdee97afa Changed usage of deprecated #copyWithoutLast: to #copyButLast:
Stefan Vogel <sv@exept.de>
parents: 14934
diff changeset
   846
        ].
3170
7b299d83c45a moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents: 3032
diff changeset
   847
    ].
7b299d83c45a moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents: 3032
diff changeset
   848
    ^ answerStream contents
7b299d83c45a moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents: 3032
diff changeset
   849
7b299d83c45a moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents: 3032
diff changeset
   850
    "
7b299d83c45a moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents: 3032
diff changeset
   851
     |s|
7b299d83c45a moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents: 3032
diff changeset
   852
     s := ReadStream on:'hello world'.
16209
f2d1d7b2d649 class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 16195
diff changeset
   853
     Transcript show:'<'; show:(s upToAll_positionBefore:'wo'); showCR:'>'. 
6494
5b3d8667c22a comment
Claus Gittinger <cg@exept.de>
parents: 6421
diff changeset
   854
     Transcript showCR:s atEnd.
3170
7b299d83c45a moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents: 3032
diff changeset
   855
     Transcript show:'<'; show:(s upToEnd); showCR:'>'. 
7b299d83c45a moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents: 3032
diff changeset
   856
    "
7b299d83c45a moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents: 3032
diff changeset
   857
    "
7b299d83c45a moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents: 3032
diff changeset
   858
     |s|
7b299d83c45a moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents: 3032
diff changeset
   859
     s := ReadStream on:'hello world'.
16209
f2d1d7b2d649 class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 16195
diff changeset
   860
     Transcript show:'<'; show:(s upToAll_positionBefore:'wo'); showCR:'>'. 
16186
dae8e43ccc46 class: PositionableStream
Claus Gittinger <cg@exept.de>
parents: 15640
diff changeset
   861
     Transcript showCR:s atEnd.
16209
f2d1d7b2d649 class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 16195
diff changeset
   862
     Transcript show:'<'; show:(s upToAll_positionBefore:'wo'); showCR:'>'. 
16186
dae8e43ccc46 class: PositionableStream
Claus Gittinger <cg@exept.de>
parents: 15640
diff changeset
   863
    "
dae8e43ccc46 class: PositionableStream
Claus Gittinger <cg@exept.de>
parents: 15640
diff changeset
   864
    "
dae8e43ccc46 class: PositionableStream
Claus Gittinger <cg@exept.de>
parents: 15640
diff changeset
   865
     |s|
dae8e43ccc46 class: PositionableStream
Claus Gittinger <cg@exept.de>
parents: 15640
diff changeset
   866
     s := ReadStream on:'hello world'.
16209
f2d1d7b2d649 class: PositionableStream
Stefan Vogel <sv@exept.de>
parents: 16195
diff changeset
   867
     Transcript show:'<'; show:(s upToAll_positionBefore:'xx'); showCR:'>'. 
3170
7b299d83c45a moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents: 3032
diff changeset
   868
     Transcript showCR:s atEnd.
7b299d83c45a moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents: 3032
diff changeset
   869
     Transcript show:'<'; show:(s upToEnd); showCR:'>'. 
7b299d83c45a moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents: 3032
diff changeset
   870
    "
7b299d83c45a moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents: 3032
diff changeset
   871
22410
60be3e20e9f0 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 22404
diff changeset
   872
    "Created: / 12-01-1998 / 22:07:01 / cg"
60be3e20e9f0 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 22404
diff changeset
   873
    "Modified: / 10-01-2018 / 18:30:07 / stefan"
20101
2571117831be #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 19890
diff changeset
   874
!
2571117831be #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 19890
diff changeset
   875
2571117831be #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 19890
diff changeset
   876
upToEnd
2571117831be #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 19890
diff changeset
   877
    collection notNil ifTrue:[
2571117831be #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 19890
diff changeset
   878
        |result|
2571117831be #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 19890
diff changeset
   879
2571117831be #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 19890
diff changeset
   880
        result := collection copyFrom:position+1 to:readLimit.
2571117831be #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 19890
diff changeset
   881
        position := readLimit.
2571117831be #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 19890
diff changeset
   882
        ^ result.
2571117831be #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 19890
diff changeset
   883
    ].
2571117831be #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 19890
diff changeset
   884
2571117831be #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 19890
diff changeset
   885
    ^ super upToEnd.
2571117831be #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 19890
diff changeset
   886
2571117831be #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 19890
diff changeset
   887
    "
2571117831be #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 19890
diff changeset
   888
        'abc' readStream upToEnd.
2571117831be #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 19890
diff changeset
   889
    "
3170
7b299d83c45a moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents: 3032
diff changeset
   890
! !
7b299d83c45a moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents: 3032
diff changeset
   891
21136
fc36f8c599ce #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21034
diff changeset
   892
!PositionableStream methodsFor:'reading-strings'!
fc36f8c599ce #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21034
diff changeset
   893
fc36f8c599ce #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21034
diff changeset
   894
nextLine
fc36f8c599ce #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21034
diff changeset
   895
    "return the characters upTo (but excluding) the next cr (carriage return)
fc36f8c599ce #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21034
diff changeset
   896
     character (i.e. read a single line of text).
fc36f8c599ce #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21034
diff changeset
   897
     If the previous-to-last character is a cr, this is also removed,
fc36f8c599ce #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21034
diff changeset
   898
     so it's possible to read alien (i.e. ms-dos) text as well.
fc36f8c599ce #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21034
diff changeset
   899
     Added for protocol compatibility with externalStreams."
fc36f8c599ce #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21034
diff changeset
   900
fc36f8c599ce #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21034
diff changeset
   901
    |start "{ Class:SmallInteger }" 
fc36f8c599ce #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21034
diff changeset
   902
     end "{ Class:SmallInteger }"|
fc36f8c599ce #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21034
diff changeset
   903
fc36f8c599ce #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21034
diff changeset
   904
    collection isString ifTrue:[
fc36f8c599ce #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21034
diff changeset
   905
        position == readLimit ifTrue:[
fc36f8c599ce #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21034
diff changeset
   906
            ^ self pastEndRead
fc36f8c599ce #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21034
diff changeset
   907
        ].
fc36f8c599ce #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21034
diff changeset
   908
        start := position+1.
fc36f8c599ce #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21034
diff changeset
   909
        end := collection indexOf:Character cr startingAt:start.
fc36f8c599ce #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21034
diff changeset
   910
fc36f8c599ce #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21034
diff changeset
   911
        (end == 0 or:[end > readLimit]) ifTrue:[
fc36f8c599ce #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21034
diff changeset
   912
            end := position := readLimit.
fc36f8c599ce #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21034
diff changeset
   913
        ] ifFalse:[
fc36f8c599ce #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21034
diff changeset
   914
            position := end.
fc36f8c599ce #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21034
diff changeset
   915
            end := end - 1.    "skip lf"
fc36f8c599ce #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21034
diff changeset
   916
        ].
fc36f8c599ce #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21034
diff changeset
   917
        start > end ifTrue:[
fc36f8c599ce #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21034
diff changeset
   918
            ^ ''.
fc36f8c599ce #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21034
diff changeset
   919
        ].
fc36f8c599ce #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21034
diff changeset
   920
        (collection at:end) == Character return ifTrue:[
fc36f8c599ce #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21034
diff changeset
   921
            end := end - 1.    "skip return"
fc36f8c599ce #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21034
diff changeset
   922
        ].
fc36f8c599ce #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21034
diff changeset
   923
        ^ collection copyFrom:start to:end.
fc36f8c599ce #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21034
diff changeset
   924
    ].
fc36f8c599ce #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21034
diff changeset
   925
    ^ super nextLine.
fc36f8c599ce #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21034
diff changeset
   926
fc36f8c599ce #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21034
diff changeset
   927
    "
fc36f8c599ce #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21034
diff changeset
   928
        '12345678' readStream nextLine
fc36f8c599ce #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21034
diff changeset
   929
        '12345678' allBold readStream nextLine
fc36f8c599ce #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21034
diff changeset
   930
        '12\34\56\78' withCRs readStream nextLine
fc36f8c599ce #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21034
diff changeset
   931
        '12\34\56\78' withCRs readStream nextLine; nextLine
fc36f8c599ce #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21034
diff changeset
   932
        (ReadStream on:('12\34\56\78' withCRs) from:1 to:4) nextLine; nextLine
fc36f8c599ce #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21034
diff changeset
   933
        ('12\' withCRs, Character return, '34') readStream nextLine; nextLine
fc36f8c599ce #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21034
diff changeset
   934
        Character cr asString readStream nextLine
fc36f8c599ce #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21034
diff changeset
   935
        Character return asString readStream nextLine
fc36f8c599ce #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21034
diff changeset
   936
        (Character return, Character cr) asString readStream nextLine
fc36f8c599ce #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21034
diff changeset
   937
        Character return asString readStream nextLine; nextLine
fc36f8c599ce #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21034
diff changeset
   938
    "
fc36f8c599ce #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21034
diff changeset
   939
! !
fc36f8c599ce #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21034
diff changeset
   940
23895
d49a46365ed5 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22514
diff changeset
   941
!PositionableStream methodsFor:'stream-to-stream copy'!
d49a46365ed5 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22514
diff changeset
   942
d49a46365ed5 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22514
diff changeset
   943
copy:numberOfElementsOrNil into:aWriteStream
d49a46365ed5 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22514
diff changeset
   944
    "read from the receiver, and write numberOfElementsOrNil data to another aWriteStream.
d49a46365ed5 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22514
diff changeset
   945
     If numberOfElementsOrNil is nil, copy until the end of myself.
d49a46365ed5 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22514
diff changeset
   946
     Return the number of elements which have been transferred.
d49a46365ed5 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22514
diff changeset
   947
     Redefined here to avoid intermediate buffers/garbage."
d49a46365ed5 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22514
diff changeset
   948
d49a46365ed5 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22514
diff changeset
   949
    |endPosition cnt|
d49a46365ed5 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22514
diff changeset
   950
d49a46365ed5 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22514
diff changeset
   951
    collection notNil ifTrue:[
d49a46365ed5 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22514
diff changeset
   952
        numberOfElementsOrNil isNil ifTrue:[
d49a46365ed5 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22514
diff changeset
   953
            endPosition := readLimit.   "/ read until end
d49a46365ed5 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22514
diff changeset
   954
        ] ifFalse:[
d49a46365ed5 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22514
diff changeset
   955
            endPosition := (position+numberOfElementsOrNil) min:readLimit.
d49a46365ed5 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22514
diff changeset
   956
        ].
d49a46365ed5 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22514
diff changeset
   957
        aWriteStream nextPutAll:collection startingAt:position+1 to:endPosition.
d49a46365ed5 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22514
diff changeset
   958
        cnt := endPosition - position.
d49a46365ed5 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22514
diff changeset
   959
        position := endPosition.
d49a46365ed5 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22514
diff changeset
   960
        ^ cnt.
d49a46365ed5 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22514
diff changeset
   961
    ].
d49a46365ed5 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22514
diff changeset
   962
d49a46365ed5 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22514
diff changeset
   963
    ^ super copy:numberOfElementsOrNil into:aWriteStream.
d49a46365ed5 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22514
diff changeset
   964
d49a46365ed5 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22514
diff changeset
   965
d49a46365ed5 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22514
diff changeset
   966
    "
d49a46365ed5 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22514
diff changeset
   967
      'hello world' readStream copy:5 into:'/tmp/mist' asFilename writeStream.
d49a46365ed5 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22514
diff changeset
   968
      'hello world' readStream 
d49a46365ed5 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22514
diff changeset
   969
                        copy:5 into:Transcript;
d49a46365ed5 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22514
diff changeset
   970
                        copy:20 into:Transcript.
d49a46365ed5 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22514
diff changeset
   971
      'hello world' readStream copy:5 into:'' writeStream inspect.
d49a46365ed5 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22514
diff changeset
   972
      #[1 2 3 4 5 6 7] readStream copy:2 into:'/tmp/mist' asFilename writeStream binary.
d49a46365ed5 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22514
diff changeset
   973
      #[1 2 3 4 5 6 7] readStream copy:3 into:#[] writeStream.
d49a46365ed5 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22514
diff changeset
   974
    "
d49a46365ed5 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22514
diff changeset
   975
d49a46365ed5 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22514
diff changeset
   976
    "
d49a46365ed5 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22514
diff changeset
   977
     |rs ws cnt|
d49a46365ed5 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22514
diff changeset
   978
d49a46365ed5 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22514
diff changeset
   979
     ws := #() writeStream.
d49a46365ed5 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22514
diff changeset
   980
     rs := #( 1 2 3 4 a nil true) readWriteStream.
d49a46365ed5 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22514
diff changeset
   981
     rs next.
d49a46365ed5 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22514
diff changeset
   982
     cnt := rs copyToEndInto:ws bufferSize:0.
d49a46365ed5 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22514
diff changeset
   983
     Transcript show:cnt; show:' '; showCR:ws contents.
d49a46365ed5 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22514
diff changeset
   984
    "
d49a46365ed5 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22514
diff changeset
   985
d49a46365ed5 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22514
diff changeset
   986
    "Modified (comment): / 13-03-2019 / 16:59:18 / Stefan Vogel"
d49a46365ed5 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22514
diff changeset
   987
!
d49a46365ed5 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22514
diff changeset
   988
d49a46365ed5 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22514
diff changeset
   989
copy:numberOfElementsOrNil into:aWriteStream bufferSize:bufferSize
d49a46365ed5 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22514
diff changeset
   990
    "read from the receiver, and write numberOfElementsOrNil to another aWriteStream.
d49a46365ed5 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22514
diff changeset
   991
     If numberOfElementsOrNil is nil, copy until the end of myself.
d49a46365ed5 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22514
diff changeset
   992
     Return the number of elements which have been transferred.
d49a46365ed5 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22514
diff changeset
   993
     Redefined here to avoid intermediate buffers/garbage
d49a46365ed5 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22514
diff changeset
   994
     - bufferSize does not matter here."
d49a46365ed5 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22514
diff changeset
   995
d49a46365ed5 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22514
diff changeset
   996
    collection notNil ifTrue:[
d49a46365ed5 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22514
diff changeset
   997
        ^ self copy:numberOfElementsOrNil into:aWriteStream.
d49a46365ed5 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22514
diff changeset
   998
    ].
d49a46365ed5 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22514
diff changeset
   999
    ^ super copy:numberOfElementsOrNil into:aWriteStream bufferSize:bufferSize.
d49a46365ed5 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22514
diff changeset
  1000
d49a46365ed5 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22514
diff changeset
  1001
    "Modified (comment): / 13-03-2019 / 17:00:11 / Stefan Vogel"
d49a46365ed5 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22514
diff changeset
  1002
! !
d49a46365ed5 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22514
diff changeset
  1003
611
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1004
!PositionableStream methodsFor:'testing'!
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1005
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1006
atEnd
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1007
    "return true, if the read-position is at the end"
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1008
15357
10a1cd767648 class: PositionableStream
Claus Gittinger <cg@exept.de>
parents: 15353
diff changeset
  1009
    ^ position >= readLimit
611
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1010
!
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1011
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1012
isEmpty
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1013
    "return true, if the contents of the stream is empty"
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1014
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1015
    ^ readLimit == 0
9006
e5509951529d *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 9003
diff changeset
  1016
!
e5509951529d *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 9003
diff changeset
  1017
10577
3a97ef89ffb1 +isInternalByteStream
Claus Gittinger <cg@exept.de>
parents: 10357
diff changeset
  1018
isInternalByteStream
3a97ef89ffb1 +isInternalByteStream
Claus Gittinger <cg@exept.de>
parents: 10357
diff changeset
  1019
    "return true, if the stream is an internal stream reading bytes"
3a97ef89ffb1 +isInternalByteStream
Claus Gittinger <cg@exept.de>
parents: 10357
diff changeset
  1020
3a97ef89ffb1 +isInternalByteStream
Claus Gittinger <cg@exept.de>
parents: 10357
diff changeset
  1021
    ^ collection class isBytes
3a97ef89ffb1 +isInternalByteStream
Claus Gittinger <cg@exept.de>
parents: 10357
diff changeset
  1022
3a97ef89ffb1 +isInternalByteStream
Claus Gittinger <cg@exept.de>
parents: 10357
diff changeset
  1023
    "Created: / 30-05-2007 / 16:16:12 / cg"
3a97ef89ffb1 +isInternalByteStream
Claus Gittinger <cg@exept.de>
parents: 10357
diff changeset
  1024
!
3a97ef89ffb1 +isInternalByteStream
Claus Gittinger <cg@exept.de>
parents: 10357
diff changeset
  1025
9006
e5509951529d *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 9003
diff changeset
  1026
isPositionable
e5509951529d *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 9003
diff changeset
  1027
    "return true, if the stream supports positioning (this one is)"
e5509951529d *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 9003
diff changeset
  1028
e5509951529d *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 9003
diff changeset
  1029
    ^ true
611
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1030
! !
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1031
1897
22d1ce9b62a4 fixes for packageQuery
Claus Gittinger <cg@exept.de>
parents: 1489
diff changeset
  1032
!PositionableStream class methodsFor:'documentation'!
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 611
diff changeset
  1033
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 611
diff changeset
  1034
version
19403
5accf347c2c1 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 17611
diff changeset
  1035
    ^ '$Header$'
13379
a4644dee3177 changed: #on:
sr
parents: 11434
diff changeset
  1036
!
a4644dee3177 changed: #on:
sr
parents: 11434
diff changeset
  1037
a4644dee3177 changed: #on:
sr
parents: 11434
diff changeset
  1038
version_CVS
19403
5accf347c2c1 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 17611
diff changeset
  1039
    ^ '$Header$'
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 611
diff changeset
  1040
! !
6777
5efddf5358b2 currentFileInDirectory query
Claus Gittinger <cg@exept.de>
parents: 6707
diff changeset
  1041
14934
5e273cddded7 setCollection: moved
Claus Gittinger <cg@exept.de>
parents: 13379
diff changeset
  1042
611
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1043
PositionableStream initialize!