0

I'm trying to read a file, from a certain point in the file for a certain number of bytes.

RandomAccessFile randomAccessFile = new RandomAccessFile(_file, "r"); randomAccessFile.seek(_offSet);
randomAccessFile.read(buffer, 0, _size);

Where the _offSet and _size type is "long".

The problem is, read method only takes "int". I overcame the offset problem by using "seek", how do I overcome the amount of bytes to read?

choppy
  • 739
  • 1
  • 12
  • 22

2 Answers2

1

There is no advantage I can see in trying to read more than 2 GB at once (other than simplicity) You can read more than 2 GB using multiple calls.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • You can't even fit more than 2^31-1 elements in an array in Java. – Louis Wasserman Apr 15 '12 at 18:56
  • Or ByteBuffer, but you can read into multiple byte[] or ByteBuffer in a collection or array. – Peter Lawrey Apr 15 '12 at 18:57
  • I mean, to be fair, you could probably design a custom list or buffer implementation that internally uses multiple `ByteBuffer`s or arrays, but exposes an API that lets you index with `long`s. – Louis Wasserman Apr 15 '12 at 18:58
  • Agreed, that is what I have done when trying to solve this problem. I have also used memory mapped files to improve the efficiency of the reads and writes. – Peter Lawrey Apr 15 '12 at 19:04
0

Where would you read them into? You can't declare an array that big anyway. Your problem doesn't exist.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • @choppy Doesn't make any difference. Each array is limited by Java to 2^31 bytes, i.e. the maximum integer, so `read()` having the same limit still isn't a problem. – user207421 Apr 16 '12 at 05:54