1use alloc::{vec, vec::Vec};
2
3use jvm::{Array, ClassInstanceRef, JavaChar, Jvm, Result, runtime::JavaLangString};
4use jvm_class_proto::JavaMethodProto;
5use jvm_types::{ClassAccessFlags, MethodAccessFlags};
6
7use crate::{
8 RuntimeClassProto, RuntimeContext,
9 classes::java::{
10 io::{DataInput, InputStream},
11 lang::String,
12 },
13};
14
15pub struct DataInputStream;
17
18impl DataInputStream {
19 pub fn as_proto() -> RuntimeClassProto {
20 RuntimeClassProto {
21 name: "java/io/DataInputStream",
22 parent_class: Some("java/io/FilterInputStream"),
23 interfaces: vec!["java/io/DataInput"],
24 methods: vec![
25 JavaMethodProto::new("<init>", "(Ljava/io/InputStream;)V", Self::init, MethodAccessFlags::PUBLIC),
26 JavaMethodProto::new(
27 "readBoolean",
28 "()Z",
29 Self::read_boolean,
30 MethodAccessFlags::PUBLIC | MethodAccessFlags::FINAL,
31 ),
32 JavaMethodProto::new("readByte", "()B", Self::read_byte, MethodAccessFlags::PUBLIC | MethodAccessFlags::FINAL),
33 JavaMethodProto::new("readChar", "()C", Self::read_char, MethodAccessFlags::PUBLIC | MethodAccessFlags::FINAL),
34 JavaMethodProto::new(
35 "readDouble",
36 "()D",
37 Self::read_double,
38 MethodAccessFlags::PUBLIC | MethodAccessFlags::FINAL,
39 ),
40 JavaMethodProto::new("readFloat", "()F", Self::read_float, MethodAccessFlags::PUBLIC | MethodAccessFlags::FINAL),
41 JavaMethodProto::new(
42 "readFully",
43 "([B)V",
44 Self::read_fully,
45 MethodAccessFlags::PUBLIC | MethodAccessFlags::FINAL,
46 ),
47 JavaMethodProto::new(
48 "readFully",
49 "([BII)V",
50 Self::read_fully_offset_length,
51 MethodAccessFlags::PUBLIC | MethodAccessFlags::FINAL,
52 ),
53 JavaMethodProto::new("readInt", "()I", Self::read_int, MethodAccessFlags::PUBLIC | MethodAccessFlags::FINAL),
54 JavaMethodProto::new("readLong", "()J", Self::read_long, MethodAccessFlags::PUBLIC | MethodAccessFlags::FINAL),
55 JavaMethodProto::new("readShort", "()S", Self::read_short, MethodAccessFlags::PUBLIC | MethodAccessFlags::FINAL),
56 JavaMethodProto::new(
57 "readUnsignedByte",
58 "()I",
59 Self::read_unsigned_byte,
60 MethodAccessFlags::PUBLIC | MethodAccessFlags::FINAL,
61 ),
62 JavaMethodProto::new(
63 "readUnsignedShort",
64 "()I",
65 Self::read_unsigned_short,
66 MethodAccessFlags::PUBLIC | MethodAccessFlags::FINAL,
67 ),
68 JavaMethodProto::new(
69 "readUTF",
70 "()Ljava/lang/String;",
71 Self::read_utf,
72 MethodAccessFlags::PUBLIC | MethodAccessFlags::FINAL,
73 ),
74 JavaMethodProto::new(
75 "readUTF",
76 "(Ljava/io/DataInput;)Ljava/lang/String;",
77 Self::read_utf_from_input,
78 MethodAccessFlags::STATIC,
79 ),
80 JavaMethodProto::new(
81 "skipBytes",
82 "(I)I",
83 Self::skip_bytes,
84 MethodAccessFlags::PUBLIC | MethodAccessFlags::FINAL,
85 ),
86 ],
87 fields: vec![],
88 access_flags: ClassAccessFlags::PUBLIC,
89 }
90 }
91
92 async fn init(jvm: &Jvm, _: &mut RuntimeContext, this: ClassInstanceRef<Self>, r#in: ClassInstanceRef<InputStream>) -> Result<()> {
93 tracing::debug!("java.io.DataInputStream::<init>({this:?}, {:?})", &r#in);
94
95 let _: () = jvm
96 .invoke_special(&this, "java/io/FilterInputStream", "<init>", "(Ljava/io/InputStream;)V", (r#in,))
97 .await?;
98
99 Ok(())
100 }
101
102 async fn read_byte(jvm: &Jvm, _: &mut RuntimeContext, this: ClassInstanceRef<Self>) -> Result<i8> {
103 tracing::debug!("java.io.DataInputStream::readByte({this:?})");
104
105 Ok(Self::read_required_byte(jvm, &this).await? as i8)
106 }
107
108 async fn read_boolean(jvm: &Jvm, _: &mut RuntimeContext, this: ClassInstanceRef<Self>) -> Result<bool> {
109 tracing::debug!("java.io.DataInputStream::readBoolean({this:?})");
110
111 Ok(Self::read_required_byte(jvm, &this).await? != 0)
112 }
113
114 async fn read_char(jvm: &Jvm, _: &mut RuntimeContext, this: ClassInstanceRef<Self>) -> Result<JavaChar> {
115 tracing::debug!("java.io.DataInputStream::readChar({this:?})");
116
117 let byte1 = Self::read_required_byte(jvm, &this).await?;
118 let byte2 = Self::read_required_byte(jvm, &this).await?;
119 Ok(JavaChar::from_be_bytes([byte1, byte2]))
120 }
121
122 async fn read_short(jvm: &Jvm, _: &mut RuntimeContext, this: ClassInstanceRef<Self>) -> Result<i16> {
123 tracing::debug!("java.io.DataInputStream::readShort({this:?})");
124
125 let byte1 = Self::read_required_byte(jvm, &this).await?;
126 let byte2 = Self::read_required_byte(jvm, &this).await?;
127 Ok(i16::from_be_bytes([byte1, byte2]))
128 }
129
130 async fn read_unsigned_byte(jvm: &Jvm, _: &mut RuntimeContext, this: ClassInstanceRef<Self>) -> Result<i32> {
131 tracing::debug!("java.io.DataInputStream::readUnsignedByte({this:?})");
132 Ok(Self::read_required_byte(jvm, &this).await? as i32)
133 }
134
135 async fn read_unsigned_short(jvm: &Jvm, _: &mut RuntimeContext, this: ClassInstanceRef<Self>) -> Result<i32> {
136 tracing::debug!("java.io.DataInputStream::readUnsignedShort({this:?})");
137
138 let byte1 = Self::read_required_byte(jvm, &this).await?;
139 let byte2 = Self::read_required_byte(jvm, &this).await?;
140 Ok(u16::from_be_bytes([byte1, byte2]) as i32)
141 }
142
143 async fn read_int(jvm: &Jvm, _: &mut RuntimeContext, this: ClassInstanceRef<Self>) -> Result<i32> {
144 tracing::debug!("java.io.DataInputStream::readInt({this:?})");
145
146 Ok(i32::from_be_bytes([
147 Self::read_required_byte(jvm, &this).await?,
148 Self::read_required_byte(jvm, &this).await?,
149 Self::read_required_byte(jvm, &this).await?,
150 Self::read_required_byte(jvm, &this).await?,
151 ]))
152 }
153
154 async fn read_long(jvm: &Jvm, _: &mut RuntimeContext, this: ClassInstanceRef<Self>) -> Result<i64> {
155 tracing::debug!("java.io.DataInputStream::readLong({this:?})");
156
157 Ok(i64::from_be_bytes([
158 Self::read_required_byte(jvm, &this).await?,
159 Self::read_required_byte(jvm, &this).await?,
160 Self::read_required_byte(jvm, &this).await?,
161 Self::read_required_byte(jvm, &this).await?,
162 Self::read_required_byte(jvm, &this).await?,
163 Self::read_required_byte(jvm, &this).await?,
164 Self::read_required_byte(jvm, &this).await?,
165 Self::read_required_byte(jvm, &this).await?,
166 ]))
167 }
168
169 async fn read_float(jvm: &Jvm, _: &mut RuntimeContext, this: ClassInstanceRef<Self>) -> Result<f32> {
170 tracing::debug!("java.io.DataInputStream::readFloat({this:?})");
171
172 Ok(f32::from_be_bytes([
173 Self::read_required_byte(jvm, &this).await?,
174 Self::read_required_byte(jvm, &this).await?,
175 Self::read_required_byte(jvm, &this).await?,
176 Self::read_required_byte(jvm, &this).await?,
177 ]))
178 }
179
180 async fn read_double(jvm: &Jvm, _: &mut RuntimeContext, this: ClassInstanceRef<Self>) -> Result<f64> {
181 tracing::debug!("java.io.DataInputStream::readDouble({this:?})");
182
183 Ok(f64::from_be_bytes([
184 Self::read_required_byte(jvm, &this).await?,
185 Self::read_required_byte(jvm, &this).await?,
186 Self::read_required_byte(jvm, &this).await?,
187 Self::read_required_byte(jvm, &this).await?,
188 Self::read_required_byte(jvm, &this).await?,
189 Self::read_required_byte(jvm, &this).await?,
190 Self::read_required_byte(jvm, &this).await?,
191 Self::read_required_byte(jvm, &this).await?,
192 ]))
193 }
194
195 async fn read_utf(jvm: &Jvm, _: &mut RuntimeContext, this: ClassInstanceRef<Self>) -> Result<ClassInstanceRef<String>> {
196 tracing::debug!("java.io.DataInputStream::readUTF({this:?})");
197
198 let length: i32 = jvm
199 .invoke_virtual(&this, "java/io/DataInputStream", "readUnsignedShort", "()I", ())
200 .await?;
201 let java_array = jvm.instantiate_array("B", length as usize).await?;
202 let _: () = jvm
203 .invoke_virtual(&this, "java/io/DataInputStream", "readFully", "([BII)V", (java_array.clone(), 0, length))
204 .await?;
205 let bytes: Vec<i8> = jvm.load_array(&java_array, 0, length as usize).await?;
206 let bytes: Vec<u8> = bytes.into_iter().map(|value| value as u8).collect();
207
208 let mut chars = Vec::with_capacity(length as usize);
209 let mut index = 0;
210 while index < bytes.len() {
211 let first = bytes[index];
212 match first >> 4 {
213 0..=7 => {
214 chars.push(first as JavaChar);
215 index += 1;
216 }
217 12 | 13 => {
218 if index + 1 >= bytes.len() || bytes[index + 1] & 0xc0 != 0x80 {
219 return Err(jvm.exception("java/io/UTFDataFormatException", "malformed modified UTF-8").await);
220 }
221 chars.push((((first & 0x1f) as JavaChar) << 6) | ((bytes[index + 1] & 0x3f) as JavaChar));
222 index += 2;
223 }
224 14 => {
225 if index + 2 >= bytes.len() || bytes[index + 1] & 0xc0 != 0x80 || bytes[index + 2] & 0xc0 != 0x80 {
226 return Err(jvm.exception("java/io/UTFDataFormatException", "malformed modified UTF-8").await);
227 }
228 chars.push(
229 (((first & 0x0f) as JavaChar) << 12)
230 | (((bytes[index + 1] & 0x3f) as JavaChar) << 6)
231 | ((bytes[index + 2] & 0x3f) as JavaChar),
232 );
233 index += 3;
234 }
235 _ => return Err(jvm.exception("java/io/UTFDataFormatException", "malformed modified UTF-8").await),
236 }
237 }
238
239 Ok(JavaLangString::from_utf16(jvm, chars).await?.into())
240 }
241
242 async fn read_utf_from_input(jvm: &Jvm, _: &mut RuntimeContext, input: ClassInstanceRef<DataInput>) -> Result<ClassInstanceRef<String>> {
243 tracing::debug!("java.io.DataInputStream::readUTF({input:?})");
244 jvm.invoke_virtual(&input, &input.class_definition().name(), "readUTF", "()Ljava/lang/String;", ())
245 .await
246 }
247
248 async fn read_fully(jvm: &Jvm, _: &mut RuntimeContext, this: ClassInstanceRef<Self>, b: ClassInstanceRef<Array<i8>>) -> Result<()> {
249 tracing::debug!("java.io.DataInputStream::readFully({this:?}, {b:?})");
250
251 let length = jvm.array_length(&b).await?;
252
253 let _: () = jvm
254 .invoke_virtual(&this, "java/io/DataInputStream", "readFully", "([BII)V", (b.clone(), 0, length as i32))
255 .await?;
256
257 Ok(())
258 }
259
260 async fn read_fully_offset_length(
261 jvm: &Jvm,
262 _: &mut RuntimeContext,
263 this: ClassInstanceRef<Self>,
264 b: ClassInstanceRef<Array<i8>>,
265 off: i32,
266 len: i32,
267 ) -> Result<()> {
268 tracing::debug!("java.io.DataInputStream::readFully({this:?}, {b:?}, {off}, {len})");
269
270 let mut read = 0;
271 while read < len {
272 let r: i32 = jvm
273 .invoke_virtual(&this, "java/io/DataInputStream", "read", "([BII)I", (b.clone(), off + read, len - read))
274 .await?;
275 if r == -1 {
276 return Err(jvm.exception("java/io/EOFException", "End of stream reached before reading fully").await);
277 }
278 read += r;
279 }
280
281 Ok(())
282 }
283
284 async fn skip_bytes(jvm: &Jvm, _: &mut RuntimeContext, this: ClassInstanceRef<Self>, n: i32) -> Result<i32> {
285 tracing::debug!("java.io.DataInputStream::skipBytes({this:?}, {n:?})");
286
287 let r#in = jvm.get_field(&this, "in", "Ljava/io/InputStream;").await?;
288 let skipped: i64 = jvm.invoke_virtual(&r#in, "java/io/InputStream", "skip", "(J)J", (n as i64,)).await?;
289
290 Ok(skipped as _)
291 }
292
293 async fn read_required_byte(jvm: &Jvm, this: &ClassInstanceRef<Self>) -> Result<u8> {
294 let r#in = jvm.get_field(this, "in", "Ljava/io/InputStream;").await?;
295 let value: i32 = jvm.invoke_virtual(&r#in, "java/io/InputStream", "read", "()I", ()).await?;
296 if value == -1 {
297 return Err(jvm.exception("java/io/EOFException", "End of stream").await);
298 }
299
300 Ok(value as u8)
301 }
302}