diff --git a/interfaces/kits/js/src/mod_fs/ani/bind_function_class.cpp b/interfaces/kits/js/src/mod_fs/ani/bind_function_class.cpp index 9498f847940af3c253fe9f8c846724ddde9c5247..2a4a42ecc763733389080f6f067e4297bba25011 100644 --- a/interfaces/kits/js/src/mod_fs/ani/bind_function_class.cpp +++ b/interfaces/kits/js/src/mod_fs/ani/bind_function_class.cpp @@ -24,6 +24,7 @@ #include "stat_ani.h" #include "unlink_ani.h" #include "rmdir_ani.h" +#include "read_ani.h" using namespace OHOS::FileManagement::ModuleFileIO::ANI; @@ -58,6 +59,7 @@ static ani_status BindFileFs(ani_vm *vm) ani_native_function { "copyFileSync", nullptr, reinterpret_cast(CopyFileAni::CopyFileSync) }, ani_native_function { "statSync", nullptr, reinterpret_cast(StatAni::StatSync) }, ani_native_function { "rmdirSync", "Lstd/core/String;:V", reinterpret_cast(RmdirAni::RmdirSync) }, + ani_native_function {"readSync", nullptr, reinterpret_cast(ReadAni::ReadSync)}, }; return BindClass(vm, className, functions); diff --git a/interfaces/kits/js/src/mod_fs/ani/file_fs_class.ets b/interfaces/kits/js/src/mod_fs/ani/file_fs_class.ets index ba52d3112e3dc794a35ff0a9542cf52d18adf96d..ecb0a70fe50332fcaac7982c84fe5eade430196a 100644 --- a/interfaces/kits/js/src/mod_fs/ani/file_fs_class.ets +++ b/interfaces/kits/js/src/mod_fs/ani/file_fs_class.ets @@ -49,6 +49,7 @@ class fileIo { static native mkdirSync(path: string, recursion: boolean): int; static native moveFileSync(src: String, dest: String, mode?: int): void; static native rmdirSync(path: string): void; + native static readSync(fd: int, buffer: ArrayBuffer, options?: ReadOptions): long; static accessSync(path: string, mode?: AccessModeType): boolean { return fileIo.doAccessSync(path, mode); @@ -430,6 +431,145 @@ function rmdirSyncTest() { console.println("rmdirSyncTest end") } + static readSync1(fd: int, buffer: ArrayBuffer): long { + return fileIo.readSync(fd, buffer); + } + static readSync2(fd: int, buffer: ArrayBuffer, options: ReadOptions): long { + return fileIo.readSync(fd, buffer, options); + } + + static read(fd: int, buffer: ArrayBuffer): Promise { + return new Promise((resolve:(result: long)=>void, reject:(e:BusinessError)=>void) => { + let promise = taskpool.execute(fileIo.readSync1, fd, buffer); + promise.then((ret:NullishType) => { + if (ret === null || ret === undefined) { + let err = new BusinessError(); + err.code = -1; + reject(err); + } else { + let r = ret as long; + resolve(r); + } + }); + }); + } + + static read(fd: int, buffer: ArrayBuffer, options: ReadOptions): Promise { + return new Promise((resolve:(result: long)=>void, reject:(e:BusinessError)=>void) => { + let promise = taskpool.execute(fileIo.readSync2, fd, buffer, options); + promise.then((ret:NullishType) => { + if (ret === null || ret === undefined) { + let err = new BusinessError(); + err.code = -1; + reject(err); + } else { + let r = ret as long; + resolve(r); + } + }); + }); + } + + static read(fd: int, buffer: ArrayBuffer, callback: AsyncCallback): void { + let p1 = taskpool.execute(fileIo.readSync1, fd, buffer); + p1.then((ret: NullishType) => { + let err = new BusinessError(); + if (ret === null || ret === undefined) { + console.println("-------- err code = -1 -------------"); + err.code = -1; + callback(err, undefined); + }else { + console.println("-------- err code = 0 -------------"); + err.code = 0; + let r = ret as long; + callback(err, r); + } + }); + } + + static read(fd: int, buffer: ArrayBuffer, options: ReadOptions, callback: AsyncCallback): void { + let p1 = taskpool.execute(fileIo.readSync2, fd, buffer, options); + p1.then((ret: NullishType) => { + let err = new BusinessError(); + if (ret === null || ret === undefined) { + console.println("-------- err code = -1 -------------"); + err.code = -1; + callback(err, undefined); + }else { + console.println("-------- err code = 0 -------------"); + err.code = 0; + let r = ret as long; + callback(err, r); + } + }); + } + +function readTest() { + console.println("readTest begin") + let fd = fileIo.openSync("/data/local/tmp/a.txt"); + console.println(`open file, fd=${fd}`) + let buffer = new ArrayBuffer(100); + let readOptions1 :ReadOptions = { + offset: 0, + length: 5, + } + + let readOptions2 :ReadOptions = { + offset: 0, + } + + console.println("set readOptions") + + let ret1 = fileIo.readSync(fd, buffer, readOptions1) + console.println("readSync options1 end") + console.println(`read ret1: ${ret1}`) + + let ret2 = fileIo.readSync(fd, buffer, readOptions2) + console.println("readSync options2 end") + console.println(`read ret2: ${ret2}`) + + let ret3 = fileIo.readSync(fd, buffer) + console.println("readSync end") + console.println(`read ret3: ${ret3}`) + + console.println("-----promise-----") + try { + fileIo.read(fd, buffer).then((result: long) => { + console.println("------- end --------") + console.println(result) + });; + } catch (error) { + console.error("Promise: Error getting temp file:", error); + } + + try { + fileIo.read(fd, buffer, readOptions1).then((result: long) => { + console.println("------- end --------") + console.println(result) + });; + } catch (error) { + console.error("Promise: Error getting temp file:", error); + } + try { + fileIo.read(fd, buffer, readOptions2).then((result: long) => { + console.println("------- end --------") + console.println(result) + });; +} catch (error) { + console.error("Promise: Error getting temp dir:", error); +} + console.println("read promise async end"); + console.println("-----callback-----") + fileIo.read(fd, buffer, readOptions1, (err: BusinessError, data?: long) => { + if (err.code) { + console.error("Callback: Error read:", err); + } else { + console.log("Callback: read:", data); + } + }); + console.println("read callback async end"); +} + function stat_test(file: FileDataType) { let stat = fileIo.statSync(file) console.info("stat, ino is " + stat.ino); @@ -458,5 +598,6 @@ function main(){ accessSyncTest() moveSyncTest() rmdirSyncTest() + readTest() console.println("---------- hello ani end ---------------") } \ No newline at end of file