author | Martin Kobetic |
Sun, 17 Nov 2013 00:21:39 -0500 | |
changeset 141 | 263190106319 |
parent 109 | 9587e2df7029 |
permissions | -rw-r--r-- |
9 | 1 |
"{ Package: 'stx:goodies/xtreams/terminals' }" |
2 |
||
3 |
"{ NameSpace: Xtreams }" |
|
4 |
||
5 |
WriteStream subclass:#FileWriteStream |
|
109 | 6 |
instanceVariableNames:'position isPositionable contentsSpecies filename' |
9 | 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 |
FileWriteStream comment:'Writes to a file. File write streams can be created via the usual #writing message or via #appending which opens the file in appending mode. In appending mode, you cannot position the stream before the end of the file contents, so you can never overwrite existing contents. In writing mode, the file will be truncated at stream''s current position when #close is called. To keep the entire contents of the file, use -= 0 to skip to the end before closing. This behavior is different from the classic streams which would erase the contents of the file on opening. The stream is binary and naturally positionable. |
|
13 |
{{{ |
|
14 |
| file | |
|
15 |
file := ''/dev/shm/xtreams-test'' asFilename. |
|
109 | 16 |
[ file writing write: ''Hello''; close. |
9 | 17 |
file appending write: '' World!!''; close. |
18 |
file contentsOfEntireFile. |
|
19 |
] ensure: [ file delete ] |
|
20 |
}}} |
|
21 |
It is also possible to send #reading or #writing to a pre-opened IOAcccessor if some other opening mode configuration is desirable. For example to emulate the classic write stream opening behavior, you can use the following: |
|
22 |
{{{ |
|
109 | 23 |
(IOAccessor openFileNamed: ''/dev/shm/xtreams-test'' |
9 | 24 |
direction: IOAccessor writeOnly |
25 |
creation: IOAccessor truncateOrCreate |
|
26 |
) writing close |
|
27 |
}}} |
|
28 |
||
29 |
Instance Variables |
|
109 | 30 |
position <Integer> current position of the stream |
31 |
isPositionable <Boolean> indicates that the file is open in append mode |
|
32 |
contentsSpecies <Class> species for collections of elements of this stream |
|
9 | 33 |
|
34 |
' |
|
35 |
! |
|
36 |
||
37 |
||
38 |
!FileWriteStream methodsFor:'accessing'! |
|
39 |
||
40 |
insert: anInteger from: aSequenceableCollection at: startIndex |
|
41 |
^self shouldNotImplement. |
|
42 |
||
43 |
"While it might seem desireable to be able to insert in to a file, the reality is you can only do so if you can read and write to the file. if you can read and write to the file, the fileSize becomes indeterminate. The only reasonable way to insert in to a file is to memory map it and access it with a PointerWriteStream instead." |
|
44 |
! |
|
45 |
||
46 |
write: anInteger from: aSequenceableCollection at: startIndex |
|
109 | 47 |
| count wrote | |
48 |
anInteger isZero ifTrue: [^0]. |
|
49 |
count := 0. |
|
50 |
[count < anInteger] whileTrue: |
|
51 |
[wrote := destination writeBytes: anInteger from: aSequenceableCollection startingAt: startIndex + count. |
|
52 |
wrote isZero ifTrue: [(Incomplete on: aSequenceableCollection count: count at: startIndex) raise]. |
|
53 |
count := count + wrote. |
|
54 |
position := position + wrote]. |
|
55 |
^anInteger |
|
9 | 56 |
! ! |
57 |
||
58 |
!FileWriteStream methodsFor:'initialize-release'! |
|
59 |
||
60 |
close |
|
109 | 61 |
destination isValid ifFalse: [^self]. |
62 |
destination close |
|
9 | 63 |
! |
64 |
||
65 |
contentsSpecies |
|
66 |
^contentsSpecies |
|
67 |
! |
|
68 |
||
69 |
contentsSpecies: aClass |
|
70 |
contentsSpecies := aClass |
|
71 |
! |
|
72 |
||
73 |
flush |
|
74 |
! |
|
75 |
||
76 |
isPositionable: aBoolean |
|
77 |
"Only set to false when the file is open in append mode." |
|
78 |
||
79 |
isPositionable := aBoolean |
|
80 |
! |
|
81 |
||
82 |
on: anAccessor |
|
83 |
super on: anAccessor. |
|
84 |
contentsSpecies := ByteArray. |
|
85 |
isPositionable := true. |
|
86 |
position := 0 |
|
87 |
! ! |
|
88 |
||
109 | 89 |
!FileWriteStream methodsFor:'private'! |
90 |
||
91 |
setFilename: fn |
|
92 |
||
93 |
filename := fn |
|
94 |
! ! |
|
95 |
||
9 | 96 |
!FileWriteStream methodsFor:'seeking'! |
97 |
||
98 |
++ anInteger |
|
109 | 99 |
| count | |
100 |
self isPositionable ifFalse: [self error: 'This stream is not positionable.']. |
|
101 |
anInteger < 0 ifTrue: [ ^self -- anInteger negated ]. |
|
102 |
count := self available min: anInteger. |
|
103 |
position := position + count. |
|
104 |
destination seekTo: position from: #begin. |
|
105 |
count < anInteger ifTrue: [(Incomplete count: count) raise]. |
|
106 |
^anInteger |
|
9 | 107 |
! |
108 |
||
109 |
-- anInteger |
|
109 | 110 |
| count | |
111 |
self isPositionable ifFalse: [self error: 'This stream is not positionable.']. |
|
112 |
anInteger < 0 ifTrue: [ ^self ++ anInteger negated ]. |
|
113 |
count := position min: anInteger. |
|
114 |
position := position - count. |
|
115 |
destination seekTo: position from: #begin. |
|
116 |
anInteger = count ifTrue: [^anInteger]. |
|
117 |
(Incomplete count: count) raise |
|
9 | 118 |
! |
119 |
||
120 |
length |
|
121 |
self isPositionable ifFalse: [self error: 'This stream is not positionable.']. |
|
109 | 122 |
^filename fileSize |
9 | 123 |
! |
124 |
||
125 |
position |
|
126 |
self isPositionable ifFalse: [self error: 'This stream is not positionable.']. |
|
127 |
^position |
|
128 |
! |
|
129 |
||
130 |
position: aPosition |
|
109 | 131 |
| available | |
132 |
self isPositionable ifFalse: [self error: 'This stream is not positionable.']. |
|
133 |
aPosition < 0 ifTrue: [ Incomplete zero raise ]. |
|
134 |
available := aPosition min: self length. |
|
135 |
destination seekTo: available from: #begin. |
|
136 |
position := available. |
|
137 |
available = aPosition ifTrue: [ ^aPosition ]. |
|
138 |
(Incomplete count: available) raise |
|
9 | 139 |
! ! |
140 |
||
141 |
!FileWriteStream methodsFor:'testing'! |
|
142 |
||
143 |
isPositionable |
|
144 |
^isPositionable |
|
145 |
! ! |
|
146 |
||
147 |
!FileWriteStream class methodsFor:'documentation'! |
|
148 |
||
109 | 149 |
version_HG |
150 |
||
151 |
^ '$Changeset: <not expanded> $' |
|
9 | 152 |
! ! |
109 | 153 |