B4J Question file read, non blocking

madru

Active Member
Licensed User
Longtime User
Good morning,

can somebody show me how to get this VB code working in B4J, the attached version does block the main thread and can't seek in the file if necessary.

THX



B4X:
'VB code
Dim fi As New IO.FileInfo(StreamLocation)
           Dim fs As New IO.FileStream(StreamLocation, IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.Read, 188, IO.FileOptions.SequentialScan)
            fs.Position = 0
            While bwReader.CancellationPending = False And EndOfStream = False
           VolumeRead = fs.Read(buffer, 0, 188)
                If VolumeRead <> 188 Then
                    ' End of file
                    EndOfStream = True
                Else
                    ' Test if sync byte isn't present at the beginning => problem
                    If buffer(0) <> &H47 Then
                        Input_BadLength += 1
                        ' Searching the next sync byte
                        SyncFound = 200
                        For i = 1 To 187
                            If buffer(i) = &H47 Then
                                If i < SyncFound Then SyncFound = i
                            End If
                        Next
                        ' If sync found, we resync the stream reading, if not, we wait for the next packet
                        If SyncFound < 200 Then
                            fs.Position = fs.Position - 188 + SyncFound
                        End If
                    Else
                        ' Packet OK
                        Input_TSPacketsReceived += 1
                       Analyzer.NewPacket(buffer)
                    End If
                End If
            End While
            fs.Close()
B4X:
'B4J code
    Dim in As InputStream = File.OpenInput(filepath, filename)
    Log( "Start: " & DateTime.Time(now))
    DoWhile VolumeRead <> - 1' Or EndOfStream = True 
        VolumeRead = in.ReadBytes( buffer, 0, buffer.Length)
        If VolumeRead <> 188 Then
            ' End of File
            EndOfStream = True
        Else
            ' test If sync byte isn't present at the beginning => problem
            If buffer(0) <> 0x47 Then
                Input_BadLength = Input_BadLength + 1
                ' Searching the Next sync byte
                SyncFound = 200
                For i = 1 To 186
                    If buffer(i) = 0x47 Then
                        If i < SyncFound Then SyncFound = i
                   End If
                Next
                ' If sync found, we resync the stream reading, If Not, we wait For the Next packet
                If SyncFound < 200 Then
                    ' fs.Position = fs.Position - 188 + SyncFound 'howto seek in the file with B4J ?
                    i = i
                End If
            Else
                ' Packet OK
                Input_TSPacketsReceived = Input_TSPacketsReceived + 1
                NewPacket(buffer)       
            EndIf
        EndIf
    Loop
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
You can use RandomAccessFile to read different parts of the file.

You can use the Threading library to run it on a background thread. I recommend you to start without it and see whether the performance is good enough.

It might be worth reading large parts (200mb +-) of the file into memory and then working with the bytes array instead of the file.
 
Upvote 0

madru

Active Member
Licensed User
Longtime User
performance is OK with my posted solution, the bad is that no GUI updates are possible in realtime.....
is RandomAccessFile non blocking?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
You shouldn't block the main thread for a long time. If you cannot make the operation fast enough then you need to use the Threading library.

Another option is to split the task into smaller tasks and then use CallSubDelayed to run each task at the end of the previous one. This will allow the main thread to update the UI.
 
Upvote 0
Top