src/core/android/SDL_android.cpp
changeset 5993 c9cb52d6d864
parent 5982 f324bd81b52c
child 5994 153d15ab3032
equal deleted inserted replaced
5992:f4b73deb9d26 5993:c9cb52d6d864
    20 */
    20 */
    21 #include "SDL_config.h"
    21 #include "SDL_config.h"
    22 #include "SDL_stdinc.h"
    22 #include "SDL_stdinc.h"
    23 
    23 
    24 #include "SDL_android.h"
    24 #include "SDL_android.h"
       
    25 
       
    26 #include <algorithm>
    25 
    27 
    26 extern "C" {
    28 extern "C" {
    27 #include "../../events/SDL_events_c.h"
    29 #include "../../events/SDL_events_c.h"
    28 #include "../../video/android/SDL_androidkeyboard.h"
    30 #include "../../video/android/SDL_androidkeyboard.h"
    29 #include "../../video/android/SDL_androidtouch.h"
    31 #include "../../video/android/SDL_androidtouch.h"
   343         goto failure;
   345         goto failure;
   344     }
   346     }
   345 
   347 
   346     ctx->hidden.androidio.inputStream = inputStream;
   348     ctx->hidden.androidio.inputStream = inputStream;
   347     ctx->hidden.androidio.inputStreamRef = mEnv->NewGlobalRef(inputStream);
   349     ctx->hidden.androidio.inputStreamRef = mEnv->NewGlobalRef(inputStream);
   348 
       
   349     // Store .skip id for seeking purposes
       
   350     mid = mEnv->GetMethodID(mEnv->GetObjectClass(inputStream),
       
   351             "skip", "(J)J");
       
   352     ctx->hidden.androidio.skipMethod = mid;
       
   353 
   350 
   354     // Despite all the visible documentation on [Asset]InputStream claiming
   351     // Despite all the visible documentation on [Asset]InputStream claiming
   355     // that the .available() method is not guaranteed to return the entire file
   352     // that the .available() method is not guaranteed to return the entire file
   356     // size, comments in <sdk>/samples/<ver>/ApiDemos/src/com/example/ ...
   353     // size, comments in <sdk>/samples/<ver>/ApiDemos/src/com/example/ ...
   357     // android/apis/content/ReadAsset.java imply that Android's
   354     // android/apis/content/ReadAsset.java imply that Android's
   516         newPosition = ctx->hidden.androidio.size;
   513         newPosition = ctx->hidden.androidio.size;
   517     }
   514     }
   518 
   515 
   519     long movement = newPosition - ctx->hidden.androidio.position;
   516     long movement = newPosition - ctx->hidden.androidio.position;
   520     jobject inputStream = (jobject)ctx->hidden.androidio.inputStream;
   517     jobject inputStream = (jobject)ctx->hidden.androidio.inputStream;
   521     jmethodID skipMethod = (jmethodID)ctx->hidden.androidio.skipMethod;
       
   522 
   518 
   523     if (movement > 0) {
   519     if (movement > 0) {
       
   520         unsigned char buffer[1024];
       
   521 
   524         // The easy case where we're seeking forwards
   522         // The easy case where we're seeking forwards
   525         while (movement > 0) {
   523         while (movement > 0) {
   526             // inputStream.skip(...);
   524             size_t result = Android_JNI_FileRead(ctx, buffer, 1,
   527             movement -= mEnv->CallLongMethod(inputStream, skipMethod, movement);
   525                 std::min(movement, (long)sizeof(buffer)));
   528             if (Android_JNI_ExceptionOccurred()) {
   526 
       
   527             if (result <= 0) {
       
   528                 // Failed to read/skip the required amount, so fail
   529                 return -1;
   529                 return -1;
   530             }
   530             }
       
   531 
       
   532             movement -= result;
   531         }
   533         }
   532     } else if (movement < 0) {
   534     } else if (movement < 0) {
   533         // We can't seek backwards so we have to reopen the file and seek
   535         // We can't seek backwards so we have to reopen the file and seek
   534         // forwards which obviously isn't very efficient
   536         // forwards which obviously isn't very efficient
   535         Android_JNI_FileClose(ctx, false);
   537         Android_JNI_FileClose(ctx, false);