«java.lang.ArrayIndexOutOfBoundsException» с System.arraycopy () - PullRequest
2 голосов
/ 23 апреля 2010

Эти несколько строк кода дают мне исключение "java.lang.ArrayIndexOutOfBoundsException", может кто-нибудь взглянет и укажет, почему (исключение вызывается во втором вызове arraycopy ()):

byte [] newContentBytes = EntityUtils.toByteArray((serverResponse.getEntity()));
  newContent = new String(newContentBytes);
  System.out.println( newContent);
  byte [] headerBytes = headers.getBytes();
  byte[] res = new byte[newContentBytes.length + headerBytes.length];
  //headerBytes.
  System.arraycopy(headerBytes, 0, res, 0, headerBytes.length);
  System.out.println( "length: " + newContentBytes.length);
  System.arraycopy(newContentBytes, 0, res, newContentBytes.length , newContentBytes.length);

Проблема заключается в выделении размера res, например, если я пишу новый байт [newContentBytes.length + headerBytes.length + 2000] вместо этого исключения не происходит, так какой должен быть точный размер?

1 Ответ

3 голосов
/ 23 апреля 2010

Ваш индекс для начала записи неверен. Попробуйте это:

 System.arraycopy(newContentBytes, 0, res, headerBytes.length , newContentBytes.length);
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...