author | Martin Kobetic |
Sun, 17 Nov 2013 00:23:18 -0500 | |
changeset 147 | bd6be28aa924 |
parent 109 | 9587e2df7029 |
permissions | -rw-r--r-- |
9 | 1 |
"{ Package: 'stx:goodies/xtreams/terminals' }" |
2 |
||
3 |
"{ NameSpace: Xtreams }" |
|
4 |
||
5 |
WriteStream subclass:#BufferedWriteStream |
|
6 |
instanceVariableNames:'buffer' |
|
7 |
classVariableNames:'' |
|
8 |
poolDictionaries:'' |
|
25
02e7c3b6f63c
added XtreamsPool to fix DefaultBufferSize; set proper category names
mkobetic
parents:
9
diff
changeset
|
9 |
category:'Xtreams-Terminals' |
9 | 10 |
! |
11 |
||
12 |
BufferedWriteStream comment:'Wraps a stream and buffers output to it based on the size of the ring-buffer it has. This is useful for doing "burst" writing, where you know exactly when you can flush the stream. To force the buffer to the underlying stream, use #flush. |
|
13 |
||
14 |
Instance Variables |
|
15 |
buffer <RingBuffer> a fixed size RingBuffer to buffer up output, released when it is full or #flush/#close is sent. |
|
16 |
||
17 |
' |
|
18 |
! |
|
19 |
||
20 |
||
21 |
!BufferedWriteStream class methodsFor:'instance creation'! |
|
22 |
||
23 |
on: aDestination bufferSize: bufferSize |
|
24 |
^self new on: aDestination bufferSize: bufferSize |
|
25 |
! ! |
|
26 |
||
27 |
!BufferedWriteStream methodsFor:'accessing'! |
|
28 |
||
29 |
write: anInteger from: aSequenceableCollection at: startIndex |
|
30 |
||
31 |
| count amount | |
|
32 |
count := 0. |
|
33 |
[[amount := buffer write: (buffer writeSize min: (anInteger - count)) from: aSequenceableCollection at: startIndex + count. |
|
34 |
count := count + amount. |
|
35 |
count < anInteger] whileTrue: [self flush]] |
|
36 |
on: Incomplete do: [:exception | (Incomplete on: aSequenceableCollection count: count + exception count at: startIndex) raise]. |
|
37 |
^anInteger |
|
38 |
! ! |
|
39 |
||
40 |
!BufferedWriteStream methodsFor:'initialize-release'! |
|
41 |
||
42 |
close |
|
43 |
super close. |
|
44 |
buffer recycle. |
|
45 |
buffer := nil |
|
46 |
! |
|
47 |
||
48 |
contentsSpecies |
|
49 |
^destination contentsSpecies |
|
50 |
! |
|
51 |
||
52 |
flush |
|
53 |
destination write: buffer |
|
54 |
! |
|
55 |
||
56 |
on: aDestination bufferSize: bufferSize |
|
57 |
buffer := RingBuffer new: bufferSize class: aDestination contentsSpecies. |
|
58 |
super on: aDestination |
|
59 |
! ! |
|
60 |
||
61 |
!BufferedWriteStream methodsFor:'printing'! |
|
62 |
||
63 |
streamingPrintOn: aStream |
|
64 |
super streamingPrintOn: aStream. |
|
65 |
aStream |
|
66 |
write: ' buffered: '; |
|
67 |
print: buffer writeSize. |
|
68 |
buffer writeSize isZero ifTrue: [^self]. |
|
69 |
aStream |
|
70 |
cr; tab; |
|
71 |
print: buffer contentsPast |
|
72 |
! ! |
|
73 |
||
74 |
!BufferedWriteStream class methodsFor:'documentation'! |
|
75 |
||
109 | 76 |
version_HG |
77 |
||
78 |
^ '$Changeset: <not expanded> $' |
|
9 | 79 |
! ! |
109 | 80 |