Skip to content

Commit 8a1bef7

Browse files
committed
Add FileInpustStream::available()
1 parent 49a0b23 commit 8a1bef7

File tree

3 files changed

+44
-5
lines changed

3 files changed

+44
-5
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("available", "()I", Self::available, Default::default()),
2526
JavaMethodProto::new("read", "()I", Self::read_byte, Default::default()),
2627
JavaMethodProto::new("close", "()V", Self::close, Default::default()),
2728
],
@@ -50,6 +51,21 @@ impl FileInputStream {
5051
Ok(())
5152
}
5253

54+
async fn available(jvm: &Jvm, _: &mut RuntimeContext, this: ClassInstanceRef<Self>) -> Result<i32> {
55+
tracing::debug!("java.io.FileInputStream::available({:?})", &this);
56+
57+
let fd = jvm.get_field(&this, "fd", "Ljava/io/FileDescriptor;").await?;
58+
let rust_file = FileDescriptor::file(jvm, fd).await?;
59+
60+
// TODO get os buffer size
61+
let stat = rust_file.metadata().await.unwrap();
62+
let tell = rust_file.tell().await.unwrap();
63+
64+
let available = stat.size - tell;
65+
66+
Ok(available as _)
67+
}
68+
5369
async fn read(jvm: &Jvm, _: &mut RuntimeContext, this: ClassInstanceRef<Self>, mut buf: ClassInstanceRef<Array<i8>>) -> Result<i32> {
5470
tracing::debug!("java.io.FileInputStream::read({:?}, {:?})", &this, &buf);
5571

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,20 @@ async fn test_file_input_stream_read_int() -> Result<()> {
4343

4444
Ok(())
4545
}
46+
47+
#[tokio::test]
48+
async fn test_file_input_stream_available() -> Result<()> {
49+
let filesystem = [("test.txt".into(), b"hello world".to_vec())];
50+
let jvm = test_jvm_filesystem(filesystem.into_iter().collect()).await?;
51+
52+
let file = JavaLangString::from_rust_string(&jvm, "test.txt").await?;
53+
54+
let java_file = jvm.new_class("java/io/File", "(Ljava/lang/String;)V", (file,)).await?;
55+
let fis = jvm.new_class("java/io/FileInputStream", "(Ljava/io/File;)V", (java_file,)).await?;
56+
57+
let available: i32 = jvm.invoke_virtual(&fis, "available", "()I", ()).await?;
58+
59+
assert!(available > 0);
60+
61+
Ok(())
62+
}

test_utils/src/lib.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,12 @@ impl Runtime for TestRuntime {
123123
#[derive(Clone)]
124124
struct DummyFile {
125125
data: Vec<u8>,
126+
pos: FileSize,
126127
}
127128

128129
impl DummyFile {
129130
pub fn new(data: Vec<u8>) -> Self {
130-
Self { data }
131+
Self { data, pos: 0 }
131132
}
132133
}
133134

@@ -145,20 +146,25 @@ impl File for DummyFile {
145146
Err(IOError::Unsupported)
146147
}
147148

148-
async fn seek(&mut self, _pos: FileSize) -> IOResult<()> {
149-
Err(IOError::Unsupported)
149+
async fn seek(&mut self, pos: FileSize) -> IOResult<()> {
150+
self.pos = pos;
151+
152+
Ok(())
150153
}
151154

152155
async fn tell(&self) -> IOResult<FileSize> {
153-
Err(IOError::Unsupported)
156+
Ok(self.pos as _)
154157
}
155158

156159
async fn set_len(&mut self, _len: FileSize) -> IOResult<()> {
157160
Err(IOError::Unsupported)
158161
}
159162

160163
async fn metadata(&self) -> IOResult<FileStat> {
161-
Err(IOError::Unsupported)
164+
Ok(FileStat {
165+
size: self.data.len() as FileSize,
166+
r#type: FileType::File,
167+
})
162168
}
163169
}
164170

0 commit comments

Comments
 (0)