author | Jan Vrany <jan.vrany@fit.cvut.cz> |
Wed, 01 Feb 2012 00:34:28 +0000 | |
changeset 97 | 2a7827f4dce2 |
parent 72 | d16c7d84d4a8 |
child 111 | 44ac233b2f83 |
permissions | -rw-r--r-- |
10 | 1 |
"{ Package: 'stx:goodies/xtreams/transforms' }" |
2 |
||
3 |
"{ NameSpace: Xtreams }" |
|
4 |
||
5 |
ReadStream subclass:#InterpretedReadStream |
|
6 |
instanceVariableNames:'elementSize cache cacheSize operation contentsSpecies' |
|
7 |
classVariableNames:'' |
|
97 | 8 |
poolDictionaries:'Xtreams::XtreamsPool' |
27
2cc5a8a3ca14
added XtreamsPool to fix DefaultBufferSize; set proper category names
mkobetic
parents:
10
diff
changeset
|
9 |
category:'Xtreams-Transforms' |
10 | 10 |
! |
11 |
||
12 |
InterpretedReadStream comment:'Interprets bytes from a binary source as values of preconfigured (primitive) CType, e.g. float, long etc. |
|
13 |
||
14 |
Instance Variables |
|
15 |
elementSize <Integer> byte size of elements of the pre-configured CType |
|
16 |
cache <InterpretedBytes> caches bulk read bytes before interpreting for speed; size = elementSize * cacheSize |
|
17 |
cacheSize <SmallInteger> how many elements (not bytes) do we want to cache |
|
18 |
operation <BlockClosure> CType translation primitive used to read from the buffer |
|
19 |
contentsSpecies <Class> collection type to use to hold collections of elements |
|
20 |
||
21 |
' |
|
22 |
! |
|
23 |
||
24 |
||
25 |
!InterpretedReadStream class methodsFor:'instance creation'! |
|
26 |
||
27 |
on: aReadStream bytesPerElement: anInteger contentsSpecies: aClass operation: aBlock cacheSize: aSize |
|
28 |
^self new on: aReadStream bytesPerElement: anInteger contentsSpecies: aClass operation: aBlock cacheSize: aSize |
|
29 |
! |
|
30 |
||
31 |
on: aReadStream type: aSymbol cacheSize: anInteger |
|
32 |
| interpretation | |
|
33 |
interpretation := InterpretedBytes perform: aSymbol. |
|
34 |
^self on: aReadStream bytesPerElement: (interpretation at: 1) contentsSpecies: (interpretation at: 2) operation: (interpretation at: 3) cacheSize: anInteger |
|
35 |
! ! |
|
36 |
||
37 |
!InterpretedReadStream methodsFor:'accessing'! |
|
38 |
||
39 |
read: anInteger into: aSequenceableCollection at: startIndex |
|
40 |
| count amount | |
|
41 |
count := 0. |
|
42 |
[[count = anInteger] whileFalse: |
|
43 |
[amount := cacheSize min: (anInteger - count). |
|
44 |
source read: amount * elementSize into: cache at: 1. |
|
45 |
0 to: amount - 1 do: [:index | aSequenceableCollection at: startIndex + count + index put: (operation value: cache value: index * elementSize + 1)]. |
|
46 |
count := count + amount]] |
|
47 |
on: Incomplete do: [:exception | |
|
48 |
0 to: exception count // elementSize - 1 do: [:index | aSequenceableCollection at: startIndex + count + index put: (operation value: cache value: index * elementSize + 1)]. |
|
49 |
(Incomplete on: aSequenceableCollection count: count + (exception count / elementSize) at: startIndex) raise]. |
|
50 |
^anInteger |
|
51 |
! ! |
|
52 |
||
53 |
!InterpretedReadStream methodsFor:'initialize-release'! |
|
54 |
||
55 |
close |
|
56 |
super close. |
|
57 |
cache recycle. |
|
58 |
cache := nil |
|
59 |
! |
|
60 |
||
61 |
contentsSpecies |
|
62 |
^contentsSpecies |
|
63 |
! |
|
64 |
||
65 |
on: aReadStream bytesPerElement: anInteger contentsSpecies: aClass operation: aBlock cacheSize: aSize |
|
66 |
super on: aReadStream. |
|
67 |
cacheSize := aSize. |
|
68 |
elementSize := anInteger. |
|
69 |
contentsSpecies := aClass. |
|
70 |
operation := aBlock. |
|
71 |
cache := InterpretedBytes newRecycled: ((elementSize * cacheSize) max: DefaultBufferSize) |
|
72 |
! ! |
|
73 |
||
74 |
!InterpretedReadStream methodsFor:'seeking'! |
|
75 |
||
76 |
++ anInteger |
|
77 |
||
78 |
anInteger < 0 ifTrue: [ ^self -- anInteger negated ]. |
|
79 |
[source ++ anInteger * elementSize] |
|
80 |
on: Incomplete do: [:exception | (Incomplete count: exception count / elementSize) raise]. |
|
81 |
^anInteger |
|
82 |
! |
|
83 |
||
84 |
-- anInteger |
|
85 |
anInteger < 0 ifTrue: [ ^self ++ anInteger negated ]. |
|
86 |
[source -- anInteger * elementSize] |
|
87 |
on: Incomplete do: [:exception | (Incomplete count: exception count / elementSize) raise]. |
|
88 |
^anInteger |
|
89 |
! |
|
90 |
||
91 |
length |
|
92 |
^source length / elementSize |
|
93 |
! |
|
94 |
||
95 |
position |
|
96 |
^source position / elementSize |
|
97 |
! |
|
98 |
||
99 |
position: aPosition |
|
100 |
||
101 |
^([ source position: aPosition * elementSize ] on: Incomplete do: [ :ex | ex count ]) // elementSize |
|
102 |
! ! |
|
103 |
||
104 |
!InterpretedReadStream methodsFor:'testing'! |
|
105 |
||
106 |
isPositionable |
|
107 |
||
108 |
^source isPositionable |
|
109 |
! ! |
|
110 |
||
111 |
!InterpretedReadStream class methodsFor:'documentation'! |
|
112 |
||
113 |
version_SVN |
|
114 |
^ '$Id$' |
|
115 |
! ! |