From 8b6cc5f00e4d3f1a43e677a43a2a809f6132a196 Mon Sep 17 00:00:00 2001 From: Sebastian Rasmussen Date: Sun, 17 Jul 2016 16:50:37 +0800 Subject: JNI: Add BufferInputStream, BufferOutputStream. These are meant to make it easy to interact with mupdf's Buffer object in a normal Java fashion. --- platform/java/mupdf_native.h | 24 ++++++++ .../com/artifex/mupdf/fitz/BufferInputStream.java | 55 ++++++++++++++++++ .../com/artifex/mupdf/fitz/BufferOutputStream.java | 66 ++++++++++++++++++++++ 3 files changed, 145 insertions(+) create mode 100644 platform/java/src/com/artifex/mupdf/fitz/BufferInputStream.java create mode 100644 platform/java/src/com/artifex/mupdf/fitz/BufferOutputStream.java (limited to 'platform/java') diff --git a/platform/java/mupdf_native.h b/platform/java/mupdf_native.h index e3902a21..3756eae9 100644 --- a/platform/java/mupdf_native.h +++ b/platform/java/mupdf_native.h @@ -180,6 +180,30 @@ JNIEXPORT void JNICALL Java_com_artifex_mupdf_fitz_Buffer_writeLines JNIEXPORT void JNICALL Java_com_artifex_mupdf_fitz_Buffer_save (JNIEnv *, jobject, jstring); +#ifdef __cplusplus +} +#endif +#endif +/* Header for class com_artifex_mupdf_fitz_BufferInputStream */ + +#ifndef _Included_com_artifex_mupdf_fitz_BufferInputStream +#define _Included_com_artifex_mupdf_fitz_BufferInputStream +#ifdef __cplusplus +extern "C" { +#endif +#undef com_artifex_mupdf_fitz_BufferInputStream_MAX_SKIP_BUFFER_SIZE +#define com_artifex_mupdf_fitz_BufferInputStream_MAX_SKIP_BUFFER_SIZE 2048L +#ifdef __cplusplus +} +#endif +#endif +/* Header for class com_artifex_mupdf_fitz_BufferOutputStream */ + +#ifndef _Included_com_artifex_mupdf_fitz_BufferOutputStream +#define _Included_com_artifex_mupdf_fitz_BufferOutputStream +#ifdef __cplusplus +extern "C" { +#endif #ifdef __cplusplus } #endif diff --git a/platform/java/src/com/artifex/mupdf/fitz/BufferInputStream.java b/platform/java/src/com/artifex/mupdf/fitz/BufferInputStream.java new file mode 100644 index 00000000..6a03dc00 --- /dev/null +++ b/platform/java/src/com/artifex/mupdf/fitz/BufferInputStream.java @@ -0,0 +1,55 @@ +package com.artifex.mupdf.fitz; + +import java.io.IOException; +import java.io.InputStream; + +public class BufferInputStream extends InputStream +{ + protected Buffer buffer; + protected int position; + protected int resetPosition; + + public BufferInputStream(Buffer buffer) { + super(); + this.buffer = buffer; + this.position = 0; + this.resetPosition = -1; + } + + public int available() { + return buffer.getLength(); + } + + public void mark(int readlimit) { + resetPosition = position; + } + + public boolean markSupported() { + return true; + } + + public int read() { + return buffer.readByte(position++); + } + + public int read(byte[] b) { + int n = buffer.readBytes(position, b); + position += n; + return n; + } + + public int read(byte[] b, int off, int len) { + int n = buffer.readBytesInto(position, b, off, len); + position += n; + return n; + } + + public void reset() throws IOException { + if (resetPosition < 0) + throw new IOException("cannot reset because mark never set"); + if (resetPosition >= buffer.getLength()) + throw new IOException("cannot reset because mark set outside of buffer"); + + position = resetPosition; + } +} diff --git a/platform/java/src/com/artifex/mupdf/fitz/BufferOutputStream.java b/platform/java/src/com/artifex/mupdf/fitz/BufferOutputStream.java new file mode 100644 index 00000000..ec87db0c --- /dev/null +++ b/platform/java/src/com/artifex/mupdf/fitz/BufferOutputStream.java @@ -0,0 +1,66 @@ +package com.artifex.mupdf.fitz; + +import java.io.IOException; +import java.io.OutputStream; + +public class BufferOutputStream extends OutputStream +{ + protected Buffer buffer; + protected int position; + protected int resetPosition; + + public BufferOutputStream(Buffer buffer) { + super(); + this.buffer = buffer; + this.position = 0; + } + + public void write(byte[] b) { + buffer.writeBytes(b); + } + + public void write(byte[] b, int off, int len) { + buffer.writeBytesFrom(b, off, len); + } + + public void write(int b) { + buffer.writeByte((byte) b); + } + + public int available() { + return buffer.getLength(); + } + + public void mark(int readlimit) { + resetPosition = position; + } + + public boolean markSupported() { + return true; + } + + public int read() { + return buffer.readByte(position++); + } + + public int read(byte[] b) { + int n = buffer.readBytes(position, b); + position += n; + return n; + } + + public int read(byte[] b, int off, int len) { + int n = buffer.readBytesInto(position, b, off, len); + position += n; + return n; + } + + public void reset() throws IOException { + if (resetPosition < 0) + throw new IOException("cannot reset because mark never set"); + if (resetPosition >= buffer.getLength()) + throw new IOException("cannot reset because mark set outside of buffer"); + + position = resetPosition; + } +} -- cgit v1.2.3