/usr/share/gnu-smalltalk/kernel/PosStream.st is in gnu-smalltalk-common 3.2.5-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 | "======================================================================
|
| PositionableStream Method Definitions
|
|
======================================================================"
"======================================================================
|
| Copyright 1988,92,94,95,99,2000,2001,2002,2006,2007,2008,2009
| Free Software Foundation, Inc.
| Written by Steve Byrne.
|
| This file is part of the GNU Smalltalk class library.
|
| The GNU Smalltalk class library is free software; you can redistribute it
| and/or modify it under the terms of the GNU Lesser General Public License
| as published by the Free Software Foundation; either version 2.1, or (at
| your option) any later version.
|
| The GNU Smalltalk class library is distributed in the hope that it will be
| useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
| General Public License for more details.
|
| You should have received a copy of the GNU Lesser General Public License
| along with the GNU Smalltalk class library; see the file COPYING.LIB.
| If not, write to the Free Software Foundation, 59 Temple Place - Suite
| 330, Boston, MA 02110-1301, USA.
|
======================================================================"
Stream subclass: PositionableStream [
| collection ptr endPtr access |
<category: 'Streams-Collections'>
<comment: 'My instances represent streams where explicit positioning is permitted.
Thus, my streams act in a manner to normal disk files: you can read
or write sequentially, but also position the file to a particular place
whenever you choose. Generally, you''ll want to use ReadStream, WriteStream
or ReadWriteStream instead of me to create and use streams.'>
PositionableStream class >> on: aCollection [
"Answer an instance of the receiver streaming on the
whole contents of aCollection"
<category: 'instance creation'>
self subclassResponsibility
]
PositionableStream class >> on: aCollection from: firstIndex to: lastIndex [
"Answer an instance of the receiver streaming from
the firstIndex-th item of aCollection to the lastIndex-th"
<category: 'instance creation'>
^self on: (aCollection copyFrom: firstIndex to: lastIndex)
]
close [
"Disassociate a stream from its backing store."
<category: 'accessing-reading'>
self flush.
collection := nil.
endPtr := nil.
ptr := nil.
access := nil
]
next [
"Answer the next item of the receiver. Returns nil when at end of stream."
<category: 'accessing-reading'>
| element |
(access bitAnd: 1) = 0 ifTrue: [^self shouldNotImplement].
ptr > endPtr ifTrue: [^self pastEnd].
element := collection at: ptr.
ptr := ptr + 1.
^element
]
nextPutAllOn: aStream [
"Write all the objects in the receiver to aStream."
| n |
(access bitAnd: 1) = 0 ifTrue: [^self shouldNotImplement].
n := endPtr - ptr + 1.
aStream next: n putAll: collection startingAt: ptr.
ptr := ptr + n.
]
nextAvailable: anInteger putAllOn: aStream [
"Copy up to anInteger objects from the receiver into
aStream, stopping if no more data is available."
<category: 'accessing-reading'>
| n |
(access bitAnd: 1) = 0 ifTrue: [^self shouldNotImplement].
n := anInteger min: endPtr - ptr + 1.
aStream next: n putAll: collection startingAt: ptr.
ptr := ptr + n.
^n
]
nextAvailable: anInteger into: aCollection startingAt: pos [
"Place up to anInteger objects from the receiver into
aCollection, starting from position pos in the collection
and stopping if no more data is available."
<category: 'accessing-reading'>
| n |
(access bitAnd: 1) = 0 ifTrue: [^self shouldNotImplement].
n := anInteger min: endPtr - ptr + 1.
aCollection
replaceFrom: pos
to: pos + n - 1
with: collection
startingAt: ptr.
ptr := ptr + n.
^n
]
peek [
"Returns the next element of the stream without moving the pointer.
Returns nil when at end of stream."
<category: 'accessing-reading'>
(access bitAnd: 1) = 0 ifTrue: [^self shouldNotImplement].
ptr > endPtr ifTrue: [^nil].
^collection at: ptr
]
peekFor: anObject [
"Returns true and gobbles the next element from the stream of it is
equal to anObject, returns false and doesn't gobble the next element
if the next element is not equal to anObject."
<category: 'accessing-reading'>
self atEnd ifTrue: [^false].
^self next = anObject
ifTrue: [true]
ifFalse:
[self skip: -1.
false]
]
copyFrom: start to: end [
"Answer the data on which the receiver is streaming, from
the start-th item to the end-th. Note that this method is 0-based,
unlike the one in Collection, because a Stream's #position method
returns 0-based values."
<category: 'accessing-reading'>
^collection copyFrom: start + 1 to: end + 1
]
contents [
"Returns a collection of the same type that the stream accesses, up to
and including the final element."
<category: 'accessing-reading'>
^collection copyFrom: 1 to: endPtr
]
readStream [
"Answer a ReadStream on the same contents as the receiver"
<category: 'accessing-reading'>
^ReadStream
on: collection
from: 1
to: endPtr
]
reverseContents [
"Returns a collection of the same type that the stream accesses, up to
and including the final element, but in reverse order."
<category: 'accessing-reading'>
| newCollection |
newCollection := collection copyEmpty: endPtr.
1 to: endPtr
do: [:i | newCollection at: i put: (collection at: endPtr - i + 1)].
^newCollection
]
atEnd [
"Answer whether the objects in the stream have reached an end"
<category: 'testing'>
^ptr > endPtr
]
basicAtEnd [
"Answer whether the objects in the stream have reached an end.
This method must NOT be overridden."
<category: 'testing'>
^ptr > endPtr
]
isEmpty [
"Answer whether the stream has no objects"
<category: 'testing'>
^endPtr = 0
]
truncate [
"Truncate the receiver to the current position - only valid
for writing streams"
<category: 'truncating'>
(access bitAnd: 2) = 0
ifTrue: [self shouldNotImplement]
ifFalse: [endPtr := ptr - 1]
]
position [
"Answer the current value of the stream pointer"
<category: 'positioning'>
^ptr - 1
]
position: anInteger [
"Move the stream pointer to the anInteger-th object"
<category: 'positioning'>
(anInteger between: 0 and: self size)
ifTrue: [ptr := anInteger + 1]
ifFalse:
[SystemExceptions.IndexOutOfRange signalOn: self withIndex: anInteger]
]
basicPosition: anInteger [
"Move the stream pointer to the anInteger-th object"
<category: 'positioning'>
(anInteger between: 0 and: self size)
ifTrue: [ptr := anInteger + 1]
ifFalse:
[SystemExceptions.IndexOutOfRange signalOn: self withIndex: anInteger]
]
reset [
"Move the stream back to its first element. For write-only streams,
the stream is truncated there."
<category: 'positioning'>
self position: 0
]
setToEnd [
"Move the current position to the end of the stream."
<category: 'positioning'>
self position: self size
]
size [
"Answer the size of data on which we are streaming."
<category: 'positioning'>
^endPtr
]
isPositionable [
"Answer true if the stream supports moving backwards with #skip:."
<category: 'positioning'>
^true
]
skip: anInteger [
"Move the current position by anInteger places, either forwards or
backwards."
<category: 'positioning'>
self position: ((self position + anInteger max: 0) min: endPtr)
]
beReadOnly [
<category: 'private'>
access := 1
]
beWriteOnly [
<category: 'private'>
access := 2
]
beReadWrite [
<category: 'private'>
access := 3
]
collection [
<category: 'private'>
^collection
]
status: aString [
"When working with a stream on strings, this method can be useful!
Format of the output:
'ABCDEFG' aString
^"
<category: 'private'>
Transcript
print: (collection copyFrom: 1 to: endPtr);
nextPutAll: ' ';
nextPutAll: aString;
nl;
next: self position put: Character space;
nextPut: $^;
nl
]
isExternalStream [
"We stream on a collection residing in the image, so answer false"
<category: 'class type methods'>
^false
]
species [
"Return the type of the collections returned by #upTo: etc., which are the
same kind as those returned by the collection with methods such as #select:."
<category: 'class type methods'>
^collection species
]
upToEnd [
"Returns a collection of the same type that the stream accesses,
containing the entire rest of the stream's contents."
<category: 'accessing-reading'>
^self next: endPtr - ptr + 1.
]
upTo: anObject [
"Returns a collection of the same type that the stream accesses, up to
but not including the object anObject. Returns the entire rest of the
stream's contents if anObject is not present."
<category: 'accessing-reading'>
| result r ws |
r := collection indexOf: anObject startingAt: ptr ifAbsent: [0].
^r = 0
ifTrue: [self upToEnd]
ifFalse: [result := self next: r - ptr. self next. ^result].
]
]
|