2

I have ArrayList of bytes.I want to use System.arraycopy function for combile all bytes of the ArrayList.Following is my code.

public void createFile(ArrayList<byte[]> arrayList) throws IOException
{
    FileOutputStream fos = new FileOutputStream(Constant.MERGE_DIRECOTRY + "out" + Constant.FILE_EXTENSION.toString());
    for(byte[] data: arrayList)
        fos.write(data);

    fos.close();
    MediaPlayer mp = MediaPlayer.create(MergerActivity.this, Uri.parse(Constant.MERGE_DIRECOTRY + "out" + Constant.FILE_EXTENSION.toString()));
    if(mp != null)
    {
        totalduration = totalduration + mp.getDuration();
        Log.d("duration",""+Utilis.milliSecondsToTimer(totalduration));
    }
}

How can i use System.arraycopy function instead if fos.write().

1
  • Where should that copy too? Please give an example. Commented Nov 22, 2017 at 6:30

1 Answer 1

2

In order to use System.arraycopy you have to first find out the required length of the output array:

int len = 0;
for(byte[] data: arrayList)
    len += data.length;

Now you can copy the source arrays to a target array:

byte[] output = new byte[len];
int pos = 0;
for(byte[] data: arrayList) {
    System.arrayscopy(data,0,output,pos,data.length);
    pos+=data.length;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.