|
| 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 | +} |
0 commit comments