0

I need to read a 3+GB file using stream with following code:

long start = some_long_value;
int len = some_int_value;
int bytesRead = this.inputStream.read(data, (int)start, len);

My question is while reading file greater than Integer.MAX_VALUE, my start value will surpass this range and while down casting, java will give some garbage negative value and then my read stream will throw error.

How do I tell my stream to read from a offset and that offset value is in long? Should i change my inputStream to some other stream?

  • You seem to mistakenly think that the `start` value of the `read()` call is an offset into the file. It is not, it is the offset into the `data` byte array. See for yourself, i.e. *read the documentation* of the [`read(byte[] b, int off, int len)`](https://docs.oracle.com/javase/8/docs/api/java/io/InputStream.html#read-byte:A-int-int-) method: *"`off` - the start **offset in array** `b` at which the data is written."* – Andreas Feb 28 '21 at 22:16
  • If you need to be able to skip to a specific offset in a file, to start reading from there, use a [`RandomAccessFile`](https://docs.oracle.com/javase/8/docs/api/java/io/RandomAccessFile.html) and the [`seek(long pos)`](https://docs.oracle.com/javase/8/docs/api/java/io/RandomAccessFile.html#seek-long-) method. --- Alternatively, use the newer NIO.2 API and the [`SeekableByteChannel`](https://docs.oracle.com/javase/8/docs/api/java/nio/channels/SeekableByteChannel.html): *"A byte channel that maintains a current position and **allows the position to be changed**."* – Andreas Feb 28 '21 at 22:21

0 Answers0