如何将结果文件输出为文件流的形式

用户使用过程中暂时不需要通过保存文件到指定路径,可能还需要对文件做一些处理,此时可能需要将文件输出为文件流的方式。要实现此功能,需要通过FileWriterCallback回调函数实现,示例代码如下:

class FileWriter extends FileWriterCallback {
    private ByteArrayOutputStream buf = new ByteArrayOutputStream();

    @Override
    public long getSize() {
        return buf.size();
    }

    @Override
    public boolean writeBlock(byte[] buffer, long offset, long size) {
        buf.write(buffer, (int) 0, (int) size);
        return true;
    }

    @Override
    public void release() {
    }

    @Override
    public boolean flush() {
        return true;
    }

    public ByteArrayOutputStream getStream() {
        return buf;
    }

}

业务代码如下:

FileWriter fileWriter = new FileWriter();
PDFDoc doc = new PDFDoc("AboutFoxit.pdf");
doc.load(null);
Progressive progressive = doc.startSaveAs(fileWriter, 0, null);
if (progressive.getRateOfProgress() != 100) {
    int state = Progressive.e_ToBeContinued;
    while (Progressive.e_ToBeContinued == state) {
        state = progressive.resume();
    }
}
ByteArrayOutputStream steam = fileWriter.getStream();
返回值steam,即为结果文件输出流。