I am looking to test a file for a valid B4X object before reading it. My best effort to prevent my app from showing as an option to handle files in Open With... dialogs wasn't very successful so I want my app to reject any file that contains data created by other applications if the user chooses to open it with my app.
At the moment I can't read the file without crashing.
Thanks in advance for ideas...
Dim Inp As InputStream = File.OpenInput(ContentDir , strUri)
Dim Out As OutputStream = File.OpenOutput(File.DirRootExternal&"/MyDir", "MyFile", False)
' Check if input stream is a valid B4X object
File.copy2(Inp, Out)
Inp.Close
Out.Close
Before I copy2 I would like to check if the input stream file contains a valid B4X object.
You are not really looking for a valid B4X object. You are looking for files created with your application. I guess that you are creating the files with RandomAccessFile, right?
If so then start each file with a 4 bytes mark and then check it before you try to read a B4X object.
That's right, a list is written to a file with writeB4xobject. What to look for in the file at 4 byte mark? What's the format of the file?
I now try to read the B4x object and try-catch exception but it doesn't look right. It's like electrician shorting the circuit to find which breaker in the breaker box the circuit is hooked up to.
'start each file with:
Dim MyHeader As Int = 23421983
raf.WriteInt(MyHeader, 0)
'check whether it is your file:
If raf.Size > 4 And raf.ReadInt(0) = MyHeader Then
'it is your file
Else
'it is not your file
End If
Unfortunately i originally didn't plan to have file sharing feature and both iOS and Android apps are released. So I can't change format of the file now but was hoping I could extract some unique features of the list object saved in the file.
Also I am attempting to prevent app from reading bitmaps and other file types that the user may click on in an Open With... dialog when my app shows as available to handle the file (but should not be there).
Perfect....checking for 1 works well. Chances they will import a wrong file are next to none.
Try-catch didn't help with larger files which were throwing out of memory error.