comparison mupdf-source/platform/java/jni/fitzinputstream.c @ 2:b50eed0cc0ef upstream

ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4. The directory name has changed: no version number in the expanded directory now.
author Franz Glasner <fzglas.hg@dom66.de>
date Mon, 15 Sep 2025 11:43:07 +0200
parents
children
comparison
equal deleted inserted replaced
1:1d09e1dec1d9 2:b50eed0cc0ef
1 // Copyright (C) 2004-2021 Artifex Software, Inc.
2 //
3 // This file is part of MuPDF.
4 //
5 // MuPDF is free software: you can redistribute it and/or modify it under the
6 // terms of the GNU Affero General Public License as published by the Free
7 // Software Foundation, either version 3 of the License, or (at your option)
8 // any later version.
9 //
10 // MuPDF is distributed in the hope that it will be useful, but WITHOUT ANY
11 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 // FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
13 // details.
14 //
15 // You should have received a copy of the GNU Affero General Public License
16 // along with MuPDF. If not, see <https://www.gnu.org/licenses/agpl-3.0.en.html>
17 //
18 // Alternative licensing terms are available from the licensor.
19 // For commercial licensing, see <https://www.artifex.com/> or contact
20 // Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
21 // CA 94129, USA, for further information.
22
23 /* FitzInputStream interface */
24
25 JNIEXPORT jboolean JNICALL
26 FUN(FitzInputStream_markSupported)(JNIEnv *env, jobject self)
27 {
28 fz_context *ctx = get_context(env);
29 fz_stream *stm = from_FitzInputStream_safe(env, self);
30 jboolean closed = JNI_TRUE;
31
32 if (!ctx || !stm) return JNI_FALSE;
33
34 closed = (*env)->GetBooleanField(env, self, fid_FitzInputStream_closed);
35 if (closed) jni_throw_uoe(env, "stream closed");
36
37 return stm->seek != NULL;
38 }
39
40 JNIEXPORT void JNICALL
41 FUN(FitzInputStream_mark)(JNIEnv *env, jobject self, jint readlimit)
42 {
43 fz_context *ctx = get_context(env);
44 fz_stream *stm = from_FitzInputStream_safe(env, self);
45 jlong markpos = 0;
46 jboolean closed = JNI_TRUE;
47
48 if (!ctx || !stm) return;
49 if (stm->seek == NULL) jni_throw_uoe_void(env, "mark not supported");
50
51 closed = (*env)->GetBooleanField(env, self, fid_FitzInputStream_closed);
52 if (closed) jni_throw_uoe_void(env, "stream closed");
53
54 fz_try(ctx)
55 markpos = fz_tell(ctx, stm);
56 fz_catch(ctx)
57 jni_rethrow_void(env, ctx);
58
59 (*env)->SetLongField(env, self, fid_FitzInputStream_markpos, markpos);
60 }
61
62 JNIEXPORT void JNICALL
63 FUN(FitzInputStream_reset)(JNIEnv *env, jobject self)
64 {
65 fz_context *ctx = get_context(env);
66 fz_stream *stm = from_FitzInputStream_safe(env, self);
67 jboolean closed = JNI_TRUE;
68 jlong markpos = -1;
69
70 if (!ctx || !stm) return;
71
72 if (stm->seek == NULL) jni_throw_uoe_void(env, "reset not supported");
73 closed = (*env)->GetBooleanField(env, self, fid_FitzInputStream_closed);
74 if (closed) jni_throw_uoe_void(env, "stream closed");
75
76 markpos = (*env)->GetLongField(env, self, fid_FitzInputStream_markpos);
77 if (markpos < 0) jni_throw_io_void(env, "mark not set");
78
79 fz_try(ctx)
80 fz_seek(ctx, stm, markpos, SEEK_SET);
81 fz_catch(ctx)
82 jni_rethrow_void(env, ctx);
83 }
84
85 JNIEXPORT jint JNICALL
86 FUN(FitzInputStream_available)(JNIEnv *env, jobject self)
87 {
88 fz_context *ctx = get_context(env);
89 fz_stream *stm = from_FitzInputStream_safe(env, self);
90 jint available = 0;
91 jboolean closed = JNI_TRUE;
92
93 if (!ctx || !stm) return -1;
94
95 closed = (*env)->GetBooleanField(env, self, fid_FitzInputStream_closed);
96 if (closed) jni_throw_uoe(env, "stream closed");
97
98 fz_try(ctx)
99 available = fz_available(ctx, stm, 1);
100 fz_catch(ctx)
101 jni_rethrow(env, ctx);
102
103 return available;
104 }
105
106 JNIEXPORT jint JNICALL
107 FUN(FitzInputStream_readByte)(JNIEnv *env, jobject self)
108 {
109 fz_context *ctx = get_context(env);
110 fz_stream *stm = from_FitzInputStream_safe(env, self);
111 jboolean closed = JNI_TRUE;
112 jbyte b = 0;
113
114 if (!ctx || !stm) return -1;
115
116 closed = (*env)->GetBooleanField(env, self, fid_FitzInputStream_closed);
117 if (closed) jni_throw_uoe(env, "stream closed");
118
119 fz_try(ctx)
120 b = fz_read_byte(ctx, stm);
121 fz_catch(ctx)
122 jni_rethrow(env, ctx);
123
124 return b;
125 }
126
127 JNIEXPORT jint JNICALL
128 FUN(FitzInputStream_readArray)(JNIEnv *env, jobject self, jobject jarr, jint off, jint len)
129 {
130 fz_context *ctx = get_context(env);
131 fz_stream *stm = from_FitzInputStream_safe(env, self);
132 jboolean closed = JNI_TRUE;
133 jbyte *arr = NULL;
134 jint n = 0;
135
136 if (!ctx || !stm) return -1;
137 if (!jarr) jni_throw_arg(env, "buffer must not be null");
138
139 closed = (*env)->GetBooleanField(env, self, fid_FitzInputStream_closed);
140 if (closed) jni_throw_uoe(env, "stream closed");
141
142 arr = (*env)->GetByteArrayElements(env, jarr, NULL);
143 if (!arr) jni_throw_arg(env, "cannot get buffer to read into");
144
145 fz_try(ctx)
146 n = fz_read(ctx, stm, (unsigned char *) arr + off, len);
147 fz_always(ctx)
148 (*env)->ReleaseByteArrayElements(env, jarr, arr, 0);
149 fz_catch(ctx)
150 jni_rethrow(env, ctx);
151
152 return n;
153 }
154
155 JNIEXPORT void JNICALL
156 FUN(FitzInputStream_finalize)(JNIEnv *env, jobject self)
157 {
158 fz_context *ctx = get_context(env);
159 fz_stream *stm = from_FitzInputStream_safe(env, self);
160 if (!ctx || !stm) return;
161 (*env)->SetLongField(env, self, fid_FitzInputStream_pointer, 0);
162 fz_drop_stream(ctx, stm);
163 }
164
165 JNIEXPORT void JNICALL
166 FUN(FitzInputStream_close)(JNIEnv *env, jobject self)
167 {
168 fz_context *ctx = get_context(env);
169 fz_stream *stm = from_FitzInputStream_safe(env, self);
170 jboolean closed = JNI_TRUE;
171
172 if (!ctx || !stm) return;
173
174 closed = (*env)->GetBooleanField(env, self, fid_FitzInputStream_closed);
175 if (closed) jni_throw_uoe_void(env, "stream closed");
176
177 (*env)->SetBooleanField(env, self, fid_FitzInputStream_closed, JNI_TRUE);
178 }