1 package frost.io
2
3 uses frost.unsafe.Pointer
4
5 ====================================================================================================
6 An `InputStream` which reads from a `String`.
7 ====================================================================================================
8 class MemoryInputStream : InputStream {
9 @private
10 def data:Pointer<UInt8>
11
12 @private
13 def length:Int
14
15 @private
16 def string:String?
17
18 @private
19 var index := 0
20
21 ================================================================================================
22 Creates a new `MemoryInputStream` which reads from a `String`.
23
24 @param source the string to read from
25 ================================================================================================
26 @unsafeAccess
27 init(source:String) {
28 self.data := source.data->Pointer<UInt8>
29 self.length := source.byteLength
30 self.string := source
31 super.init()
32 }
33
34 @override
35 method read():UInt8? {
36 if index < length {< length {
37 var result:UInt8 := data[index]
38 index += 1
39 return result
40 }
41 return null
42 }
43 }