Link to home
Start Free TrialLog in
Avatar of onaled777
onaled777Flag for United States of America

asked on

Potential for IndexOutOfBoundException

I am currently looking into the potential for an IndexOutOfBoundException. I am examing the code below because it reoccurs pretty frequently.

				byte[] arr = new byte[2048];
				int length = 0;
				while ((length = pdfInputStream.read(arr)) > 0) {
					response.getOutputStream().write(arr, 0, length);  //
				}

Open in new window


Does code like this have the potential to throw an IndexOutOfBoundsException?  My thought is yes because the Arrays are not synchronized. I would prefer some verification.
ASKER CERTIFIED SOLUTION
Avatar of CPColin
CPColin
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of onaled777

ASKER

Thank you!
Avatar of CEHJ
If off is negative, or len is negative, or off+len is greater than the length of the array b, then an IndexOutOfBoundsException is thrown.
(API docs for OutputStream.write)

There is no connection between synchronization and such an exception. The way the code is currently written can not cause the violations mentioned above