0

I have a Processing script in which I'm plotting a small subset of data from a very large file that is constantly being updated. The data will be randomly accessed based on user input, and so to increase speed without loading the whole file into memory I've been memory mapping the file as a ByteBuffer. This works exceptionally well when run on a local machine, but unfortunately the end goal of the project is a web applet. Is there a good way of creating memory mapped or random access file structures from files accessed within an applet? I looked a lot at URL.openStream(), but couldn't figure out how to turn the InputStream into what I need. Here is what I had for the offline app:

//within my GraphicPlot class:

ByteBuffer bbuf;
FileChannel fc;

SetUpPlot(String fileName){    
  try {
    FileInputStream instream = new FileInputStream(fileName);
    fc = instream.getChannel();
    bbuf = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
  }
  catch (IOException e) { 
   e.printStackTrace();
   exit();
  } 
}

No matter how I send the URL to the function (absolute URL, relative URL, etc) I get a file not found error.

Any help would be greatly appreciated!

EDIT: I should probably mention that the applet will be signed, and the file in question will be hosted in the same directory as the applet, so there shouldn't be any applet permission problems.

1 Answers1

0

FileInputStream and memory mapping only works for files on your local system. You can't memory map a file which is only available via http or some other protocol. To use FileInputStream or memory mapping, you must first take a copy on the local system.

This is not a limitation of Java but a limitation of the operating system.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • Thank you for the response, Peter. This is what I feared/assumed. Do you have any recommendations for how to randomly access a protocol-served file? I'm considering implementing seek() on InputStream using a combination of mark(), reset(), and skip(), but it seems like this is going to be too slow, because most of the time will be spent wasting bytes to get to the desired area... – mrosasco Jul 18 '12 at 17:18
  • You should be able to do this with a http client which allows you to specify an offset and size. Some HTTP servers support loading portions of a file. – Peter Lawrey Jul 18 '12 at 20:52