author | mkobetic |
Tue, 31 Jan 2012 03:47:19 +0000 | |
changeset 90 | 59f68d289949 |
parent 74 | 752e2d88fe73 |
child 94 | 7e6a328c5f8b |
permissions | -rw-r--r-- |
2 | 1 |
"{ Package: 'stx:goodies/xtreams/core' }" |
2 |
||
3 |
"{ NameSpace: Xtreams }" |
|
4 |
||
5 |
WriteStream subclass:#PositionWriteStream |
|
6 |
instanceVariableNames:'buffer' |
|
7 |
classVariableNames:'' |
|
36 | 8 |
poolDictionaries:'XtreamsPool' |
20
51de794993c3
added XtreamsPool to fix DefaultBufferSize; set proper category names
mkobetic
parents:
2
diff
changeset
|
9 |
category:'Xtreams-Core' |
2 | 10 |
! |
11 |
||
12 |
PositionWriteStream comment:'Wraps a non-positionable stream and provides positioning capability by buffering written elements. Buffering strategy can be configured via different Buffer classes. |
|
13 |
||
14 |
Instance Variables |
|
15 |
buffer <Buffer> holds written elements |
|
16 |
||
17 |
' |
|
18 |
! |
|
19 |
||
20 |
||
21 |
!PositionWriteStream methodsFor:'accessing'! |
|
22 |
||
23 |
insert: anInteger from: aSequenceableCollection at: startIndex |
|
24 |
^buffer insert: anInteger from: aSequenceableCollection at: startIndex |
|
25 |
! |
|
26 |
||
27 |
write: anInteger from: aSequenceableCollection at: startIndex |
|
28 |
||
29 |
| toFlush bufferFlush | |
|
30 |
(buffer hasFixedWriteSpace not or: [buffer writeSize >= anInteger ]) ifTrue: [ |
|
31 |
buffer write: anInteger from: aSequenceableCollection at: startIndex. |
|
32 |
^anInteger ]. |
|
33 |
toFlush := buffer readSize + anInteger - buffer cacheSize. |
|
34 |
bufferFlush := toFlush min: buffer readSize. |
|
35 |
destination write: bufferFlush from: buffer. |
|
36 |
(toFlush > bufferFlush) ifTrue: [ |
|
37 |
toFlush := toFlush - bufferFlush. |
|
38 |
destination write: toFlush from: aSequenceableCollection at: startIndex ]. |
|
39 |
buffer write: anInteger - toFlush from: aSequenceableCollection at: startIndex + toFlush. |
|
40 |
^anInteger |
|
41 |
! ! |
|
42 |
||
43 |
!PositionWriteStream methodsFor:'initialize-release'! |
|
44 |
||
45 |
buffer |
|
46 |
||
47 |
^buffer |
|
48 |
! |
|
49 |
||
50 |
buffer: aBuffer |
|
51 |
||
52 |
buffer recycle. |
|
53 |
buffer := aBuffer |
|
54 |
! |
|
55 |
||
56 |
close |
|
57 |
super close. |
|
58 |
buffer recycle. |
|
59 |
buffer := nil |
|
60 |
! |
|
61 |
||
62 |
contentsSpecies |
|
63 |
^destination contentsSpecies |
|
64 |
! |
|
65 |
||
66 |
flush |
|
67 |
buffer ifNil: [^self]. |
|
68 |
destination write: buffer. |
|
69 |
buffer clear |
|
70 |
! |
|
71 |
||
72 |
on: aDestination |
|
73 |
buffer := Buffer new: DefaultBufferSize class: aDestination contentsSpecies. |
|
74 |
super on: aDestination |
|
75 |
! ! |
|
76 |
||
77 |
!PositionWriteStream methodsFor:'printing'! |
|
78 |
||
79 |
streamingPrintOn: aStream |
|
80 |
super streamingPrintOn: aStream. |
|
81 |
aStream |
|
82 |
write: ' buffered: '; |
|
83 |
print: buffer writeSize. |
|
84 |
buffer writeSize isZero ifTrue: [^self]. |
|
85 |
aStream |
|
86 |
cr; tab; |
|
87 |
print: buffer contentsPast |
|
88 |
! ! |
|
89 |
||
90 |
!PositionWriteStream methodsFor:'seeking'! |
|
91 |
||
92 |
++ anInteger |
|
93 |
| skipped | |
|
94 |
anInteger < 0 ifTrue: [ ^self -- anInteger negated ]. |
|
95 |
skipped := buffer writeSkip: anInteger. |
|
96 |
skipped = anInteger ifTrue: [^anInteger]. |
|
97 |
[destination ++ (anInteger - skipped)] on: Incomplete do: [:incomplete | |
|
98 |
(Incomplete count: incomplete count + skipped) raise]. |
|
99 |
^anInteger |
|
100 |
! |
|
101 |
||
102 |
-- anInteger |
|
103 |
| skipped | |
|
104 |
anInteger < 0 ifTrue: [ ^self ++ anInteger negated ]. |
|
105 |
skipped := (buffer writeSkip: anInteger negated) negated. |
|
106 |
skipped = anInteger ifTrue: [^anInteger]. |
|
107 |
(Incomplete count: skipped) raise |
|
108 |
! |
|
109 |
||
110 |
available |
|
111 |
^buffer writeSize |
|
112 |
! |
|
113 |
||
114 |
length |
|
115 |
^buffer activeSize |
|
116 |
! |
|
117 |
||
118 |
position |
|
119 |
^buffer writePosition |
|
120 |
! |
|
121 |
||
122 |
position: aPosition |
|
123 |
| startPosition delta | |
|
124 |
aPosition < 0 ifTrue: [Incomplete zero raise]. |
|
125 |
startPosition := buffer writePosition. |
|
126 |
delta := aPosition - startPosition. |
|
127 |
^[ self ++ delta. aPosition |
|
128 |
] on: Incomplete do: [ :ex | |
|
129 |
(Incomplete count: startPosition + ex count) raise ] |
|
130 |
! ! |
|
131 |
||
132 |
!PositionWriteStream methodsFor:'testing'! |
|
133 |
||
134 |
isPositionable |
|
135 |
^true |
|
136 |
! ! |
|
137 |
||
138 |
!PositionWriteStream methodsFor:'transforming'! |
|
139 |
||
140 |
positioning |
|
141 |
^self |
|
142 |
! ! |
|
143 |
||
144 |
!PositionWriteStream class methodsFor:'documentation'! |
|
145 |
||
146 |
version_SVN |
|
147 |
^ '$Id$' |
|
148 |
! ! |