Exemplo n.º 1
0
extern "C" long Android_JNI_FileSeek(SDL_RWops* ctx, long offset, int whence)
{
    long newPosition;

    switch (whence) {
        case RW_SEEK_SET:
            newPosition = offset;
            break;
        case RW_SEEK_CUR:
            newPosition = ctx->hidden.androidio.position + offset;
            break;
        case RW_SEEK_END:
            newPosition = ctx->hidden.androidio.size + offset;
            break;
        default:
            SDL_SetError("Unknown value for 'whence'");
            return -1;
    }
    if (newPosition < 0) {
        newPosition = 0;
    }
    if (newPosition > ctx->hidden.androidio.size) {
        newPosition = ctx->hidden.androidio.size;
    }

    long movement = newPosition - ctx->hidden.androidio.position;
    jobject inputStream = (jobject)ctx->hidden.androidio.inputStreamRef;

    if (movement > 0) {
        unsigned char buffer[1024];

        // The easy case where we're seeking forwards
        while (movement > 0) {
            long amount = (long) sizeof (buffer);
            if (amount > movement) {
                amount = movement;
            }
            size_t result = Android_JNI_FileRead(ctx, buffer, 1, amount);

            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
        // forwards which obviously isn't very efficient
        Android_JNI_FileClose(ctx, false);
        Android_JNI_FileOpen(ctx);
        Android_JNI_FileSeek(ctx, newPosition, RW_SEEK_SET);
    }

    ctx->hidden.androidio.position = newPosition;

    return ctx->hidden.androidio.position;
}
Exemplo n.º 2
0
extern "C" Sint64 Android_JNI_FileSeek(SDL_RWops* ctx, Sint64 offset, int whence)
{
    Sint64 newPosition;

    switch (whence) {
        case RW_SEEK_SET:
            newPosition = offset;
            break;
        case RW_SEEK_CUR:
            newPosition = ctx->hidden.androidio.position + offset;
            break;
        case RW_SEEK_END:
            newPosition = ctx->hidden.androidio.size + offset;
            break;
        default:
            SDL_SetError("Unknown value for 'whence'");
            return -1;
    }

    /* Validate the new position */
    if (newPosition < 0) {
        SDL_Error(SDL_EFSEEK);
        return -1;
    }
    if (newPosition > ctx->hidden.androidio.size) {
        newPosition = ctx->hidden.androidio.size;
    }

    Sint64 movement = newPosition - ctx->hidden.androidio.position;
    if (movement > 0) {
        unsigned char buffer[4096];

        // The easy case where we're seeking forwards
        while (movement > 0) {
            Sint64 amount = sizeof (buffer);
            if (amount > movement) {
                amount = movement;
            }
            size_t result = Android_JNI_FileRead(ctx, buffer, 1, amount);
            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
        // forwards which obviously isn't very efficient
        Android_JNI_FileClose(ctx, false);
        Android_JNI_FileOpen(ctx);
        Android_JNI_FileSeek(ctx, newPosition, RW_SEEK_SET);
    }

    return ctx->hidden.androidio.position;
}