Sub Class_Globals
Private Root As B4XView 'ignore
Private xui As XUI 'ignore
Private lblfoundchar As Label
End Sub
'You can add more parameters here.
Public Sub Initialize
End Sub
'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
Root = Root1
'load the layout to Root
Root.LoadLayout("frmB4XRegexQ")
B4XPages.SetTitle(Me, "RegexQ Download Page")
End Sub
Private Sub DownloadAndCountQs(Link As String, BlockSize As Int)
Dim j As HttpJob
j.Initialize("", Me)
j.Download(Link)
Wait For (j) JobDone(j As HttpJob)
If j.Success Then
Dim in As InputStream = j.GetInputStream
Dim buffer(BlockSize) As Byte
Dim bytesRead As Int
Dim totalQs As Int = 0
Dim blockCount As Int = 0
Do While in.BytesAvailable > 0
bytesRead = in.ReadBytes(buffer, 0, BlockSize)
Dim blockText As String = BytesToString(buffer, 0, bytesRead, "UTF-8")
'Count Q characters in this block
Dim qCount As Int = CountChar(blockText, "R")
totalQs = totalQs + qCount
blockCount = blockCount + 1
lblfoundchar.Text = $"Block ${blockCount}: ${qCount} Qs (Total: ${totalQs})"$
Sleep(0)
Log($"Block ${blockCount}: ${qCount} Qs (Total: ${totalQs})"$)
Loop
in.Close
Log($"Final count: ${totalQs} Q characters found"$)
Else
Log("Download failed")
End If
j.Release
End Sub
Private Sub CountChar(text As String, CountCharStr As String) As Int
Dim pattern As String = CountCharStr 'Simple pattern to match the character
Dim tmp_count As Int = 0
Dim Matcher1 As Matcher = Regex.Matcher(pattern, text)
Do While Matcher1.Find
tmp_count = tmp_count + 1
Loop
Return tmp_count
End Sub
Private Sub btnTest_Click
lblfoundchar.Text = "Start download"
Sleep(0)
DownloadAndCountQs("https://files.testfile.org/PDF/200MB-TESTFILE.ORG.pdf",1024000)
End Sub