author | Martin Kobetic |
Sun, 17 Nov 2013 00:22:31 -0500 | |
changeset 144 | e193a6772be4 |
parent 109 | 9587e2df7029 |
permissions | -rw-r--r-- |
9 | 1 |
"{ Package: 'stx:goodies/xtreams/terminals' }" |
2 |
||
3 |
"{ NameSpace: Xtreams }" |
|
4 |
||
5 |
WriteStream subclass:#SequenceableCollectionWriteStream |
|
6 |
instanceVariableNames:'position length' |
|
7 |
classVariableNames:'' |
|
109 | 8 |
poolDictionaries:'XtreamsPool' |
25
02e7c3b6f63c
added XtreamsPool to fix DefaultBufferSize; set proper category names
mkobetic
parents:
9
diff
changeset
|
9 |
category:'Xtreams-Terminals' |
9 | 10 |
! |
11 |
||
12 |
SequenceableCollectionWriteStream comment:'Write stream on a sequenceable collection. The collection is grown automatically to accommodate any elements written. Closing a collection write stream will truncate the collection to the current stream position. This behavior is useful as a replacement for the traditional #contents message. The contents can be accessed with the #terminal message after the stream is closed. |
|
13 |
{{{ |
|
14 |
String new writing write: ''Hello World''; -- 6; close; terminal |
|
15 |
}}} |
|
16 |
||
17 |
Instance Variables |
|
109 | 18 |
position <Integer> current position of the stream |
19 |
length <Integer> number of valid elements in the destination |
|
9 | 20 |
|
21 |
' |
|
22 |
! |
|
23 |
||
24 |
||
25 |
!SequenceableCollectionWriteStream methodsFor:'accessing'! |
|
26 |
||
27 |
contents |
|
28 |
^destination copyFrom: 1 to: position |
|
29 |
! |
|
30 |
||
31 |
insert: anInteger from: aSequenceableCollection at: startIndex |
|
109 | 32 |
destination := destination copyGrownToAtLeast: destination size + anInteger. |
33 |
destination replaceFrom: position + anInteger + 1 to: length + anInteger with: destination startingAt: position + 1. |
|
34 |
destination replaceFrom: position + 1 to: position + anInteger with: aSequenceableCollection startingAt: startIndex. |
|
35 |
position := position + anInteger. |
|
36 |
length := length + anInteger. |
|
37 |
^anInteger |
|
9 | 38 |
! |
39 |
||
40 |
put: anObject |
|
109 | 41 |
| free | |
42 |
free := destination size - position. |
|
43 |
free < 1 ifTrue: [destination := destination copyGrownToAtLeast: destination size + 1]. |
|
44 |
position := position + 1. |
|
45 |
destination at: position put: anObject. |
|
46 |
length := length + 1. |
|
47 |
^anObject |
|
9 | 48 |
! |
49 |
||
50 |
write: anInteger from: aSequenceableCollection at: startIndex |
|
109 | 51 |
| available | |
52 |
available := destination size - position. |
|
53 |
available < anInteger ifTrue: [destination := destination copyGrownToAtLeast: destination size + anInteger - available]. |
|
54 |
destination replaceFrom: position + 1 to: position + anInteger with: aSequenceableCollection startingAt: startIndex. |
|
55 |
position := position + anInteger. |
|
56 |
position > length ifTrue: [length := position]. |
|
57 |
^anInteger |
|
9 | 58 |
! ! |
59 |
||
60 |
!SequenceableCollectionWriteStream methodsFor:'initialize-release'! |
|
61 |
||
62 |
close |
|
109 | 63 |
destination := destination copyFrom: 1 to: position |
9 | 64 |
! |
65 |
||
66 |
contentsSpecies |
|
67 |
^destination species |
|
68 |
! |
|
69 |
||
70 |
flush |
|
71 |
! |
|
72 |
||
73 |
on: aSequenceableCollection |
|
74 |
super on: aSequenceableCollection. |
|
75 |
position := 0. |
|
76 |
length := 0 |
|
77 |
! ! |
|
78 |
||
79 |
!SequenceableCollectionWriteStream methodsFor:'printing'! |
|
80 |
||
81 |
streamingPrintOn: aStream |
|
82 |
| tail head | |
|
83 |
super streamingPrintOn: aStream. |
|
84 |
tail := position min: 5. |
|
85 |
head := (destination size - position) min: (10 - tail). |
|
86 |
aStream |
|
87 |
write: ' position: '; |
|
88 |
print: position; |
|
89 |
write: ' size: '; |
|
90 |
print: destination size; |
|
91 |
cr; tab; |
|
92 |
write: (position > 5 ifTrue: ['...'] ifFalse: ['']); |
|
93 |
print: (destination copyFrom: position + 1 - tail to: position); |
|
94 |
write: ' | '; |
|
95 |
print: (destination copyFrom: position + 1 to: position + head); |
|
96 |
write: (destination size - position > (10 - tail) ifTrue: ['...'] ifFalse: ['']) |
|
97 |
! ! |
|
98 |
||
99 |
!SequenceableCollectionWriteStream methodsFor:'private'! |
|
100 |
||
101 |
streamingInsert: anInteger from: aStreamable |
|
109 | 102 |
| reading | |
103 |
reading := aStreamable reading. |
|
104 |
destination := destination copyGrownToAtLeast: destination size + anInteger. |
|
105 |
destination replaceFrom: position + anInteger to: destination size with: destination startingAt: position. |
|
106 |
[reading read: anInteger into: destination at: position + 1] |
|
107 |
on: Incomplete do: [:error | position := position + error count. error pass]. |
|
108 |
position := position + anInteger |
|
9 | 109 |
! |
110 |
||
111 |
streamingInsertFrom: aStreamable |
|
112 |
self error: 'Not Yet Implemented' |
|
113 |
! |
|
114 |
||
115 |
streamingWrite: anInteger from: aReadStream |
|
109 | 116 |
| available | |
117 |
available := destination size - position. |
|
118 |
available < anInteger ifTrue: [destination := destination copyGrownToAtLeast: destination size + anInteger - available]. |
|
119 |
[aReadStream read: anInteger into: destination at: position + 1] |
|
120 |
on: Incomplete do: [:error | |
|
121 |
position := position + error count. |
|
122 |
position > length ifTrue: [length := position]. |
|
123 |
error pass]. |
|
124 |
position := position + anInteger. |
|
125 |
position > length ifTrue: [length := position]. |
|
126 |
^anInteger |
|
9 | 127 |
! |
128 |
||
129 |
streamingWriteFrom: aReadStream |
|
109 | 130 |
| available count chunkSize | |
131 |
count := 0. |
|
132 |
chunkSize := DefaultBufferSize. |
|
133 |
^[[available := destination size - position. |
|
134 |
available < chunkSize ifTrue: [destination := destination copyGrownToAtLeast: destination size + chunkSize]. |
|
135 |
aReadStream read: chunkSize into: destination at: position + 1. |
|
136 |
position := position + chunkSize. |
|
137 |
count := count + chunkSize] repeat] |
|
138 |
on: Incomplete do: [:exception | |
|
139 |
position := position + exception count. |
|
140 |
position > length ifTrue: [length := position]. |
|
141 |
count + exception count] |
|
9 | 142 |
! ! |
143 |
||
144 |
!SequenceableCollectionWriteStream methodsFor:'seeking'! |
|
145 |
||
146 |
++ anInteger |
|
147 |
| count | |
|
148 |
anInteger < 0 ifTrue: [ ^self -- anInteger negated ]. |
|
149 |
count := self available min: anInteger. |
|
150 |
position := position + count. |
|
151 |
count < anInteger ifTrue: [(Incomplete count: count) raise]. |
|
152 |
^anInteger |
|
153 |
! |
|
154 |
||
155 |
-- anInteger |
|
156 |
| count | |
|
157 |
anInteger < 0 ifTrue: [ ^self ++ anInteger negated ]. |
|
158 |
count := position min: anInteger. |
|
159 |
position := position - count. |
|
160 |
count = anInteger ifTrue: [^anInteger]. |
|
161 |
(Incomplete count: count) raise |
|
162 |
! |
|
163 |
||
164 |
length |
|
165 |
^length |
|
166 |
! |
|
167 |
||
168 |
position |
|
169 |
^position |
|
170 |
! |
|
171 |
||
172 |
position: aPosition |
|
173 |
aPosition < 0 ifTrue: [ Incomplete zero raise ]. |
|
174 |
position := aPosition min: self length. |
|
175 |
position = aPosition ifTrue: [^aPosition]. |
|
176 |
(Incomplete count: position) raise |
|
177 |
! ! |
|
178 |
||
179 |
!SequenceableCollectionWriteStream methodsFor:'testing'! |
|
180 |
||
181 |
isPositionable |
|
182 |
^true |
|
183 |
! ! |
|
184 |
||
185 |
!SequenceableCollectionWriteStream class methodsFor:'documentation'! |
|
186 |
||
109 | 187 |
version_HG |
188 |
||
189 |
^ '$Changeset: <not expanded> $' |
|
9 | 190 |
! ! |
109 | 191 |