Android Question SOLVED: Appending arrays

Discussion in 'Android Questions' started by udg, Feb 18, 2015.

  1. udg

    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:
    Code:
    '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
     
  2. thedesolatesoul

    thedesolatesoul Expert Licensed User

    Can you try removing this line and re-run and report back?
    Code:
    raf.CurrentPosition = raf.Size 'set to 992, the first "free" slot
     
  3. sorex

    sorex Expert Licensed User

    raf.CurrentPosition = raf.Size -1
     
  4. udg

    udg Expert Licensed User

    Hi TDS,

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

    thedesolatesoul Expert Licensed User

    Then I guess try what sorex says?
     
  6. thedesolatesoul

    thedesolatesoul Expert Licensed User

    Actually look at the documentation of Initialize3:
     
    Erel likes this.
  7. Erel

    Erel Administrator Staff Member Licensed User

    Use ByteConverter.ArrayCopy to copy the two arrays to a new large array.
     
    Last edited: Feb 18, 2015
    thedesolatesoul likes this.
  8. thedesolatesoul

    thedesolatesoul Expert Licensed User

    As you have the bytes, I would just do a normal Initialize and WriteBytes for data1 and data2.
     
  9. udg

    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: Feb 18, 2015
  10. udg

    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
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice