1use core::future::Future;
2
3use alloc::vec;
4
5use jvm::{Array, ClassInstanceRef, JavaChar, Jvm, Result};
6use jvm_class_proto::{JavaFieldProto, JavaMethodProto};
7use jvm_types::{ClassAccessFlags, FieldAccessFlags, MethodAccessFlags};
8
9use crate::{RuntimeClassProto, RuntimeContext, classes::java::lang::Object};
10
11pub struct CharArrayReader;
13
14impl CharArrayReader {
15 pub fn as_proto() -> RuntimeClassProto {
16 RuntimeClassProto {
17 name: "java/io/CharArrayReader",
18 parent_class: Some("java/io/Reader"),
19 interfaces: vec![],
20 methods: vec![
21 JavaMethodProto::new("<init>", "([C)V", Self::init, MethodAccessFlags::PUBLIC),
22 JavaMethodProto::new("<init>", "([CII)V", Self::init_with_range, MethodAccessFlags::PUBLIC),
23 JavaMethodProto::new("read", "()I", Self::read_char, MethodAccessFlags::PUBLIC),
24 JavaMethodProto::new("read", "([CII)I", Self::read, MethodAccessFlags::PUBLIC),
25 JavaMethodProto::new("skip", "(J)J", Self::skip, MethodAccessFlags::PUBLIC),
26 JavaMethodProto::new("ready", "()Z", Self::ready, MethodAccessFlags::PUBLIC),
27 JavaMethodProto::new("markSupported", "()Z", Self::mark_supported, MethodAccessFlags::PUBLIC),
28 JavaMethodProto::new("mark", "(I)V", Self::mark, MethodAccessFlags::PUBLIC),
29 JavaMethodProto::new("reset", "()V", Self::reset, MethodAccessFlags::PUBLIC),
30 JavaMethodProto::new("close", "()V", Self::close, MethodAccessFlags::PUBLIC),
31 ],
32 fields: vec![
33 JavaFieldProto::new("buf", "[C", FieldAccessFlags::PROTECTED),
34 JavaFieldProto::new("pos", "I", FieldAccessFlags::PROTECTED),
35 JavaFieldProto::new("markedPos", "I", FieldAccessFlags::PROTECTED),
36 JavaFieldProto::new("count", "I", FieldAccessFlags::PROTECTED),
37 ],
38 access_flags: ClassAccessFlags::PUBLIC,
39 }
40 }
41
42 async fn init(jvm: &Jvm, _: &mut RuntimeContext, this: ClassInstanceRef<Self>, buffer: ClassInstanceRef<Array<JavaChar>>) -> Result<()> {
43 tracing::debug!("java.io.CharArrayReader::<init>({this:?}, {buffer:?})");
44
45 if buffer.is_null() {
46 return Err(jvm.exception("java/lang/NullPointerException", "buffer is null").await);
47 }
48 let length = jvm.array_length(&buffer).await? as i32;
49 jvm.invoke_special(&this, "java/io/CharArrayReader", "<init>", "([CII)V", (buffer, 0, length))
50 .await
51 }
52
53 async fn init_with_range(
54 jvm: &Jvm,
55 _: &mut RuntimeContext,
56 mut this: ClassInstanceRef<Self>,
57 buffer: ClassInstanceRef<Array<JavaChar>>,
58 offset: i32,
59 length: i32,
60 ) -> Result<()> {
61 tracing::debug!("java.io.CharArrayReader::<init>({this:?}, {buffer:?}, {offset}, {length})");
62
63 if buffer.is_null() {
64 return Err(jvm.exception("java/lang/NullPointerException", "buffer is null").await);
65 }
66 let buffer_length = jvm.array_length(&buffer).await? as i32;
67 let end = offset as i64 + length as i64;
68 if offset < 0 || offset > buffer_length || length < 0 || end < 0 {
69 return Err(jvm.exception("java/lang/IllegalArgumentException", "Invalid offset or length").await);
70 }
71
72 let _: () = jvm.invoke_special(&this, "java/io/Reader", "<init>", "()V", ()).await?;
73 jvm.put_field(&mut this, "buf", "[C", buffer).await?;
74 jvm.put_field(&mut this, "pos", "I", offset).await?;
75 jvm.put_field(&mut this, "markedPos", "I", offset).await?;
76 jvm.put_field(&mut this, "count", "I", end.min(buffer_length as i64) as i32).await
77 }
78
79 async fn with_lock<T, F>(jvm: &Jvm, lock: &ClassInstanceRef<Object>, operation: F) -> Result<T>
80 where
81 F: Future<Output = Result<T>>,
82 {
83 jvm.monitor_enter(lock).await?;
84 match operation.await {
85 Ok(value) => {
86 jvm.monitor_exit(lock).await?;
87 Ok(value)
88 }
89 Err(error) => {
90 if let Err(exit_error) = jvm.monitor_exit(lock).await {
91 tracing::error!(?exit_error, "failed to release CharArrayReader lock");
92 }
93 Err(error)
94 }
95 }
96 }
97
98 async fn read_char(jvm: &Jvm, _: &mut RuntimeContext, this: ClassInstanceRef<Self>) -> Result<i32> {
99 let lock: ClassInstanceRef<Object> = jvm.get_field(&this, "lock", "Ljava/lang/Object;").await?;
100 Self::with_lock(jvm, &lock, Self::read_char_locked(jvm, this)).await
101 }
102
103 async fn read_char_locked(jvm: &Jvm, mut this: ClassInstanceRef<Self>) -> Result<i32> {
104 tracing::debug!("java.io.CharArrayReader::read({this:?})");
105
106 let buffer: ClassInstanceRef<Array<JavaChar>> = jvm.get_field(&this, "buf", "[C").await?;
107 if buffer.is_null() {
108 return Err(jvm.exception("java/io/IOException", "Stream closed").await);
109 }
110 let position: i32 = jvm.get_field(&this, "pos", "I").await?;
111 let count: i32 = jvm.get_field(&this, "count", "I").await?;
112 if position >= count {
113 return Ok(-1);
114 }
115 let value = jvm.load_array::<JavaChar>(&buffer, position as usize, 1).await?[0];
116 jvm.put_field(&mut this, "pos", "I", position + 1).await?;
117 Ok(value as i32)
118 }
119
120 async fn read(
121 jvm: &Jvm,
122 _: &mut RuntimeContext,
123 this: ClassInstanceRef<Self>,
124 target: ClassInstanceRef<Array<JavaChar>>,
125 offset: i32,
126 length: i32,
127 ) -> Result<i32> {
128 let lock: ClassInstanceRef<Object> = jvm.get_field(&this, "lock", "Ljava/lang/Object;").await?;
129 Self::with_lock(jvm, &lock, Self::read_locked(jvm, this, target, offset, length)).await
130 }
131
132 async fn read_locked(
133 jvm: &Jvm,
134 mut this: ClassInstanceRef<Self>,
135 target: ClassInstanceRef<Array<JavaChar>>,
136 offset: i32,
137 length: i32,
138 ) -> Result<i32> {
139 tracing::debug!("java.io.CharArrayReader::read({this:?}, {target:?}, {offset}, {length})");
140
141 let buffer: ClassInstanceRef<Array<JavaChar>> = jvm.get_field(&this, "buf", "[C").await?;
142 if buffer.is_null() {
143 return Err(jvm.exception("java/io/IOException", "Stream closed").await);
144 }
145 if target.is_null() {
146 return Err(jvm.exception("java/lang/NullPointerException", "target is null").await);
147 }
148 let target_length = jvm.array_length(&target).await? as i32;
149 if offset < 0 || length < 0 || offset > target_length - length {
150 return Err(jvm.exception("java/lang/IndexOutOfBoundsException", "Invalid offset or length").await);
151 }
152 if length == 0 {
153 return Ok(0);
154 }
155 let position: i32 = jvm.get_field(&this, "pos", "I").await?;
156 let count: i32 = jvm.get_field(&this, "count", "I").await?;
157 if position >= count {
158 return Ok(-1);
159 }
160 let copied = length.min(count - position);
161 let _: () = jvm
162 .invoke_static(
163 "java/lang/System",
164 "arraycopy",
165 "(Ljava/lang/Object;ILjava/lang/Object;II)V",
166 (buffer, position, target, offset, copied),
167 )
168 .await?;
169 jvm.put_field(&mut this, "pos", "I", position + copied).await?;
170 Ok(copied)
171 }
172
173 async fn skip(jvm: &Jvm, _: &mut RuntimeContext, this: ClassInstanceRef<Self>, amount: i64) -> Result<i64> {
174 let lock: ClassInstanceRef<Object> = jvm.get_field(&this, "lock", "Ljava/lang/Object;").await?;
175 Self::with_lock(jvm, &lock, Self::skip_locked(jvm, this, amount)).await
176 }
177
178 async fn skip_locked(jvm: &Jvm, mut this: ClassInstanceRef<Self>, amount: i64) -> Result<i64> {
179 tracing::debug!("java.io.CharArrayReader::skip({this:?}, {amount})");
180
181 let buffer: ClassInstanceRef<Array<JavaChar>> = jvm.get_field(&this, "buf", "[C").await?;
182 if buffer.is_null() {
183 return Err(jvm.exception("java/io/IOException", "Stream closed").await);
184 }
185 if amount <= 0 {
186 return Ok(0);
187 }
188 let position: i32 = jvm.get_field(&this, "pos", "I").await?;
189 let count: i32 = jvm.get_field(&this, "count", "I").await?;
190 let skipped = amount.min((count - position) as i64);
191 jvm.put_field(&mut this, "pos", "I", position + skipped as i32).await?;
192 Ok(skipped)
193 }
194
195 async fn ready(jvm: &Jvm, _: &mut RuntimeContext, this: ClassInstanceRef<Self>) -> Result<bool> {
196 let lock: ClassInstanceRef<Object> = jvm.get_field(&this, "lock", "Ljava/lang/Object;").await?;
197 Self::with_lock(jvm, &lock, Self::ready_locked(jvm, this)).await
198 }
199
200 async fn ready_locked(jvm: &Jvm, this: ClassInstanceRef<Self>) -> Result<bool> {
201 tracing::debug!("java.io.CharArrayReader::ready({this:?})");
202
203 let buffer: ClassInstanceRef<Array<JavaChar>> = jvm.get_field(&this, "buf", "[C").await?;
204 if buffer.is_null() {
205 return Err(jvm.exception("java/io/IOException", "Stream closed").await);
206 }
207 let position: i32 = jvm.get_field(&this, "pos", "I").await?;
208 let count: i32 = jvm.get_field(&this, "count", "I").await?;
209 Ok(count - position > 0)
210 }
211
212 async fn mark_supported(_: &Jvm, _: &mut RuntimeContext, this: ClassInstanceRef<Self>) -> Result<bool> {
213 tracing::debug!("java.io.CharArrayReader::markSupported({this:?})");
214 Ok(true)
215 }
216
217 async fn mark(jvm: &Jvm, _: &mut RuntimeContext, this: ClassInstanceRef<Self>, read_ahead_limit: i32) -> Result<()> {
218 let lock: ClassInstanceRef<Object> = jvm.get_field(&this, "lock", "Ljava/lang/Object;").await?;
219 Self::with_lock(jvm, &lock, Self::mark_locked(jvm, this, read_ahead_limit)).await
220 }
221
222 async fn mark_locked(jvm: &Jvm, mut this: ClassInstanceRef<Self>, read_ahead_limit: i32) -> Result<()> {
223 tracing::debug!("java.io.CharArrayReader::mark({this:?}, {read_ahead_limit})");
224
225 let buffer: ClassInstanceRef<Array<JavaChar>> = jvm.get_field(&this, "buf", "[C").await?;
226 if buffer.is_null() {
227 return Err(jvm.exception("java/io/IOException", "Stream closed").await);
228 }
229 let position: i32 = jvm.get_field(&this, "pos", "I").await?;
230 jvm.put_field(&mut this, "markedPos", "I", position).await
231 }
232
233 async fn reset(jvm: &Jvm, _: &mut RuntimeContext, this: ClassInstanceRef<Self>) -> Result<()> {
234 let lock: ClassInstanceRef<Object> = jvm.get_field(&this, "lock", "Ljava/lang/Object;").await?;
235 Self::with_lock(jvm, &lock, Self::reset_locked(jvm, this)).await
236 }
237
238 async fn reset_locked(jvm: &Jvm, mut this: ClassInstanceRef<Self>) -> Result<()> {
239 tracing::debug!("java.io.CharArrayReader::reset({this:?})");
240
241 let buffer: ClassInstanceRef<Array<JavaChar>> = jvm.get_field(&this, "buf", "[C").await?;
242 if buffer.is_null() {
243 return Err(jvm.exception("java/io/IOException", "Stream closed").await);
244 }
245 let marked_position: i32 = jvm.get_field(&this, "markedPos", "I").await?;
246 jvm.put_field(&mut this, "pos", "I", marked_position).await
247 }
248
249 async fn close(jvm: &Jvm, _: &mut RuntimeContext, this: ClassInstanceRef<Self>) -> Result<()> {
250 let lock: ClassInstanceRef<Object> = jvm.get_field(&this, "lock", "Ljava/lang/Object;").await?;
251 Self::with_lock(jvm, &lock, Self::close_locked(jvm, this)).await
252 }
253
254 async fn close_locked(jvm: &Jvm, mut this: ClassInstanceRef<Self>) -> Result<()> {
255 tracing::debug!("java.io.CharArrayReader::close({this:?})");
256
257 let null_buffer: ClassInstanceRef<Array<JavaChar>> = None.into();
258 jvm.put_field(&mut this, "buf", "[C", null_buffer).await
259 }
260}