Android Code Snippet [B4X] Join arrays of bytes

Input = A list (or array of objects) with one or more arrays of bytes.
Output = An array of bytes with the input data.

B4X:
Sub JoinBytes(ListOfArraysOfBytes As List) As Byte()
   Dim size As Int
   For Each b() As Byte In ListOfArraysOfBytes
     size = size + b.Length
   Next
   Dim result(size) As Byte
   Dim index As Int
   Dim bc As ByteConverter 'ByteConverter library
   For Each b() As Byte In ListOfArraysOfBytes
     bc.ArrayCopy(b, 0, result, index, b.Length)
     index = index + b.Length
   Next
   Return result
End Sub

Usage example:
B4X:
Dim b() As Byte = JoinBytes(Array("abcde".GetBytes("utf8"), "fghij".GetBytes("utf8"), _
   "klmnop".GetBytes("utf8")))
Log(BytesToString(b, 0, b.Length, "utf8"))

Depends on ByteConverter library (iRandomAccessFile in B4i).
 
Top