1/** 2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import featureAbility from '@ohos.ability.featureAbility'; 17import fileIo from '@ohos.fileio'; 18import { Log } from '../utils/Log'; 19 20const TAG = 'FileManager'; 21const READ_DATA_SIZE = 4096; 22 23/** 24 * Wrapper class for fileIo interfaces. 25 */ 26export default class FileManager { 27 private baseDir: string | undefined; 28 29 static getInstance(): FileManager { 30 if (globalThis.FileManagerInstance == null) { 31 globalThis.FileManagerInstance = new FileManager(); 32 } 33 return globalThis.FileManagerInstance; 34 } 35 36 private async getBaseDir(): Promise<void> { 37 const context = featureAbility.getContext(); 38 await context.getFilesDir() 39 .then((data) => { 40 Log.showDebug(TAG, `getFilesDir File directory obtained. Data: ${JSON.stringify(data)}`); 41 this.baseDir = data + '/'; 42 }).catch((error) => { 43 Log.showError(TAG, `getFilesDir Failed to obtain the file directory. Cause: ${JSON.stringify(error)}`); 44 }); 45 } 46 47 async getFileContent(fpath: string): Promise<string> { 48 if (this.baseDir == undefined) { 49 await this.getBaseDir(); 50 } 51 Log.showDebug(TAG, `getFileContent fpath: ${fpath}`); 52 const fd = fileIo.openSync(fpath, 0o2); 53 const content = this.getContent(fd); 54 fileIo.closeSync(fd); 55 return content; 56 } 57 58 private getContent(fd): string { 59 const bufArray = []; 60 let totalLength = 0; 61 let buf = new ArrayBuffer(READ_DATA_SIZE); 62 let len = fileIo.readSync(fd, buf); 63 while (len != 0) { 64 Log.showDebug(TAG, `getContent fileIo reading ${len}`); 65 totalLength += len; 66 if (len < READ_DATA_SIZE) { 67 buf = buf.slice(0, len); 68 bufArray.push(buf); 69 break; 70 } 71 bufArray.push(buf); 72 buf = new ArrayBuffer(READ_DATA_SIZE); 73 len = fileIo.readSync(fd, buf); 74 } 75 Log.showDebug(TAG, `getContent read finished ${totalLength}`); 76 const contentBuf = new Uint8Array(totalLength); 77 let offset = 0; 78 for (const bufArr of bufArray) { 79 Log.showDebug(TAG, `getContent collecting ${offset}`); 80 const uInt8Arr = new Uint8Array(bufArr); 81 contentBuf.set(uInt8Arr, offset); 82 offset += uInt8Arr.byteLength; 83 } 84 const content = String.fromCharCode.apply(null, new Uint8Array(contentBuf)); 85 return content; 86 } 87 88 writeToFile(fpath: string, content: string): void { 89 if (this.baseDir == undefined) { 90 this.getBaseDir(); 91 } 92 const fd = fileIo.openSync(this.baseDir + fpath, 0o102, 0o666); 93 fileIo.writeSync(fd, content); 94 fileIo.closeSync(fd); 95 } 96}