<commit_before>#include <jni.h>


#include <android/bitmap.h>
#include <android/log.h>
#include <utils/Mutex.h>

extern "C" {
    #include <stdlib.h>
    #include <unistd.h>
    #include <sys/mman.h>
    #include <sys/stat.h>
    #include <string.h>
}

#define LOG_TAG "pdfRenderer"
#define LOGI(...)   __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
#define LOGE(...)   __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
#define LOGD(...)   __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)

#include <fpdfview.h>
#include <fsdk_rendercontext.h>

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdelete-non-virtual-dtor"
#pragma GCC diagnostic pop

#define ll_jniThrowException(env,clazz,msg) \
  jclass clsj = env->FindClass("L#clazz;");\
  if (clsj!=NULL) {\
    env->ThrowNew(clsj, msg);\
    env->DeleteLocalRef(clsj);\
  }

using namespace android;

#ifdef __cplusplus
extern "C"
{
#endif

static const int RENDER_MODE_FOR_DISPLAY = 1;
static const int RENDER_MODE_FOR_PRINT = 2;

static struct {
    jfieldID x;
    jfieldID y;
} gPointClassInfo;

static Mutex sLibraryLock;

static int sLibraryReferenceCount = 0;

static void initializeLibraryIfNeeded(){
    Mutex::Autolock lock(sLibraryLock);
    if(sLibraryReferenceCount == 0){
        LOGD("Init FPDF library");
        FPDF_InitLibrary(NULL);
    }
    sLibraryReferenceCount++;
}

static void destroyLibraryIfNeeded(){
    Mutex::Autolock lock(sLibraryLock);
    sLibraryReferenceCount--;
    if(sLibraryReferenceCount == 0){
        LOGD("Destroy FPDF library");
        FPDF_DestroyLibrary();
    }
}

static int getBlock(void* param, unsigned long position, unsigned char* outBuffer,
        unsigned long size) {
    const int fd = reinterpret_cast<intptr_t>(param);
    const int readCount = pread(fd, outBuffer, size, position);
    if (readCount < 0) {
        LOGE("Cannot read from file descriptor. Error:%d", errno);
        return 0;
    }
    return 1;
}

inline long getFileSize(int fd){
    struct stat file_state;
    if(fstat(fd, &file_state) >= 0){
        return (long)(file_state.st_size);
    }else{
        LOGE("Error getting file size");
        return 0;
    }
}
static void DropContext(void* data) {
    delete (CRenderContext*) data;
}

JNIEXPORT jlong JNICALL Java_tv_loilo_pdfium_PdfRenderer_nativeCreate
(JNIEnv *env, jobject thiz, jint fd, jlong size) {
    initializeLibraryIfNeeded();

    FPDF_FILEACCESS loader;
    loader.m_FileLen = size;
    loader.m_Param = reinterpret_cast<void *>(intptr_t(fd));
    loader.m_GetBlock = &getBlock;

    FPDF_DOCUMENT document = FPDF_LoadCustomDocument(&loader, NULL);

    if (!document) {
        const long error = FPDF_GetLastError();
        switch (error) {
            case FPDF_ERR_PASSWORD:
            case FPDF_ERR_SECURITY: {
                ll_jniThrowException(env, "java/lang/SecurityException",
                                     "cannot create document. Error:");// %ld, error);
                return -1;
            }
                break;
            default: {
                ll_jniThrowException(env, "java/io/IOException",
                                     "cannot create document. Error:");// %ld", error);
                return -1;
            }
                break;
        }
        destroyLibraryIfNeeded();
        return -1;
    }

    return reinterpret_cast<jlong>(document);
}

JNIEXPORT jlong JNICALL Java_tv_loilo_pdfium_PdfRenderer_nativeOpenPageAndGetSize
(JNIEnv *env, jobject thiz, jlong documentPtr, jint pageIndex, jdoubleArray outSize) {
    FPDF_DOCUMENT document = reinterpret_cast<FPDF_DOCUMENT>(documentPtr);

    FPDF_PAGE page = FPDF_LoadPage(document, pageIndex);
<commit_msg>eliminate dead codes<commit_after>#include <jni.h>


#include <android/bitmap.h>
#include <android/log.h>
#include <utils/Mutex.h>

extern "C" {
    #include <stdlib.h>
    #include <unistd.h>
    #include <sys/mman.h>
    #include <sys/stat.h>
    #include <string.h>
}

#define LOG_TAG "pdfRenderer"
#define LOGI(...)   __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
#define LOGE(...)   __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
#define LOGD(...)   __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)

#include <fpdfview.h>
#include <fsdk_rendercontext.h>

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdelete-non-virtual-dtor"
#pragma GCC diagnostic pop

#define ll_jniThrowException(env,clazz,msg) \
  jclass clsj = env->FindClass("L#clazz;");\
  if (clsj!=NULL) {\
    env->ThrowNew(clsj, msg);\
    env->DeleteLocalRef(clsj);\
  }

using namespace android;

#ifdef __cplusplus
extern "C"
{
#endif

static const int RENDER_MODE_FOR_DISPLAY = 1;
static const int RENDER_MODE_FOR_PRINT = 2;

static Mutex sLibraryLock;

static int sLibraryReferenceCount = 0;

static void initializeLibraryIfNeeded(){
    Mutex::Autolock lock(sLibraryLock);
    if(sLibraryReferenceCount == 0){
        LOGD("Init FPDF library");
        FPDF_InitLibrary(NULL);
    }
    sLibraryReferenceCount++;
}

static void destroyLibraryIfNeeded(){
    Mutex::Autolock lock(sLibraryLock);
    sLibraryReferenceCount--;
    if(sLibraryReferenceCount == 0){
        LOGD("Destroy FPDF library");
        FPDF_DestroyLibrary();
    }
}

static int getBlock(void* param, unsigned long position, unsigned char* outBuffer,
        unsigned long size) {
    const int fd = reinterpret_cast<intptr_t>(param);
    const int readCount = pread(fd, outBuffer, size, position);
    if (readCount < 0) {
        LOGE("Cannot read from file descriptor. Error:%d", errno);
        return 0;
    }
    return 1;
}

static void DropContext(void* data) {
    delete (CRenderContext*) data;
}

JNIEXPORT jlong JNICALL Java_tv_loilo_pdfium_PdfRenderer_nativeCreate
(JNIEnv *env, jobject thiz, jint fd, jlong size) {
    initializeLibraryIfNeeded();

    FPDF_FILEACCESS loader;
    loader.m_FileLen = size;
    loader.m_Param = reinterpret_cast<void *>(intptr_t(fd));
    loader.m_GetBlock = &getBlock;

    FPDF_DOCUMENT document = FPDF_LoadCustomDocument(&loader, NULL);

    if (!document) {
        const long error = FPDF_GetLastError();
        switch (error) {
            case FPDF_ERR_PASSWORD:
            case FPDF_ERR_SECURITY: {
                ll_jniThrowException(env, "java/lang/SecurityException",
                                     "cannot create document. Error:");// %ld, error);
            }
                break;
            default: {
                ll_jniThrowException(env, "java/io/IOException",
                                     "cannot create document. Error:");// %ld", error);
            }
                break;
        }
        destroyLibraryIfNeeded();
        return -1;
    }

    return reinterpret_cast<jlong>(document);
}

JNIEXPORT jlong JNICALL Java_tv_loilo_pdfium_PdfRenderer_nativeOpenPageAndGetSize
(JNIEnv *env, jobject thiz, jlong documentPtr, jint pageIndex, jdoubleArray outSize) {
    FPDF_DOCUMENT document = reinterpret_cast<FPDF_DOCUMENT>(documentPtr);

    FPDF_PAGE page = FPDF_LoadPage(document, pageIndex);

{'fraction_non_alphanumeric': 0.11007882882882883, 'fraction_numerical': 0.003096846846846847, 'mean_word_length': 3.145274212368728, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 40, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3728562', 'n_tokens_mistral': 2385, 'n_tokens_neox': 2314, 'n_words': 520}