From 5f90cd61cb22c752217dc5754eef7a0ea4c4a175 Mon Sep 17 00:00:00 2001 From: Tor Andersson Date: Sat, 27 Jan 2018 00:34:30 +0100 Subject: jni: Add Java interfaces to fz_stream and fz_output types. This will allow us to read and write documents using I/O written in Java, exposed by SeekableInputStream and SeekableOutputStream. We supply an example FileStream which implements seekable streams backed by a RandomAccessFile. --- .../java/src/com/artifex/mupdf/fitz/Document.java | 5 +++ .../src/com/artifex/mupdf/fitz/FileStream.java | 43 ++++++++++++++++++++++ .../src/com/artifex/mupdf/fitz/PDFDocument.java | 6 +++ .../artifex/mupdf/fitz/SeekableInputStream.java | 9 +++++ .../artifex/mupdf/fitz/SeekableOutputStream.java | 9 +++++ .../src/com/artifex/mupdf/fitz/SeekableStream.java | 14 +++++++ 6 files changed, 86 insertions(+) create mode 100644 platform/java/src/com/artifex/mupdf/fitz/FileStream.java create mode 100644 platform/java/src/com/artifex/mupdf/fitz/SeekableInputStream.java create mode 100644 platform/java/src/com/artifex/mupdf/fitz/SeekableOutputStream.java create mode 100644 platform/java/src/com/artifex/mupdf/fitz/SeekableStream.java (limited to 'platform/java/src/com/artifex') diff --git a/platform/java/src/com/artifex/mupdf/fitz/Document.java b/platform/java/src/com/artifex/mupdf/fitz/Document.java index 9b512c89..0da94d87 100644 --- a/platform/java/src/com/artifex/mupdf/fitz/Document.java +++ b/platform/java/src/com/artifex/mupdf/fitz/Document.java @@ -27,6 +27,7 @@ public class Document protected native static Document openNativeWithPath(String filename); protected native static Document openNativeWithBuffer(byte buffer[], String magic); + protected native static Document openNativeWithStream(SeekableInputStream stream, String mimeType); public static Document openDocument(String filename) { Document doc = openNativeWithPath(filename); @@ -38,6 +39,10 @@ public class Document return openNativeWithBuffer(buffer, magic); } + public static Document openDocument(SeekableInputStream stream, String magic) { + return openNativeWithStream(stream, magic); + } + public static native boolean recognize(String magic); public native boolean needsPassword(); diff --git a/platform/java/src/com/artifex/mupdf/fitz/FileStream.java b/platform/java/src/com/artifex/mupdf/fitz/FileStream.java new file mode 100644 index 00000000..3d9ad0d3 --- /dev/null +++ b/platform/java/src/com/artifex/mupdf/fitz/FileStream.java @@ -0,0 +1,43 @@ +package com.artifex.mupdf.fitz; + +import java.io.File; +import java.io.IOException; +import java.io.RandomAccessFile; + +public class FileStream implements SeekableInputStream, SeekableOutputStream +{ + protected RandomAccessFile file; + + public FileStream(String path, String mode) throws IOException { + file = new RandomAccessFile(path, mode); + } + + public FileStream(File path, String mode) throws IOException { + file = new RandomAccessFile(path, mode); + } + + public int read(byte[] buf) throws IOException { + return file.read(buf); + } + + public void write(byte[] buf, int off, int len) throws IOException { + file.write(buf, off, len); + } + + public long seek(long offset, int whence) throws IOException { + switch (whence) { + case SEEK_SET: file.seek(offset); break; + case SEEK_CUR: file.seek(file.getFilePointer() + offset); break; + case SEEK_END: file.seek(file.length() + offset); break; + } + return file.getFilePointer(); + } + + public long position() throws IOException { + return file.getFilePointer(); + } + + public void close() throws IOException { + file.close(); + } +} diff --git a/platform/java/src/com/artifex/mupdf/fitz/PDFDocument.java b/platform/java/src/com/artifex/mupdf/fitz/PDFDocument.java index 68835aaa..945294f3 100644 --- a/platform/java/src/com/artifex/mupdf/fitz/PDFDocument.java +++ b/platform/java/src/com/artifex/mupdf/fitz/PDFDocument.java @@ -100,5 +100,11 @@ public class PDFDocument extends Document public native PDFObject addFont(Font font); public native boolean hasUnsavedChanges(); public native boolean canBeSavedIncrementally(); + public native int save(String filename, String options); + + protected native int nativeSaveWithStream(SeekableOutputStream stream, String options); + public int save(SeekableOutputStream stream, String options) { + return nativeSaveWithStream(stream, options); + } } diff --git a/platform/java/src/com/artifex/mupdf/fitz/SeekableInputStream.java b/platform/java/src/com/artifex/mupdf/fitz/SeekableInputStream.java new file mode 100644 index 00000000..74b1a185 --- /dev/null +++ b/platform/java/src/com/artifex/mupdf/fitz/SeekableInputStream.java @@ -0,0 +1,9 @@ +package com.artifex.mupdf.fitz; + +import java.io.IOException; + +public interface SeekableInputStream extends SeekableStream +{ + /* Behaves like java.io.InputStream.read */ + int read(byte[] b) throws IOException; +} diff --git a/platform/java/src/com/artifex/mupdf/fitz/SeekableOutputStream.java b/platform/java/src/com/artifex/mupdf/fitz/SeekableOutputStream.java new file mode 100644 index 00000000..d22ba996 --- /dev/null +++ b/platform/java/src/com/artifex/mupdf/fitz/SeekableOutputStream.java @@ -0,0 +1,9 @@ +package com.artifex.mupdf.fitz; + +import java.io.IOException; + +public interface SeekableOutputStream extends SeekableStream +{ + /* Behaves like java.io.OutputStream.write */ + void write(byte[] b, int off, int len) throws IOException; +} diff --git a/platform/java/src/com/artifex/mupdf/fitz/SeekableStream.java b/platform/java/src/com/artifex/mupdf/fitz/SeekableStream.java new file mode 100644 index 00000000..83f79ea2 --- /dev/null +++ b/platform/java/src/com/artifex/mupdf/fitz/SeekableStream.java @@ -0,0 +1,14 @@ +package com.artifex.mupdf.fitz; + +import java.io.IOException; + +public interface SeekableStream +{ + int SEEK_SET = 0; /* set file position to offset */ + int SEEK_CUR = 1; /* set file position to current location plus offset */ + int SEEK_END = 2; /* set file position to EOF plus offset */ + + long seek(long offset, int whence) throws IOException; + long position() throws IOException; + void close() throws IOException; +} -- cgit v1.2.3