Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Commit

Permalink
* Android's InputStream::skip is apparently buggy, so instead read in…
Browse files Browse the repository at this point in the history
…to a dummy buffer
  • Loading branch information
Tim Angus committed Oct 14, 2011
1 parent efa3969 commit a04d19e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
1 change: 0 additions & 1 deletion include/SDL_rwops.h
Expand Up @@ -89,7 +89,6 @@ typedef struct SDL_RWops
void *fileNameRef;
void *inputStream;
void *inputStreamRef;
void *skipMethod;
void *readableByteChannel;
void *readableByteChannelRef;
void *readMethod;
Expand Down
20 changes: 11 additions & 9 deletions src/core/android/SDL_android.cpp
Expand Up @@ -23,6 +23,8 @@

#include "SDL_android.h"

#include <algorithm>

extern "C" {
#include "../../events/SDL_events_c.h"
#include "../../video/android/SDL_androidkeyboard.h"
Expand Down Expand Up @@ -346,11 +348,6 @@ static int Android_JNI_FileOpen(SDL_RWops* ctx)
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/ ...
Expand Down Expand Up @@ -518,16 +515,21 @@ extern "C" long Android_JNI_FileSeek(SDL_RWops* ctx, long offset, int whence)

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
Expand Down

0 comments on commit a04d19e

Please sign in to comment.