views:

147

answers:

1

I am writing some data access test implementation and I need random access to file content. Here's the code:

RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rwd");
final byte b[] = IOUtils.toByteArray(source);
randomAccessFile.write(b, (int) offset, size);

where offset has type long. Why doesn't RandomAccessFile provide method:

public void write(byte b[], long off, int len)

?

How to override this issue?

+11  A: 

I think you are looking for the seek method.

The offset in write is an offset into the array. Arrays have int offsets. There have been proposals for "long arrays", but were these implemented, you'd still need an overload.

Mapped files in NIO have a problem in that the equivalent MappedByteBuffer.position for some reason only uses int. See CR 6347833 (9 votes).

Tom Hawtin - tackline
+1. Interestingly, the JDK `MemoryMappedBuffer`, which is Java's sole interface to mmap, only takes an `int` in its `position()` method, which makes it kind of wimpy in the same way that te OP was concerned about. Go figure.
Jonathan Feinberg
Yes, I was going to look that up...
Tom Hawtin - tackline
So, it's not an offset within file? Then int is ok, and I'll alwasy call .seek(), thanks.
Shaman