MappedByteBuffer to access Linux device driver through MMAP
Hello. I'd like to access the mmap method of a file using the FileChannel -> MappedByteBuffer class. The kicker though is that what I want to access isn't really a file, but a character device on Linux. My understanding is that on Linux /dev/zero for example should be appear just like any other file (implements read/write/mmap/ioctl/llseek etc). This doesn't quite work though, the following code will run ...
FileChannel fc = new FileInputStream("/dev/zero").getChannel();
ByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
... but the file channel size is zero. If I try to hard code a non zero number it will throw an IO exception (invalid argument). It looks like the FileChannel.map method is actually checking the size of the file before it implements the the low level "mmap" system call. I guess my question is, how is java getting the size of the file? If it's from the file-system then I'm out of luck, but if it's through some kind of normal file operation I could mimic that behavior in my driver (I realize /dev/zero probably does return that it has no size, but a custom char driver could possibly look like it does have >0 bytes).
Perhaps if I really want to use MMAP fromJava for char drivers I need to use JNI (??)
Thanks for reading this
Re: MappedByteBuffer to access Linux device driver through MMAP
I have the same problem : Need to MMAP a linux device driver using java.
Did you get an answer to your question ?
Do we have to use JNI or you found a solution with FileChannel for instance ?