1e75ebbc8Sopenharmony_ci/**
2e75ebbc8Sopenharmony_ci * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3e75ebbc8Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4e75ebbc8Sopenharmony_ci * you may not use this file except in compliance with the License.
5e75ebbc8Sopenharmony_ci * You may obtain a copy of the License at
6e75ebbc8Sopenharmony_ci *
7e75ebbc8Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8e75ebbc8Sopenharmony_ci *
9e75ebbc8Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10e75ebbc8Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11e75ebbc8Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12e75ebbc8Sopenharmony_ci * See the License for the specific language governing permissions and
13e75ebbc8Sopenharmony_ci * limitations under the License.
14e75ebbc8Sopenharmony_ci */
15e75ebbc8Sopenharmony_ci
16e75ebbc8Sopenharmony_ciimport Log from './Log';
17e75ebbc8Sopenharmony_ciimport FileIo from '@ohos.fileio';
18e75ebbc8Sopenharmony_ci
19e75ebbc8Sopenharmony_ciconst DFAULT_SIZE = 4096;
20e75ebbc8Sopenharmony_ciconst CHAR_CODE_AT_INDEX = 0;
21e75ebbc8Sopenharmony_ciconst TAG = 'ReadConfigUtil';
22e75ebbc8Sopenharmony_ci
23e75ebbc8Sopenharmony_ciexport class ReadConfigUtil {
24e75ebbc8Sopenharmony_ci  readConfigFile(fileName: string): any {
25e75ebbc8Sopenharmony_ci    Log.showDebug(TAG, `readConfigFile fileName:${fileName}`);
26e75ebbc8Sopenharmony_ci    try {
27e75ebbc8Sopenharmony_ci      let stream = FileIo.createStreamSync(fileName, 'r');
28e75ebbc8Sopenharmony_ci      Log.showDebug(TAG, `readConfigFile stream:${JSON.stringify(stream)}`);
29e75ebbc8Sopenharmony_ci      let buf = new ArrayBuffer(DFAULT_SIZE);
30e75ebbc8Sopenharmony_ci      let len = stream.readSync(buf);
31e75ebbc8Sopenharmony_ci      Log.showDebug(TAG, `readConfigFile len:${len}`);
32e75ebbc8Sopenharmony_ci      let arr = new Uint8Array(buf);
33e75ebbc8Sopenharmony_ci      let charAt = ' '.charCodeAt(CHAR_CODE_AT_INDEX);
34e75ebbc8Sopenharmony_ci      for (let i = len;i < DFAULT_SIZE; i++) {
35e75ebbc8Sopenharmony_ci        arr[i] = charAt;
36e75ebbc8Sopenharmony_ci      }
37e75ebbc8Sopenharmony_ci      let content = String.fromCharCode.apply(null, arr);
38e75ebbc8Sopenharmony_ci      stream.closeSync();
39e75ebbc8Sopenharmony_ci      Log.showDebug(TAG, `readConfigFile content:${JSON.stringify(content)}`);
40e75ebbc8Sopenharmony_ci      return JSON.parse(content);
41e75ebbc8Sopenharmony_ci    } catch (error) {
42e75ebbc8Sopenharmony_ci      Log.showError(TAG, `readConfigFile error:${JSON.stringify(error)}`);
43e75ebbc8Sopenharmony_ci    }
44e75ebbc8Sopenharmony_ci  }
45e75ebbc8Sopenharmony_ci}
46e75ebbc8Sopenharmony_ci
47e75ebbc8Sopenharmony_cilet readConfigUtil = new ReadConfigUtil();
48e75ebbc8Sopenharmony_ci
49e75ebbc8Sopenharmony_ciexport default readConfigUtil;
50