100% found this document useful (1 vote)
38 views3 pages

Async Streams for File Writing in Python

This class initializes and manages an AsyncStreams object to read input and write output in an asynchronous manner. It initializes the AsyncStreams object, writes text without line endings, and contains callback methods that are called when new data is received, the stream terminates, or an error occurs to pass information to the target object.

Uploaded by

amin rusydi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
38 views3 pages

Async Streams for File Writing in Python

This class initializes and manages an AsyncStreams object to read input and write output in an asynchronous manner. It initializes the AsyncStreams object, writes text without line endings, and contains callback methods that are called when new data is received, the stream terminates, or an error occurs to pass information to the target object.

Uploaded by

amin rusydi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd

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

[Link](In, out, "astreams")

[Link]

End Sub

'Sends the text. Note that this method does not add end of line characters.

Public Sub Write(Text As String)

[Link]([Link](charset))

End Sub

Private Sub astreams_NewData (Buffer() As Byte)

Dim newDataStart As Int = [Link]

[Link](BytesToString(Buffer, 0, [Link], charset))

Dim s As String = [Link]

Dim start As Int = 0


For i = newDataStart To [Link] - 1

Dim c As Char = [Link](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 < [Link] - 1 And [Link](i + 1) = Chr(10) Then '\r\n

i=i+1

End If

start = i + 1

End If

Next

If start > 0 Then [Link](0, start)

End Sub

Private Sub astreams_Terminated

CallSubDelayed(mTarget, mEventName & "_Terminated")

End Sub

Private Sub astreams_Error


Log("error: " & LastException)

[Link]

CallSubDelayed(mTarget, mEventName & "_Terminated")

End Sub

Public Sub Close

[Link]

End Sub

You might also like