Skip to main content

rustjava_runtime/classes/java/io/
buffered_reader.rs

1use core::{cmp::min, future::Future};
2
3use alloc::{vec, vec::Vec};
4
5use jvm::{Array, ClassInstanceRef, JavaChar, Jvm, Result, runtime::JavaLangString};
6use jvm_class_proto::{JavaFieldProto, JavaMethodProto};
7use jvm_types::{ClassAccessFlags, FieldAccessFlags, MethodAccessFlags};
8
9use crate::{
10    RuntimeClassProto, RuntimeContext,
11    classes::java::{
12        io::Reader,
13        lang::{Object, String},
14    },
15};
16
17const DEFAULT_BUFFER_SIZE: usize = 8192;
18const INVALIDATED: i32 = -2;
19const UNMARKED: i32 = -1;
20
21// class java.io.BufferedReader
22pub struct BufferedReader;
23
24impl BufferedReader {
25    pub fn as_proto() -> RuntimeClassProto {
26        RuntimeClassProto {
27            name: "java/io/BufferedReader",
28            parent_class: Some("java/io/Reader"),
29            interfaces: vec![],
30            methods: vec![
31                JavaMethodProto::new("<init>", "(Ljava/io/Reader;)V", Self::init, MethodAccessFlags::PUBLIC),
32                JavaMethodProto::new("<init>", "(Ljava/io/Reader;I)V", Self::init_with_size, MethodAccessFlags::PUBLIC),
33                JavaMethodProto::new("read", "()I", Self::read_char, MethodAccessFlags::PUBLIC),
34                JavaMethodProto::new("read", "([CII)I", Self::read, MethodAccessFlags::PUBLIC),
35                JavaMethodProto::new("readLine", "()Ljava/lang/String;", Self::read_line, MethodAccessFlags::PUBLIC),
36                JavaMethodProto::new("skip", "(J)J", Self::skip, MethodAccessFlags::PUBLIC),
37                JavaMethodProto::new("ready", "()Z", Self::ready, MethodAccessFlags::PUBLIC),
38                JavaMethodProto::new("markSupported", "()Z", Self::mark_supported, MethodAccessFlags::PUBLIC),
39                JavaMethodProto::new("mark", "(I)V", Self::mark, MethodAccessFlags::PUBLIC),
40                JavaMethodProto::new("reset", "()V", Self::reset, MethodAccessFlags::PUBLIC),
41                JavaMethodProto::new("close", "()V", Self::close, MethodAccessFlags::PUBLIC),
42            ],
43            fields: vec![
44                JavaFieldProto::new("in", "Ljava/io/Reader;", FieldAccessFlags::PRIVATE),
45                JavaFieldProto::new("cb", "[C", FieldAccessFlags::PRIVATE),
46                JavaFieldProto::new("nChars", "I", FieldAccessFlags::PRIVATE),
47                JavaFieldProto::new("nextChar", "I", FieldAccessFlags::PRIVATE),
48                JavaFieldProto::new("markedChar", "I", FieldAccessFlags::PRIVATE),
49                JavaFieldProto::new("readAheadLimit", "I", FieldAccessFlags::PRIVATE),
50                JavaFieldProto::new("skipLF", "Z", FieldAccessFlags::PRIVATE),
51                JavaFieldProto::new("markedSkipLF", "Z", FieldAccessFlags::PRIVATE),
52            ],
53            access_flags: ClassAccessFlags::PUBLIC,
54        }
55    }
56
57    async fn init(jvm: &Jvm, _: &mut RuntimeContext, this: ClassInstanceRef<Self>, r#in: ClassInstanceRef<Reader>) -> Result<()> {
58        tracing::debug!("java.io.BufferedReader::<init>({this:?}, {in:?})", in = &r#in);
59
60        jvm.invoke_special(
61            &this,
62            "java/io/BufferedReader",
63            "<init>",
64            "(Ljava/io/Reader;I)V",
65            (r#in, DEFAULT_BUFFER_SIZE as i32),
66        )
67        .await
68    }
69
70    async fn init_with_size(
71        jvm: &Jvm,
72        _: &mut RuntimeContext,
73        mut this: ClassInstanceRef<Self>,
74        r#in: ClassInstanceRef<Reader>,
75        size: i32,
76    ) -> Result<()> {
77        tracing::debug!("java.io.BufferedReader::<init>({this:?}, {in:?}, {size})", in = &r#in);
78
79        if r#in.is_null() {
80            return Err(jvm.exception("java/lang/NullPointerException", "reader is null").await);
81        }
82        if size <= 0 {
83            return Err(jvm.exception("java/lang/IllegalArgumentException", "buffer size is not positive").await);
84        }
85
86        let _: () = jvm
87            .invoke_special(&this, "java/io/Reader", "<init>", "(Ljava/lang/Object;)V", (r#in.clone(),))
88            .await?;
89
90        let cb = jvm.instantiate_array("C", size as usize).await?;
91        jvm.put_field(&mut this, "in", "Ljava/io/Reader;", r#in).await?;
92        jvm.put_field(&mut this, "cb", "[C", cb).await?;
93        jvm.put_field(&mut this, "nChars", "I", 0).await?;
94        jvm.put_field(&mut this, "nextChar", "I", 0).await?;
95        jvm.put_field(&mut this, "markedChar", "I", UNMARKED).await?;
96        jvm.put_field(&mut this, "readAheadLimit", "I", 0).await?;
97        jvm.put_field(&mut this, "skipLF", "Z", false).await?;
98        jvm.put_field(&mut this, "markedSkipLF", "Z", false).await?;
99        Ok(())
100    }
101
102    async fn with_lock<T, F>(jvm: &Jvm, lock: &ClassInstanceRef<Object>, operation: F) -> Result<T>
103    where
104        F: Future<Output = Result<T>>,
105    {
106        jvm.monitor_enter(lock).await?;
107        match operation.await {
108            Ok(value) => {
109                jvm.monitor_exit(lock).await?;
110                Ok(value)
111            }
112            Err(error) => {
113                if let Err(exit_error) = jvm.monitor_exit(lock).await {
114                    tracing::error!(?exit_error, "failed to release BufferedReader lock");
115                }
116                Err(error)
117            }
118        }
119    }
120
121    async fn fill(jvm: &Jvm, this: &mut ClassInstanceRef<Self>) -> Result<i32> {
122        let r#in: ClassInstanceRef<Reader> = jvm.get_field(this, "in", "Ljava/io/Reader;").await?;
123        if r#in.is_null() {
124            return Err(jvm.exception("java/io/IOException", "stream is closed").await);
125        }
126
127        let mut cb: ClassInstanceRef<Array<JavaChar>> = jvm.get_field(this, "cb", "[C").await?;
128        let mut destination = 0;
129        let marked_char: i32 = jvm.get_field(this, "markedChar", "I").await?;
130        if marked_char >= 0 {
131            let next_char: i32 = jvm.get_field(this, "nextChar", "I").await?;
132            let delta = next_char - marked_char;
133            let read_ahead_limit: i32 = jvm.get_field(this, "readAheadLimit", "I").await?;
134            if delta >= read_ahead_limit {
135                jvm.put_field(this, "markedChar", "I", INVALIDATED).await?;
136                jvm.put_field(this, "readAheadLimit", "I", 0).await?;
137            } else {
138                let capacity = jvm.array_length(&cb).await?;
139                if read_ahead_limit as usize > capacity {
140                    let expanded_capacity = min(capacity.saturating_mul(2), read_ahead_limit as usize);
141                    let expanded = jvm.instantiate_array("C", expanded_capacity).await?;
142                    let _: () = jvm
143                        .invoke_static(
144                            "java/lang/System",
145                            "arraycopy",
146                            "(Ljava/lang/Object;ILjava/lang/Object;II)V",
147                            (cb, marked_char, expanded.clone(), 0, delta),
148                        )
149                        .await?;
150                    cb = expanded.into();
151                    jvm.put_field(this, "cb", "[C", cb.clone()).await?;
152                } else if delta > 0 {
153                    let _: () = jvm
154                        .invoke_static(
155                            "java/lang/System",
156                            "arraycopy",
157                            "(Ljava/lang/Object;ILjava/lang/Object;II)V",
158                            (cb.clone(), marked_char, cb.clone(), 0, delta),
159                        )
160                        .await?;
161                }
162
163                jvm.put_field(this, "markedChar", "I", 0).await?;
164                destination = delta;
165            }
166        }
167
168        jvm.put_field(this, "nextChar", "I", destination).await?;
169        jvm.put_field(this, "nChars", "I", destination).await?;
170
171        let capacity = jvm.array_length(&cb).await? as i32;
172        let mut read;
173        loop {
174            read = jvm
175                .invoke_virtual(
176                    &r#in,
177                    "java/io/Reader",
178                    "read",
179                    "([CII)I",
180                    (cb.clone(), destination, capacity - destination),
181                )
182                .await?;
183            if read != 0 {
184                break;
185            }
186        }
187        if read > 0 {
188            jvm.put_field(this, "nChars", "I", destination + read).await?;
189            jvm.put_field(this, "nextChar", "I", destination).await?;
190        }
191
192        Ok(read)
193    }
194
195    async fn read_char(jvm: &Jvm, _: &mut RuntimeContext, this: ClassInstanceRef<Self>) -> Result<i32> {
196        let lock: ClassInstanceRef<Object> = jvm.get_field(&this, "lock", "Ljava/lang/Object;").await?;
197        Self::with_lock(jvm, &lock, Self::read_char_locked(jvm, this)).await
198    }
199
200    async fn read_char_locked(jvm: &Jvm, mut this: ClassInstanceRef<Self>) -> Result<i32> {
201        tracing::debug!("java.io.BufferedReader::read({this:?})");
202
203        let r#in: ClassInstanceRef<Reader> = jvm.get_field(&this, "in", "Ljava/io/Reader;").await?;
204        if r#in.is_null() {
205            return Err(jvm.exception("java/io/IOException", "stream is closed").await);
206        }
207
208        loop {
209            let mut next_char: i32 = jvm.get_field(&this, "nextChar", "I").await?;
210            let mut n_chars: i32 = jvm.get_field(&this, "nChars", "I").await?;
211            if next_char >= n_chars {
212                if Self::fill(jvm, &mut this).await? <= 0 {
213                    return Ok(-1);
214                }
215                next_char = jvm.get_field(&this, "nextChar", "I").await?;
216                n_chars = jvm.get_field(&this, "nChars", "I").await?;
217            }
218
219            if jvm.get_field::<bool>(&this, "skipLF", "Z").await? {
220                jvm.put_field(&mut this, "skipLF", "Z", false).await?;
221                let cb: ClassInstanceRef<Array<JavaChar>> = jvm.get_field(&this, "cb", "[C").await?;
222                if jvm.load_array::<JavaChar>(&cb, next_char as usize, 1).await?[0] == '\n' as JavaChar {
223                    next_char += 1;
224                    jvm.put_field(&mut this, "nextChar", "I", next_char).await?;
225                    if next_char >= n_chars {
226                        continue;
227                    }
228                }
229            }
230
231            let cb: ClassInstanceRef<Array<JavaChar>> = jvm.get_field(&this, "cb", "[C").await?;
232            let value = jvm.load_array::<JavaChar>(&cb, next_char as usize, 1).await?[0];
233            jvm.put_field(&mut this, "nextChar", "I", next_char + 1).await?;
234            return Ok(value as i32);
235        }
236    }
237
238    async fn read(
239        jvm: &Jvm,
240        _: &mut RuntimeContext,
241        this: ClassInstanceRef<Self>,
242        target: ClassInstanceRef<Array<JavaChar>>,
243        offset: i32,
244        length: i32,
245    ) -> Result<i32> {
246        let lock: ClassInstanceRef<Object> = jvm.get_field(&this, "lock", "Ljava/lang/Object;").await?;
247        Self::with_lock(jvm, &lock, Self::read_locked(jvm, this, target, offset, length)).await
248    }
249
250    async fn read_locked(
251        jvm: &Jvm,
252        mut this: ClassInstanceRef<Self>,
253        target: ClassInstanceRef<Array<JavaChar>>,
254        offset: i32,
255        length: i32,
256    ) -> Result<i32> {
257        tracing::debug!("java.io.BufferedReader::read({this:?}, {target:?}, {offset}, {length})");
258
259        let r#in: ClassInstanceRef<Reader> = jvm.get_field(&this, "in", "Ljava/io/Reader;").await?;
260        if r#in.is_null() {
261            return Err(jvm.exception("java/io/IOException", "stream is closed").await);
262        }
263        if target.is_null() {
264            return Err(jvm.exception("java/lang/NullPointerException", "target is null").await);
265        }
266
267        let target_length = jvm.array_length(&target).await? as i32;
268        if offset < 0 || length < 0 || offset > target_length || length > target_length - offset {
269            return Err(jvm.exception("java/lang/IndexOutOfBoundsException", "invalid offset or length").await);
270        }
271        if length == 0 {
272            return Ok(0);
273        }
274
275        let mut total = 0;
276        while total < length {
277            let mut next_char: i32 = jvm.get_field(&this, "nextChar", "I").await?;
278            let mut n_chars: i32 = jvm.get_field(&this, "nChars", "I").await?;
279            if next_char >= n_chars {
280                if total > 0 && !jvm.invoke_virtual::<_, bool>(&r#in, "java/io/Reader", "ready", "()Z", ()).await? {
281                    break;
282                }
283                if Self::fill(jvm, &mut this).await? <= 0 {
284                    break;
285                }
286                next_char = jvm.get_field(&this, "nextChar", "I").await?;
287                n_chars = jvm.get_field(&this, "nChars", "I").await?;
288            }
289
290            if jvm.get_field::<bool>(&this, "skipLF", "Z").await? {
291                jvm.put_field(&mut this, "skipLF", "Z", false).await?;
292                let cb: ClassInstanceRef<Array<JavaChar>> = jvm.get_field(&this, "cb", "[C").await?;
293                if jvm.load_array::<JavaChar>(&cb, next_char as usize, 1).await?[0] == '\n' as JavaChar {
294                    next_char += 1;
295                    jvm.put_field(&mut this, "nextChar", "I", next_char).await?;
296                    if next_char >= n_chars {
297                        continue;
298                    }
299                }
300            }
301
302            let count = min(length - total, n_chars - next_char);
303            let cb: ClassInstanceRef<Array<JavaChar>> = jvm.get_field(&this, "cb", "[C").await?;
304            let _: () = jvm
305                .invoke_static(
306                    "java/lang/System",
307                    "arraycopy",
308                    "(Ljava/lang/Object;ILjava/lang/Object;II)V",
309                    (cb, next_char, target.clone(), offset + total, count),
310                )
311                .await?;
312            total += count;
313            jvm.put_field(&mut this, "nextChar", "I", next_char + count).await?;
314        }
315
316        if total == 0 { Ok(-1) } else { Ok(total) }
317    }
318
319    async fn read_line(jvm: &Jvm, _: &mut RuntimeContext, this: ClassInstanceRef<Self>) -> Result<ClassInstanceRef<String>> {
320        let lock: ClassInstanceRef<Object> = jvm.get_field(&this, "lock", "Ljava/lang/Object;").await?;
321        Self::with_lock(jvm, &lock, Self::read_line_locked(jvm, this)).await
322    }
323
324    async fn read_line_locked(jvm: &Jvm, mut this: ClassInstanceRef<Self>) -> Result<ClassInstanceRef<String>> {
325        tracing::debug!("java.io.BufferedReader::readLine({this:?})");
326
327        let r#in: ClassInstanceRef<Reader> = jvm.get_field(&this, "in", "Ljava/io/Reader;").await?;
328        if r#in.is_null() {
329            return Err(jvm.exception("java/io/IOException", "stream is closed").await);
330        }
331
332        let mut omit_lf: bool = jvm.get_field(&this, "skipLF", "Z").await?;
333        jvm.put_field(&mut this, "skipLF", "Z", false).await?;
334        let mut line = Vec::new();
335
336        loop {
337            let mut next_char: i32 = jvm.get_field(&this, "nextChar", "I").await?;
338            let mut n_chars: i32 = jvm.get_field(&this, "nChars", "I").await?;
339            if next_char >= n_chars {
340                if Self::fill(jvm, &mut this).await? <= 0 {
341                    if line.is_empty() {
342                        return Ok(None.into());
343                    }
344
345                    return Ok(JavaLangString::from_utf16(jvm, line).await?.into());
346                }
347                next_char = jvm.get_field(&this, "nextChar", "I").await?;
348                n_chars = jvm.get_field(&this, "nChars", "I").await?;
349            }
350
351            let cb: ClassInstanceRef<Array<JavaChar>> = jvm.get_field(&this, "cb", "[C").await?;
352            if omit_lf {
353                omit_lf = false;
354                if jvm.load_array::<JavaChar>(&cb, next_char as usize, 1).await?[0] == '\n' as JavaChar {
355                    next_char += 1;
356                    jvm.put_field(&mut this, "nextChar", "I", next_char).await?;
357                    if next_char >= n_chars {
358                        continue;
359                    }
360                }
361            }
362
363            let buffered: Vec<JavaChar> = jvm.load_array(&cb, next_char as usize, (n_chars - next_char) as usize).await?;
364            if let Some(index) = buffered.iter().position(|value| *value == '\n' as JavaChar || *value == '\r' as JavaChar) {
365                line.extend_from_slice(&buffered[..index]);
366                let terminator = buffered[index];
367                jvm.put_field(&mut this, "nextChar", "I", next_char + index as i32 + 1).await?;
368                if terminator == '\r' as JavaChar {
369                    jvm.put_field(&mut this, "skipLF", "Z", true).await?;
370                }
371
372                return Ok(JavaLangString::from_utf16(jvm, line).await?.into());
373            }
374
375            line.extend(buffered);
376            jvm.put_field(&mut this, "nextChar", "I", n_chars).await?;
377        }
378    }
379
380    async fn skip(jvm: &Jvm, _: &mut RuntimeContext, this: ClassInstanceRef<Self>, count: i64) -> Result<i64> {
381        let lock: ClassInstanceRef<Object> = jvm.get_field(&this, "lock", "Ljava/lang/Object;").await?;
382        Self::with_lock(jvm, &lock, Self::skip_locked(jvm, this, count)).await
383    }
384
385    async fn skip_locked(jvm: &Jvm, mut this: ClassInstanceRef<Self>, count: i64) -> Result<i64> {
386        tracing::debug!("java.io.BufferedReader::skip({this:?}, {count})");
387
388        let r#in: ClassInstanceRef<Reader> = jvm.get_field(&this, "in", "Ljava/io/Reader;").await?;
389        if r#in.is_null() {
390            return Err(jvm.exception("java/io/IOException", "stream is closed").await);
391        }
392        if count < 0 {
393            return Err(jvm.exception("java/lang/IllegalArgumentException", "skip value is negative").await);
394        }
395
396        let mut remaining = count;
397        while remaining > 0 {
398            let mut next_char: i32 = jvm.get_field(&this, "nextChar", "I").await?;
399            let mut n_chars: i32 = jvm.get_field(&this, "nChars", "I").await?;
400            if next_char >= n_chars {
401                if Self::fill(jvm, &mut this).await? <= 0 {
402                    break;
403                }
404                next_char = jvm.get_field(&this, "nextChar", "I").await?;
405                n_chars = jvm.get_field(&this, "nChars", "I").await?;
406            }
407
408            if jvm.get_field::<bool>(&this, "skipLF", "Z").await? {
409                jvm.put_field(&mut this, "skipLF", "Z", false).await?;
410                let cb: ClassInstanceRef<Array<JavaChar>> = jvm.get_field(&this, "cb", "[C").await?;
411                if jvm.load_array::<JavaChar>(&cb, next_char as usize, 1).await?[0] == '\n' as JavaChar {
412                    next_char += 1;
413                    jvm.put_field(&mut this, "nextChar", "I", next_char).await?;
414                    if next_char >= n_chars {
415                        continue;
416                    }
417                }
418            }
419
420            let skipped = min(remaining, (n_chars - next_char) as i64);
421            remaining -= skipped;
422            jvm.put_field(&mut this, "nextChar", "I", next_char + skipped as i32).await?;
423        }
424
425        Ok(count - remaining)
426    }
427
428    async fn ready(jvm: &Jvm, _: &mut RuntimeContext, this: ClassInstanceRef<Self>) -> Result<bool> {
429        let lock: ClassInstanceRef<Object> = jvm.get_field(&this, "lock", "Ljava/lang/Object;").await?;
430        Self::with_lock(jvm, &lock, Self::ready_locked(jvm, this)).await
431    }
432
433    async fn ready_locked(jvm: &Jvm, mut this: ClassInstanceRef<Self>) -> Result<bool> {
434        tracing::debug!("java.io.BufferedReader::ready({this:?})");
435
436        let r#in: ClassInstanceRef<Reader> = jvm.get_field(&this, "in", "Ljava/io/Reader;").await?;
437        if r#in.is_null() {
438            return Err(jvm.exception("java/io/IOException", "stream is closed").await);
439        }
440
441        let mut next_char: i32 = jvm.get_field(&this, "nextChar", "I").await?;
442        let mut n_chars: i32 = jvm.get_field(&this, "nChars", "I").await?;
443        if jvm.get_field::<bool>(&this, "skipLF", "Z").await? {
444            if next_char >= n_chars && jvm.invoke_virtual::<_, bool>(&r#in, "java/io/Reader", "ready", "()Z", ()).await? {
445                let _ = Self::fill(jvm, &mut this).await?;
446                next_char = jvm.get_field(&this, "nextChar", "I").await?;
447                n_chars = jvm.get_field(&this, "nChars", "I").await?;
448            }
449
450            if next_char < n_chars {
451                let cb: ClassInstanceRef<Array<JavaChar>> = jvm.get_field(&this, "cb", "[C").await?;
452                if jvm.load_array::<JavaChar>(&cb, next_char as usize, 1).await?[0] == '\n' as JavaChar {
453                    next_char += 1;
454                    jvm.put_field(&mut this, "nextChar", "I", next_char).await?;
455                }
456                jvm.put_field(&mut this, "skipLF", "Z", false).await?;
457            }
458        }
459
460        if next_char < n_chars {
461            return Ok(true);
462        }
463        jvm.invoke_virtual(&r#in, "java/io/Reader", "ready", "()Z", ()).await
464    }
465
466    async fn mark_supported(_: &Jvm, _: &mut RuntimeContext, this: ClassInstanceRef<Self>) -> Result<bool> {
467        tracing::debug!("java.io.BufferedReader::markSupported({this:?})");
468        Ok(true)
469    }
470
471    async fn mark(jvm: &Jvm, _: &mut RuntimeContext, this: ClassInstanceRef<Self>, read_ahead_limit: i32) -> Result<()> {
472        let lock: ClassInstanceRef<Object> = jvm.get_field(&this, "lock", "Ljava/lang/Object;").await?;
473        Self::with_lock(jvm, &lock, Self::mark_locked(jvm, this, read_ahead_limit)).await
474    }
475
476    async fn mark_locked(jvm: &Jvm, mut this: ClassInstanceRef<Self>, read_ahead_limit: i32) -> Result<()> {
477        tracing::debug!("java.io.BufferedReader::mark({this:?}, {read_ahead_limit})");
478
479        let r#in: ClassInstanceRef<Reader> = jvm.get_field(&this, "in", "Ljava/io/Reader;").await?;
480        if r#in.is_null() {
481            return Err(jvm.exception("java/io/IOException", "stream is closed").await);
482        }
483        if read_ahead_limit < 0 {
484            return Err(jvm.exception("java/lang/IllegalArgumentException", "read-ahead limit is negative").await);
485        }
486
487        let next_char: i32 = jvm.get_field(&this, "nextChar", "I").await?;
488        let skip_lf: bool = jvm.get_field(&this, "skipLF", "Z").await?;
489        jvm.put_field(&mut this, "readAheadLimit", "I", read_ahead_limit).await?;
490        jvm.put_field(&mut this, "markedChar", "I", next_char).await?;
491        jvm.put_field(&mut this, "markedSkipLF", "Z", skip_lf).await?;
492        Ok(())
493    }
494
495    async fn reset(jvm: &Jvm, _: &mut RuntimeContext, this: ClassInstanceRef<Self>) -> Result<()> {
496        let lock: ClassInstanceRef<Object> = jvm.get_field(&this, "lock", "Ljava/lang/Object;").await?;
497        Self::with_lock(jvm, &lock, Self::reset_locked(jvm, this)).await
498    }
499
500    async fn reset_locked(jvm: &Jvm, mut this: ClassInstanceRef<Self>) -> Result<()> {
501        tracing::debug!("java.io.BufferedReader::reset({this:?})");
502
503        let r#in: ClassInstanceRef<Reader> = jvm.get_field(&this, "in", "Ljava/io/Reader;").await?;
504        if r#in.is_null() {
505            return Err(jvm.exception("java/io/IOException", "stream is closed").await);
506        }
507
508        let marked_char: i32 = jvm.get_field(&this, "markedChar", "I").await?;
509        if marked_char < 0 {
510            let message = if marked_char == INVALIDATED {
511                "mark invalid"
512            } else {
513                "stream not marked"
514            };
515            return Err(jvm.exception("java/io/IOException", message).await);
516        }
517
518        let marked_skip_lf: bool = jvm.get_field(&this, "markedSkipLF", "Z").await?;
519        jvm.put_field(&mut this, "nextChar", "I", marked_char).await?;
520        jvm.put_field(&mut this, "skipLF", "Z", marked_skip_lf).await?;
521        Ok(())
522    }
523
524    async fn close(jvm: &Jvm, _: &mut RuntimeContext, this: ClassInstanceRef<Self>) -> Result<()> {
525        let lock: ClassInstanceRef<Object> = jvm.get_field(&this, "lock", "Ljava/lang/Object;").await?;
526        Self::with_lock(jvm, &lock, Self::close_locked(jvm, this)).await
527    }
528
529    async fn close_locked(jvm: &Jvm, mut this: ClassInstanceRef<Self>) -> Result<()> {
530        tracing::debug!("java.io.BufferedReader::close({this:?})");
531
532        let r#in: ClassInstanceRef<Reader> = jvm.get_field(&this, "in", "Ljava/io/Reader;").await?;
533        if r#in.is_null() {
534            return Ok(());
535        }
536
537        let result = jvm.invoke_virtual(&r#in, "java/io/Reader", "close", "()V", ()).await;
538        let closed: ClassInstanceRef<Reader> = None.into();
539        jvm.put_field(&mut this, "in", "Ljava/io/Reader;", closed).await?;
540        let released: ClassInstanceRef<Array<JavaChar>> = None.into();
541        jvm.put_field(&mut this, "cb", "[C", released).await?;
542        result
543    }
544}