@@ -8,31 +8,29 @@ use crate::{Metadata, OfficeParserConfig, PdfParserConfig, TesseractOcrConfig, D
88use bytemuck:: cast_slice_mut;
99use jni:: objects:: { GlobalRef , JByteArray , JObject , JValue } ;
1010use jni:: sys:: jsize;
11- use jni:: JNIEnv ;
11+ use jni:: { AttachGuard , JNIEnv } ;
1212
1313/// Wrapper for [`JObject`]s that contain `org.apache.commons.io.input.ReaderInputStream`
1414/// It saves a GlobalRef to the java object, which is cleared when the last GlobalRef is dropped
1515/// Implements [`Drop] trait to properly close the `org.apache.commons.io.input.ReaderInputStream`
16- # [ derive ( Clone ) ]
17- pub struct JReaderInputStream {
16+ pub struct JReaderInputStream < ' local > {
17+ _guard : AttachGuard < ' local > ,
1818 internal : GlobalRef ,
1919 buffer : GlobalRef ,
2020 capacity : jsize ,
2121}
2222
23- impl JReaderInputStream {
24- pub ( crate ) fn new < ' local > (
25- env : & mut JNIEnv < ' local > ,
26- obj : JObject < ' local > ,
27- ) -> ExtractResult < Self > {
23+ impl < ' local > JReaderInputStream < ' local > {
24+ pub ( crate ) fn new ( guard : AttachGuard < ' local > , obj : JObject < ' local > ) -> ExtractResult < Self > {
2825 // Creates new jbyte array
2926 let capacity = DEFAULT_BUF_SIZE as jsize ;
30- let jbyte_array = env . new_byte_array ( capacity) ?;
27+ let jbyte_array = guard . new_byte_array ( capacity) ?;
3128
3229 Ok ( Self {
33- internal : env . new_global_ref ( obj) ?,
34- buffer : env . new_global_ref ( jbyte_array) ?,
30+ internal : guard . new_global_ref ( obj) ?,
31+ buffer : guard . new_global_ref ( jbyte_array) ?,
3532 capacity,
33+ _guard : guard,
3634 } )
3735 }
3836
@@ -96,7 +94,7 @@ impl JReaderInputStream {
9694 }
9795}
9896
99- impl Drop for JReaderInputStream {
97+ impl Drop for JReaderInputStream < ' _ > {
10098 fn drop ( & mut self ) {
10199 if let Ok ( mut env) = vm ( ) . attach_current_thread ( ) {
102100 // Call the Java Reader's `close` method
0 commit comments