标签:document tor exception exce UNC return dwr clu amp
// // Created by svenj on 2019/2/3. // extern "C" { #include <unistd.h> #include <sys/mman.h> #include <sys/stat.h> #include <string.h> #include <stdio.h> } #include <fpdfview.h> #include <fpdf_save.h> #include "util.hpp" struct PdfToFdWriter : FPDF_FILEWRITE { int dstFd; }; static 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; while (remainingBytes > 0) { ssize_t writtenByteCount = write(fd, writeBuffer, remainingBytes); if (writtenByteCount == -1) { if (errno == EINTR) { continue; } LOGE("Error writing to buffer: %d", errno); return false; } remainingBytes -= writtenByteCount; writeBuffer += writtenByteCount; } return true; } static int writeBlock(FPDF_FILEWRITE* owner, const void* buffer, unsigned long size) { const PdfToFdWriter* writer = reinterpret_cast<PdfToFdWriter*>(owner); const bool success = writeAllBytes(writer->dstFd, buffer, size); if (success < 0) { LOGE("Cannot write to file descriptor. Error:%d", errno); return 0; } return 1; } JNI_FUNC(jboolean, PdfWriter, nativeWrite)(JNI_ARGS, jlong docPtr, jint fd) { auto document = reinterpret_cast<FPDF_DOCUMENT>(docPtr); PdfToFdWriter writer; writer.dstFd = fd; writer.WriteBlock = &writeBlock; FPDF_BOOL success = FPDF_SaveAsCopy(document, &writer, FPDF_NO_INCREMENTAL); if (!success) { jniThrowExceptionFmt(env, "java/io/IOException", "cannot write to fd. Error: %d", errno); } }
标签:document tor exception exce UNC return dwr clu amp
原文地址:https://www.cnblogs.com/hshy/p/12014069.html