Android Question SOLVED: Appending arrays

udg

Expert
Licensed User
Hi all,

I have two arrays of bytes and I'd like to append one after the other resulting in a third array containing all the data from the source ones.
So far I coded like this:
B4X:
'first array comes from a string builder object
Dim data1() As Byte = sb.ToString.GetBytes("UTF8")
Log("data1 size: "&data1.Length) '992 bytes. OK

Dim raf As RandomAccessFile
raf.Initialize3(data1,False)
raf.CurrentPosition = raf.Size 'set to 992, the first "free" slot

'second array comes from a file 
Dim InS As InputStream
InS = File.OpenInput(File.DirAssets,"myfile.png")
Dim data2() As Byte
data2=Bit.InputStreamToBytes(InS)
Log("data2 size: "&data2.Length)  '13607 bytes. OK

Dim i As Int
i=raf.WriteBytes(data2,0,data2.Length,raf.CurrentPosition) 'ERROR out of bounds on 992!
WriteBytes causes error OutOfBounds (lenght:992 - index:992).
It looks like raf is fixed at its first length, so whenever a byte goes to position 992 it gets mad.

My question: what's wrong with the above code and, eventually, how can I append an array to an existing one?

TIA
 

udg

Expert
Licensed User
Hi TDS,

same error. Checking raf.CurrentPosition without the assign operation it results in 0 as its value
 

udg

Expert
Licensed User
I tried sorex's solution beforehand (and just repeated the test to be sure).
In my opinion
raf.CurrentPosition = raf.Size
is correct since we want the new array to start at the first unused byte.
The error is raised even if I write
raf.WriteBytes(data2,0,data2.Length,991)

I suspect the problem arises when a byte from second array gets added because it gets written past original size.

Edit: just read about the "check Initialize3" and subsequent message. Thanks all
 
Last edited:

udg

Expert
Licensed User
I add this just to better summarize about RAF, as of my understanding. Sorry if it is partially off-topic but I felt it useless to start another thread.

RAF.Initialize - starts the RAF stream with content from a file on disk (DirAssets not allowed); this stream can grow in size (i.e. appending should work) if third parameter is set to False
RAF. Initialize2 - same as above
RAF.Initialize3 - in a way, it maps RAF functions on the array given as first parameter but, since they still work on an array, boundary limits are to be taken in account.

So, if I understand it correctly, when the data source for RAF is a file, it gets copied to an internal stream while, when the source is an array, RAF adopts a memory-wise attitude working on the same chunk of memory held by the array instead of copying it to an internal stream. That's the reason why "initialize3" refers to a costant size.

udg
 
Top