/usr/share/gnu-smalltalk/kernel/WriteStream.st is in gnu-smalltalk-common 3.2.5-1build2.
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 | "======================================================================
|
| WriteStream Method Definitions
|
|
======================================================================"
"======================================================================
|
| Copyright 1988,92,94,95,99,2000,2001,2002,2006,2007,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.
|
======================================================================"
PositionableStream subclass: WriteStream [
<category: 'Streams-Collections'>
<comment: 'I am the class of writeable streams. I only allow write operations to
my instances; reading is strictly forbidden.'>
WriteStream class >> on: aCollection [
"Answer a new instance of the receiver which streams on aCollection.
Every item of aCollection is discarded."
<category: 'instance creation'>
^(self basicNew initCollection: aCollection)
beWriteOnly;
truncate;
yourself
]
WriteStream class >> with: aCollection [
"Answer a new instance of the receiver which streams from the end
of aCollection."
<category: 'instance creation'>
^(self basicNew initCollection: aCollection)
beWriteOnly;
moveToEnd;
yourself
]
WriteStream class >> with: aCollection from: firstIndex to: lastIndex [
"Answer a new instance of the receiver which streams from the
firstIndex-th item of aCollection to the lastIndex-th. The
pointer is moved to the last item in that range."
<category: 'instance creation'>
^self with: (aCollection copyFrom: firstIndex to: lastIndex)
]
contents [
"Returns a collection of the same type that the stream accesses, up to
and including the final element."
<category: 'accessing-writing'>
^collection copyFrom: 1 to: ptr - 1
]
nextPut: anObject [
"Store anObject as the next item in the receiver. Grow the
collection if necessary"
"(access bitAnd: 2) = 0
ifTrue: [ ^self shouldNotImplement ]."
<category: 'accessing-writing'>
ptr > endPtr
ifTrue:
[ptr > collection size ifTrue: [self growCollection].
endPtr := ptr].
collection at: ptr put: anObject.
ptr := ptr + 1.
^anObject
]
next: n putAll: aCollection startingAt: pos [
"Put n characters or bytes of aCollection, starting at the pos-th,
in the collection buffer."
<category: 'accessing-writing'>
| end |
end := ptr + n.
end > collection size ifTrue: [ self growCollectionTo: end - 1].
collection
replaceFrom: ptr
to: end - 1
with: aCollection
startingAt: pos.
ptr := end.
ptr > endPtr ifTrue: [endPtr := ptr - 1]
]
readStream [
"Answer a ReadStream on the same contents as the receiver"
<category: 'accessing-writing'>
^ReadStream
on: collection
from: 1
to: ptr - 1
]
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-writing'>
| newCollection |
newCollection := self species new: ptr - 1.
1 to: endPtr do: [:i | newCollection at: i put: (collection at: ptr - i)].
^newCollection
]
emptyStream [
"Extension - Reset the stream"
<category: 'positioning'>
ptr := 1.
endPtr := 0
]
initCollection: aCollection [
<category: 'private methods'>
collection := aCollection.
ptr := 1.
endPtr := 0
]
moveToEnd [
<category: 'private methods'>
endPtr := collection size.
ptr := endPtr + 1
]
growCollection [
"Private - Double the size of the collection or, if its size < 8,
grow it to 8 places"
<category: 'private methods'>
| size |
size := collection size.
"Guess which collection is which :-)"
collection := (collection copyEmpty: (size * 2 max: 8))
replaceFrom: 1
to: collection size
with: collection
startingAt: 1;
yourself
]
growCollectionTo: n [
"Private - Double the size of the collection or, if its size < 8,
grow it to 8 places"
<category: 'private methods'>
| size |
n <= (size := collection size) ifTrue: [ ^self ].
size * 2 > n ifTrue: [ ^self growCollection ].
"Guess which collection is which :-)"
collection := (collection copyEmpty: n)
replaceFrom: 1
to: collection size
with: collection
startingAt: 1;
yourself
]
]
|