author | mkobetic |
Tue, 31 Jan 2012 03:47:19 +0000 | |
changeset 90 | 59f68d289949 |
parent 78 | a9dd8b69b39f |
child 96 | 85e395d8e3d7 |
permissions | -rw-r--r-- |
9 | 1 |
"{ Package: 'stx:goodies/xtreams/terminals' }" |
2 |
||
3 |
"{ NameSpace: Xtreams }" |
|
4 |
||
5 |
ReadStream subclass:#PointerReadStream |
|
6 |
instanceVariableNames:'length contentsSpecies position' |
|
7 |
classVariableNames:'' |
|
39 | 8 |
poolDictionaries:'XtreamsPool' |
25
02e7c3b6f63c
added XtreamsPool to fix DefaultBufferSize; set proper category names
mkobetic
parents:
9
diff
changeset
|
9 |
category:'Xtreams-Terminals' |
9 | 10 |
! |
11 |
||
12 |
PointerReadStream comment:'Read stream on external heap, commonly used to unmarshal output or an external library call. The terminal is a CPointer and its associated CType is used to interpret the bytes on the heap. The length of the stream should be set, to avoid reading past the allocated memory space. |
|
13 |
{{{ |
|
14 |
| buffer | |
|
15 |
buffer := CIntegerType unsignedChar malloc: 50. |
|
16 |
[ buffer writing |
|
17 |
length: 50; |
|
18 |
write: ''Hello World!!''. |
|
19 |
buffer reading |
|
20 |
length: 12; |
|
21 |
contentsSpecies: ByteString; |
|
22 |
rest |
|
23 |
] ensure: [ buffer free ] |
|
24 |
}}} |
|
25 |
||
26 |
Instance Variables |
|
27 |
length <Integer> allocated size of the stream as a number of elements of associated CType |
|
28 |
contentsSpecies <Class> species for collections of elements of this stream |
|
29 |
position <Integer> current position of the stream |
|
30 |
||
31 |
' |
|
32 |
! |
|
33 |
||
34 |
||
35 |
!PointerReadStream methodsFor:'accessing'! |
|
36 |
||
37 |
get |
|
38 |
| object | |
|
39 |
(length isNil or: [ position < length ]) ifFalse: [ Incomplete zero raise ]. |
|
40 |
object := source at: position. |
|
41 |
position := position + 1. |
|
42 |
^object |
|
43 |
! |
|
44 |
||
45 |
read: anInteger into: aSequenceableCollection at: startIndex |
|
46 |
||
47 |
| count | |
|
48 |
count := self available ifNil: [ anInteger ] ifNotNil: [ :available | available min: anInteger ]. |
|
49 |
source type referentType = UnsignedChar |
|
50 |
ifTrue: [ source copyAt: position to: aSequenceableCollection size: anInteger startingAt: startIndex. ] |
|
51 |
ifFalse: [ startIndex to: startIndex + count - 1 do: [ :i | aSequenceableCollection at: i put: self get ] ]. |
|
52 |
position := position + count. |
|
53 |
^count < anInteger |
|
54 |
ifTrue: [ (Incomplete on: aSequenceableCollection count: count at: startIndex) raise ] |
|
55 |
ifFalse: [ count ] |
|
56 |
! ! |
|
57 |
||
58 |
!PointerReadStream methodsFor:'initialize-release'! |
|
59 |
||
60 |
close |
|
61 |
! |
|
62 |
||
63 |
contentsSpecies |
|
64 |
^contentsSpecies |
|
65 |
! |
|
66 |
||
67 |
contentsSpecies: aClass |
|
68 |
contentsSpecies := aClass |
|
69 |
! |
|
70 |
||
71 |
length: anObject |
|
72 |
length := anObject |
|
73 |
! |
|
74 |
||
75 |
on: aPointer |
|
76 |
super on: aPointer. |
|
77 |
contentsSpecies := Array. |
|
78 |
position := 0 |
|
79 |
! ! |
|
80 |
||
81 |
!PointerReadStream methodsFor:'private'! |
|
82 |
||
83 |
streamingInsertInto: aWriteStream |
|
84 |
^length |
|
85 |
ifNil: [ self error: 'Cannot stream from an unbounded stream' ] |
|
86 |
ifNotNil: [ super streamingInsertInto: aWriteStream ] |
|
87 |
! |
|
88 |
||
89 |
streamingWriteInto: aWriteStream |
|
90 |
^length |
|
91 |
ifNil: [ self error: 'Cannot stream from an unbounded stream' ] |
|
92 |
ifNotNil: [ super streamingWriteInto: aWriteStream ] |
|
93 |
! ! |
|
94 |
||
95 |
!PointerReadStream methodsFor:'seeking'! |
|
96 |
||
97 |
++ anInteger |
|
98 |
||
99 |
| count | |
|
100 |
anInteger < 0 ifTrue: [ ^self -- anInteger negated ]. |
|
101 |
count := self available ifNil: [ anInteger ] ifNotNil: [ :available | available min: anInteger ]. |
|
102 |
position := position + anInteger. |
|
103 |
count < anInteger ifTrue: [(Incomplete count: count) raise]. |
|
104 |
^anInteger |
|
105 |
! |
|
106 |
||
107 |
-- anInteger |
|
108 |
| count | |
|
109 |
anInteger < 0 ifTrue: [ ^self ++ anInteger negated ]. |
|
110 |
count := position min: anInteger. |
|
111 |
position := position - count. |
|
112 |
count = anInteger ifTrue: [^anInteger]. |
|
113 |
(Incomplete count: count) raise |
|
114 |
! |
|
115 |
||
116 |
available |
|
117 |
||
118 |
^self length ifNotNil: [ super available ] |
|
119 |
! |
|
120 |
||
121 |
length |
|
122 |
^length |
|
123 |
! |
|
124 |
||
125 |
position |
|
126 |
^position |
|
127 |
! |
|
128 |
||
129 |
position: aPosition |
|
130 |
||
131 |
aPosition < 0 ifTrue: [Incomplete zero raise]. |
|
132 |
length ifNil: [ ^position := aPosition ]. |
|
133 |
aPosition > length ifFalse: [ ^position := aPosition ]. |
|
134 |
position := length. |
|
135 |
^(Incomplete count: length) raise |
|
136 |
! ! |
|
137 |
||
138 |
!PointerReadStream methodsFor:'testing'! |
|
139 |
||
140 |
isPositionable |
|
141 |
^true |
|
142 |
! ! |
|
143 |
||
144 |
!PointerReadStream class methodsFor:'documentation'! |
|
145 |
||
146 |
version_SVN |
|
147 |
^ '$Id$' |
|
148 |
! ! |