List<byte[]> listOfByteArrays;
I want to get all the byte[] stored in this List to just one byte[] by appending all elements in the List. I know one way to do it and that is by using System.arrayCopy()but using that inside a loop is going to be a little messy (temporary variables and byte[] arrays). Tried finding a better way of doing it but couldn't. Any pointers? Any external API's I can use?
Try using ByteArrayOutputStream:
ByteArrayOutputStream out= new ByteArrayOutputStream( );
in loop
out.write(eachBytearray);
After loop
byte result[] = out.toByteArray( );
Why would you need temporary arrays?
System.arraycopy to copy the array from the list to its appropriate place in the target array. (You would need to keep track of the offset in the target array, obviously.)If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With