Whimusical
Whimusical

Reputation: 6629

Fastest and cleanest way to load a FileSystem entry into a memory-mapped file (NIO2)

I'd like to extract a zip/jar entry into memory so I can close the zip stream/FS and keep the file in the JVM without dealing with temporary copies.

One option is to use Files.readAllLines(Path pathToZipFSentry), but it seems it used a buffered reader which can penalize a lot for big files.

So I am researching how to do it in Java NIO2 and it seems the way is ending with a MappedByteBuffer through FileChannels.

I cannot use the RandomAccessFile.getChannel() as I come from a Path of a virtual FileSystem, not a literal File. I cannot use Files.newByteChannel(rscPath, StandardOpenOption.READ) and then (fileChannel.)map() because it returns a SeekableByteChannel which doesn't got map in the interface...

Is thee any one/two high level liners for this by means of Path(s)/File(s)/FileSystem(s)/FileChannel(s) in Java8? I would expect something like InMemoryFile file = Files.loadIntoMem(Path) I've been 1 hour looking for a close option...

Upvotes: 0

Views: 509

Answers (1)

fge
fge

Reputation: 121710

You cannot really load this directly into memory.

You do have FileChannel.open() from which you can then .map() but that will create a temporary entry on your disk anyway.

There is also memoryfilesystem, but it will not handle files big enough for it to be useful, unfortunately.

The best solution I see is to Files.copy() into a temporary file and mmap() that; then copy back to the zip file when you're done with the modifications.

But basically, this is what you already do, so...

Upvotes: 1

Related Questions