0

I'm trying something new, There is an application who send data to a memory-mapped file located at Local\MemFileName

I would like to read it in java,

I tried some tutorials like https://www.baeldung.com/java-mapped-byte-buffer, https://howtodoinjava.com/java7/nio/memory-mapped-files-mappedbytebuffer/

But all seem to read a file in JVM, or I did not understand...

How can I read the content of the file Located in windows system Local\MemFileName

Thanks!

Following: Example code of what i tried

public class Main {

private static final String IRSDKMEM_MAP_FILE_NAME = StringEscapeUtils.unescapeJava("Local\\IRSDKMemMapFileName");
private static final String IRSDKDATA_VALID_EVENT  = StringEscapeUtils.unescapeJava("Local\\IRSDKDataValidEvent");

public static final CharSequence charSequence = "Local\\IRSDKMemMapFileName";

public static void main(String[] args) throws IOException, InterruptedException {

    System.out.println(charSequence);

    try (RandomAccessFile file = new RandomAccessFile(new File(IRSDKMEM_MAP_FILE_NAME), "r")) {
        //Get file channel in read-only mode
        FileChannel fileChannel = file.getChannel();

        //Get direct byte buffer access using channel.map() operation
        MappedByteBuffer buffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, fileChannel.size());

        // the buffer now reads the file as if it were loaded in memory.
        System.out.println("Loaded " + buffer.isLoaded());  //prints false
        System.out.println("capacity" + buffer.capacity());  //Get the size based on content size of file

        //You can read the file from this buffer the way you like.
        for (int i = 0; i < buffer.limit(); i++) {
            System.out.println((char) buffer.get()); //Print the content of file
        }
    }


}

}

2 Answers 2

1

To read a memory mapped file:

Open a FileChannel on the file using FileChannel.open.

Invoke the map method on the FileChannel to create a MappedByteBuffer covering the area of the file you want to read.

Read the data from the MappedByteBuffer.

Sign up to request clarification or add additional context in comments.

5 Comments

I did it, and I got a NullPointerException. I saw that if i write instead of read, it create the file Local\\MemFileName in the project directory
@JoffreyBonifay Please post the code to show what you tried.
The code you just posted works fine for me! Where do you get a NullPointerException?
Exception in thread "main" java.io.FileNotFoundException: LocalIRSDKMemMapFileName (File not found) at java.io.RandomAccessFile.open0(Native Method) at java.io.RandomAccessFile.open(RandomAccessFile.java:316) at java.io.RandomAccessFile.<init>(RandomAccessFile.java:243) at io.mappedbus.Main.main(Main.java:21)
the file "Local\\IRSDKMemMapFileName" is mapped in memory by an application
1

The solution for me was to use a WindowsService.class implementing methods from JNA library, as you can see: My Library

With this I could open a file mapped in Windows system.

All the previous answers was correct for a file accessible from JVM, but from outside the JVM it was impossible.

Thanks !

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.