• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Max Size of Byte Array?

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 5114
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 261
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic