Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Max Size of Byte Array?

 
Shaun Mc
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my application I read in a file to a byte array:

The app works fine when i use small files, but fails with the following error, when I read in a big file (20mb or so), (I HAVE to read the file into the byte array).

Is there a limit to the size a byte array can grow??

Does anybody know a solution to the problem?


Thanks

My code is:



"Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1076677835"

InputStream is = new FileInputStream(fileName);
long length = spssfile.length();
byte[] bytes = new byte[(int)length];
is.read(bytes, 0, bytes.length);
is.close();
 
Joe Ess
Bartender
Posts: 9319
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since the Java Language Specification says the argument to an array creation expression is an int and the largest int value is 2,147,483,647, I'd say that is the largest allowable array size. Are you certain that length == bytes.length? Your downcast from long to int could lose the most significant digit.
 
Henry Wong
author
Marshal
Pie
Posts: 21506
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the files are supposed to be only 20 MB, why do you have indexes as high as 1 GB? Maybe there is a size miscalculation somewhere.

In any case, 1 GB devoted to a single byte array is a bit ridiculous. Memory may be cheap, but it aint that cheap yet.

Henry
 
Norm Radder
Bartender
Posts: 1351
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What line of code do you get the error on?
What are the values you are working with? What is the value of length?
Add some println()s to show them.
 
Anirvan Majumdar
Ranch Hand
Posts: 261
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe you should load the file into the byte array in parts. Basically the buffer "byte []" can be flushed after loading say, every 256K of the file. I faced a similar problem sometime back too and flushing the byte array solved it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic