473,586 Members | 2,854 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

java.lang.Negat iveArraySizeExc eption

28 New Member
Here is thread code of my java project, I want to make multiple download processes with threadpool, but I got java.lang.Negat iveArraySizeExc eption 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
1 6993
r035198x
13,262 MVP
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
4493
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 java.lang.Class.getDeclaredConstructors0(Native Method) at java.lang.Class.privateGetDeclaredConstructors(Class.java:1590) at...
1
47604
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 java1.4.2_04 (JIT: null) My Java version is:
6
6466
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: java.lang.ExceptionInInitializerError:
0
1874
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
2004
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 java.lang.VMClassLoader.resolveClass(java.lang.Class) (/usr/lib/libgcj.so.5.0.0) at java.lang.Class.initializeClass() (/usr/lib/libgcj.so.5.0.0) at...
2
4531
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 Keywords, String condition) { ::: ::: return description; }
4
14710
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 "appframework-1.0.3.jar;swing-worker-1.1.jar";CurrentStrobe.jar com.visionpro.currentstrobe.CurrentStrobeApp However, the statement below produces the error: java -cp...
1
4344
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; import org.sf.feeling.swt.win32.extension.hook.listener.HookEventListener; public class HotMouse {
1
10050
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 java.io.IOException;
1
3472
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.*; import java.sql.*; public class ColumnName{
0
8200
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8338
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8215
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6610
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5390
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3836
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3864
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2345
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1448
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.