简体   繁体   中英

Why am I getting an ArraIndexOutOfBoundException?

I am trying to copy an byte array (fileData), which I read from a file, into another byte array (subdata) (below the code):

                ByteBuffer inputBuffer = decoderInputBuffers[intBufIndex];
                int limit = inputBuffer.capacity();
                int pos = frameIndex * limit;
                byte[] subData = new byte[limit];

                System.arraycopy(fileData, pos, subData, 0, subData.length);

My question is why I am gettng this error?

java.lang.ArrayIndexOutOfBoundsException: src.length=732542 srcPos=0 dst.length=1572864 dstPos=0 length=1572864

How ist that even possible since I say explicitly say what the size of the array should be.

If you look at the docs (which you have linknd in comments) it says that the last parameter of the function is:

length − This is the number of array elements to be copied.

so if you look closely you are trying to copy 1572864 from an array of 732542 and that's why you are getting the exception

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM