Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

write file in memory with java.nio?

With nio it is possible to map an existing file in memory. But is it possible to create it only in memory without file on the hard drive ?

I want to mimic the CreateFileMapping windows functions which allow you to write in memory.

Is there an equivalent system in Java ?

The goal is to write in memory in order for another program ( c ) to read it.

like image 251
Foobyto Avatar asked Mar 22 '12 17:03

Foobyto


1 Answers

Have a look at the following. A file is created but this might be as close as your going to get.

MappedByteBuffer
MappedByteBuffer.load()
FileChannel
FileChannel.map()

Here is a snippet to try and get you started.

    filePipe = new File(tempDirectory, namedPipe.getName() + ".pipe");
    try {
        int pipeSize = 4096;
        randomAccessFile = new RandomAccessFile(filePipe, "rw");
        fileChannel = randomAccessFile.getChannel();
        mappedByteBuffer = fileChannel.map(FileChannel.MapMode.READ_WRITE, 0, pipeSize);
        mappedByteBuffer.load();
    } catch (Exception e) {
    ...
like image 164
Java42 Avatar answered Oct 17 '22 08:10

Java42