• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2023 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 fs from '@ohos.file.fs';
17import Logger from '../util/Logger';
18
19const TAG: string = '[Crypto_Framework]';
20const FILE_SLEEP_TIME: number = 100;
21const TEXT_MAX_READ_LEN: number = 8192;
22// filePicker和文件管理,在OH 4.0.8.2镜像版本,读取uri后,无法直接read、write文件内容,需要sleep几十ms,这里sleep100毫秒
23function sleep(time: number): Promise<number> {
24  return new Promise((resolve) => setTimeout(resolve, time));
25}
26
27class TextFileManager {
28  public static readString: string = '';
29  public static readResult: number = 0;
30  public static writeResult: number = 0;
31
32  async readTextFile(textFileUri: string): Promise<void> {
33    await sleep(FILE_SLEEP_TIME);
34    try {
35      // 读取
36      let file = fs.openSync(textFileUri, fs.OpenMode.READ_ONLY);
37      Logger.info(TAG, 'success, read only file ' + file.fd);
38      // 从文件读取一段内容,限制最大长度为8192
39      let buf = new ArrayBuffer(TEXT_MAX_READ_LEN);
40      try {
41        let readLen: number = fs.readSync(file.fd, buf, { offset: 0 });
42        TextFileManager.readString = String.fromCharCode.apply(null, new Uint8Array(buf.slice(0, readLen)));
43        TextFileManager.readResult = readLen;
44      } catch (error1) {
45        Logger.error(TAG, `file read failed, ${error1.code}, ${error1.message}`);
46      }
47      fs.closeSync(file);
48    } catch (error) {
49      Logger.error(TAG, `file open failed, ${error.code}, ${error.message}`);
50      return;
51    }
52  }
53
54  async writeTextFile(textFileUri: string, textString: string): Promise<void> {
55    await sleep(FILE_SLEEP_TIME);
56    try {
57      // 读写
58      let file = fs.openSync(textFileUri, fs.OpenMode.READ_WRITE);
59      Logger.info(TAG, 'success, read write file' + file.fd);
60      try {
61        // 写入
62        let writeLen = fs.writeSync(file.fd, textString);
63        Logger.info(TAG, 'success, The length of str is: ' + writeLen);
64        TextFileManager.writeResult = writeLen;
65      } catch (error1) {
66        Logger.error(TAG, `file write failed, ${error1.code}, ${error1.message}`);
67      }
68      fs.closeSync(file);
69    } catch (error) {
70      Logger.error(TAG, `file open failed, ${error.code}, ${error.message}`);
71      return;
72    }
73  }
74
75  getString(): string {
76    return TextFileManager.readString;
77  }
78}
79
80export default new TextFileManager();