By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,805 Members | 1,350 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,805 IT Pros & Developers. It's quick & easy.

java.lang.NegativeArraySizeException

P: 28
Here is thread code of my java project, I want to make multiple download processes with threadpool, but I got java.lang.NegativeArraySizeException error.Why is this so?
Expand|Select|Wrap|Line Numbers
  1.     public void run() {
  2.  
  3.         InputStream stream = null;
  4.         File file=new File(url);
  5.         RandomAccessFile rfile=null;
  6.         System.out.println("Download started: "+id);
  7. try {
  8.  
  9.  
  10.             // Open connection to URL.
  11.             HttpURLConnection connection =
  12.                     (HttpURLConnection) url.openConnection();
  13.  
  14.             // Specify what portion of file to download.
  15.             connection.setRequestProperty("Range",
  16.                     "bytes=" + downloaded + "-");
  17.  
  18.             // Connect to server.
  19.             connection.connect();
  20.  
  21.  
  22.             int contentLength = connection.getContentLength();
  23.             System.out.println(contentLength);
  24.  
  25.       /* Set the size for this download if it
  26.          hasn't been already set. */
  27.             if (size == -1) {
  28.                 size = contentLength;
  29.  
  30.             }
  31.  
  32.             // Open file and seek to the end of it.
  33.             rfile=file.openRandomAccessFile(url);
  34.             rfile.seek(downloaded);
  35.  
  36.             stream = connection.getInputStream();
  37.  
  38.             while (status == DOWNLOADING) {
  39.         /* Size buffer according to how much of the
  40.            file is left to download. */
  41.                 byte buffer[];
  42.  
  43.                 if (size - downloaded > MAX_BUFFER_SIZE) {
  44.                     buffer = new byte[MAX_BUFFER_SIZE];
  45.  
  46.                 } else {
  47.                     buffer = new byte[size - downloaded];
  48.  
  49.                 }
  50.  
  51.                 // Read from server into buffer.
  52.                 int read = stream.read(buffer);
  53.  
  54.                 if (read == -1){
  55.                     System.out.println(buffer);
  56.                     System.out.println(file.getFileName()+" was downloaded");
  57.                     break;
  58.                 }
  59.  
  60.                 // Write buffer to file.
  61.                 rfile.write(buffer, 0, read);
  62.                 downloaded += read;
  63.  
  64.             }
  65.             //Thread.sleep(1000);
  66.       /* Change status to complete if this point was
  67.          reached because downloading has finished. */
  68.             if (status == DOWNLOADING) {
  69.                 status = COMPLETE;
  70.  
  71.             }
  72.         } catch (Exception e) {
  73.            System.out.println("There is an Error!"+e);
  74.         }
Thanks for any reply.
Apr 19 '13 #1

✓ answered by r035198x

That exception is thrown if an application tries to create an array with negative size. Check where you are creating an array and verify that that your logic there is correct.

Share this Question
Share on Google+
1 Reply


10K+
P: 13,264
That exception is thrown if an application tries to create an array with negative size. Check where you are creating an array and verify that that your logic there is correct.
Apr 19 '13 #2

Post your reply

Sign in to post your reply or Sign up for a free account.