473,405 Members | 2,154 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

java.lang.NegativeArraySizeException

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.

1 6978
r035198x
13,262 8TB
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

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

Similar topics

0
by: Shawn | last post by:
I am getting the following error with a Java Applet being served out by IIS over HTTPS/SSL using a Verisign certificate: java.lang.NoClassDefFoundError: javax/help/HelpSetException at...
1
by: greg.knaddison | last post by:
Hi, I'm trying to use the httpclient within Jython (see http://jakarta.apache.org/commons/httpclient/ for more information on the httpclient). My Jython version is: Jython 2.1 on...
6
by: ganesh.m | last post by:
Hi, I am new to DB2. I am getting this error while loading the DB2Driver. I don't have any idea about where i might have gone wrong. please help me. Below is the stack trace. Stack Trace:...
0
by: snkssa | last post by:
Hi, I got the following error while building an application with ant. please can you give the solution or reason why it is failing? Thank you create_zip_linux: ...
3
by: jayattarde | last post by:
Hi, While running .jar file I get the following error. Please helpme to sort it out. ption in thread "main" java.lang.NoClassDefFoundError: while resolving class: Cutter at...
2
by: cloudy | last post by:
First at all, i'm sorry, i not good in writing english. i want to retrieve a string from my applet via Javascript. I use a method named ReadAll. public String ReadAll(String StrOut, String...
4
by: jmitch89 | last post by:
I don't why I get this error: Exception in thread "main" java.lang.NoClassDefFoundError The statement below works just fine: java -cp...
1
Ganesh9u
by: Ganesh9u | last post by:
Hi All, import org.sf.feeling.swt.win32.extension.hook.Hook; import org.sf.feeling.swt.win32.extension.hook.data.HookData; import org.sf.feeling.swt.win32.extension.hook.data.MouseHookData; ...
1
by: rajujrk | last post by:
Hai All, I am Having a problem in the following... import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPFile; import org.apache.commons.io.FileUtils; import...
1
by: James Kalmadge | last post by:
I have a test program that compiles but does not run. Here is the code which I grabbed from from a web site: import java.awt.*; import java.awt.event.*; import java.io.*; import java.util.*;...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.