This repository has been archived by the owner on Nov 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
AsyncStreamsText.bas
75 lines (68 loc) · 1.9 KB
/
AsyncStreamsText.bas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
B4A=true
Group=Default Group
ModulesStructureVersion=1
Type=Class
Version=6.5
@EndOfDesignText@
#Event: NewText (Text As String)
#Event: Terminated
'version: 1.00
'Class module
Sub Class_Globals
Private mTarget As Object
Private mEventName As String
Private astreams As AsyncStreams
Public charset As String = "UTF8"
Private sb As StringBuilder
End Sub
Public Sub Initialize (TargetModule As Object, EventName As String, In As InputStream, out As OutputStream)
mTarget = TargetModule
mEventName = EventName
astreams.Initialize(In, out, "astreams")
sb.Initialize
End Sub
'Sends the text. Note that this method does not add end of line characters.
Public Sub Write(Text As String)
astreams.Write(Text.GetBytes(charset))
End Sub
Private Sub astreams_NewData (Buffer() As Byte)
Dim newDataStart As Int = sb.Length
sb.Append(BytesToString(Buffer, 0, Buffer.Length, charset))
Dim s As String = sb.ToString
Dim start As Int = 0
For i = newDataStart To s.Length - 1
Dim c As Char = s.CharAt(i)
If i = 0 And c = Chr(10) Then '\n...
start = 1 'might be a broken end of line character
Continue
End If
If c = Chr(10) Then '\n
CallSubDelayed2(mTarget, mEventName & "_NewText", s.SubString2(start, i))
start = i + 1
Else If c = Chr(13) Then '\r
CallSubDelayed2(mTarget, mEventName & "_NewText", s.SubString2(start, i))
If i < s.Length - 1 And s.CharAt(i + 1) = Chr(10) Then '\r\n
i = i + 1
End If
start = i + 1
End If
Next
If start > 0 Then sb.Remove(0, start)
End Sub
Private Sub astreams_Terminated
CallSubDelayed(mTarget, mEventName & "_Terminated")
End Sub
Private Sub astreams_Error
Log("error: " & LastException)
astreams.Close
CallSubDelayed(mTarget, mEventName & "_Terminated")
End Sub
Public Sub CloseGracefully
If astreams.SendAllAndClose = False Then
astreams.Close
astreams_Terminated
End If
End Sub
Public Sub Close
If astreams.IsInitialized Then astreams.Close
End Sub