-1

As a java guru, what would you suggest to abstract random access so that a code can be agnostic to whether the data its accessing is in memory or in a file on the harddrive?

(The files in question could be several gigabytes in size. Having random access is the most important feature.)

  • I posted a comment here, but then I realized that I may have misinterpreted "to abstract", so I removed it. To ensure, your actual question is which Java 1.6 (N)IO API to choose for random access? – BalusC Jan 04 '10 at 02:15
  • Yes, that's a better way to ask the question than I did. What would the Java API solution to abstracted random access from a Byte[] or File? – Jason Knight Jan 04 '10 at 02:18
  • OK, you already have the right answers :) – BalusC Jan 04 '10 at 02:21

2 Answers2

1

This is what Java NIO is for.

See Random Access Files and Memory-mapped files.

cletus
  • 616,129
  • 168
  • 910
  • 942
  • This looks like a good solution for my application. Thank you very much Stack Overflow, and cletus and BalusC and bmargulies and danben! – Jason Knight Jan 04 '10 at 02:21
1

I can offer memory mapping of files. Those give you a java.nio.ByteBuffer. And you can also make those as ordinary in-memory objects, so there you go, perfect agnosis, if not perfect ignorance.

And we've had them since 1.4.

The question is in some respects unclear. If you want file-style access, you are rather out of luck, since a RandomAccessFile can't be constructed over any in-memory resource.

bmargulies
  • 97,814
  • 39
  • 186
  • 310