* Android's InputStream::skip is apparently buggy, so instead read into a dummy buffer
authorTim Angus <tim@blackcompanystudios.co.uk>
Fri, 14 Oct 2011 17:29:49 +0100
changeset 5993 c9cb52d6d864
parent 5992 f4b73deb9d26
child 5994 153d15ab3032
* Android's InputStream::skip is apparently buggy, so instead read into a dummy buffer
include/SDL_rwops.h
src/core/android/SDL_android.cpp
--- a/include/SDL_rwops.h	Thu Oct 13 01:30:01 2011 -0400
+++ b/include/SDL_rwops.h	Fri Oct 14 17:29:49 2011 +0100
@@ -89,7 +89,6 @@
             void *fileNameRef;
             void *inputStream;
             void *inputStreamRef;
-            void *skipMethod;
             void *readableByteChannel;
             void *readableByteChannelRef;
             void *readMethod;
--- a/src/core/android/SDL_android.cpp	Thu Oct 13 01:30:01 2011 -0400
+++ b/src/core/android/SDL_android.cpp	Fri Oct 14 17:29:49 2011 +0100
@@ -23,6 +23,8 @@
 
 #include "SDL_android.h"
 
+#include <algorithm>
+
 extern "C" {
 #include "../../events/SDL_events_c.h"
 #include "../../video/android/SDL_androidkeyboard.h"
@@ -346,11 +348,6 @@
     ctx->hidden.androidio.inputStream = inputStream;
     ctx->hidden.androidio.inputStreamRef = mEnv->NewGlobalRef(inputStream);
 
-    // Store .skip id for seeking purposes
-    mid = mEnv->GetMethodID(mEnv->GetObjectClass(inputStream),
-            "skip", "(J)J");
-    ctx->hidden.androidio.skipMethod = mid;
-
     // Despite all the visible documentation on [Asset]InputStream claiming
     // that the .available() method is not guaranteed to return the entire file
     // size, comments in <sdk>/samples/<ver>/ApiDemos/src/com/example/ ...
@@ -518,16 +515,21 @@
 
     long movement = newPosition - ctx->hidden.androidio.position;
     jobject inputStream = (jobject)ctx->hidden.androidio.inputStream;
-    jmethodID skipMethod = (jmethodID)ctx->hidden.androidio.skipMethod;
 
     if (movement > 0) {
+        unsigned char buffer[1024];
+
         // The easy case where we're seeking forwards
         while (movement > 0) {
-            // inputStream.skip(...);
-            movement -= mEnv->CallLongMethod(inputStream, skipMethod, movement);
-            if (Android_JNI_ExceptionOccurred()) {
+            size_t result = Android_JNI_FileRead(ctx, buffer, 1,
+                std::min(movement, (long)sizeof(buffer)));
+
+            if (result <= 0) {
+                // Failed to read/skip the required amount, so fail
                 return -1;
             }
+
+            movement -= result;
         }
     } else if (movement < 0) {
         // We can't seek backwards so we have to reopen the file and seek