美文网首页
制作安卓PDF阅读器:七、再战PDF保存

制作安卓PDF阅读器:七、再战PDF保存

作者: 天下第九九八十一 | 来源:发表于2020-11-26 19:20 被阅读0次

对底层的保存机制不大清楚。不过发现了一个“BUG”,原位保存特定文件的的时候,保存后出现乱码,但若测试“另存为”其他文件,一切正常。这个“BUG”说明,PDFium在保存文件的时候,可能发生边读边写的情况,一旦写指针超过读指针,就会发生未知异常。所以,需要保证“写”落后于“读”,也就是使用Java中BufferedOutputStream的思想。

但是Jni怎么调用BufferedOutputStream?在jni层,写入文件是直接通过write函数完成的。所以只需为write函数包上一层Buffered方法即可,无需Jni call来call去。

参照 BufferedOutputStream.java,很简单地实现如下:

BufferedBlockWriter.h

#ifndef _BBW_
#define _BBW_
#include <fpdf_save.h>

// Start Save PDF
struct PdfToFdWriter : FPDF_FILEWRITE {
    int dstFd;
};

void flushBuffer(int fd);

bool writeAllBytes(const int fd, const void *buffer, const size_t byteCount);

int writeBlock(FPDF_FILEWRITE* owner, const void* buffer, unsigned long size);

int writeBlockBuffered(FPDF_FILEWRITE* owner, const void* buffer, unsigned long size);

void startBufferedWriting(size_t buffer_size);

#endif

BufferedBlockWriter.cpp

#include "util.hpp"

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

#include "BufferedBlockWriter.h"
}

#include <android/native_window.h>
#include <android/native_window_jni.h>
#include <android/bitmap.h>
#include <utils/Mutex.h>
using namespace android;

#include <fpdfview.h>
#include <fpdf_doc.h>
#include <fpdf_text.h>
#include <fpdf_annot.h>
#include <fpdf_save.h>
#include <string>
#include <vector>

#include "BufferedBlockWriter.h"

bool writeAllBytes(const int fd, const void *buffer, const size_t byteCount) {
    char *writeBuffer = static_cast<char *>(const_cast<void *>(buffer));
    size_t remainingBytes = byteCount;
    LOGE("fatal writeAllBytes: %d %d %d", buffer, byteCount, remainingBytes);
    while (remainingBytes > 0) {
        ssize_t writtenByteCount = write(fd, writeBuffer, remainingBytes);
        if (writtenByteCount == -1) {
            if (errno == EINTR) {
                continue;
            }
            LOGE("fatal Error writing to buffer: %d", errno);
            return true;
        }
        remainingBytes -= writtenByteCount;
        writeBuffer += writtenByteCount;
    }
    return true;
}

int writeBlock(FPDF_FILEWRITE* owner, const void* buffer, unsigned long size) {
    LOGE("fatal writeBlock: %d %d %d", buffer, size, owner);
    const PdfToFdWriter* writer = reinterpret_cast<PdfToFdWriter*>(owner);
    const bool success = writeAllBytes(writer->dstFd, buffer, size);
    if (!success) {
        LOGE("fatal Cannot write to file descriptor. Error:%d", errno);
        return 0;
    }
    return 1;
}

// 以下实现 buffered 方法

static char* buf = new char[1024*1024*4];

static size_t buf_length = 1024*1024*4;

static size_t count;

int error = 0;

void flushBuffer(int fd) {
    LOGE("fatal flushBuffer count=%d  buf_length=%d", count, buf_length);
    if (count > 0) {
        if(!writeAllBytes(fd, buf, count)) {
            error = 1;
        }
        count = 0;
    }
}

bool writeAllBytesBuffered(const int fd, const void *buffer, const size_t len) {
    if (len >= buf_length) {
        /* If the request length exceeds the size of the output buffer,
        flush the output buffer and then write the data directly.
        In this way buffered streams will cascade harmlessly. */
        flushBuffer(fd);
        return writeAllBytes(fd, buffer, len);
    }
    if (len > buf_length - count) {
        flushBuffer(fd);
    }
    if(error) {
        return false;
    }
    memcpy(buf+count, buffer, len);
    count += len;
    return true;
}

int writeBlockBuffered(FPDF_FILEWRITE* owner, const void* buffer, unsigned long size) {
    LOGE("fatal writeBlock: %d %d %d", buffer, size, owner);
    const PdfToFdWriter* writer = reinterpret_cast<PdfToFdWriter*>(owner);
    const bool success = writeAllBytesBuffered(writer->dstFd, buffer, size);
    if (!success) {
        LOGE("fatal Cannot write to file descriptor. Error:%d", errno);
        return 0;
    }
    //flushBuffer(writer->dstFd);
    return 1;
}


void startBufferedWriting(size_t buffer_size) {
    count = 0;
}

使用(PDFium实现原位保存):

JNI_FUNC(void, PdfiumCore, nativeSaveAsCopy)(JNI_ARGS, jlong docPtr, jint fd, jboolean incremental) {
    DocumentFile* docFile = (DocumentFile*)docPtr;
    PdfToFdWriter writer;
    writer.dstFd = fd;
    //writer.dstFd = docFile->fileFd;
    if(false) {
        writer.WriteBlock = &writeBlock;
    } else {
        writer.WriteBlock = &writeBlockBuffered;
        startBufferedWriting(0);
    }
    FPDF_BOOL success = FPDF_SaveAsCopy(docFile->pdfDocument, &writer, incremental?FPDF_INCREMENTAL:FPDF_NO_INCREMENTAL); // FPDF_INCREMENTAL FPDF_NO_INCREMENTAL
    if (!success) {
        jniThrowExceptionFmt(env, "java/io/IOException", "cannot write to fd. Error: %d", errno);
    } else {
        flushBuffer(fd);
    }
}

附:出现“BUG”的文件

https://comserv.cs.ut.ee/ati_thesis/datasheet.php?id=61865&year=2018&language=en

BUG 图示

事实上不能完全解决问题。打印发现,很多情况(比如Gpu pro 1)下PDFium保存末端文件块的时候需要读取偏移为17、大小为512字节、已经被覆盖了的文件块。 另一些情况(比如Gpu pro 4)则会读取偏移更大的已覆写文件块,毫无规律。所以,最终还是通过将PDF文件整个加载进内存解决问题。不过,这个 BufferedWriter 写入方法还是可以保留的。

相关文章

网友评论

      本文标题:制作安卓PDF阅读器:七、再战PDF保存

      本文链接:https://www.haomeiwen.com/subject/aseziktx.html