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 |
ExternalReadStream subclass:#FileReadStream |
|
109 | 6 |
instanceVariableNames:'filename position' |
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 |
FileReadStream comment:'Reads from a file. The stream is usually created by sending #reading to a Filename. However the actual terminal is an IOAccessor. The original filename is accessible through the IOAccessor. The stream is binary and is naturally positionable. |
|
13 |
{{{ |
|
14 |
| file | |
|
15 |
file := ObjectMemory imageName asFilename reading. |
|
16 |
[ file read: 13 ] ensure: [ file close ] |
|
17 |
}}} |
|
18 |
As a convenience, sending #reading to a Filename of a directory will create a stream of all filenames in that directory. |
|
19 |
{{{ |
|
20 |
''/tmp'' asFilename reading rest |
|
21 |
}}} |
|
22 |
||
23 |
Instance Variables |
|
109 | 24 |
position <Integer> current position of the stream |
9 | 25 |
|
26 |
' |
|
27 |
! |
|
28 |
||
29 |
||
30 |
!FileReadStream methodsFor:'accessing'! |
|
31 |
||
32 |
get |
|
33 |
||
34 |
| object | |
|
35 |
object := super get. |
|
36 |
position := position + 1. |
|
37 |
^object |
|
38 |
! |
|
39 |
||
40 |
read: anInteger into: aSequenceableCollection at: startIndex |
|
41 |
||
42 |
super read: anInteger into: aSequenceableCollection at: startIndex. |
|
43 |
position := position + anInteger. |
|
44 |
^anInteger |
|
45 |
! ! |
|
46 |
||
47 |
!FileReadStream methodsFor:'initialize-release'! |
|
48 |
||
49 |
on: anAccessor |
|
50 |
super on: anAccessor. |
|
51 |
position := 0 |
|
52 |
! ! |
|
53 |
||
54 |
!FileReadStream methodsFor:'private'! |
|
55 |
||
56 |
bytesForRead |
|
57 |
||
58 |
"Ideally we'd ask for the fileSize and subtract that from our position, but calling #fileSize is a slow primitive" |
|
59 |
^cache size |
|
60 |
! |
|
61 |
||
62 |
incompleteRead: incomplete |
|
63 |
||
64 |
position := position + incomplete count. |
|
65 |
super incompleteRead: incomplete. |
|
39 | 66 |
! |
67 |
||
68 |
readBytes:count into:aByteBuffer startingAt:firstIndex |
|
69 |
||
70 |
^source readBytes:count into:aByteBuffer startingAt:firstIndex |
|
109 | 71 |
! |
72 |
||
73 |
setFilename: fn |
|
74 |
||
75 |
filename := fn |
|
9 | 76 |
! ! |
77 |
||
78 |
!FileReadStream methodsFor:'seeking'! |
|
79 |
||
80 |
++ anInteger |
|
109 | 81 |
| count | |
82 |
anInteger < 0 ifTrue: [ ^self -- anInteger negated ]. |
|
83 |
count := self available min: anInteger. |
|
84 |
position := position + count. |
|
85 |
source seekTo: position from: #begin. |
|
86 |
cacheDataSize := cachePosition := 0. |
|
87 |
count < anInteger ifTrue: [(Incomplete count: count) raise]. |
|
88 |
^anInteger |
|
9 | 89 |
! |
90 |
||
91 |
-- anInteger |
|
109 | 92 |
| count | |
93 |
anInteger < 0 ifTrue: [ ^self ++ anInteger negated ]. |
|
94 |
count := position min: anInteger. |
|
95 |
position := position - count. |
|
96 |
source seekTo: position from: #begin. |
|
97 |
cacheDataSize := cachePosition := 0. |
|
98 |
anInteger = count ifTrue: [^anInteger]. |
|
99 |
(Incomplete count: count) raise |
|
9 | 100 |
! |
101 |
||
102 |
length |
|
109 | 103 |
^filename fileSize |
9 | 104 |
! |
105 |
||
106 |
position |
|
107 |
^position |
|
108 |
! |
|
109 |
||
110 |
position: aPosition |
|
111 |
||
109 | 112 |
| available | |
113 |
aPosition < 0 ifTrue: [ Incomplete zero raise ]. |
|
114 |
available := aPosition min: self length. |
|
115 |
source seekTo: available from: #begin. |
|
116 |
position := available. |
|
117 |
cacheDataSize := cachePosition := 0. |
|
118 |
available = aPosition ifTrue: [^aPosition]. |
|
119 |
(Incomplete count: available) raise |
|
9 | 120 |
! ! |
121 |
||
122 |
!FileReadStream methodsFor:'testing'! |
|
123 |
||
124 |
isPositionable |
|
125 |
^true |
|
126 |
! ! |
|
127 |
||
128 |
!FileReadStream class methodsFor:'documentation'! |
|
129 |
||
109 | 130 |
version_HG |
131 |
||
132 |
^ '$Changeset: <not expanded> $' |
|
9 | 133 |
! ! |
109 | 134 |