16e80583aSopenharmony_ci/**
26e80583aSopenharmony_ci * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
36e80583aSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
46e80583aSopenharmony_ci * you may not use this file except in compliance with the License.
56e80583aSopenharmony_ci * You may obtain a copy of the License at
66e80583aSopenharmony_ci *
76e80583aSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
86e80583aSopenharmony_ci *
96e80583aSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
106e80583aSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
116e80583aSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
126e80583aSopenharmony_ci * See the License for the specific language governing permissions and
136e80583aSopenharmony_ci * limitations under the License.
146e80583aSopenharmony_ci */
156e80583aSopenharmony_ci
166e80583aSopenharmony_ci/**
176e80583aSopenharmony_ci * An util that provides io functionality between file and JSON object.
186e80583aSopenharmony_ci */
196e80583aSopenharmony_ci
206e80583aSopenharmony_ciimport fs from '@ohos.file.fs';
216e80583aSopenharmony_ciimport util from '@ohos.util';
226e80583aSopenharmony_ciimport { Log } from './Log';
236e80583aSopenharmony_ci
246e80583aSopenharmony_ciconst TAG = 'FileUtils';
256e80583aSopenharmony_ci
266e80583aSopenharmony_ciconst READ_DATA_SIZE = 4096;
276e80583aSopenharmony_ci
286e80583aSopenharmony_ciexport default class FileUtils {
296e80583aSopenharmony_ci
306e80583aSopenharmony_ci  /**
316e80583aSopenharmony_ci   * Read Json file from disk by file path.
326e80583aSopenharmony_ci   *
336e80583aSopenharmony_ci   * @param {string} filePath - filePath as the absolute path to the target file.
346e80583aSopenharmony_ci   * @return {any} - read object from file
356e80583aSopenharmony_ci   */
366e80583aSopenharmony_ci  static readJsonFile(filePath: string): any {
376e80583aSopenharmony_ci    Log.showDebug(TAG, 'readJsonFile start execution');
386e80583aSopenharmony_ci    let readStreamSync = null;
396e80583aSopenharmony_ci    try {
406e80583aSopenharmony_ci      readStreamSync = fs.createStreamSync(filePath, 'r');
416e80583aSopenharmony_ci      const content = this.getContent(readStreamSync);
426e80583aSopenharmony_ci      return JSON.parse(content);
436e80583aSopenharmony_ci    } catch (e) {
446e80583aSopenharmony_ci      Log.showError(TAG, `readJsonFile error: ${JSON.stringify(e)}`);
456e80583aSopenharmony_ci    } finally {
466e80583aSopenharmony_ci      readStreamSync.closeSync();
476e80583aSopenharmony_ci    }
486e80583aSopenharmony_ci  }
496e80583aSopenharmony_ci
506e80583aSopenharmony_ci  /**
516e80583aSopenharmony_ci   * Read String from disk by bundleName.
526e80583aSopenharmony_ci   *
536e80583aSopenharmony_ci   * @param {string} filePath - filePath as the absolute path to the target file.
546e80583aSopenharmony_ci   * @return {string} - read string from file
556e80583aSopenharmony_ci   */
566e80583aSopenharmony_ci  static readStringFromFile(filePath: string): string {
576e80583aSopenharmony_ci    Log.showDebug(TAG, 'readStringFromFile start execution');
586e80583aSopenharmony_ci    let readStreamSync = null;
596e80583aSopenharmony_ci    try {
606e80583aSopenharmony_ci      readStreamSync = fs.createStreamSync(filePath, 'r');
616e80583aSopenharmony_ci      const content = this.getContent(readStreamSync);
626e80583aSopenharmony_ci      return content;
636e80583aSopenharmony_ci    } catch (e) {
646e80583aSopenharmony_ci      Log.showError(TAG, `readStringFromFile error: ${JSON.stringify(e)}, filePath: ${filePath}`);
656e80583aSopenharmony_ci    } finally {
666e80583aSopenharmony_ci      if (readStreamSync) {
676e80583aSopenharmony_ci        readStreamSync.closeSync();
686e80583aSopenharmony_ci      }
696e80583aSopenharmony_ci    }
706e80583aSopenharmony_ci  }
716e80583aSopenharmony_ci
726e80583aSopenharmony_ci  /**
736e80583aSopenharmony_ci   * Write string to a file.
746e80583aSopenharmony_ci   *
756e80583aSopenharmony_ci   * @param {string} str - target string will be written to file.
766e80583aSopenharmony_ci   * @param {string} filePath - filePath as the absolute path to the target file.
776e80583aSopenharmony_ci   */
786e80583aSopenharmony_ci  static writeStringToFile(str: string, filePath: string): void {
796e80583aSopenharmony_ci    Log.showDebug(TAG, 'writeStringToFile start execution');
806e80583aSopenharmony_ci    let writeStreamSync = null;
816e80583aSopenharmony_ci    try {
826e80583aSopenharmony_ci      writeStreamSync = fs.createStreamSync(filePath, 'w+');
836e80583aSopenharmony_ci      let number = writeStreamSync.writeSync(str);
846e80583aSopenharmony_ci      Log.showDebug(TAG, 'writeStringToFile number: ' + number);
856e80583aSopenharmony_ci    } catch (e) {
866e80583aSopenharmony_ci      Log.showError(TAG, `writeStringToFile error: ${JSON.stringify(e)}`);
876e80583aSopenharmony_ci    } finally {
886e80583aSopenharmony_ci      writeStreamSync.closeSync();
896e80583aSopenharmony_ci      Log.showDebug(TAG, 'writeStringToFile close sync');
906e80583aSopenharmony_ci    }
916e80583aSopenharmony_ci  }
926e80583aSopenharmony_ci
936e80583aSopenharmony_ci  /**
946e80583aSopenharmony_ci   * Read JSON object from a file.
956e80583aSopenharmony_ci   *
966e80583aSopenharmony_ci   * @param {object} readStreamSync - stream of target file
976e80583aSopenharmony_ci   * @return {object} - object read from file stream
986e80583aSopenharmony_ci   */
996e80583aSopenharmony_ci  static getContent(readStreamSync: any): string {
1006e80583aSopenharmony_ci    Log.showDebug(TAG, 'getContent start');
1016e80583aSopenharmony_ci    const bufArray = [];
1026e80583aSopenharmony_ci    let totalLength = 0;
1036e80583aSopenharmony_ci    let buf = new ArrayBuffer(READ_DATA_SIZE);
1046e80583aSopenharmony_ci    let len = readStreamSync.readSync(buf);
1056e80583aSopenharmony_ci    while (len != 0) {
1066e80583aSopenharmony_ci      Log.showDebug(TAG, `getContent FileIO reading ${len}`);
1076e80583aSopenharmony_ci      totalLength += len;
1086e80583aSopenharmony_ci      if (len < READ_DATA_SIZE) {
1096e80583aSopenharmony_ci        buf = buf.slice(0, len);
1106e80583aSopenharmony_ci        bufArray.push(buf);
1116e80583aSopenharmony_ci        break;
1126e80583aSopenharmony_ci      }
1136e80583aSopenharmony_ci      bufArray.push(buf);
1146e80583aSopenharmony_ci      buf = new ArrayBuffer(READ_DATA_SIZE);
1156e80583aSopenharmony_ci      len = readStreamSync.readSync(buf);
1166e80583aSopenharmony_ci    }
1176e80583aSopenharmony_ci    Log.showDebug(TAG, `getContent read finished: ${totalLength}`);
1186e80583aSopenharmony_ci    const contentBuf = new Uint8Array(totalLength);
1196e80583aSopenharmony_ci    let offset = 0;
1206e80583aSopenharmony_ci    for (const bufArr of bufArray) {
1216e80583aSopenharmony_ci      Log.showDebug(TAG, `getContent collecting: ${offset}`);
1226e80583aSopenharmony_ci      const uInt8Arr = new Uint8Array(bufArr);
1236e80583aSopenharmony_ci      contentBuf.set(uInt8Arr, offset);
1246e80583aSopenharmony_ci      offset += uInt8Arr.byteLength;
1256e80583aSopenharmony_ci    }
1266e80583aSopenharmony_ci    let textDecoder = util.TextDecoder.create('utf-8', {ignoreBOM: true});
1276e80583aSopenharmony_ci    const content = textDecoder.decodeWithStream(contentBuf, {stream: false});
1286e80583aSopenharmony_ci    return content;
1296e80583aSopenharmony_ci  }
1306e80583aSopenharmony_ci
1316e80583aSopenharmony_ci  /**
1326e80583aSopenharmony_ci   * Check if the file exists.
1336e80583aSopenharmony_ci   *
1346e80583aSopenharmony_ci   * @param {string} filePath - filePath as the absolute path to the target file.
1356e80583aSopenharmony_ci   * @return {boolean} - boolean true(Exist)
1366e80583aSopenharmony_ci   */
1376e80583aSopenharmony_ci  static isExist(filePath: string): boolean {
1386e80583aSopenharmony_ci    let result: boolean = false;
1396e80583aSopenharmony_ci    try {
1406e80583aSopenharmony_ci      result = fs.accessSync(filePath);
1416e80583aSopenharmony_ci      Log.showDebug(TAG, 'accessSync success.');
1426e80583aSopenharmony_ci    } catch(e) {
1436e80583aSopenharmony_ci      Log.showError(TAG, `isExit error: ${JSON.stringify(e)}`);
1446e80583aSopenharmony_ci      result = false;
1456e80583aSopenharmony_ci    }
1466e80583aSopenharmony_ci    return result;
1476e80583aSopenharmony_ci  }
1486e80583aSopenharmony_ci
1496e80583aSopenharmony_ci  /**
1506e80583aSopenharmony_ci   * Delete Files.
1516e80583aSopenharmony_ci   *
1526e80583aSopenharmony_ci   * @param {string} filePath - filePath as the absolute path to the target file.
1536e80583aSopenharmony_ci   */
1546e80583aSopenharmony_ci  static deleteConfigFile(filePath: string): void {
1556e80583aSopenharmony_ci    try {
1566e80583aSopenharmony_ci      fs.unlinkSync(filePath);
1576e80583aSopenharmony_ci    } catch(e) {
1586e80583aSopenharmony_ci      Log.showError(TAG, `deleteFile error: ${JSON.stringify(e)}`);
1596e80583aSopenharmony_ci    }
1606e80583aSopenharmony_ci  }
1616e80583aSopenharmony_ci}