Android Question RandomAccessFile new member needed

slugger

Member
Licensed User
Longtime User
Hello,

I am in need for a member similar to

B4X:
WriteBytes (Buffer() As Byte, StartOffset As Int, Length As Int, Position As Long) As Int

for Short values rather than bytes.

The RandomAccessFile object has both WriteByte and WriteBytes to write a single byte or a buffer respectively.

What I'd like is a sort of WriteShorts member which does the same work as the WriteBytes but using an array of Short instead of Byte.

I have tried the WriteShort member in a FOR loop but the code is needed in a situation where the timing and speed are important and the FOR loop seems to take ages to complete.

Any hints?

Thanks in advance.


steve
 

sorex

Expert
Licensed User
Longtime User
can you explain what you mean with short values?

is it numers from 0-63 or so? then it fits in a byte so there's no reason for going for another solution.

you could merge/span data over byte bits to gain some space but it will make it slower.


Edit: did a seach on the forum and short is like a word / double byte.

what you could do is something like this (syntax might be incorrect but you get the idea)

B4X:
for x=0 to shortArr.size
byteArr.size(x*2)=bit.and (shortArr(x),255)
byteArr.size((x*2)+1)=bit.shiftright(shortArr(x),8)
next

and do 1 buffer write instead of writing in the loop.
 
Last edited:
Upvote 0

slugger

Member
Licensed User
Longtime User
Thanks for the answers.

As I wrote I can't use a loop because it is too slow.

I made a simple test:

B4X:
    Dim i As Int
    Dim ByteArr(10000) As Byte
    Dim OutFile As RandomAccessFile


    For i=0 To ByteArr.Length-1
        ByteArr(i)=1
    Next
   
    Dim nowTime As Long
   
    If File.Exists(File.DirDefaultExternal,"test.bin") Then
            File.Delete(File.DirDefaultExternal,"test.bin")
    End If
    OutFile.Initialize2(File.DirDefaultExternal,"test.bin",False,True)
   
    nowTime=DateTime.Now
    'OutFile.WriteBytes(ByteArr,0,ByteArr.Length,0)
    OutFile.WriteObject(ByteArr,False,0)
    'For i=0 To ByteArr.Length-1
    '    OutFile.WriteByte(ByteArr(i),OutFile.Size)
    'Next
    OutFile.Flush
    OutFile.Close
   
    Log(DateTime.Now-nowTime)

Commenting out only one of the three methods at a time.

The Writebytes method is lightning fast.

The FOR loop is about 50 times slower at best.

The speed of Writeobject is somewhere in between with the unpleasant surprise of the file not containing what I wanted to save.


The method I need has to be as fast as the Writebytes one but has to work with Short objects rather than Byte.
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
As I wrote I can't use a loop because it is too slow.

in my answer I wrote that you should loop through the data to convert it to bytes and use 1 write command after the loop (not in the loop),
should be fast enough since it's 1 write.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…