!111 在finally里关闭文件流

Merge pull request !111 from 陈怡博/master
This commit is contained in:
openharmony_ci 2024-06-06 07:33:21 +00:00 committed by Gitee
commit bf9b167ed0
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F

View File

@ -20,18 +20,22 @@ import { BusinessError } from '@ohos.base';
export class FileIoModel {
getMediaFileData(mediaUri: string, callback: Function): void {
console.log('CertManager FA getMediaFile start');
let file : fs.File | undefined = undefined;
try {
let file = fs.openSync(mediaUri, fs.OpenMode.READ_ONLY);
file = fs.openSync(mediaUri, fs.OpenMode.READ_ONLY);
let stat = fs.statSync(file.fd);
let buf = new ArrayBuffer(Number(stat.size));
let num = fs.readSync(file.fd, buf);
fs.closeSync(file.fd);
console.log('CertManager FA getMediaFile success');
callback(new Uint8Array(buf));
} catch (err) {
let e: BusinessError = err as BusinessError;
console.error('CertManager FA getMediaFileData failed with err, message: ' + e.message + ', code: ' + e.code);
callback(undefined);
} finally {
if (file !== undefined && file !== null) {
fs.closeSync(file.fd);
}
}
}