0

I am uploading video using multipartentity in chunks, to upload I am reading data from file. In the first loop I am able to read data, but in the next loop it is getting ArrayIndexOutOfBoundsException

Exception :

> java.lang.ArrayIndexOutOfBoundsException: length=1024;
> regionStart=1024; regionLength=1024

I am reading 1024 bytes in every loop.

totalSize = 441396

offset starts from 0

chunkSize = 1024

My code:

do {
    currentChunkSize = totalSize - offset > chunkSize ? chunkSize : totalSize - offset;

    String urlString = "http://capmem.omsoftware.co/Event/UploadVideo?" +
                            "callback=localJsonpCallback&" +
                            "filename="+ filename +"&" +
                            "ext="+ exten +"&" +
                            "totalsize="+ size +"&" +
                            "EventID="+ eventid +"&" +
                            "UserID="+ userid +"&" +
                            "comment="+ coment +"&" +
                            "VideoLength="+ videolength +
                            "&chunk=" + currentChunkSize;

    httppost1 = new HttpPost(urlString);

    byte[] currentBytes = new byte[currentChunkSize];
    buf = new BufferedInputStream(new FileInputStream(file));
    buf.read(currentBytes, offset, currentChunkSize);

    offset += currentChunkSize;

    MultipartEntity reqEntity = new MultipartEntity();
    reqEntity.addPart("videofile", new ByteArrayBody(currentBytes, "application/octet-stream", filename));

    httppost1.setEntity(reqEntity);
    DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpResponse response = httpClient.execute(httppost1);
    int resCode = response.getStatusLine().getStatusCode();

} while(totalSize != offset);

Getting Exception in

buf.read(currentBytes, offset, currentChunkSize);

2 Answers 2

2

The offset parameter is the offset into currentBytes you want to write, not the offset into the stream. Since currentBytes is of length currentChunkSize, if offset is anything other than 0 you'll go past the end of the array.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your reply, but I could not understood your explanation. Can you please explain in code.?
@user2085965: Gabe meant to say that the offset value you are passing to your read method points to the starting index of your array to which the read bytes are being copied; the BufferedInputStream remembers and continues to read from the point it has previously read. If you want your array to contain only the current chunk, you must replace buf.read(currentBytes, offset, currentChunkSize); with buf.read(currentBytes, 0, currentChunkSize);
0

your currentChunkSize is always 1024 since your only check is

currentChunkSize = totalSize - offset > chunkSize ? chunkSize : totalSize - offset;

you never modify the totalSize. You need to know how many bytes are left to determine the chunk size you need.

Try adding

totalSize = totalSize-currentChunkSize; 

also you can change the while condition to

while(totalSize!=0)

and even better to change it to a pre-condition loop instead of the do-while (the file might be empty)

2 Comments

Thanks for your reply, Same error in the same line of code.
well, I also notice you are opening the buffer on every iteration without closing it after... This might cause a problem also

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.