0

I'd like to know if it possible to read the content of file from SDCARD using JAVA API in Android.
I can do it using NDK C code by

int fd = open(str, O_RDWR | O_NONBLOCK | O_DIRECT);  
readCount = read(fd, pBuffer, size); 

when pBuffer is aligned buffer but when I read it using directly from Java using

bytesRead = fin.read(originalBuffer)

It brings file data from cache

4
  • I selected the lines of code in question and pressed CTRL+K and it changed their look. easy to read :D Dec 20, 2011 at 8:50
  • Thanks :) Now I know how to do it ))) Dec 20, 2011 at 8:56
  • just google! stackoverflow.com/questions/3779944/…
    – woodshy
    Dec 20, 2011 at 10:15
  • No, this issue differs - I need access to file which is not in cache. Thanks anyway Dec 20, 2011 at 10:58

2 Answers 2

0

So after long investigation and test I understand there is no way to open file directly from Java.
The only way to do it is to use NDK and open the file using O_DIRECT parameter as explained in the body of the question.
Note that in order to compile the code -D_GNU_SOURCE directive should be used.

0

I think that the main problem is to get a page aligned memory buffer in Java, in order to read the file, because passing the file descriptor with O_DIRECT is still possible through JNI ( http://www.kfu.com/~nsayer/Java/jni-filedesc.html - I did use this technique to be able to open file named with UTF-8 names on the Oracle Java VM for ARM which can't do that and this works).

But to fulfill your needs we still have to provide to the system a destination buffer for the read operation which is page aligned, because without it the underlying linux system gives you an EINVAL error after the first try to read data.

I'm not aware of any Java code which can provide page aligned read buffer.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.