author | Martin Kobetic |
Sun, 17 Nov 2013 00:22:31 -0500 | |
changeset 144 | e193a6772be4 |
parent 109 | 9587e2df7029 |
permissions | -rw-r--r-- |
9 | 1 |
"{ Package: 'stx:goodies/xtreams/terminals' }" |
2 |
||
3 |
"{ NameSpace: Xtreams }" |
|
4 |
||
5 |
ReadStream subclass:#ExternalReadStream |
|
6 |
instanceVariableNames:'cache cachePosition cacheDataSize' |
|
7 |
classVariableNames:'' |
|
109 | 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 |
ExternalReadStream comment:'Used to read from BlockableIOAccessors (e.g sockets or pipes). Elements are read-ahead but only what''s available. This is a binary stream (produces bytes/ByteArrays). |
|
13 |
||
14 |
{{{ |
|
15 |
[ :in :out | |
|
109 | 16 |
[ out writing write: ''Hello''; close. |
9 | 17 |
in reading read: 5 |
18 |
] ensure: [ in close. out close ] |
|
19 |
] valueWithArguments: SocketAccessor openPair |
|
20 |
}}} |
|
21 |
{{{ |
|
22 |
[ :in :out | |
|
109 | 23 |
[ out writing write: ''Hello''; close. |
9 | 24 |
in reading read: 5 |
25 |
] ensure: [ in close. out close ] |
|
26 |
] valueWithArguments: OSSystemSupport concreteClass pipeAccessorClass openPair |
|
27 |
}}} |
|
28 |
||
29 |
Instance Variables |
|
109 | 30 |
cache <ByteArray | ByteString> read-ahead buffer |
31 |
cachePosition <SmallInteger> position in the buffer |
|
32 |
cacheDataSize <SmallInteger> size of valid data in the buffer |
|
9 | 33 |
|
34 |
' |
|
35 |
! |
|
36 |
||
37 |
||
38 |
!ExternalReadStream methodsFor:'accessing'! |
|
39 |
||
40 |
get |
|
109 | 41 |
cachePosition < cacheDataSize ifFalse: [ |
42 |
cachePosition := 0. |
|
43 |
cacheDataSize := (1 max: self bytesForRead) min: cache size. |
|
44 |
cacheDataSize := self readBytes: cacheDataSize into: cache startingAt: 1. |
|
45 |
cacheDataSize isZero ifTrue: [ self incompleteRead: Incomplete zero ] ]. |
|
46 |
^cache at: (cachePosition := cachePosition + 1) |
|
9 | 47 |
! |
48 |
||
49 |
read: anInteger into: aSequenceableCollection at: startIndex |
|
50 |
||
109 | 51 |
| index toRead | |
52 |
anInteger isZero ifTrue: [^0]. |
|
53 |
index := startIndex. |
|
54 |
toRead := anInteger. |
|
55 |
[toRead > 0] whileTrue: [ | amount | |
|
56 |
cachePosition < cacheDataSize ifFalse: [ |
|
57 |
cachePosition := 0. |
|
58 |
cacheDataSize := (toRead max: self bytesForRead) min: cache size. |
|
59 |
cacheDataSize := self readBytes: cacheDataSize into: cache startingAt: 1. |
|
60 |
cacheDataSize isZero ifTrue: [ self incompleteRead: (Incomplete on: aSequenceableCollection count: anInteger - toRead at: startIndex) ] ]. |
|
61 |
amount := (cacheDataSize - cachePosition) min: toRead. |
|
62 |
aSequenceableCollection replaceFrom: index to: index + amount - 1 with: cache startingAt: cachePosition + 1. |
|
63 |
cachePosition := cachePosition + amount. |
|
64 |
index := index + amount. |
|
65 |
toRead := toRead - amount ]. |
|
66 |
^anInteger |
|
9 | 67 |
! ! |
68 |
||
69 |
!ExternalReadStream methodsFor:'initialize-release'! |
|
70 |
||
71 |
close |
|
72 |
super close. |
|
73 |
cache recycle |
|
74 |
! |
|
75 |
||
76 |
contentsSpecies |
|
77 |
^cache class |
|
78 |
! |
|
79 |
||
80 |
contentsSpecies: aClass |
|
109 | 81 |
cache ifNotNil: [ cache recycle ]. |
82 |
cache := aClass newRecycled: DefaultBufferSize. |
|
83 |
cachePosition := 0. |
|
84 |
cacheDataSize := 0. |
|
9 | 85 |
! |
86 |
||
87 |
on: anAccessor |
|
88 |
super on: anAccessor. |
|
89 |
self contentsSpecies: ByteArray |
|
90 |
! ! |
|
91 |
||
92 |
!ExternalReadStream methodsFor:'private'! |
|
93 |
||
94 |
bytesForRead |
|
95 |
||
109 | 96 |
^source numAvailable |
9 | 97 |
! |
98 |
||
99 |
incompleteRead: incomplete |
|
100 |
||
101 |
incomplete raise |
|
39 | 102 |
! |
103 |
||
104 |
readBytes:count into:aByteBuffer startingAt:firstIndex |
|
105 |
||
106 |
^source nextBytes:count into:aByteBuffer startingAt:firstIndex |
|
9 | 107 |
! ! |
108 |
||
109 |
!ExternalReadStream class methodsFor:'documentation'! |
|
110 |
||
109 | 111 |
version_HG |
112 |
||
113 |
^ '$Changeset: <not expanded> $' |
|
9 | 114 |
! ! |
109 | 115 |