Skip to content

Commit be6ec38

Browse files
committed
Remove load_bytes, store_bytes, instead expose array raw buffer
1 parent 37d0f8d commit be6ec38

23 files changed

+160
-135
lines changed

java_runtime/src/classes/java/io/buffered_reader.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,6 @@ impl BufferedReader {
103103

104104
#[cfg(test)]
105105
mod test {
106-
use bytemuck::cast_vec;
107-
108106
use jvm::{runtime::JavaLangString, ClassInstanceRef, Result};
109107

110108
use crate::{classes::java::lang::String, test::test_jvm};
@@ -114,7 +112,7 @@ mod test {
114112
let jvm = test_jvm().await?;
115113

116114
let mut buffer = jvm.instantiate_array("B", 11).await?;
117-
jvm.store_byte_array(&mut buffer, 0, cast_vec(b"Hello\nWorld".to_vec())).await?;
115+
jvm.array_raw_buffer_mut(&mut buffer).await?.write(0, b"Hello\nWorld")?;
118116

119117
let is = jvm.new_class("java/io/ByteArrayInputStream", "([B)V", (buffer,)).await?;
120118
let isr = jvm.new_class("java/io/InputStreamReader", "(Ljava/io/InputStream;)V", (is,)).await?;

java_runtime/src/classes/java/io/byte_array_input_stream.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,11 @@ impl ByteArrayInputStream {
9393
return Ok(-1);
9494
}
9595

96-
let result = jvm.load_byte_array(&buf, pos as _, 1).await?[0] as u8;
96+
let result: i8 = jvm.load_array(&buf, pos as _, 1).await?[0];
9797

9898
jvm.put_field(&mut this, "pos", "I", pos + 1).await?;
9999

100-
Ok(result as _)
100+
Ok(result as u8 as _)
101101
}
102102

103103
async fn close(_: &Jvm, _: &mut RuntimeContext, this: ClassInstanceRef<ByteArrayInputStream>) -> Result<()> {

java_runtime/src/classes/java/io/byte_array_output_stream.rs

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use alloc::{vec, vec::Vec};
1+
use alloc::vec;
22

33
use java_class_proto::{JavaFieldProto, JavaMethodProto};
44
use jvm::{Array, ClassInstanceRef, Jvm, Result};
@@ -57,7 +57,7 @@ impl ByteArrayOutputStream {
5757
Self::ensure_capacity(jvm, &mut this, (pos + 1) as _).await?;
5858

5959
let mut buf = jvm.get_field(&this, "buf", "[B").await?;
60-
jvm.store_byte_array(&mut buf, pos as _, vec![b as i8]).await?;
60+
jvm.store_array(&mut buf, pos as _, vec![b as i8]).await?;
6161

6262
jvm.put_field(&mut this, "pos", "I", pos + 1).await?;
6363

@@ -67,28 +67,41 @@ impl ByteArrayOutputStream {
6767
async fn to_byte_array(jvm: &Jvm, _: &mut RuntimeContext, this: ClassInstanceRef<Self>) -> Result<ClassInstanceRef<Array<i8>>> {
6868
tracing::debug!("java.io.ByteArrayOutputStream::to_byte_array({:?})", &this);
6969

70-
let buf = jvm.get_field(&this, "buf", "[B").await?;
70+
let buf: ClassInstanceRef<Array<i8>> = jvm.get_field(&this, "buf", "[B").await?;
7171
let pos: i32 = jvm.get_field(&this, "pos", "I").await?;
7272

73-
let bytes = jvm.load_byte_array(&buf, 0, pos as _).await?;
74-
let mut array = jvm.instantiate_array("B", bytes.len()).await?;
75-
jvm.store_byte_array(&mut array, 0, bytes).await?;
73+
let dest = jvm.instantiate_array("B", pos as _).await?;
74+
let _: () = jvm
75+
.invoke_static(
76+
"java/lang/System",
77+
"arraycopy",
78+
"(Ljava/lang/Object;ILjava/lang/Object;II)V",
79+
(buf.clone(), 0, dest.clone(), 0, pos),
80+
)
81+
.await?;
7682

77-
Ok(array.into())
83+
Ok(dest.into())
7884
}
7985

8086
async fn ensure_capacity(jvm: &Jvm, this: &mut ClassInstanceRef<Self>, capacity: usize) -> Result<()> {
8187
let old_buf = jvm.get_field(this, "buf", "[B").await?;
8288
let current_capacity = jvm.array_length(&old_buf).await?;
8389

8490
if current_capacity < capacity {
85-
let old_values: Vec<i8> = jvm.load_byte_array(&old_buf, 0, current_capacity).await?;
8691
let new_capacity = capacity * 2;
92+
let new_buf = jvm.instantiate_array("B", new_capacity).await?;
93+
94+
let _: () = jvm
95+
.invoke_static(
96+
"java/lang/System",
97+
"arraycopy",
98+
"(Ljava/lang/Object;ILjava/lang/Object;II)V",
99+
(old_buf.clone(), 0, new_buf.clone(), 0, current_capacity as i32),
100+
)
101+
.await?;
87102

88-
let mut new_buf = jvm.instantiate_array("B", new_capacity).await?;
89103
jvm.put_field(this, "buf", "[B", new_buf.clone()).await?;
90-
jvm.store_byte_array(&mut new_buf, 0, old_values).await?;
91-
jvm.destroy(old_buf)?;
104+
jvm.destroy(old_buf)?; // temporary before GC
92105
}
93106

94107
Ok(())
@@ -97,6 +110,7 @@ impl ByteArrayOutputStream {
97110

98111
#[cfg(test)]
99112
mod test {
113+
use alloc::vec;
100114

101115
use bytemuck::cast_vec;
102116

@@ -117,7 +131,9 @@ mod test {
117131

118132
let buf = jvm.invoke_virtual(&stream, "toByteArray", "()[B", ()).await?;
119133

120-
let bytes = jvm.load_byte_array(&buf, 0, 5).await?;
134+
let mut bytes = vec![0; 5];
135+
jvm.array_raw_buffer(&buf).await?.read(0, &mut bytes)?;
136+
121137
assert_eq!(bytes, cast_vec(b"Hello".to_vec()));
122138

123139
Ok(())

java_runtime/src/classes/java/io/data_input_stream.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
use alloc::{string::String as RustString, vec};
22

3-
use bytemuck::cast_vec;
4-
53
use java_class_proto::{JavaFieldProto, JavaMethodProto};
64
use jvm::{runtime::JavaLangString, Array, ClassInstanceRef, JavaChar, Jvm, Result};
75

@@ -218,10 +216,11 @@ impl DataInputStream {
218216
let java_array = jvm.instantiate_array("B", length as _).await?;
219217
let _: i32 = jvm.invoke_virtual(&this, "read", "([BII)I", (java_array.clone(), 0, length)).await?;
220218

221-
let bytes = jvm.load_byte_array(&java_array, 0, length as _).await?;
219+
let mut buf = vec![0; length as _];
220+
jvm.array_raw_buffer(&java_array).await?.read(0, &mut buf)?;
222221

223222
// TODO handle modified utf-8
224-
let string = RustString::from_utf8(cast_vec(bytes)).unwrap();
223+
let string = RustString::from_utf8(buf).unwrap();
225224

226225
Ok(JavaLangString::from_rust_string(jvm, &string).await?.into())
227226
}
@@ -257,7 +256,7 @@ mod test {
257256
let data_len = data.len();
258257

259258
let mut data_array = jvm.instantiate_array("B", data_len).await?;
260-
jvm.store_byte_array(&mut data_array, 0, data).await?;
259+
jvm.array_raw_buffer_mut(&mut data_array).await?.write(0, &data)?;
261260

262261
let input_stream = jvm.new_class("java/io/ByteArrayInputStream", "([B)V", (data_array,)).await?;
263262
let data_input_stream = jvm
@@ -299,7 +298,7 @@ mod test {
299298
let data_len = data.len();
300299

301300
let mut data_array = jvm.instantiate_array("B", data_len).await?;
302-
jvm.store_byte_array(&mut data_array, 0, data).await?;
301+
jvm.array_raw_buffer_mut(&mut data_array).await?.write(0, &data)?;
303302

304303
let input_stream = jvm.new_class("java/io/ByteArrayInputStream", "([B)V", (data_array,)).await?;
305304
let data_input_stream = jvm

java_runtime/src/classes/java/io/data_output_stream.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ impl DataOutputStream {
5353

5454
let bytes = i.to_be_bytes();
5555
let mut byte_array = jvm.instantiate_array("B", bytes.len() as _).await?;
56-
jvm.store_byte_array(&mut byte_array, 0, cast_vec(bytes.to_vec())).await?;
56+
jvm.store_array(&mut byte_array, 0, cast_vec::<u8, i8>(bytes.to_vec())).await?;
5757

5858
let out = jvm.get_field(&this, "out", "Ljava/io/OutputStream;").await?;
5959
let _: () = jvm.invoke_virtual(&out, "write", "([B)V", (byte_array,)).await?;
@@ -66,7 +66,7 @@ impl DataOutputStream {
6666

6767
let bytes = l.to_be_bytes();
6868
let mut byte_array = jvm.instantiate_array("B", bytes.len() as _).await?;
69-
jvm.store_byte_array(&mut byte_array, 0, cast_vec(bytes.to_vec())).await?;
69+
jvm.store_array(&mut byte_array, 0, cast_vec::<u8, i8>(bytes.to_vec())).await?;
7070

7171
let out = jvm.get_field(&this, "out", "Ljava/io/OutputStream;").await?;
7272
let _: () = jvm.invoke_virtual(&out, "write", "([B)V", (byte_array,)).await?;
@@ -99,7 +99,6 @@ impl DataOutputStream {
9999
mod test {
100100
use alloc::vec;
101101

102-
use bytemuck::cast_vec;
103102
use jvm::{runtime::JavaLangString, Result};
104103

105104
use crate::test::test_jvm;
@@ -122,10 +121,13 @@ mod test {
122121
let _: () = jvm.invoke_virtual(&data_output_stream, "writeLong", "(J)V", (123412341324i64,)).await?;
123122

124123
let bytes = jvm.invoke_virtual(&stream, "toByteArray", "()[B", ()).await?;
125-
let bytes = jvm.load_byte_array(&bytes, 0, jvm.array_length(&bytes).await? as _).await?;
124+
125+
let length = jvm.array_length(&bytes).await?;
126+
let mut buf = vec![0; length];
127+
jvm.array_raw_buffer(&bytes).await?.read(0, &mut buf)?;
126128

127129
assert_eq!(
128-
cast_vec::<i8, u8>(bytes),
130+
buf,
129131
vec![
130132
1, b'h', b'e', b'l', b'l', b'o', b',', b' ', b'w', b'o', b'r', b'l', b'd', 0, 0xbc, 0x4f, 0xf2, 0, 0, 0, 0x1c, 0xbb, 0xf2, 0xe2, 0x4c
131133
]

java_runtime/src/classes/java/io/file_input_stream.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use alloc::vec;
22

33
use bytemuck::cast_vec;
4+
45
use java_class_proto::{JavaFieldProto, JavaMethodProto};
56
use jvm::{runtime::JavaLangString, Array, ClassInstanceRef, Jvm, Result};
67

@@ -58,7 +59,7 @@ impl FileInputStream {
5859
return Ok(-1);
5960
}
6061

61-
jvm.store_byte_array(&mut buf, 0, cast_vec(rust_buf)).await?;
62+
jvm.store_array(&mut buf, 0, cast_vec::<u8, i8>(rust_buf)).await?;
6263

6364
Ok(read as _)
6465
}

java_runtime/src/classes/java/io/file_output_stream.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ impl FileOutputStream {
8484
let fd = jvm.get_field(&this, "fd", "Ljava/io/FileDescriptor;").await?;
8585
let mut file = FileDescriptor::file(jvm, fd).await?;
8686

87-
let buf = jvm.load_byte_array(&buffer, offset as _, length as _).await?;
87+
let mut buf = vec![0; length as _];
88+
jvm.array_raw_buffer(&buffer).await?.read(offset as _, &mut buf).unwrap();
8889

8990
file.write(cast_slice(&buf)).await.unwrap();
9091

java_runtime/src/classes/java/io/input_stream_reader.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,8 @@ impl InputStreamReader {
110110
}
111111

112112
let read_buf_size: i32 = jvm.get_field(&this, "readBufSize", "I").await?;
113-
let read_buf_data = jvm.load_byte_array(&read_buf, 0, read_buf_size as _).await?;
113+
let mut read_buf_data = vec![0; read_buf_size as _];
114+
jvm.array_raw_buffer(&read_buf).await?.read(0, &mut read_buf_data).unwrap();
114115

115116
let decoder: Arc<Mutex<Decoder>> = jvm.get_rust_object_field(&this, "decoder").await?;
116117

@@ -182,8 +183,6 @@ impl InputStreamReader {
182183
mod test {
183184
use alloc::{vec, vec::Vec};
184185

185-
use bytemuck::cast_vec;
186-
187186
use jvm::{JavaChar, Result};
188187

189188
use crate::test::test_jvm;
@@ -193,7 +192,7 @@ mod test {
193192
let jvm = test_jvm().await?;
194193

195194
let mut buffer = jvm.instantiate_array("B", 11).await?;
196-
jvm.store_byte_array(&mut buffer, 0, cast_vec(b"Hello\nWorld".to_vec())).await?;
195+
jvm.array_raw_buffer_mut(&mut buffer).await?.write(0, b"Hello\nWorld")?;
197196

198197
let is = jvm.new_class("java/io/ByteArrayInputStream", "([B)V", (buffer,)).await?;
199198
let isr = jvm.new_class("java/io/InputStreamReader", "(Ljava/io/InputStream;)V", (is,)).await?;

java_runtime/src/classes/java/io/output_stream.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ impl OutputStream {
5252
) -> Result<()> {
5353
tracing::debug!("java.io.OutputStream::write({:?}, {:?}, {:?}, {:?})", &this, &buffer, &offset, &length);
5454

55-
let bytes = jvm.load_byte_array(&buffer, offset as _, length as _).await?;
55+
let mut bytes = vec![0; length as usize];
56+
jvm.array_raw_buffer(&buffer).await?.read(offset as _, &mut bytes)?;
5657
for byte in bytes {
5758
let _: () = jvm.invoke_virtual(&this, "write", "(I)V", (byte as i32,)).await?;
5859
}

java_runtime/src/classes/java/io/print_stream.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
use alloc::{format, string::ToString, vec};
22

3-
use bytemuck::cast_vec;
4-
53
use java_class_proto::JavaMethodProto;
64
use jvm::{runtime::JavaLangString, ClassInstanceRef, JavaChar, Jvm, Result};
75

@@ -62,7 +60,7 @@ impl PrintStream {
6260
let bytes = result.into_bytes();
6361

6462
let mut string_bytes = jvm.instantiate_array("B", bytes.len()).await?;
65-
jvm.store_byte_array(&mut string_bytes, 0, cast_vec(bytes)).await?;
63+
jvm.array_raw_buffer_mut(&mut string_bytes).await?.write(0, &bytes)?;
6664

6765
let _: () = jvm.invoke_virtual(&this, "write", "([B)V", (string_bytes,)).await?;
6866

@@ -81,7 +79,7 @@ impl PrintStream {
8179
let bytes = result.into_bytes();
8280

8381
let mut string_bytes = jvm.instantiate_array("B", bytes.len()).await?;
84-
jvm.store_byte_array(&mut string_bytes, 0, cast_vec(bytes)).await?;
82+
jvm.array_raw_buffer_mut(&mut string_bytes).await?.write(0, &bytes)?;
8583

8684
let _: () = jvm.invoke_virtual(&this, "write", "([B)V", (string_bytes,)).await?;
8785

0 commit comments

Comments
 (0)