• 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:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

How read into byte array when file size is of type long?

 
Ranch Hand
Posts: 585
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want to read a file into a byte array, but the problem is I can't create an array big enough to ensure I get all data. A file's size could be longer than an int and arrays can only be created of size int:
byte b = new byte[ int_value ];
What can/should I do? Should I just have a while loop that does a check on available and creates a new byte array of that size and then fills it, then checks avail again, etc? Then put all the byte arrays into a List or something?
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Robert, are you sure you will actually be able accommodate more than 2GB worth of byte arrays in the first place? You'd need to be running your JVM on a 64-bit architecture for this to work, and of course you'll have to have the actual memory as well.
Do you really have to buffer all of the file?
- Peter
 
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Robert,
You may want to look into the new java.nio packages for working with huge files. The java.nio.channels.FileChannel class gives you access to all the bytes in the file similar to a byte array, although accessing those bytes will be a bit more tedious. You can also map portions of the file directly into memory for a performance boost.
Michael Morris
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic