Fixes Android_JNI_FileRead behaviour where reading past the end of a file returns zero instead of the number of bytes read.
authorGabriel Jacobo <gabomdq@gmail.com>
Fri, 20 Jul 2012 15:22:48 -0300
changeset 6377 3d868ca4782f
parent 6376 f7ec45572c54
child 6378 58c9e692aec5
Fixes Android_JNI_FileRead behaviour where reading past the end of a file returns zero instead of the number of bytes read.
src/core/android/SDL_android.cpp
--- a/src/core/android/SDL_android.cpp	Fri Jul 20 13:52:25 2012 -0400
+++ b/src/core/android/SDL_android.cpp	Fri Jul 20 15:22:48 2012 -0300
@@ -591,9 +591,13 @@
         size_t size, size_t maxnum)
 {
     LocalReferenceHolder refs;
-    int bytesRemaining = size * maxnum;
+    jlong bytesRemaining = (jlong) (size * maxnum);
+    jlong bytesMax = (jlong) (ctx->hidden.androidio.size -  ctx->hidden.androidio.position);
     int bytesRead = 0;
 
+    /* Don't read more bytes than those that remain in the file, otherwise we get an exception */
+    if (bytesRemaining >  bytesMax) bytesRemaining = bytesMax;
+
     JNIEnv *mEnv = Android_JNI_GetEnv();
     if (!refs.init(mEnv)) {
         return -1;