author | mkobetic |
Sun, 15 Jan 2012 22:50:34 +0000 | |
changeset 52 | a1363827b596 |
parent 43 | b9a077d6ce14 |
child 78 | a9dd8b69b39f |
permissions | -rw-r--r-- |
52 | 1 |
'From Smalltalk/X, Version:6.2.1 on 15-01-2012 at 05:49:32 PM' ! |
39 | 2 |
|
9 | 3 |
"{ Package: 'stx:goodies/xtreams/terminals' }" |
4 |
||
5 |
"{ NameSpace: Xtreams }" |
|
6 |
||
7 |
ExternalReadStream subclass:#FileReadStream |
|
8 |
instanceVariableNames:'position' |
|
9 |
classVariableNames:'' |
|
10 |
poolDictionaries:'' |
|
25
02e7c3b6f63c
added XtreamsPool to fix DefaultBufferSize; set proper category names
mkobetic
parents:
9
diff
changeset
|
11 |
category:'Xtreams-Terminals' |
9 | 12 |
! |
13 |
||
14 |
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. |
|
15 |
{{{ |
|
16 |
| file | |
|
17 |
file := ObjectMemory imageName asFilename reading. |
|
18 |
[ file read: 13 ] ensure: [ file close ] |
|
19 |
}}} |
|
20 |
As a convenience, sending #reading to a Filename of a directory will create a stream of all filenames in that directory. |
|
21 |
{{{ |
|
22 |
''/tmp'' asFilename reading rest |
|
23 |
}}} |
|
24 |
||
25 |
Instance Variables |
|
26 |
position <Integer> current position of the stream |
|
27 |
||
28 |
' |
|
29 |
! |
|
30 |
||
31 |
||
32 |
!FileReadStream methodsFor:'accessing'! |
|
33 |
||
34 |
get |
|
35 |
||
36 |
| object | |
|
37 |
object := super get. |
|
38 |
position := position + 1. |
|
39 |
^object |
|
40 |
! |
|
41 |
||
42 |
read: anInteger into: aSequenceableCollection at: startIndex |
|
43 |
||
44 |
super read: anInteger into: aSequenceableCollection at: startIndex. |
|
45 |
position := position + anInteger. |
|
46 |
^anInteger |
|
47 |
! ! |
|
48 |
||
49 |
!FileReadStream methodsFor:'initialize-release'! |
|
50 |
||
51 |
on: anAccessor |
|
52 |
super on: anAccessor. |
|
53 |
position := 0 |
|
54 |
! ! |
|
55 |
||
56 |
!FileReadStream methodsFor:'private'! |
|
57 |
||
58 |
bytesForRead |
|
59 |
||
60 |
"Ideally we'd ask for the fileSize and subtract that from our position, but calling #fileSize is a slow primitive" |
|
61 |
^cache size |
|
62 |
! |
|
63 |
||
64 |
incompleteRead: incomplete |
|
65 |
||
66 |
position := position + incomplete count. |
|
67 |
super incompleteRead: incomplete. |
|
39 | 68 |
! |
69 |
||
70 |
readBytes:count into:aByteBuffer startingAt:firstIndex |
|
71 |
||
72 |
^source readBytes:count into:aByteBuffer startingAt:firstIndex |
|
9 | 73 |
! ! |
74 |
||
75 |
!FileReadStream methodsFor:'seeking'! |
|
76 |
||
77 |
++ anInteger |
|
39 | 78 |
| count | |
79 |
anInteger < 0 ifTrue: [ ^self -- anInteger negated ]. |
|
80 |
count := self available min: anInteger. |
|
81 |
position := position + count. |
|
82 |
source seekTo: position from: #begin. |
|
83 |
cacheDataSize := cachePosition := 0. |
|
84 |
count < anInteger ifTrue: [(Incomplete count: count) raise]. |
|
85 |
^anInteger |
|
9 | 86 |
! |
87 |
||
88 |
-- anInteger |
|
39 | 89 |
| count | |
90 |
anInteger < 0 ifTrue: [ ^self ++ anInteger negated ]. |
|
91 |
count := position min: anInteger. |
|
92 |
position := position - count. |
|
93 |
source seekTo: position from: #begin. |
|
94 |
cacheDataSize := cachePosition := 0. |
|
95 |
anInteger = count ifTrue: [^anInteger]. |
|
96 |
(Incomplete count: count) raise |
|
9 | 97 |
! |
98 |
||
99 |
length |
|
100 |
^source fileSize |
|
101 |
! |
|
102 |
||
103 |
position |
|
104 |
^position |
|
105 |
! |
|
106 |
||
107 |
position: aPosition |
|
108 |
||
39 | 109 |
| available | |
110 |
aPosition < 0 ifTrue: [ Incomplete zero raise ]. |
|
111 |
available := aPosition min: self length. |
|
112 |
source seekTo: available from: #begin. |
|
113 |
position := available. |
|
114 |
cacheDataSize := cachePosition := 0. |
|
115 |
available = aPosition ifTrue: [^aPosition]. |
|
116 |
(Incomplete count: available) raise |
|
9 | 117 |
! ! |
118 |
||
119 |
!FileReadStream methodsFor:'testing'! |
|
120 |
||
121 |
isPositionable |
|
122 |
^true |
|
123 |
! ! |
|
124 |
||
125 |
!FileReadStream class methodsFor:'documentation'! |
|
126 |
||
127 |
version_SVN |
|
128 |
^ '$Id$' |
|
129 |
! ! |