Skip to content

Commit 0dce04a

Browse files
committed
Implement fileinputstream::read and test
1 parent a984b55 commit 0dce04a

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ impl FileInputStream {
2222
methods: vec![
2323
JavaMethodProto::new("<init>", "(Ljava/io/File;)V", Self::init, Default::default()),
2424
JavaMethodProto::new("read", "([B)I", Self::read, Default::default()),
25+
JavaMethodProto::new("read", "()I", Self::read_byte, Default::default()),
2526
JavaMethodProto::new("close", "()V", Self::close, Default::default()),
2627
],
2728
fields: vec![JavaFieldProto::new("fd", "Ljava/io/FileDescriptor;", Default::default())],
@@ -68,6 +69,21 @@ impl FileInputStream {
6869
Ok(read as _)
6970
}
7071

72+
async fn read_byte(jvm: &Jvm, _: &mut RuntimeContext, this: ClassInstanceRef<Self>) -> Result<i32> {
73+
tracing::debug!("java.io.FileInputStream::read({:?})", &this);
74+
75+
let fd = jvm.get_field(&this, "fd", "Ljava/io/FileDescriptor;").await?;
76+
let mut rust_file = FileDescriptor::file(jvm, fd).await?;
77+
78+
let mut buf = [0; 1];
79+
let read = rust_file.read(&mut buf).await.unwrap();
80+
if read == 0 {
81+
return Ok(-1);
82+
}
83+
84+
Ok(buf[0] as i32)
85+
}
86+
7187
async fn close(_jvm: &Jvm, _: &mut RuntimeContext, this: ClassInstanceRef<Self>) -> Result<()> {
7288
tracing::debug!("stub java.io.FileInputStream::close({:?})", &this);
7389

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
use alloc::vec;
2+
3+
use jvm::{Result, runtime::JavaLangString};
4+
5+
use test_utils::test_jvm_filesystem;
6+
7+
#[tokio::test]
8+
async fn test_file_input_stream_read_buf() -> Result<()> {
9+
let filesystem = [("test.txt".into(), b"hello world".to_vec())];
10+
let jvm = test_jvm_filesystem(filesystem.into_iter().collect()).await?;
11+
12+
let file = JavaLangString::from_rust_string(&jvm, "test.txt").await?;
13+
14+
let java_file = jvm.new_class("java/io/File", "(Ljava/lang/String;)V", (file,)).await?;
15+
let fis = jvm.new_class("java/io/FileInputStream", "(Ljava/io/File;)V", (java_file,)).await?;
16+
17+
let bytes = jvm.instantiate_array("B", 11).await?;
18+
let read: i32 = jvm.invoke_virtual(&fis, "read", "([B)I", (bytes.clone(),)).await?;
19+
20+
let length = jvm.array_length(&bytes).await?;
21+
let mut buf = vec![0; length];
22+
jvm.array_raw_buffer(&bytes).await?.read(0, &mut buf)?;
23+
24+
assert_eq!(read, 11);
25+
assert_eq!(buf, vec![104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100]);
26+
27+
Ok(())
28+
}
29+
30+
#[tokio::test]
31+
async fn test_file_input_stream_read_int() -> Result<()> {
32+
let filesystem = [("test.txt".into(), b"hello world".to_vec())];
33+
let jvm = test_jvm_filesystem(filesystem.into_iter().collect()).await?;
34+
35+
let file = JavaLangString::from_rust_string(&jvm, "test.txt").await?;
36+
37+
let java_file = jvm.new_class("java/io/File", "(Ljava/lang/String;)V", (file,)).await?;
38+
let fis = jvm.new_class("java/io/FileInputStream", "(Ljava/io/File;)V", (java_file,)).await?;
39+
40+
let read: i32 = jvm.invoke_virtual(&fis, "read", "()I", ()).await?;
41+
42+
assert_eq!(read, 104);
43+
44+
Ok(())
45+
}

java_runtime/tests/classes/java/io/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ mod buffered_reader;
22
mod byte_array_output_stream;
33
mod data_input_stream;
44
mod data_output_stream;
5+
mod file_input_stream;
56
mod input_stream_reader;
67
mod print_writer;
78
mod random_access_file;

0 commit comments

Comments
 (0)