author | Jan Vrany <jan.vrany@fit.cvut.cz> |
Wed, 01 Feb 2012 00:37:59 +0000 | |
changeset 98 | bd334e72464f |
parent 96 | 85e395d8e3d7 |
child 100 | bd080ca99f68 |
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:'' |
|
96 | 8 |
poolDictionaries:'Xtreams::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 |
|
98 | 46 |
|
47 |
| count available | |
|
48 |
count := (available := self available) isNil ifTrue: [ anInteger ] ifFalse: [ available min: anInteger ]. |
|
49 |
self breakPoint: #jv. |
|
50 |
self breakPoint: #mk. |
|
51 |
"There is no UnsignedChar here..." |
|
52 |
source type referentType = UnsignedChar |
|
53 |
ifTrue: [ source copyAt: position to: aSequenceableCollection size: anInteger startingAt: startIndex. ] |
|
54 |
ifFalse: [ startIndex to: startIndex + count - 1 do: [ :i | aSequenceableCollection at: i put: self get ] ]. |
|
55 |
position := position + count. |
|
56 |
^count < anInteger |
|
57 |
ifTrue: [ (Incomplete on: aSequenceableCollection count: count at: startIndex) raise ] |
|
58 |
ifFalse: [ count ] |
|
59 |
||
60 |
"Modified: / 01-02-2012 / 00:37:43 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
9 | 61 |
! ! |
62 |
||
63 |
!PointerReadStream methodsFor:'initialize-release'! |
|
64 |
||
65 |
close |
|
66 |
! |
|
67 |
||
68 |
contentsSpecies |
|
69 |
^contentsSpecies |
|
70 |
! |
|
71 |
||
72 |
contentsSpecies: aClass |
|
73 |
contentsSpecies := aClass |
|
74 |
! |
|
75 |
||
76 |
length: anObject |
|
77 |
length := anObject |
|
78 |
! |
|
79 |
||
80 |
on: aPointer |
|
81 |
super on: aPointer. |
|
82 |
contentsSpecies := Array. |
|
83 |
position := 0 |
|
84 |
! ! |
|
85 |
||
86 |
!PointerReadStream methodsFor:'private'! |
|
87 |
||
88 |
streamingInsertInto: aWriteStream |
|
89 |
^length |
|
90 |
ifNil: [ self error: 'Cannot stream from an unbounded stream' ] |
|
91 |
ifNotNil: [ super streamingInsertInto: aWriteStream ] |
|
92 |
! |
|
93 |
||
94 |
streamingWriteInto: aWriteStream |
|
95 |
^length |
|
96 |
ifNil: [ self error: 'Cannot stream from an unbounded stream' ] |
|
97 |
ifNotNil: [ super streamingWriteInto: aWriteStream ] |
|
98 |
! ! |
|
99 |
||
100 |
!PointerReadStream methodsFor:'seeking'! |
|
101 |
||
102 |
++ anInteger |
|
103 |
||
104 |
| count | |
|
105 |
anInteger < 0 ifTrue: [ ^self -- anInteger negated ]. |
|
106 |
count := self available ifNil: [ anInteger ] ifNotNil: [ :available | available min: anInteger ]. |
|
107 |
position := position + anInteger. |
|
108 |
count < anInteger ifTrue: [(Incomplete count: count) raise]. |
|
109 |
^anInteger |
|
110 |
! |
|
111 |
||
112 |
-- anInteger |
|
113 |
| count | |
|
114 |
anInteger < 0 ifTrue: [ ^self ++ anInteger negated ]. |
|
115 |
count := position min: anInteger. |
|
116 |
position := position - count. |
|
117 |
count = anInteger ifTrue: [^anInteger]. |
|
118 |
(Incomplete count: count) raise |
|
119 |
! |
|
120 |
||
121 |
available |
|
122 |
||
123 |
^self length ifNotNil: [ super available ] |
|
124 |
! |
|
125 |
||
126 |
length |
|
127 |
^length |
|
128 |
! |
|
129 |
||
130 |
position |
|
131 |
^position |
|
132 |
! |
|
133 |
||
134 |
position: aPosition |
|
135 |
||
136 |
aPosition < 0 ifTrue: [Incomplete zero raise]. |
|
137 |
length ifNil: [ ^position := aPosition ]. |
|
138 |
aPosition > length ifFalse: [ ^position := aPosition ]. |
|
139 |
position := length. |
|
140 |
^(Incomplete count: length) raise |
|
141 |
! ! |
|
142 |
||
143 |
!PointerReadStream methodsFor:'testing'! |
|
144 |
||
145 |
isPositionable |
|
146 |
^true |
|
147 |
! ! |
|
148 |
||
149 |
!PointerReadStream class methodsFor:'documentation'! |
|
150 |
||
151 |
version_SVN |
|
152 |
^ '$Id$' |
|
153 |
! ! |