Bug? RandomAccessFile ReadObject on e.g. text file hangs, high CPU

walt61

Active Member
Licensed User
Longtime User
Am I doing something wrong or is this a bug? I know I shouldn't be using ReadObject if I know the file wasn't written with WriteObject, but I want to let the user select any file they choose and then read the Map that was written to it - if there is one:

B4X:
    File.WriteString(File.DirApp, "test.txt", "Just a text file")

    Dim r As RandomAccessFile
    r.Initialize(File.DirApp, "test.txt", True)
    Dim o As Object = r.ReadObject(r.CurrentPosition)
    r.Close
 

Attachments

  • RAFtest.zip
    2 KB · Views: 167

agraham

Expert
Licensed User
Longtime User
I know I shouldn't be using ReadObject if I know the file wasn't written with WriteObject
That's the problem. ReadObject is looping endlessly trying to read data that isn't there because the file is not in the expected format. Having said that it could probably do with a sanity check coded into it to stop such behaviour and throw an exception.
 

walt61

Active Member
Licensed User
Longtime User
Just for the record/discussion:

That is of course fine if one knows how the file has been created.

However, if the user selects a text file (extension can't be trusted to reflect the contents, so there's no point in checking that), the process runs away with ReadObject; with ReadB4XObject it throws an exception, which is the desired behaviour.

Unless I'm missing something, ReadB4XObject also throws an error if a file was created with WriteObject, so it would still not be possible to e.g. first try with ReadB4XObject and, if that fails, with ReadObject, as it would let the process run away on text files (probably others too, I didn't test that). But if that's just how it is, now I know :)
 
Top