java.lang.ArrayIndexOutOfBoundsException

JDK Bug System | Nelson Dcosta | 9 years ago
  1. 0

    Bug ID: JDK-6631048 Problem when writing on output stream of HttpURLConnection

    java.com | 4 months ago
    java.lang.ArrayIndexOutOfBoundsException
  2. 0

    FULL PRODUCT VERSION : java version "1.6.0_05-ea" Java(TM) SE Runtime Environment (build 1.6.0_05-ea-b06) Java HotSpot(TM) Client VM (build 1.6.0_05-ea-b06, mixed mode, sharing) ADDITIONAL OS VERSION INFORMATION : Microsoft Windows XP [Version 5.1.2600] (C) Copyright 1985-2001 Microsoft Corp. A DESCRIPTION OF THE PROBLEM : when using the HttpURLConnection's output stream to write data we are facing problem. The chunking size is set to 48K. This piece of code works well in jdk 5. but the same code fails for jdk 6. Looking into the source code, there seems a 10K threshold limit is introduced in jdk 6. This piece of code is creating problem. I have attached a test program to reproduce the problem. The program works fine for JDK 5 without any problem. The exception stack trace is: java.lang.ArrayIndexOutOfBoundsException at java.lang.System.arraycopy(Native Method) at sun.net.www.http.ChunkedOutputStream.write(ChunkedOutputStream.java:161) at sun.net.www.protocol.http.HttpURLConnection$StreamingOutputStream.write(HttpURLConnection.java:2492) at com.documentum.ucf.common.transport.impl.TestHttpURLConnection.writeData(TestHttpURLConnection.java:63) at com.documentum.ucf.common.transport.impl.TestHttpURLConnection.testHttpUrlConnectionWrite(TestHttpURLConnection.java:45) at com.documentum.ucf.common.transport.impl.TestHttpURLConnection.main(TestHttpURLConnection.java:26) STEPS TO FOLLOW TO REPRODUCE THE PROBLEM : 1. compile the source 2. Run it with jdk 1.5 - you wont see any problem 3. Run it with jdk 6 - you will find the problem. You can connect to a Test servlet, in which case also you can see the problem. ERROR MESSAGES/STACK TRACES THAT OCCUR : C:\Trials\Test\src>java TestHttpURLConnection Writing buf len, len: 32120,32120 java.lang.ArrayIndexOutOfBoundsException at java.lang.System.arraycopy(Native Method) at sun.net.www.http.ChunkedOutputStream.write(Unknown Source) at sun.net.www.protocol.http.HttpURLConnection$StreamingOutputStream.write(Unknown Source) at TestHttpURLConnection.writeData(TestHttpURLConnection.java:60) at TestHttpURLConnection.testHttpUrlConnectionWrite(TestHttpURLConnection.java:42) at TestHttpURLConnection.main(TestHttpURLConnection.java:24) REPRODUCIBILITY : This bug can be reproduced always. ---------- BEGIN SOURCE ---------- import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; public class TestHttpURLConnection { public static void main (String[] args) { TestHttpURLConnection testHttpURLConnection = new TestHttpURLConnection(); try { testHttpURLConnection.testHttpUrlConnectionWrite(); } catch (Exception e) { e.printStackTrace(); } } public void testHttpUrlConnectionWrite () throws Exception { //Test - If you want to , change it to the url of a test servlet in any app server, the problem will be there even otherwise. URL url = new URL("http://some_host:8080"); HttpURLConnection urlConnection = open(url); setChunkingMode(urlConnection, CHUNK_SIZE); OutputStream out = urlConnection.getOutputStream(); writeData(out); out.close(); } private void writeData (OutputStream out) throws IOException { //you can replace this with any file or simply a byte[] data String file = "C:\\tmp\\test_1m.pdf"; InputStream is = new FileInputStream(file); byte[] buf = new byte[BUFFER_SIZE]; int len = 0; while ((len = is.read(buf)) >= 0) { System.out.println("Writing buf len, len: " + buf.length + "," + len); out.write(buf, 0, len); } is.close(); } private HttpURLConnection open (URL url) throws IOException { HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setDoInput(true); urlConnection.setDoOutput(true); return urlConnection; } private void setChunkingMode (HttpURLConnection urlConnection, int chunkSize) throws IOException { urlConnection.setChunkedStreamingMode(chunkSize); } /** * Test comments - I played with the BUFFER_SIZE and CHUNK_SIZE values, but that didn't fix the problem. */ private static final int BUFFER_SIZE = 32120; private static final int CHUNK_SIZE = 48 * 1024; } ---------- END SOURCE ---------- CUSTOMER SUBMITTED WORKAROUND : I coudn't find anything. By changing the buffer size and chunk size, you may reduce the problem, but happens though.

    JDK Bug System | 9 years ago | Nelson Dcosta
    java.lang.ArrayIndexOutOfBoundsException
  3. Speed up your debug routine!

    Automated exception search integrated into your IDE

  4. 0

    FULL PRODUCT VERSION : java version "1.6.0_05-ea" Java(TM) SE Runtime Environment (build 1.6.0_05-ea-b06) Java HotSpot(TM) Client VM (build 1.6.0_05-ea-b06, mixed mode, sharing) ADDITIONAL OS VERSION INFORMATION : Microsoft Windows XP [Version 5.1.2600] (C) Copyright 1985-2001 Microsoft Corp. A DESCRIPTION OF THE PROBLEM : when using the HttpURLConnection's output stream to write data we are facing problem. The chunking size is set to 48K. This piece of code works well in jdk 5. but the same code fails for jdk 6. Looking into the source code, there seems a 10K threshold limit is introduced in jdk 6. This piece of code is creating problem. I have attached a test program to reproduce the problem. The program works fine for JDK 5 without any problem. The exception stack trace is: java.lang.ArrayIndexOutOfBoundsException at java.lang.System.arraycopy(Native Method) at sun.net.www.http.ChunkedOutputStream.write(ChunkedOutputStream.java:161) at sun.net.www.protocol.http.HttpURLConnection$StreamingOutputStream.write(HttpURLConnection.java:2492) at com.documentum.ucf.common.transport.impl.TestHttpURLConnection.writeData(TestHttpURLConnection.java:63) at com.documentum.ucf.common.transport.impl.TestHttpURLConnection.testHttpUrlConnectionWrite(TestHttpURLConnection.java:45) at com.documentum.ucf.common.transport.impl.TestHttpURLConnection.main(TestHttpURLConnection.java:26) STEPS TO FOLLOW TO REPRODUCE THE PROBLEM : 1. compile the source 2. Run it with jdk 1.5 - you wont see any problem 3. Run it with jdk 6 - you will find the problem. You can connect to a Test servlet, in which case also you can see the problem. ERROR MESSAGES/STACK TRACES THAT OCCUR : C:\Trials\Test\src>java TestHttpURLConnection Writing buf len, len: 32120,32120 java.lang.ArrayIndexOutOfBoundsException at java.lang.System.arraycopy(Native Method) at sun.net.www.http.ChunkedOutputStream.write(Unknown Source) at sun.net.www.protocol.http.HttpURLConnection$StreamingOutputStream.write(Unknown Source) at TestHttpURLConnection.writeData(TestHttpURLConnection.java:60) at TestHttpURLConnection.testHttpUrlConnectionWrite(TestHttpURLConnection.java:42) at TestHttpURLConnection.main(TestHttpURLConnection.java:24) REPRODUCIBILITY : This bug can be reproduced always. ---------- BEGIN SOURCE ---------- import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; public class TestHttpURLConnection { public static void main (String[] args) { TestHttpURLConnection testHttpURLConnection = new TestHttpURLConnection(); try { testHttpURLConnection.testHttpUrlConnectionWrite(); } catch (Exception e) { e.printStackTrace(); } } public void testHttpUrlConnectionWrite () throws Exception { //Test - If you want to , change it to the url of a test servlet in any app server, the problem will be there even otherwise. URL url = new URL("http://some_host:8080"); HttpURLConnection urlConnection = open(url); setChunkingMode(urlConnection, CHUNK_SIZE); OutputStream out = urlConnection.getOutputStream(); writeData(out); out.close(); } private void writeData (OutputStream out) throws IOException { //you can replace this with any file or simply a byte[] data String file = "C:\\tmp\\test_1m.pdf"; InputStream is = new FileInputStream(file); byte[] buf = new byte[BUFFER_SIZE]; int len = 0; while ((len = is.read(buf)) >= 0) { System.out.println("Writing buf len, len: " + buf.length + "," + len); out.write(buf, 0, len); } is.close(); } private HttpURLConnection open (URL url) throws IOException { HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setDoInput(true); urlConnection.setDoOutput(true); return urlConnection; } private void setChunkingMode (HttpURLConnection urlConnection, int chunkSize) throws IOException { urlConnection.setChunkedStreamingMode(chunkSize); } /** * Test comments - I played with the BUFFER_SIZE and CHUNK_SIZE values, but that didn't fix the problem. */ private static final int BUFFER_SIZE = 32120; private static final int CHUNK_SIZE = 48 * 1024; } ---------- END SOURCE ---------- CUSTOMER SUBMITTED WORKAROUND : I coudn't find anything. By changing the buffer size and chunk size, you may reduce the problem, but happens though.

    JDK Bug System | 9 years ago | Nelson Dcosta
    java.lang.ArrayIndexOutOfBoundsException

    Not finding the right solution?
    Take a tour to get the most out of Samebug.

    Tired of useless tips?

    Automated exception search integrated into your IDE

    Root Cause Analysis

    1. java.lang.ArrayIndexOutOfBoundsException

      No message provided

      at java.lang.System.arraycopy()
    2. Java RT
      HttpURLConnection$StreamingOutputStream.write
      1. java.lang.System.arraycopy(Native Method)
      2. sun.net.www.http.ChunkedOutputStream.write(Unknown Source)
      3. sun.net.www.protocol.http.HttpURLConnection$StreamingOutputStream.write(Unknown Source)
      3 frames
    3. Unknown
      TestHttpURLConnection.main
      1. TestHttpURLConnection.writeData(TestHttpURLConnection.java:60)
      2. TestHttpURLConnection.testHttpUrlConnectionWrite(TestHttpURLConnection.java:42)
      3. TestHttpURLConnection.main(TestHttpURLConnection.java:24)
      3 frames