Android Question How to write an array of floats to a File

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Hi to All. Maybe it is a silly question, but I don't see a way to write an array of floats to the disk. The File object doesn't contain a method for this. The need for using low level functions is related to efficiency. I'll describe the problem first, to explain from where the need of low level I/O functions arises.
I have a map with objects of the folllowing type:
B4X:
Type VertexData(changed As Boolean, vertexList As List, vertexArray() As Float, beginIndices(4) As Int, endIndices(4) As Int)
I save/load this maps with RandomAccessFile.

B4X:
'
' mData is a Map  with key "color" and value  a VertexData object
'
'
private Sub UtSave(raf As RandomAccessFile,mData As Map,pos As Long) As Long
    
    raf.WriteInt(mData.Size,pos)            ' cannot use directly  raf.WriteObject(mData,true,pos); App is compiled but crashes saying that mData contains "primitives" 
    pos=pos+4                                      ' so I save the map size and each component 
    For Each color As Int In mData.Keys  ' 
        raf.WriteInt(color,pos)
        pos=pos+4
        raf.WriteObject(mData.Get(color),True,pos)  '  mData.Get(color) is a VertexData object
        pos=raf.CurrentPosition
    Next
    return pos
end sub
'' calling function
'
pos=UtSave(raf,ldata.pointMap,pos)
'

The problem arises because vertexArray is huge and the App takes minutes to save/load various tens of Mb, most of them floats contained in the vertexArray of each vertexData object. My idea was to use FILE object, but It only allows to write/read bytes arrays, Lists, Maps etc, and of course, it doesn't manage the whole Map of vertexData objects...
Thanks in advance for any hint
 

Sagenut

Expert
Licensed User
Longtime User
You need to convert your map to bytes with B4XSerializator.Convertobjecttobytes and save the file as bytes.
Then you will read it back to a map with Convertbytestoobject.
I can't make you an example because I am not in front of pc.
But at least you have a hint what to search in the forum.
 
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
You need to convert your map to bytes with B4XSerializator.Convertobjecttobytes and save the file as bytes.
Then you will read it back to a map with Convertbytestoobject.
I can't make you an example because I am not in front of pc.
But at least you have a hint what to search in the forum.
Thanks, but it seems that Serializator fails to convert the whole map too, as long as the B4XWriteObject fails. Topics are related. As a matter of fact, my vertexData object, which is a component of the Map, is a custom object containing one list, one array of floats and two arrays of ints. I guess that this is the reason of failure. Anyway, I am finding an apparently efficient solution. As a matter of fact, I think that using File is anyway much faster than using RandomAccesFile. Therefore I am converting the arrays to lists (with AddAll which is very useful). Then I save the List with File.WriteList. It seems an acceptable solution. Same work is done in 4 secs instead of 38 sec. Of course, as a C/C++ programmer I would like to work at lower level to achieve maximum speed, but maybe what I am doing is enough. The fact is that I really have big data, forced to be in Arrays because they feed OpenGL engine. Thanks for attention.
 
Upvote 0

Sagenut

Expert
Licensed User
Longtime User
Took from another thread
B4X:
Dim serializator As B4XSerializator
Dim byte_array as byte()=serializator.ConvertObjectToBytes(YourMapHere)
Dim out As OutputStream=File.OpenOutput(File.DirApp,"aaa_test_serializzazione.txt",False)
out.WriteBytes(byte_array,0,byte_array.Length)
out.Close
 
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Dim byte_array as byte()=serializator.ConvertObjectToBytes(YourMapHere)
As I wrote in previous message, if I try to convert the Map, App crashes with:

java.lang.RuntimeException: java.lang.RuntimeException: java.lang.RuntimeException: This method does not support arrays of primitives.

At this point, I have leaved up ... this the beginning of my problems... My Map object cannot be converted ..
 
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Try converting the array into List if possible.
Yes. It is what I am doing. See my first reply to you. Much probably I will resolve the problem by myself, now. The doubt still remain whether there could be a more efficient solution. Maybe using native Java etc.. Thanks for your attention.
 
Upvote 0
Top