fitzinputstream.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. /* FitzInputStream interface */
  23. JNIEXPORT jboolean JNICALL
  24. FUN(FitzInputStream_markSupported)(JNIEnv *env, jobject self)
  25. {
  26. fz_context *ctx = get_context(env);
  27. fz_stream *stm = from_FitzInputStream_safe(env, self);
  28. jboolean closed = JNI_TRUE;
  29. if (!ctx || !stm) return JNI_FALSE;
  30. closed = (*env)->GetBooleanField(env, self, fid_FitzInputStream_closed);
  31. if (closed) jni_throw_uoe(env, "stream closed");
  32. return stm->seek != NULL;
  33. }
  34. JNIEXPORT void JNICALL
  35. FUN(FitzInputStream_mark)(JNIEnv *env, jobject self, jint readlimit)
  36. {
  37. fz_context *ctx = get_context(env);
  38. fz_stream *stm = from_FitzInputStream_safe(env, self);
  39. jlong markpos = 0;
  40. jboolean closed = JNI_TRUE;
  41. if (!ctx || !stm) return;
  42. if (stm->seek == NULL) jni_throw_uoe_void(env, "mark not supported");
  43. closed = (*env)->GetBooleanField(env, self, fid_FitzInputStream_closed);
  44. if (closed) jni_throw_uoe_void(env, "stream closed");
  45. fz_try(ctx)
  46. markpos = fz_tell(ctx, stm);
  47. fz_catch(ctx)
  48. jni_rethrow_void(env, ctx);
  49. (*env)->SetLongField(env, self, fid_FitzInputStream_markpos, markpos);
  50. }
  51. JNIEXPORT void JNICALL
  52. FUN(FitzInputStream_reset)(JNIEnv *env, jobject self)
  53. {
  54. fz_context *ctx = get_context(env);
  55. fz_stream *stm = from_FitzInputStream_safe(env, self);
  56. jboolean closed = JNI_TRUE;
  57. jlong markpos = -1;
  58. if (!ctx || !stm) return;
  59. if (stm->seek == NULL) jni_throw_uoe_void(env, "reset not supported");
  60. closed = (*env)->GetBooleanField(env, self, fid_FitzInputStream_closed);
  61. if (closed) jni_throw_uoe_void(env, "stream closed");
  62. markpos = (*env)->GetLongField(env, self, fid_FitzInputStream_markpos);
  63. if (markpos < 0) jni_throw_io_void(env, "mark not set");
  64. fz_try(ctx)
  65. fz_seek(ctx, stm, markpos, SEEK_SET);
  66. fz_catch(ctx)
  67. jni_rethrow_void(env, ctx);
  68. }
  69. JNIEXPORT jint JNICALL
  70. FUN(FitzInputStream_available)(JNIEnv *env, jobject self)
  71. {
  72. fz_context *ctx = get_context(env);
  73. fz_stream *stm = from_FitzInputStream_safe(env, self);
  74. jint available = 0;
  75. jboolean closed = JNI_TRUE;
  76. if (!ctx || !stm) return -1;
  77. closed = (*env)->GetBooleanField(env, self, fid_FitzInputStream_closed);
  78. if (closed) jni_throw_uoe(env, "stream closed");
  79. fz_try(ctx)
  80. available = fz_available(ctx, stm, 1);
  81. fz_catch(ctx)
  82. jni_rethrow(env, ctx);
  83. return available;
  84. }
  85. JNIEXPORT jint JNICALL
  86. FUN(FitzInputStream_readByte)(JNIEnv *env, jobject self)
  87. {
  88. fz_context *ctx = get_context(env);
  89. fz_stream *stm = from_FitzInputStream_safe(env, self);
  90. jboolean closed = JNI_TRUE;
  91. jbyte b = 0;
  92. if (!ctx || !stm) return -1;
  93. closed = (*env)->GetBooleanField(env, self, fid_FitzInputStream_closed);
  94. if (closed) jni_throw_uoe(env, "stream closed");
  95. fz_try(ctx)
  96. b = fz_read_byte(ctx, stm);
  97. fz_catch(ctx)
  98. jni_rethrow(env, ctx);
  99. return b;
  100. }
  101. JNIEXPORT jint JNICALL
  102. FUN(FitzInputStream_readArray)(JNIEnv *env, jobject self, jobject jarr, jint off, jint len)
  103. {
  104. fz_context *ctx = get_context(env);
  105. fz_stream *stm = from_FitzInputStream_safe(env, self);
  106. jboolean closed = JNI_TRUE;
  107. jbyte *arr = NULL;
  108. jint n = 0;
  109. if (!ctx || !stm) return -1;
  110. if (!jarr) jni_throw_arg(env, "buffer must not be null");
  111. closed = (*env)->GetBooleanField(env, self, fid_FitzInputStream_closed);
  112. if (closed) jni_throw_uoe(env, "stream closed");
  113. arr = (*env)->GetByteArrayElements(env, jarr, NULL);
  114. if (!arr) jni_throw_arg(env, "cannot get buffer to read into");
  115. fz_try(ctx)
  116. n = fz_read(ctx, stm, (unsigned char *) arr + off, len);
  117. fz_always(ctx)
  118. (*env)->ReleaseByteArrayElements(env, jarr, arr, 0);
  119. fz_catch(ctx)
  120. jni_rethrow(env, ctx);
  121. return n;
  122. }
  123. JNIEXPORT void JNICALL
  124. FUN(FitzInputStream_finalize)(JNIEnv *env, jobject self)
  125. {
  126. fz_context *ctx = get_context(env);
  127. fz_stream *stm = from_FitzInputStream_safe(env, self);
  128. if (!ctx || !stm) return;
  129. (*env)->SetLongField(env, self, fid_FitzInputStream_pointer, 0);
  130. fz_drop_stream(ctx, stm);
  131. }
  132. JNIEXPORT void JNICALL
  133. FUN(FitzInputStream_close)(JNIEnv *env, jobject self)
  134. {
  135. fz_context *ctx = get_context(env);
  136. fz_stream *stm = from_FitzInputStream_safe(env, self);
  137. jboolean closed = JNI_TRUE;
  138. if (!ctx || !stm) return;
  139. closed = (*env)->GetBooleanField(env, self, fid_FitzInputStream_closed);
  140. if (closed) jni_throw_uoe_void(env, "stream closed");
  141. (*env)->SetBooleanField(env, self, fid_FitzInputStream_closed, JNI_TRUE);
  142. }