• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/**
2 * Copyright (c) 2021 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 */
15import BaseModel from './BaseModel';
16import LogUtil from '../baseUtil/LogUtil';
17import FileIo from '@ohos.fileio';
18import { BaseData } from '../bean/BaseData';
19
20const DFAULT_SIZE = 4096;
21const CHAR_CODE_AT_INDEX = 0;
22
23/**
24  * read local file
25  */
26export class BaseParseConfModel extends BaseModel {
27  public getJsonData(fileName: string): BaseData[]{
28    LogUtil.info('settings BaseParseConfModel getJsonData in');
29    return this.readLocalFile(fileName);
30    LogUtil.info('settings BaseParseConfModel getJsonData end');
31  }
32
33  public getJsonDataBase(fileName, callback) {
34    LogUtil.info('settings BaseParseConfModel getJsonDataBase in');
35    callback(this.readLocalFile(fileName));
36    LogUtil.info('settings BaseParseConfModel getJsonDataBase end');
37  }
38  /**
39   * read local file
40   * @param fileName - file name
41   */
42  readLocalFile(fileName): BaseData[]{
43    try {
44      let stream = FileIo.createStreamSync(fileName, 'r');
45      LogUtil.info('settings BaseParseConfModel getJsonData try stream:' + stream);
46      let buf = new ArrayBuffer(DFAULT_SIZE);
47      let len = stream.readSync(buf);
48      LogUtil.info('settings BaseParseConfModel getJsonData try len:' + len);
49      let arr = new Uint8Array(buf);
50      let charAt = ' '.charCodeAt(CHAR_CODE_AT_INDEX);
51      for (let i = len;i < DFAULT_SIZE; i++) {
52        arr[i] = charAt;
53      }
54      let content = String.fromCharCode.apply(null, arr);
55      stream.closeSync();
56      LogUtil.info('settings BaseParseConfModel getJsonData try content:' + JSON.stringify(content));
57      return JSON.parse(content);
58    } catch (jsonError) {
59      LogUtil.info('settings BaseParseConfModel getJsonData catch jsonError:' + jsonError);
60    }
61  }
62}
63
64let baseParseConfModel = new BaseParseConfModel();
65
66export default baseParseConfModel as BaseParseConfModel
67;