diff --git a/code/ArkTS1.2/FilesSample/.gitignore b/code/ArkTS1.2/FilesSample/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..d2ff20141ceed86d87c0ea5d99481973005bab2b --- /dev/null +++ b/code/ArkTS1.2/FilesSample/.gitignore @@ -0,0 +1,12 @@ +/node_modules +/oh_modules +/local.properties +/.idea +**/build +/.hvigor +.cxx +/.clangd +/.clang-format +/.clang-tidy +**/.test +/.appanalyzer \ No newline at end of file diff --git a/code/ArkTS1.2/FilesSample/AppScope/app.json5 b/code/ArkTS1.2/FilesSample/AppScope/app.json5 new file mode 100644 index 0000000000000000000000000000000000000000..97d9b582d6fcdd149dccff6ae9b0f89e31e72d88 --- /dev/null +++ b/code/ArkTS1.2/FilesSample/AppScope/app.json5 @@ -0,0 +1,36 @@ +/** + * + * Copyright (c) 2025 Huawei Device Co., Ltd. + * + * All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice,this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS, + * + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +{ + "app": { + "bundleName": "com.samples.filessample", + "vendor": "example", + "versionCode": 1000000, + "versionName": "1.0.0", + "icon": "$media:app_icon", + "label": "$string:app_name" + } +} diff --git a/code/ArkTS1.2/FilesSample/AppScope/resources/base/element/string.json b/code/ArkTS1.2/FilesSample/AppScope/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..baa7bc6a69431300847f525d11c76083f1a9f92a --- /dev/null +++ b/code/ArkTS1.2/FilesSample/AppScope/resources/base/element/string.json @@ -0,0 +1,8 @@ +{ + "string": [ + { + "name": "app_name", + "value": "FilesSample" + } + ] +} diff --git a/code/ArkTS1.2/FilesSample/AppScope/resources/base/media/app_icon.png b/code/ArkTS1.2/FilesSample/AppScope/resources/base/media/app_icon.png new file mode 100644 index 0000000000000000000000000000000000000000..a39445dc87828b76fed6d2ec470dd455c45319e3 Binary files /dev/null and b/code/ArkTS1.2/FilesSample/AppScope/resources/base/media/app_icon.png differ diff --git a/code/ArkTS1.2/FilesSample/README.md b/code/ArkTS1.2/FilesSample/README.md new file mode 100644 index 0000000000000000000000000000000000000000..1e174fd59b77307e0b1759076a9e1b7c3d738325 --- /dev/null +++ b/code/ArkTS1.2/FilesSample/README.md @@ -0,0 +1,88 @@ + +# 多文件下载,下载后对文件进行剪裁/压缩 + +### 介绍 +示例一展示如何使用buffer来将大文件的rawfile复制到应用沙箱; + +示例二在EAWorker子线程使用@ohos.zlib模块提供的zlib.decompressfile接口对沙箱目录中的压缩文件进行解压操作,解压成功后获取解压文件列表; + +示例三在EAWorker子线程使用@ohos.zlib模块提供的zlib.compressFile接口对沙箱目录中的文件进行压缩操作。 + +### 效果图预览 +| 主界面 | 大文件复制前 | 大文件复制后 | 大文件复制预览 | 文件压缩前 | 文件压缩后 | 文件解压缩前 | 文件解压缩后 | +|-------------------------------------------------------|-------------------------------------------|-------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------|-------------------------------------------------------------------------|---------------------------------------------------------------------------------|-----------------------------------------------------------------------| +| ![main](entry/src/main/resources/base/media/main.png) | ![bigFileCopy](entry/src/main/resources/base/media/bigFileCopy_1.png) | ![bigFileCopy](entry/src/main/resources/base/media/bigFileCopy_2.png) | ![bigFileCopy](entry/src/main/resources/base/media/bigFileCopy_3.png) | ![compressFile](entry/src/main/resources/base/media/compressFile_1.png) | ![compressFile](entry/src/main/resources/base/media/compressFile_2.png) | ![deCompressFile](entry/src/main/resources/base/media/deCompressFile_1.png) | ![deCompressFile](entry/src/main/resources/base/media/deCompressFile_2.png) | + +**使用说明** +1. 在主界面,点击蓝色按钮"大文件复制" + * 点击Start Copy按钮开始复制 + * 当复制进度达到100%之后,点击Preview按钮进行文件的预览,以验证文件复制的正确性 + * 如果要反复验证本场景,请在复制完成之后,点击Reset按钮,重置进度,再进行后续验证 +2. 在主界面,点击蓝色按钮"文件压缩" + * 点击压缩按钮,压缩待压缩文件,显示压缩结果 +3. 在主界面,点击蓝色按钮"文件解压缩" + * 点击解压缩按钮,解压缩压缩包,显示解压缩结果 + + +### 工程结构&模块类型 + + ``` + entry/src/main/ets/ +|---pages +| |---Example1 +| | |---DecompressFileViewComponent.ets // 视图层-文件解压缩页面 +| | |---Example1.ets +| |---Example2 +| | |---BigFileCopyConstants // 常量 +| | |---BigFileCopyView.ets // 视图层-文件复制页面 +| | |---Example2.ets +| |---Example3 +| | |---CompressFile.ets // 视图层-文件压缩页面 +| | |---CompressFileComponent.ets // 压缩组件 +| | |---Example3.ets +| |---index.ets // 首页 + ``` + +### 具体实现 + +* Example1(大文件拷贝案例) 源码参考: [Example1](entry/src/main/ets/pages/Example1) + * 根据rawfile文件名获取其所属hap包的RawFileDescriptor,其内部包含真正rawfile文件的长度、在hap包中的偏移量,hap包的fd。 + * 打开即将写入的目标文件 + * 创建buffer,用于读写文件内容 + * 使用buffer进行文件内容的循环读写,只要实际读入buffer的内容长度不为0,就表示文件内容没有读取完毕,就将读到的内容写入目标文件。注意,这里使用了buffSize来控制想要读取内容的长度,因此需要注意在循环体内对其进行更新 + * 因为复制的是图片文件,复制完毕之后使用Image组件加载该图片进行显示,以验证复制过程的正确性 +* Example2(文件压缩案例) 源码参考: [Example2](entry/src/main/ets/pages/Example2) + * 使用fs.access判断输出目录是否已经存在,如果不存在使用fs.mkdirSync()创建空目录用于放置压缩后的文件。 + * 空目录创建成功后使用zlib.compressFile接口压缩文件,输出到空目录中 + * 在页面上显示压缩包的名字 +* Example3(文件解压缩案例) 源码参考: [Example3](entry/src/main/ets/pages/Example3) + * 将项目目录rawfile下的压缩文件写入到应用的沙箱目录 + * 对沙箱目录下的压缩包进行解压 + * 根据解压后的文件所属沙箱目录,获取解压后的文件列表 + + +### 相关权限 + +无 + +### 依赖 + +无 + +### 约束与限制 + +1. 本示例仅支持标准系统上运行,支持设备:Phone; +2. 本示例为Stage模型,支持API20版本SDK,SDK版本号(API Version 20),镜像版本号(6.0.0.31)。 +3. 本示例需要使用DevEco Studio 版本号(6.0.0.21)版本才可编译运行。 + +### 下载 + +如需单独下载本工程,执行如下命令: + +``` +git init +git config core.sparsecheckout true +echo code/ArkTS1.2/ComponentSample/ > .git/info/sparse-checkout +git remote add origin https://gitee.com/openharmony/applications_app_samples.git +git pull +``` \ No newline at end of file diff --git a/code/ArkTS1.2/FilesSample/build-profile.json5 b/code/ArkTS1.2/FilesSample/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..325f69ce3922d8e67015cc3e5dd436901efb9923 --- /dev/null +++ b/code/ArkTS1.2/FilesSample/build-profile.json5 @@ -0,0 +1,67 @@ +/** + * + * Copyright (c) 2025 Huawei Device Co., Ltd. + * + * All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice,this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS, + * + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +{ + "app": { + "signingConfigs": [], + "products": [ + { + "name": "default", + "signingConfig": "default", + "compatibleSdkVersion": "6.0.0(20)", + "runtimeOS": "HarmonyOS", + "buildOption": { + "strictMode": { + "caseSensitiveCheck": true, + "useNormalizedOHMUrl": true + } + } + } + ], + "buildModeSet": [ + { + "name": "debug", + }, + { + "name": "release" + } + ] + }, + "modules": [ + { + "name": "entry", + "srcPath": "./entry", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + } + ] +} \ No newline at end of file diff --git a/code/ArkTS1.2/FilesSample/code-linter.json5 b/code/ArkTS1.2/FilesSample/code-linter.json5 new file mode 100644 index 0000000000000000000000000000000000000000..87b3919d419c09728067f1b545b7e2d5116adc07 --- /dev/null +++ b/code/ArkTS1.2/FilesSample/code-linter.json5 @@ -0,0 +1,58 @@ +/** + * + * Copyright (c) 2025 Huawei Device Co., Ltd. + * + * All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice,this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS, + * + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +{ + "files": [ + "**/*.ets" + ], + "ignore": [ + "**/src/ohosTest/**/*", + "**/src/test/**/*", + "**/src/mock/**/*", + "**/node_modules/**/*", + "**/oh_modules/**/*", + "**/build/**/*", + "**/.preview/**/*" + ], + "ruleSet": [ + "plugin:@performance/recommended", + "plugin:@typescript-eslint/recommended" + ], + "rules": { + "@security/no-unsafe-aes": "error", + "@security/no-unsafe-hash": "error", + "@security/no-unsafe-mac": "warn", + "@security/no-unsafe-dh": "error", + "@security/no-unsafe-dsa": "error", + "@security/no-unsafe-ecdsa": "error", + "@security/no-unsafe-rsa-encrypt": "error", + "@security/no-unsafe-rsa-sign": "error", + "@security/no-unsafe-rsa-key": "error", + "@security/no-unsafe-dsa-key": "error", + "@security/no-unsafe-dh-key": "error", + "@security/no-unsafe-3des": "error" + } +} \ No newline at end of file diff --git a/code/ArkTS1.2/FilesSample/entry/.gitignore b/code/ArkTS1.2/FilesSample/entry/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/code/ArkTS1.2/FilesSample/entry/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/code/ArkTS1.2/FilesSample/entry/build-profile.json5 b/code/ArkTS1.2/FilesSample/entry/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..ae354da257a9b7d08066736a31669a30216274ee --- /dev/null +++ b/code/ArkTS1.2/FilesSample/entry/build-profile.json5 @@ -0,0 +1,55 @@ +/** + * + * Copyright (c) 2025 Huawei Device Co., Ltd. + * + * All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice,this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS, + * + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +{ + "apiType": "stageMode", + "arkTSVersion": "1.2", + "buildOption": { + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/code/ArkTS1.2/FilesSample/entry/hvigorfile.ts b/code/ArkTS1.2/FilesSample/entry/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..8f830cdfd5bfdd8bde92ebfc27fc0562423dca3d --- /dev/null +++ b/code/ArkTS1.2/FilesSample/entry/hvigorfile.ts @@ -0,0 +1,32 @@ +/** + * + * Copyright (c) 2025 Huawei Device Co., Ltd. + * + * All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice,this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS, + * + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/code/ArkTS1.2/FilesSample/entry/obfuscation-rules.txt b/code/ArkTS1.2/FilesSample/entry/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/code/ArkTS1.2/FilesSample/entry/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/code/ArkTS1.2/FilesSample/entry/oh-package.json5 b/code/ArkTS1.2/FilesSample/entry/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..39ab3f901494a87d5ab258dfaa9e6882de1f5166 --- /dev/null +++ b/code/ArkTS1.2/FilesSample/entry/oh-package.json5 @@ -0,0 +1,36 @@ +/** + * + * Copyright (c) 2025 Huawei Device Co., Ltd. + * + * All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice,this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS, + * + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +{ + "name": "entry", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": {} +} + diff --git a/code/ArkTS1.2/FilesSample/entry/src/main/ets/entryability/EntryAbility.ets b/code/ArkTS1.2/FilesSample/entry/src/main/ets/entryability/EntryAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..66f9044befd1c512bdc0032343a37778acb5e5d7 --- /dev/null +++ b/code/ArkTS1.2/FilesSample/entry/src/main/ets/entryability/EntryAbility.ets @@ -0,0 +1,56 @@ +/** + * + * Copyright (c) 2025 Huawei Device Co., Ltd. + * + * All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice,this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS, + * + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +import UIAbility from '@ohos.app.ability.UIAbility'; +import AbilityConstant from '@ohos.app.ability.AbilityConstant'; +import Want from '@ohos.app.ability.Want'; +import window from '@ohos.window'; +import { BusinessError } from '@ohos.base'; +import hilog from '@ohos.hilog'; +import { MyCallback } from '../models/MyCallback'; + +class EntryAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(0x0000, 'testTag', 'EntryAbility onCreate'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + hilog.info(0x0000, 'testTag', 'EntryAbility onWindowStageCreate'); + try { + windowStage.loadContent('pages/Index', (err: BusinessError): void => { + hilog.info(0x0000, 'testTag', 'loadContent entering'); + MyCallback.runCasesOfCallback(); + if (err.code) { + hilog.info(0x0000, 'testTag', 'loadContent error'); + return; + } + hilog.info(0x0000, 'testTag', 'loadContent ok'); + }); + } catch (e: Error) { + hilog.info(0x0000, 'testTag', 'loadContent catch error:-----------' + e.message); + } + } +} \ No newline at end of file diff --git a/code/ArkTS1.2/FilesSample/entry/src/main/ets/models/MyCallback.ets b/code/ArkTS1.2/FilesSample/entry/src/main/ets/models/MyCallback.ets new file mode 100644 index 0000000000000000000000000000000000000000..430e22de4fa5bfa011d66e22f32031967ca99119 --- /dev/null +++ b/code/ArkTS1.2/FilesSample/entry/src/main/ets/models/MyCallback.ets @@ -0,0 +1,64 @@ +/** + * + * Copyright (c) 2025 Huawei Device Co., Ltd. + * + * All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice,this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS, + * + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +import { AsyncCallback, BusinessError, Callback, ErrorCallback } from '@ohos.base'; +import hilog from '@ohos.hilog'; + +const SYNTAX_ERROR_CODE: double = 1002; + +export class MyCallback { + static myCallback(callback: Callback): void { + hilog.info(0x0000, 'testTag', 'myCallback'); + callback('myCallback'); + } + + static myAsyncCallback(callback: AsyncCallback): void { + hilog.info(0x0000, 'testTag', 'myAsyncCallback'); + let error = new Error('Business Error', 'basicError2 message', undefined); + let be2: BusinessError = new BusinessError(SYNTAX_ERROR_CODE, error); + callback(be2, 'yAsyncCallback'); + } + + static myErrorCallback(callback: ErrorCallback>): void { + hilog.info(0x0000, 'testTag', 'myErrorCallback'); + let error = new Error('Business Error', 'myErrorCallback message', undefined); + let result: string = 'test'; + let be: BusinessError = new BusinessError(SYNTAX_ERROR_CODE, result, error); + callback(be); + } + + static runCasesOfCallback() { + MyCallback.myCallback((data: string) => { + hilog.info(0x0000, 'testTag', 'MyCallback' + data); + }) + MyCallback.myAsyncCallback((error: BusinessError, result: string) => { + hilog.info(0x0000, 'testTag', `myAsyncCallback, error: ${error.message}, result:${result}`); + }) + MyCallback.myErrorCallback((error: BusinessError) => { + hilog.info(0x0000, 'testTag', `myAsyncCallback, error: ${error.message}`); + }) + } +} \ No newline at end of file diff --git a/code/ArkTS1.2/FilesSample/entry/src/main/ets/pages/Example1/BigFileCopyConstants.ets b/code/ArkTS1.2/FilesSample/entry/src/main/ets/pages/Example1/BigFileCopyConstants.ets new file mode 100644 index 0000000000000000000000000000000000000000..b74054b4f22408cc9089acfda45db27a3a1ec87f --- /dev/null +++ b/code/ArkTS1.2/FilesSample/entry/src/main/ets/pages/Example1/BigFileCopyConstants.ets @@ -0,0 +1,34 @@ +/** + * + * Copyright (c) 2025 Huawei Device Co., Ltd. + * + * All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice,this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS, + * + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +export class BigFileCopyConstants { + public static readonly PROGRESS_MAX: number = 100; + public static readonly PROGRESS_MIN: number = 0; + public static readonly BUFF_SIZE: number = 4096; + public static readonly COMPONENT_SIZE: number = 200; + public static readonly SANDBOX_PREFIX: string = 'file://'; + public static readonly TEST_FILE_NAME: string = 'bigfilecopy_test.jpg'; +} \ No newline at end of file diff --git a/code/ArkTS1.2/FilesSample/entry/src/main/ets/pages/Example1/BigFileCopyView.ets b/code/ArkTS1.2/FilesSample/entry/src/main/ets/pages/Example1/BigFileCopyView.ets new file mode 100644 index 0000000000000000000000000000000000000000..3f5cd48fa4ce8557d89dd634354983983d0249b7 --- /dev/null +++ b/code/ArkTS1.2/FilesSample/entry/src/main/ets/pages/Example1/BigFileCopyView.ets @@ -0,0 +1,176 @@ +/** + * + * Copyright (c) 2025 Huawei Device Co., Ltd. + * + * All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice,this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS, + * + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +import { + memo, + __memo_context_type, + __memo_id_type +} from '@ohos.arkui.stateManagement'; // should be insert by ui-plugins +import { + Stack, + Entry, + Text, + TextAttribute, + Column, + Component, + Button, + ButtonAttribute, + ClickEvent, + UserView, + Row, + TextAlign, + Visibility, + List, + ListItem, + ForEach, + TextOverflow, + Image, + $r, + HorizontalAlign, + Progress, + ProgressType, + FlexAlign, + SafeAreaType +} from '@ohos.arkui.component'; // TextAttribute should be insert by ui-plugins +import { State, Link, MutableState, stateOf, observableProxy, Prop } from '@ohos.arkui.stateManagement'; + +import { BigFileCopyConstants } from './BigFileCopyConstants'; +import { common } from '@kit.AbilityKit'; +import fs from '@ohos.file.fs'; +import hilog from '@ohos.hilog'; +import resourceManager from '@ohos.resourceManager'; + +const IMAGE_ID: string = 'bigFileImage'; + +/** + * 功能描述:文件拷贝是应用开发中的一个常见场景,本例将展示如何使用buffer来将大文件的rawfile复制到应用沙箱。 + * + * 推荐场景:需要数据备份的软件,如网盘,文件管理器等 + * + * 核心组件: + * 1.BigFileCopyView + * + * 实现步骤: + * 1. 根据rawfile获得其所在hap包的RawFileDescriptor + * 2. 根据RawFileDescriptor中的fd、偏移、文件长度,使用buffer进行内容读取 + * 3. 调用图库的预览器对复制的文件进行预览,以验证复制过程的正确性 + */ +@Entry +@Component +export struct BigFileCopyViewComponent { + @State progress: number = BigFileCopyConstants.PROGRESS_MIN; + fileName: string = BigFileCopyConstants.TEST_FILE_NAME; + targetFilePath: string = ''; + @State isCopyFinished: boolean = false; + @State isEnableSmooth: boolean = true; + @State isPreview: boolean = false; + context: common.Context = this.getUIContext().getHostContext() as common.Context; + + copyFile2Sandbox() { + this.isCopyFinished = false; + const data: resourceManager.RawFileDescriptor = this.context.resourceManager.getRawFdSync(this.fileName); + const targetPath: string = this.context.filesDir + '/' + this.fileName; + const destFile = fs.openSync(targetPath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); + let buffSize: number = BigFileCopyConstants.BUFF_SIZE; + const buffer: ArrayBuffer = new ArrayBuffer(Math.min(buffSize, data.length)); + // 记录读取位置的偏移(相较于文件起始偏移) + let off: number = 0; + // 本次读取内容的实际长度 + let len: number = 0; + // 记录已读文件长度 + let readedLen: number = 0; + let offset = data.offset + off; + let len = fs.readSync(data.fd, buffer, { offset: offset, length: buffSize }) + while (len) { + readedLen += len; + fs.writeSync(destFile.fd, buffer, { offset: off, length: len }); + this.progress = Math.floor((readedLen / data.length) * 100); + off = off + len; + if ((data.length - readedLen) < buffSize) { + buffSize = data.length - readedLen; + } + } + this.targetFilePath = targetPath; + fs.close(destFile.fd); + this.isCopyFinished = true; + } + + setIsPreview(status: boolean): void { + this.isPreview = status; + } + + build() { + Column() { + Row() { + Text('← 返回').fontSize(20) + .onClick((e: ClickEvent) => { + this.getUIContext().getRouter().back(); + }).width('100%') + }.height('5%') + Column() { + Stack() { + if (!this.isPreview) { + Progress({ + value: this.progress, + total: BigFileCopyConstants.PROGRESS_MAX, + type: ProgressType.Eclipse + }) + .size({ width: BigFileCopyConstants.COMPONENT_SIZE, height: BigFileCopyConstants.COMPONENT_SIZE }) + } else { + Image(BigFileCopyConstants.SANDBOX_PREFIX + this.targetFilePath) + .width(BigFileCopyConstants.COMPONENT_SIZE) + .height(BigFileCopyConstants.COMPONENT_SIZE) + .borderRadius(10) + .id(IMAGE_ID) + } + } + + Text(`当前复制进度:${this.progress}%`) + .margin(30) + Button('Start Copy') + .enabled(!this.isCopyFinished) + .onClick((): void => { + this.copyFile2Sandbox(); + }) + Button('Reset') + .margin(10) + .enabled(this.isCopyFinished) + .onClick((): void => { + this.progress = BigFileCopyConstants.PROGRESS_MIN; + this.isCopyFinished = false; + this.setIsPreview(false); + }) + Button('Preview') + .enabled(this.isCopyFinished) + .onClick((): void => { + this.setIsPreview(true); + }) + } + }.width('100%') + .height('100%') + .justifyContent(FlexAlign.Start) + } +} \ No newline at end of file diff --git a/code/ArkTS1.2/FilesSample/entry/src/main/ets/pages/Example2/CompressFile.ets b/code/ArkTS1.2/FilesSample/entry/src/main/ets/pages/Example2/CompressFile.ets new file mode 100644 index 0000000000000000000000000000000000000000..55c1398cb977340b5326ed52b93c48ddf709fd29 --- /dev/null +++ b/code/ArkTS1.2/FilesSample/entry/src/main/ets/pages/Example2/CompressFile.ets @@ -0,0 +1,238 @@ +/** + * + * Copyright (c) 2025 Huawei Device Co., Ltd. + * + * All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice,this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS, + * + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +import { + memo, + __memo_context_type, + __memo_id_type +} from '@ohos.arkui.stateManagement'; // should be insert by ui-plugins +import { + Entry, + Text, + TextAttribute, + Column, + Component, + Button, + ButtonAttribute, + Row, + TextAlign, + List, + ListItem, + ForEach, + Image, + $r, + HorizontalAlign, + FlexAlign, + GradientDirection, + ClickEvent +} from '@ohos.arkui.component'; // TextAttribute should be insert by ui-plugins +import { + State, + Link, + MutableState, + stateOf, + observableProxy, + Prop +} from '@ohos.arkui.stateManagement'; // should be insert by ui-plugins +import zlib from '@ohos.zlib'; +import common from '@ohos.app.ability.common'; +import fs from '@ohos.file.fs'; +import { BusinessError } from '@ohos.base'; +import { CompressFileComponent } from './CompressFileComponent'; +import hilog from '@ohos.hilog'; + +const TAG: string = 'compressFileComponent'; +const LINEAR_GRADIENT_START = 0; +const LINEAR_GRADIENT_END = 1; + +/** + * 功能描述:本示例主要介绍在EAWorker子线程中使用@ohos.zlib模块提供的zlib.compressFile接口对沙箱目录中的文件进行压缩操作。 + * + * 推荐场景:文件管理中压缩文件时使用 + * + * 核心组件: + * 1.CompressFileComponent: 在子线程中压缩文件 + * + * 实现步骤: + * 1. 目录名称配置:指定压缩成功后压缩包路径 + * @example + * @State compressZipDir: string = 'bundlefile/compress_file.zip'; + * 2. 目录名称配置:指定待压缩文件在rawfile下和应用沙箱目录下 + * @example + * @State beCompressFileDir: string = 'compressfile'; + * 3. 数据存放配置:压缩后的压缩包存放变量 + * @example + * @State compressBundleName: string = ''; + * 4.构建压缩组件 + * @example + * CompressFileComponent({ + compressBundleName: this.compressBundleName, + beCompressFileDir: this.beCompressFileDir, + compressZipPath: this.compressZipDir, + }) + */ +@Entry +@Component +export struct CompressFileViewComponent { + // rawfile下所有待压缩文件 + @State beCompressFiles: Array = new Array(); + // 压缩成功后压缩包名字 + @State compressBundleName: string = ''; + // 压缩成功后压缩包文件路径 + @State compressZipDir: string = 'bundlefile/compress_file.zip'; + // 待压缩文件所在目录名 + @State beCompressFileDir: string = 'compressfile'; + context: common.Context = this.getUIContext().getHostContext() as common.Context; + + aboutToAppear(): void { + this.beCompressFiles = this.context.resourceManager.getRawFileListSync(this.beCompressFileDir); + } + + build() { + Column() { + Row() { + Text('← 返回').fontSize(20) + .onClick((e: ClickEvent) => { + this.getUIContext().getRouter().back(); + }).width('100%') + }.height('5%') + Column() { + Row() { + Column() { + Text('文件压缩') + .margin(10) + .fontSize(28) + .fontColor('#FFFFFF') + Text('使用@ohos.zlib在Worker子线程压缩文件') + .margin(10) + .fontColor('#FFFFFF') + } + .width(220) + .alignItems(HorizontalAlign.Start) + + Image($r('app.media.zlib')) + .margin(10) + .width(40) + .height(48) + } + .width('90%') + .height('15%') + .backgroundColor('#94c4f5') + .borderRadius(12) + .justifyContent(FlexAlign.SpaceBetween) + .linearGradient({ + direction: GradientDirection.Top, + repeating: true, + colors: [['#800f55ab', LINEAR_GRADIENT_START], + ['#B354A1FF', LINEAR_GRADIENT_END]] + }) + + Row() { + Text('待压缩文件') + .fontSize(18) + .fontColor('#FFFFFF') + /** + * 构建压缩组件 + * compressBundleName: 压缩成功后压缩包的名字 + * beCompressFileDir: 待压缩文件所在目录名 + * compressZipPath: 压缩成功后压缩包路径 + */ + CompressFileComponent({ + compressBundleName: this.compressBundleName, + beCompressFileDir: this.beCompressFileDir, + compressZipPath: this.compressZipDir, + }) + } + .borderRadius(12) + .width('90%') + .backgroundColor('#94c4f5') + .padding(10) + .margin(5) + .justifyContent(FlexAlign.SpaceBetween) + + CompressFileList({ fileNames: this.beCompressFiles, iconName: 'app.media.txt' }) + + Text('压缩结果') + .fontSize(18) + .textAlign(TextAlign.Start) + .width('90%') + .padding(10) + .backgroundColor('#94c4f5') + .fontColor('#F1F3F5') + .borderRadius(12) + + if (this.compressBundleName) { + Row() { + Image($r('app.media.zlib')) + .width(20) + .height(24) + .margin(5) + Text(this.compressBundleName) + .fontSize(14) + } + .id('compressZip') + .justifyContent(FlexAlign.Start) + .width('90%') + .height(50) + } + } + } + .width('100%') + .height('100%') + .justifyContent(FlexAlign.Start) + } +} + +@Component +struct CompressFileList { + // 文件列表 + @Prop fileNames: Array = new Array(); + // 文件图标 + @Prop iconName: string = ''; + + build() { + Column() { + List() { + ForEach(this.fileNames, (item: string, index: number) => { + ListItem() { + Row() { + Image($r(this.iconName)) + .width(20) + .height(24) + .margin(5) + Text(item) + .fontSize(14) + } + .height(50) + } + }, (item: string, index: number) => item) + } + .width('90%') + .height(150) + } + .width('100%') + } +} \ No newline at end of file diff --git a/code/ArkTS1.2/FilesSample/entry/src/main/ets/pages/Example2/CompressFileComponent.ets b/code/ArkTS1.2/FilesSample/entry/src/main/ets/pages/Example2/CompressFileComponent.ets new file mode 100644 index 0000000000000000000000000000000000000000..3c7ee62d9b703b99cc867c3e582e7981d4b64a99 --- /dev/null +++ b/code/ArkTS1.2/FilesSample/entry/src/main/ets/pages/Example2/CompressFileComponent.ets @@ -0,0 +1,161 @@ +/** + * + * Copyright (c) 2025 Huawei Device Co., Ltd. + * + * All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice,this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS, + * + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +import { memo, __memo_context_type, __memo_id_type } from '@ohos.arkui.stateManagement'; +import { + Text, + TextAttribute, + Column, + Component, + Button, + ButtonAttribute, + ClickEvent, + Row, + HorizontalAlign, + ForEach, + Image, + $r, + Entry +} from '@ohos.arkui.component'; +import { State, MutableState, stateOf, observableProxy, Link } from '@ohos.arkui.stateManagement'; +import promptAction from '@ohos.promptAction'; +import { BusinessError } from '@ohos.base'; +import hilog from '@ohos.hilog'; +import zlib from '@ohos.zlib'; +import common from '@ohos.app.ability.common'; +import fs from '@ohos.file.fs'; +import { UIContext, PromptAction } from '@ohos.arkui.UIContext'; + +const TAG: string = 'compressFileComponent'; +@Entry +@Component +export struct CompressFileComponent { + @Link compressBundleName: string = ''; + // 压缩成功后压缩包路径 + @Link compressZipPath: string = ''; + // 待压缩文件所在目录名 + @Link beCompressFileDir: string = ''; + // rawfile被压缩文件的应用沙箱路径 + private rawfilePath: string = ''; + // 应用沙箱目录 + @State pathDir: string = ''; + // 压缩后的文件所处的应用沙箱目录 + @State outFileDir: string = ''; + @State beCompressFiles: Array = new Array(); + context: common.Context = this.getUIContext().getHostContext() as common.Context; + uiContext: UIContext | undefined = undefined; + + aboutToAppear(): void { + this.uiContext = this.getUIContext(); + this.initCompressFile(this.beCompressFileDir); + } + + childWorker(): void { + this.pathDir = this.context.filesDir; // 沙箱目录 + const rawfileDirName: string = this.beCompressFileDir; // 被压缩文件所在目录名 + const outFilePath: string = `${this.pathDir}/${this.compressZipPath}`; // 压缩包输出路径 + const outFileDir: string = outFilePath.slice(0, outFilePath.lastIndexOf('/')); // 压缩包输出目录 + // 判断结果输出目录是否存在 + if (!fs.accessSync(outFileDir)) { + fs.mkdirSync(outFileDir); + } + + let options: zlib.Options = { + level: zlib.CompressLevel.COMPRESS_LEVEL_DEFAULT_COMPRESSION, + memLevel: zlib.MemLevel.MEM_LEVEL_DEFAULT, + strategy: zlib.CompressStrategy.COMPRESS_STRATEGY_DEFAULT_STRATEGY + }; + try { + // 对目录下的文件进行压缩 + zlib.compressFile(`${this.pathDir}/${this.beCompressFileDir}`, outFilePath, options, + (errData: BusinessError | null) => { + if (errData && errData.code == 0) { + this.compressBundleName = outFilePath.slice(outFilePath.lastIndexOf('/') + 1, outFilePath.length); + let isSuccess = fs.accessSync(`${outFilePath}`); + if(isSuccess){ + let promptAction: PromptAction = this.uiContext!.getPromptAction(); + promptAction.showToast({ + message: '压缩成功', + duration: 2000 + }); + } + } else if(errData){ + hilog.error(0x0000, 'testTag', + `compress failed with error message: ${errData.message}, error code: ${errData.code}`); + } + }) + } catch (errData) { + let code = (errData as BusinessError).code; + let message = (errData as BusinessError).message; + hilog.error(0x0000, 'testTag', `compress errData is error code: ${code}, message: ${message}`); + } + } + + /** + * 调用EAWorker对文件进行压缩 + * @returns + */ + compressByWorker(): void { + let worker: EAWorker = new EAWorker(true); + worker.run(this.childWorker); + worker.join(); + } + + /** + * 将待压缩文件写入到应用沙箱目录 + * @param beCompressFileDir 待压缩文件所处路径 + * @returns + */ + initCompressFile(beCompressFileDir: string): void { + //获取rawfile下的所有待压缩文件名 + this.beCompressFiles = this.context.resourceManager.getRawFileListSync(beCompressFileDir); + this.beCompressFiles.forEach((fileName: string) => { + let content = this.context.resourceManager.getRawFileContentSync(`${beCompressFileDir}/${fileName}`); + this.pathDir = this.context.filesDir; // 获取应用沙箱目录 + this.rawfilePath = `${this.pathDir}/${beCompressFileDir}`; // 设置rawfile压缩文件的应用沙箱路径 + if (!fs.accessSync(this.rawfilePath)) { + fs.mkdirSync(this.rawfilePath); + } + // 在指定路径以同步方法打开或创建文件 + let file = fs.openSync(this.rawfilePath + `/${fileName}`, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); + fs.writeSync(file.fd, 'hello world!'); + fs.closeSync(file); + }) + } + + build() { + Row() { + Button('压缩') + .width(60) + .height(28) + .borderRadius(12) + .id('compressButton') + .onClick((): void => { + this.compressByWorker(); + }) + } + } +} \ No newline at end of file diff --git a/code/ArkTS1.2/FilesSample/entry/src/main/ets/pages/Example3/DecompressFileViewComponent.ets b/code/ArkTS1.2/FilesSample/entry/src/main/ets/pages/Example3/DecompressFileViewComponent.ets new file mode 100644 index 0000000000000000000000000000000000000000..43abbb7ddcc60902d80ca4b77214afdf12ff0c76 --- /dev/null +++ b/code/ArkTS1.2/FilesSample/entry/src/main/ets/pages/Example3/DecompressFileViewComponent.ets @@ -0,0 +1,276 @@ +/** + * + * Copyright (c) 2025 Huawei Device Co., Ltd. + * + * All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice,this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS, + * + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +import { memo, __memo_context_type, __memo_id_type } from '@ohos.arkui.stateManagement'; +import { + Text, + TextAttribute, + Column, + Component, + Button, + ButtonAttribute, + ClickEvent, + UserView, + Row, + List, + ListItem, + HorizontalAlign, + LazyForEach, + ForEach, + Flex, + FlexAlign, + Image, + $r, + GradientDirection, + FontWeight, + TextAlign, + Divider, + Entry +} from '@ohos.arkui.component'; +import { State, MutableState, stateOf, observableProxy, Link, Prop } from '@ohos.arkui.stateManagement'; +import { BusinessError } from '@ohos.base'; +import hilog from '@ohos.hilog'; +import zlib from '@ohos.zlib'; +import fs from '@ohos.file.fs'; +import common from '@ohos.app.ability.common'; +import promptAction from '@ohos.promptAction'; +import { UIContext, PromptAction } from '@ohos.arkui.UIContext'; + +const LAYOUT_WEIGHT = 1; +const CACHED_COUNT = 5; // LazyForEach预加载的Item数量 +const LINEAR_GRADIENT_START = 0; +const LINEAR_GRADIENT_END = 1; + +/** + * 功能描述: 本示例介绍在Worker 子线程使用@ohos.zlib 提供的zlib.decompressfile接口对沙箱目录中的压缩文件进行解压操作,解压成功后将解压路径返回主线程,获取解压文件列表 + * + * 推荐场景: 解压文件 + * + * 核心组件: + * 1. decompressFileByWorker() + * + * 实现步骤: + * 1. 将项目目录rawfile下的压缩文件写入到应用的沙箱目录,用于后续介绍解压操作 + * 2. 解压按钮回调中创建EAWorker线程,在子线程中解压文件 + * 4. 根据解压后的文件所属沙箱目录,获取解压后的文件列表 + */ +@Entry +@Component +export struct DecompressFileViewComponent { + @State pathDir: string = ''; + // 解压后的文件所处的应用沙箱目录 + @State outFileDir: string = ''; + // rawfile压缩文件的应用沙箱路径 + private rawfilePath: string = ''; + // 待解压的文件名 + private rawfileZipName: string = 'decompress_file_test.zip'; + @State fileList: Array = new Array(); + context: common.Context = this.getUIContext().getHostContext() as common.Context; + uiContext: UIContext | undefined = undefined; + + aboutToAppear(): void { + this.uiContext = this.getUIContext(); + this.initZip(this.rawfileZipName) + } + + childWorker(): void { + const pathDir: string = this.context.filesDir; + // 沙箱目录和去掉文件名后缀的压缩文件名称拼接成解压输出目录 + const outFileDir: string = `${pathDir}/${this.rawfileZipName.split('.')[0]}`; + fs.access(outFileDir).then((res: boolean) => { + if (!res) { + fs.mkdirSync(outFileDir); + hilog.info(0x0000, 'testTag', 'mkdirSync succeed'); + } + // 如果待解压的文件或文件夹在解压后的路径下已经存在,则会直接覆盖同名文件或同名文件夹中的同名文件。 + zlib.decompressFile(`${pathDir}/${this.rawfileZipName}`, outFileDir, + (err: BusinessError | null) => { + if (err && err.code == 0) { + // 根据解压后的文件所属沙箱目录,获取解压后的文件列表 + this.getFileListData(outFileDir); + let isSuccess = fs.accessSync(`${outFileDir}`); + hilog.info(0x0000, 'testTag', 'accessSync: ' + isSuccess); + if (isSuccess) { + let promptAction: PromptAction = this.uiContext!.getPromptAction(); + promptAction.showToast({ + message: '解压缩成功', + duration: 2000 + }); + } + } else if(err){ + hilog.error(0x0000, 'testTag', + `decompressFile failed. code is ${err.code}, message is ${err.message}`); + } + }) + }).catch((message: Error) => { + hilog.error(0x0000, 'testTag', `access failed with error message: ${message}`); + }); + } + + // 使用Worker子线程解压文件 + decompressFileByWorker(): void { + let worker: EAWorker = new EAWorker(true); + worker.run(this.childWorker); + worker.join(); + } + + // 将项目目录rawfile下的压缩文件写入到应用的沙箱目录,用于后续介绍解压操作。 + initZip(rawfileZipName: string) { + // 使用getRowFileContent接口以字节数组的形式获取到rawfile中的文件内容。 + this.context.resourceManager.getRawFileContent(rawfileZipName, + (error: BusinessError | null, value: Uint8Array | undefined) => { + if (value) { + const rawFile: Uint8Array = value; + // 获取应用沙箱目录 + this.pathDir = this.context.filesDir; + // 设置rawfile压缩文件的应用沙箱路径 + this.rawfilePath = `${this.pathDir}/${rawfileZipName}`; + // 在指定路径以同步方法打开或创建文件 + const file = fs.openSync(this.rawfilePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); + // 使用fs.write接口将字节数组形式的rawfile的文件内容写入到指定沙箱路径filePath中 + fs.write(file.fd, rawFile.buffer).then((writeLen: number) => { + hilog.info(0x0000, 'testTag', `write data to file succeed and size is: ${writeLen}`); + }).catch((message: Error) => { + hilog.info(0x0000, 'testTag', `${message}`); + }).finally(() => { + hilog.info(0x0000, 'testTag', 'write finished'); + fs.closeSync(file); + }); + } else { + hilog.info(0x0000, 'testTag', + `error message value: ${value}`); + } + }); + } + + // 获取解压后的文件列表 + getFileListData(outFileDir: string) { + // TODO:知识点:使用fs.listFile接口获取解压得到的沙箱目录下的所有文件名。 + fs.listFile(outFileDir).then((fileNames: Array) => { + this.fileList = fileNames; + }).catch((err: Error) => { + hilog.error(0x0000, 'testTag', `list file failed with error message: ${err.message}`); + }); + } + + build() { + Column() { + Row() { + Text('← 返回').fontSize(20) + .onClick((e: ClickEvent) => { + this.getUIContext().getRouter().back(); + }).width('100%') + }.height('5%') + + Row() { + Column() { + Text('文件解压') + .fontColor('#FFFFFF') + .fontWeight(FontWeight.Bolder) + .fontSize(26) + .margin(12) + Text('使用@ohos.zlib在Worker子线程解压文件') + .fontColor('#FFFFFF') + .margin(12) + } + .width(200) + .alignItems(HorizontalAlign.Start) + + Image($r('app.media.zlib')) + .width(40) + .height(48) + } + .width('100%') + .height(140) + .padding(20) + .backgroundColor('#94c4f5') + .justifyContent(FlexAlign.SpaceBetween) + .borderRadius(12) + .linearGradient({ + direction: GradientDirection.Top, + repeating: true, + colors: [['#800f55ab', LINEAR_GRADIENT_START], + ['#B354A1FF', LINEAR_GRADIENT_END]] + }) + // 压缩文件组件 + Row() { + Image($r('app.media.zlib')) + .width(20) + .height(24) + .margin(5) + Text(this.rawfileZipName) + .fontSize(14) + .layoutWeight(LAYOUT_WEIGHT) + Button('解压') + .fontWeight(FontWeight.Bold) + .width(60) + .height(28) + .borderRadius(12) + .onClick((): void => { + this.decompressFileByWorker(); + }) + } + .padding(12) + .backgroundColor('#FFFFFF') + + Divider() + + Text('解压结果') + .textAlign(TextAlign.Start) + .fontSize(20) + .fontWeight(FontWeight.Bold) + .width('100%') + .padding(12) + .backgroundColor('#4CADD8FF') + List() { + ForEach(this.fileList, (item: string) => { + ListItem() { + FileList({ item: item }) + } + }, (item: string) => item) + } + .width('100%') + } + .height('100%') + .padding(12) + .backgroundColor('#F1F3F5') + } +} + +@Component +struct FileList { + @State item: string = ''; + + build() { + Row() { + Image($r('app.media.txt')) + .width(20) + .height(20) + .margin(12) + Text(this.item).fontSize(14) + } + } +} \ No newline at end of file diff --git a/code/ArkTS1.2/FilesSample/entry/src/main/ets/pages/Index.ets b/code/ArkTS1.2/FilesSample/entry/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..1b60fd948f3285c22ed5a7d1688f69892d6a414b --- /dev/null +++ b/code/ArkTS1.2/FilesSample/entry/src/main/ets/pages/Index.ets @@ -0,0 +1,93 @@ +/** + * + * Copyright (c) 2025 Huawei Device Co., Ltd. + * + * All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice,this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS, + * + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +import { + memo, + __memo_context_type, + __memo_id_type +} from "@ohos.arkui.stateManagement"; // should be insert by ui-plugins +import { + Entry, + Row, + Text, + TextAttribute, + Column, + Component, + Button, + ButtonAttribute, + ClickEvent, + UserView, + TextArea, + Scroll +} from "@ohos.arkui.component"; // TextAttribute should be insert by ui-plugins +import { + State, + MutableState, + stateOf, + observableProxy +} from "@ohos.arkui.stateManagement"; // should be insert by ui-plugins +import hilog from '@ohos.hilog'; +import { Context, UIContext } from '@ohos.arkui.UIContext'; +import { DecompressFileViewComponent } from './Example3/DecompressFileViewComponent'; +import { CompressFileViewComponent } from './Example2/CompressFile'; +import { BigFileCopyViewComponent } from './Example1/BigFileCopyView'; + + +@Component +struct MyStateSample { + build() { + Scroll() { + Column() { + Button('BigFileCopyView') + .onClick((e: ClickEvent) => { + this.getUIContext().getRouter().pushUrl({ url: "pages/Example1/BigFileCopyView" }) + }).fontSize(20).margin(15).width("100%") + Button('CompressFileView') + .onClick((e: ClickEvent) => { + this.getUIContext().getRouter().pushUrl({ url: "pages/Example2/CompressFile" }) + }).fontSize(20).margin(15).width("100%") + Button('DecompressFileView') + .onClick((e: ClickEvent) => { + this.getUIContext().getRouter().pushUrl({ url: "pages/Example3/DecompressFileViewComponent" }) + }).fontSize(18).margin(15).width("100%") + } + .padding(15) + .width("100%") + } + } +} + +export class ComExampleTrivialApplication extends UserView { + getBuilder() { + hilog.info(0x0000, 'testTag', 'getBuilder'); + let wrapper = @memo() => + { + hilog.info(0x0000, 'testTag', 'MyStateSample'); + MyStateSample(undefined) + } + return wrapper + } +} \ No newline at end of file diff --git a/code/ArkTS1.2/FilesSample/entry/src/main/module.json5 b/code/ArkTS1.2/FilesSample/entry/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..0c6512e21da8c15fbc98b90bea5267574b08a053 --- /dev/null +++ b/code/ArkTS1.2/FilesSample/entry/src/main/module.json5 @@ -0,0 +1,63 @@ +/** + * + * Copyright (c) 2025 Huawei Device Co., Ltd. + * + * All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice,this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS, + * + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +{ + "module": { + "name": "entry", + "type": "entry", + "description": "$string:module_desc", + "mainElement": "EntryAbility", + "deviceTypes": [ + "default", + "tablet", + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "EntryAbility", + "srcEntry": "./ets/entryability/EntryAbility.ets", + "description": "$string:EntryAbility_desc", + "icon": "$media:layered_image", + "label": "$string:EntryAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true, + "skills": [ + { + "entities": [ + "entity.system.home" + ], + "actions": [ + "action.system.home" + ] + } + ] + } + ] + } +} \ No newline at end of file diff --git a/code/ArkTS1.2/FilesSample/entry/src/main/resources/base/element/color.json b/code/ArkTS1.2/FilesSample/entry/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/code/ArkTS1.2/FilesSample/entry/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/code/ArkTS1.2/FilesSample/entry/src/main/resources/base/element/float.json b/code/ArkTS1.2/FilesSample/entry/src/main/resources/base/element/float.json new file mode 100644 index 0000000000000000000000000000000000000000..33ea22304f9b1485b5f22d811023701b5d4e35b6 --- /dev/null +++ b/code/ArkTS1.2/FilesSample/entry/src/main/resources/base/element/float.json @@ -0,0 +1,8 @@ +{ + "float": [ + { + "name": "page_text_font_size", + "value": "50fp" + } + ] +} diff --git a/code/ArkTS1.2/FilesSample/entry/src/main/resources/base/element/string.json b/code/ArkTS1.2/FilesSample/entry/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..de817017d6ca67e37ddd6d9743fe89a720f4bab4 --- /dev/null +++ b/code/ArkTS1.2/FilesSample/entry/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "EntryAbility_desc", + "value": "description" + }, + { + "name": "EntryAbility_label", + "value": "FilesSample" + } + ] +} \ No newline at end of file diff --git a/code/ArkTS1.2/FilesSample/entry/src/main/resources/base/media/background.png b/code/ArkTS1.2/FilesSample/entry/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..923f2b3f27e915d6871871deea0420eb45ce102f Binary files /dev/null and b/code/ArkTS1.2/FilesSample/entry/src/main/resources/base/media/background.png differ diff --git a/code/ArkTS1.2/FilesSample/entry/src/main/resources/base/media/bigFileCopy_1.png b/code/ArkTS1.2/FilesSample/entry/src/main/resources/base/media/bigFileCopy_1.png new file mode 100644 index 0000000000000000000000000000000000000000..c1d10e84b702f994db6aa22529107bc21efccc7b Binary files /dev/null and b/code/ArkTS1.2/FilesSample/entry/src/main/resources/base/media/bigFileCopy_1.png differ diff --git a/code/ArkTS1.2/FilesSample/entry/src/main/resources/base/media/bigFileCopy_2.png b/code/ArkTS1.2/FilesSample/entry/src/main/resources/base/media/bigFileCopy_2.png new file mode 100644 index 0000000000000000000000000000000000000000..3b28e4bcbbb2bc56b7a6a729ae3296c945fabb7f Binary files /dev/null and b/code/ArkTS1.2/FilesSample/entry/src/main/resources/base/media/bigFileCopy_2.png differ diff --git a/code/ArkTS1.2/FilesSample/entry/src/main/resources/base/media/bigFileCopy_3.png b/code/ArkTS1.2/FilesSample/entry/src/main/resources/base/media/bigFileCopy_3.png new file mode 100644 index 0000000000000000000000000000000000000000..81c8c8581e37cb1fac41e705f6e82c5823171c27 Binary files /dev/null and b/code/ArkTS1.2/FilesSample/entry/src/main/resources/base/media/bigFileCopy_3.png differ diff --git a/code/ArkTS1.2/FilesSample/entry/src/main/resources/base/media/compressFile_1.png b/code/ArkTS1.2/FilesSample/entry/src/main/resources/base/media/compressFile_1.png new file mode 100644 index 0000000000000000000000000000000000000000..b001103fd3803c44ee769889a44ad79f522300a9 Binary files /dev/null and b/code/ArkTS1.2/FilesSample/entry/src/main/resources/base/media/compressFile_1.png differ diff --git a/code/ArkTS1.2/FilesSample/entry/src/main/resources/base/media/compressFile_2.png b/code/ArkTS1.2/FilesSample/entry/src/main/resources/base/media/compressFile_2.png new file mode 100644 index 0000000000000000000000000000000000000000..c9ef891d9579ff20556fc4f8f2297502ac12ab15 Binary files /dev/null and b/code/ArkTS1.2/FilesSample/entry/src/main/resources/base/media/compressFile_2.png differ diff --git a/code/ArkTS1.2/FilesSample/entry/src/main/resources/base/media/decompressFile_1.png b/code/ArkTS1.2/FilesSample/entry/src/main/resources/base/media/decompressFile_1.png new file mode 100644 index 0000000000000000000000000000000000000000..e7cbcd2b54e64686c3350b8b22c33a57df4748b5 Binary files /dev/null and b/code/ArkTS1.2/FilesSample/entry/src/main/resources/base/media/decompressFile_1.png differ diff --git a/code/ArkTS1.2/FilesSample/entry/src/main/resources/base/media/decompressFile_2.png b/code/ArkTS1.2/FilesSample/entry/src/main/resources/base/media/decompressFile_2.png new file mode 100644 index 0000000000000000000000000000000000000000..8fb3adaf9b8512fb69067583775d40e7bcf71cbf Binary files /dev/null and b/code/ArkTS1.2/FilesSample/entry/src/main/resources/base/media/decompressFile_2.png differ diff --git a/code/ArkTS1.2/FilesSample/entry/src/main/resources/base/media/foreground.png b/code/ArkTS1.2/FilesSample/entry/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..97014d3e10e5ff511409c378cd4255713aecd85f Binary files /dev/null and b/code/ArkTS1.2/FilesSample/entry/src/main/resources/base/media/foreground.png differ diff --git a/code/ArkTS1.2/FilesSample/entry/src/main/resources/base/media/layered_image.json b/code/ArkTS1.2/FilesSample/entry/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/code/ArkTS1.2/FilesSample/entry/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/code/ArkTS1.2/FilesSample/entry/src/main/resources/base/media/main.png b/code/ArkTS1.2/FilesSample/entry/src/main/resources/base/media/main.png new file mode 100644 index 0000000000000000000000000000000000000000..f26561f76fd74134294821cfd8aa243fc2178dd6 Binary files /dev/null and b/code/ArkTS1.2/FilesSample/entry/src/main/resources/base/media/main.png differ diff --git a/code/ArkTS1.2/FilesSample/entry/src/main/resources/base/media/startIcon.png b/code/ArkTS1.2/FilesSample/entry/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/code/ArkTS1.2/FilesSample/entry/src/main/resources/base/media/startIcon.png differ diff --git a/code/ArkTS1.2/FilesSample/entry/src/main/resources/base/media/txt.png b/code/ArkTS1.2/FilesSample/entry/src/main/resources/base/media/txt.png new file mode 100644 index 0000000000000000000000000000000000000000..1c206221dd2f94369c917122478dba52a1c7ab7f Binary files /dev/null and b/code/ArkTS1.2/FilesSample/entry/src/main/resources/base/media/txt.png differ diff --git a/code/ArkTS1.2/FilesSample/entry/src/main/resources/base/media/zlib.png b/code/ArkTS1.2/FilesSample/entry/src/main/resources/base/media/zlib.png new file mode 100644 index 0000000000000000000000000000000000000000..f74a2b903da711ce8ab5f96a4b9d785c7eab458d Binary files /dev/null and b/code/ArkTS1.2/FilesSample/entry/src/main/resources/base/media/zlib.png differ diff --git a/code/ArkTS1.2/FilesSample/entry/src/main/resources/base/profile/backup_config.json b/code/ArkTS1.2/FilesSample/entry/src/main/resources/base/profile/backup_config.json new file mode 100644 index 0000000000000000000000000000000000000000..78f40ae7c494d71e2482278f359ec790ca73471a --- /dev/null +++ b/code/ArkTS1.2/FilesSample/entry/src/main/resources/base/profile/backup_config.json @@ -0,0 +1,3 @@ +{ + "allowToBackupRestore": true +} \ No newline at end of file diff --git a/code/ArkTS1.2/FilesSample/entry/src/main/resources/base/profile/main_pages.json b/code/ArkTS1.2/FilesSample/entry/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/code/ArkTS1.2/FilesSample/entry/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/code/ArkTS1.2/FilesSample/entry/src/main/resources/dark/element/color.json b/code/ArkTS1.2/FilesSample/entry/src/main/resources/dark/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..79b11c2747aec33e710fd3a7b2b3c94dd9965499 --- /dev/null +++ b/code/ArkTS1.2/FilesSample/entry/src/main/resources/dark/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#000000" + } + ] +} \ No newline at end of file diff --git a/code/ArkTS1.2/FilesSample/entry/src/main/resources/rawfile/bigfilecopy_test.jpg b/code/ArkTS1.2/FilesSample/entry/src/main/resources/rawfile/bigfilecopy_test.jpg new file mode 100644 index 0000000000000000000000000000000000000000..a2f02e4be32cb6ac31644424ba03576d0b0d91a5 Binary files /dev/null and b/code/ArkTS1.2/FilesSample/entry/src/main/resources/rawfile/bigfilecopy_test.jpg differ diff --git a/code/ArkTS1.2/FilesSample/entry/src/main/resources/rawfile/compressfile/compress_file1.txt b/code/ArkTS1.2/FilesSample/entry/src/main/resources/rawfile/compressfile/compress_file1.txt new file mode 100644 index 0000000000000000000000000000000000000000..0c670769ce449a446b361caf12e68c471372d3e1 --- /dev/null +++ b/code/ArkTS1.2/FilesSample/entry/src/main/resources/rawfile/compressfile/compress_file1.txt @@ -0,0 +1 @@ +compressFile1内容 \ No newline at end of file diff --git a/code/ArkTS1.2/FilesSample/entry/src/main/resources/rawfile/compressfile/compress_file2.txt b/code/ArkTS1.2/FilesSample/entry/src/main/resources/rawfile/compressfile/compress_file2.txt new file mode 100644 index 0000000000000000000000000000000000000000..32d838aec1e990f22aaad91aa060e3f34a5116e7 --- /dev/null +++ b/code/ArkTS1.2/FilesSample/entry/src/main/resources/rawfile/compressfile/compress_file2.txt @@ -0,0 +1 @@ +compressFile2内容 \ No newline at end of file diff --git a/code/ArkTS1.2/FilesSample/entry/src/main/resources/rawfile/compressfile/compress_file3.txt b/code/ArkTS1.2/FilesSample/entry/src/main/resources/rawfile/compressfile/compress_file3.txt new file mode 100644 index 0000000000000000000000000000000000000000..c88b9fb639ceb275be02393cbbc9d4bd6161744d --- /dev/null +++ b/code/ArkTS1.2/FilesSample/entry/src/main/resources/rawfile/compressfile/compress_file3.txt @@ -0,0 +1 @@ +compressFile3内容 \ No newline at end of file diff --git a/code/ArkTS1.2/FilesSample/entry/src/main/resources/rawfile/decompress_file_test.zip b/code/ArkTS1.2/FilesSample/entry/src/main/resources/rawfile/decompress_file_test.zip new file mode 100644 index 0000000000000000000000000000000000000000..a371a0000f131d641a0f72d1e64de897ca85c4a6 --- /dev/null +++ b/code/ArkTS1.2/FilesSample/entry/src/main/resources/rawfile/decompress_file_test.zip @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:5d9e60cdd315f4a199e2936b9f3b2bccda470bfb53efb00ada145a19b4e10f8c +size 748 diff --git a/code/ArkTS1.2/FilesSample/hvigor/hvigor-config.json5 b/code/ArkTS1.2/FilesSample/hvigor/hvigor-config.json5 new file mode 100644 index 0000000000000000000000000000000000000000..fe83c3e838abfb3629eed84f6c7092f306d818f9 --- /dev/null +++ b/code/ArkTS1.2/FilesSample/hvigor/hvigor-config.json5 @@ -0,0 +1,22 @@ +{ + "modelVersion": "5.0.2", + "dependencies": { + }, + "execution": { + // "analyze": "normal", /* Define the build analyze mode. Value: [ "normal" | "advanced" | false ]. Default: "normal" */ + // "daemon": true, /* Enable daemon compilation. Value: [ true | false ]. Default: true */ + // "incremental": true, /* Enable incremental compilation. Value: [ true | false ]. Default: true */ + // "parallel": true, /* Enable parallel compilation. Value: [ true | false ]. Default: true */ + // "typeCheck": false, /* Enable typeCheck. Value: [ true | false ]. Default: false */ + }, + "logging": { + // "level": "info" /* Define the log level. Value: [ "debug" | "info" | "warn" | "error" ]. Default: "info" */ + }, + "debugging": { + // "stacktrace": false /* Disable stacktrace compilation. Value: [ true | false ]. Default: false */ + }, + "nodeOptions": { + // "maxOldSpaceSize": 8192 /* Enable nodeOptions maxOldSpaceSize compilation. Unit M. Used for the daemon process. Default: 8192*/ + // "exposeGC": true /* Enable to trigger garbage collection explicitly. Default: true*/ + } +} diff --git a/code/ArkTS1.2/FilesSample/hvigorfile.ts b/code/ArkTS1.2/FilesSample/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..f435606b5766719f7203539113d84a2b281b0353 --- /dev/null +++ b/code/ArkTS1.2/FilesSample/hvigorfile.ts @@ -0,0 +1,32 @@ +/** + * + * Copyright (c) 2025 Huawei Device Co., Ltd. + * + * All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice,this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS, + * + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +import { appTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: appTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/code/ArkTS1.2/FilesSample/oh-package.json5 b/code/ArkTS1.2/FilesSample/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..b6f7090658c0e69d5582af5d236fde4eab954ed6 --- /dev/null +++ b/code/ArkTS1.2/FilesSample/oh-package.json5 @@ -0,0 +1,34 @@ +/** + * + * Copyright (c) 2025 Huawei Device Co., Ltd. + * + * All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice,this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS, + * + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +{ + "modelVersion": "5.0.2", + "description": "Please describe the basic information.", + "dependencies": { + }, + "devDependencies": { + } +} diff --git a/code/ArkTS1.2/FilesSample/ohosTest.md b/code/ArkTS1.2/FilesSample/ohosTest.md new file mode 100644 index 0000000000000000000000000000000000000000..c1e8c54de625804bef9252212f63b8ca42194a05 --- /dev/null +++ b/code/ArkTS1.2/FilesSample/ohosTest.md @@ -0,0 +1,11 @@ +# 文件并行化处理场景测试用例 + +## 用例表 + +| 测试功能 | 预置条件 | 输入 | 预期输出 | 是否自动 | 测试结果 | +|-----------------------------------|-------------------------------------|----------------------|------------------------------------|------|------| +| 压缩文件功能验证 | 1. 需在真机测试
2. 构建并安装测试hap
3.进入【文件压缩案例】| 1、点击压缩按钮 | 1、压缩结果标题下出现被压缩后的zip文件名 | 否 | Pass | +| 验证开始拷贝功能 | 1. 需在真机测试
2. 构建并安装测试hap
3.进入【大文件拷贝案例】| 1、点击BigFileCopyView | 1、拷贝完成后提示当前复制进度:100% | 否 | Pass | +| 验证预览功能 | 1. 需在真机测试
2. 构建并安装测试hap
3.进入【大文件拷贝案例】| 1、复制完成后点击CompressFileView按钮 | 显示复制成功的图片 | 否 | Pass | +| 验证重置功能 | 1. 需在真机测试
2. 构建并安装测试hap
3.进入【大文件拷贝案例】| 1、复制完成后点击DecompressFileView按钮 | 1、重置完成后提示当前复制进度:0% | 否 | Pass | +| 压缩包解压功能 | 1. 需在真机测试
2. 构建并安装测试hap
3.进入【Worker子线程中解压文件案例】| 1、点击解压按钮 | 1、解压结果下方显示三个txt文件,页面弹出"文件解压成功"提示 | 否 | Pass | diff --git a/code/ArkTS1.2/ThreadSafeContainersSample/AppScope/app.json5 b/code/ArkTS1.2/ThreadSafeContainersSample/AppScope/app.json5 index 8b63a1f32a1fac14c4bafea21e3625acc92d49b4..a301783e60341af8d18df83591566594820cad60 100644 --- a/code/ArkTS1.2/ThreadSafeContainersSample/AppScope/app.json5 +++ b/code/ArkTS1.2/ThreadSafeContainersSample/AppScope/app.json5 @@ -1,3 +1,29 @@ +/** + * + * Copyright (c) 2025 Huawei Device Co., Ltd. + * + * All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice,this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS, + * + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + { "app": { "bundleName": "com.samples.threadsafecontainerssample", diff --git a/code/ArkTS1.2/ThreadSafeContainersSample/README.md b/code/ArkTS1.2/ThreadSafeContainersSample/README.md new file mode 100644 index 0000000000000000000000000000000000000000..a2e1017f5c05f01e7fa50f29c6a8a58455cf4a2b --- /dev/null +++ b/code/ArkTS1.2/ThreadSafeContainersSample/README.md @@ -0,0 +1,94 @@ + +# 线程安全容器 + +### 介绍 +验证线程安全容器(ArrayBlockingQueue、ConcurrentHashMap、ConcurrentSet、LinkedBlockingQueue)在多线程的下的安全性 + +### 效果图预览 +![main](entry/src/main/resources/base/media/threadSafeContainers_1.png) +![main](entry/src/main/resources/base/media/threadSafeContainers_2.png) +![main](entry/src/main/resources/base/media/threadSafeContainers_3.png) + +**使用说明** +1. ArrayBlockingQueue + * 点击场景一:接口调用,下方文本域显示接口调用结果 + * 点击场景二:并发入队,下方文本域显示push 200个数字过程和测试成功 + * 点击场景三:并发出队,下方文本域显示pop 200个数字过程和测试成功 + * 点击场景四:队列为空时阻塞,下方文本域显示pop数据log,但无成功log,等到输出push数据并成功之后,显示pop数据成功 +2. ConcurrentHashMap + * 点击场景一:接口调用,下方文本域显示接口调用结果 + * 点击场景二:并发入队,下方文本域显示set 100个数字过程和测试成功 + * 点击场景三:并发出队,下方文本域显示delete 100个数字过程和测试成功 + * 点击场景四:Benchmark +3. ConcurrentSet + * 点击场景一:接口调用,下方文本域显示接口调用结果 + * 点击场景二:并发入队,下方文本域显示add 200个数字过程和测试成功 + * 点击场景三:并发出队,下方文本域显示delete 100个数字过程和测试成功 +4. LinkedBlockingQueue + * 点击场景一:接口调用,下方文本域显示接口调用结果 + * 点击场景二:并发入队,下方文本域显示push 200个数字过程和测试成功 + * 点击场景三:并发出队,下方文本域显示pop 200个数字过程和测试成功 + * 点击场景四:队列为满时阻塞,下方文本域显示push数据log,但无成功log,等到输出pop数据并成功之后,显示push数据成功 + * 点击场景五:队列为空时阻塞,下方文本域显示pop数据log,但无成功log,等到输出push数据并成功之后,显示pop数据成功 + +### 工程结构&模块类型 + + ``` + entry/src/main/ets/ +|---pages +| |---containers +| | |---ArrayBlockingQueue.ets +| | |---ConcurrentHashMap.ets +| | |---ConcurrentSet.ets +| | |---LinkedBlockingQueue.ets +| |---index.ets // 首页 + ``` + +### 具体实现 + +1. ArrayBlockingQueue + * 场景一:接口调用, ArrayBlockingQueue接口全调用,验证其正确性 + * 场景二:并发入队,开启两个线程,同时向ArrayBlockingQueue中push100个数字,确认数据是否正确push + * 场景三:并发出队,开启两个线程,同时pop ArrayBlockingQueue中的200个数字,确认ArrayBlockingQueue是否为空 + * 场景四:队列为空时阻塞,开启两个线程,一个线程pop数据,一个线程push数据,ArrayBlockingQueue为空时不能pop数据,需要等push数据之后才能pop +2. ConcurrentHashMap + * 场景一:接口调用,ConcurrentHashMap接口全调用,验证其正确性 + * 场景二:并发入队,开启两个线程,同时向ConcurrentHashMap中set50个数字,确认数据是否正确set + * 场景三:并发出队,开启两个线程,同时delete ConcurrentHashMap中的100个数字,确认 ConcurrentHashMap是否为空 + * 场景四:Benchmark,开启六个线程,计算执行六个任务所需时间 +3. ConcurrentSet + * 场景一:接口调用,ConcurrentSet接口全调用,验证其正确性 + * 场景二:并发入队,开启两个线程,同时向ConcurrentSet中add 100个数字,确认数据是否正确add + * 场景三:并发出队,开启两个线程,同时delete ConcurrentSet中的200个数字,确认ConcurrentSet是否为空 +4. LinkedBlockingQueue + * 场景一:接口调用,LinkedBlockingQueue接口全调用,验证其正确性 + * 场景二:并发入队,开启两个线程,同时向LinkedBlockingQueue中push 100个数字,确认数据是否正确push + * 场景三:并发出队,开启两个线程,同时pop LinkedBlockingQueue中的200个数字,确认LinkedBlockingQueue是否为空 + * 场景四:队列为满时阻塞,开启两个线程,一个线程push数据,一个线程pop数据,LinkedBlockingQueue为满时不能push数据,需要等pop数据之后才能push + * 场景五:队列为空时阻塞,开启两个线程,一个线程pop数据,一个线程push数据,LinkedBlockingQueue为空时不能pop数据,需要等push数据之后才能pop + +### 相关权限 + +无 + +### 依赖 + +无 + +### 约束与限制 + +1. 本示例仅支持标准系统上运行,支持设备:Phone; +2. 本示例为Stage模型,支持API20版本SDK,SDK版本号(API Version 20),镜像版本号(6.0.0.31)。 +3. 本示例需要使用DevEco Studio 版本号(6.0.0.21)版本才可编译运行。 + +### 下载 + +如需单独下载本工程,执行如下命令: + +``` +git init +git config core.sparsecheckout true +echo code/ArkTS1.2/ComponentSample/ > .git/info/sparse-checkout +git remote add origin https://gitee.com/openharmony/applications_app_samples.git +git pull +``` \ No newline at end of file diff --git a/code/ArkTS1.2/ThreadSafeContainersSample/build-profile.json5 b/code/ArkTS1.2/ThreadSafeContainersSample/build-profile.json5 index d5b13fa941f6f3912e974615e2e9a02f1a819a02..325f69ce3922d8e67015cc3e5dd436901efb9923 100644 --- a/code/ArkTS1.2/ThreadSafeContainersSample/build-profile.json5 +++ b/code/ArkTS1.2/ThreadSafeContainersSample/build-profile.json5 @@ -1,3 +1,29 @@ +/** + * + * Copyright (c) 2025 Huawei Device Co., Ltd. + * + * All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice,this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS, + * + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + { "app": { "signingConfigs": [], @@ -5,9 +31,8 @@ { "name": "default", "signingConfig": "default", - "compatibleSdkVersion": "5.0.2(14)", + "compatibleSdkVersion": "6.0.0(20)", "runtimeOS": "HarmonyOS", - "arkTSVersion": "1.2", "buildOption": { "strictMode": { "caseSensitiveCheck": true, diff --git a/code/ArkTS1.2/ThreadSafeContainersSample/code-linter.json5 b/code/ArkTS1.2/ThreadSafeContainersSample/code-linter.json5 index 073990fa45394e1f8e85d85418ee60a8953f9b99..87b3919d419c09728067f1b545b7e2d5116adc07 100644 --- a/code/ArkTS1.2/ThreadSafeContainersSample/code-linter.json5 +++ b/code/ArkTS1.2/ThreadSafeContainersSample/code-linter.json5 @@ -1,3 +1,29 @@ +/** + * + * Copyright (c) 2025 Huawei Device Co., Ltd. + * + * All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice,this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS, + * + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + { "files": [ "**/*.ets" diff --git a/code/ArkTS1.2/ThreadSafeContainersSample/entry/build-profile.json5 b/code/ArkTS1.2/ThreadSafeContainersSample/entry/build-profile.json5 index 8ac81840b69fd7c78c2024c6570ed01e4292cf6d..ae354da257a9b7d08066736a31669a30216274ee 100644 --- a/code/ArkTS1.2/ThreadSafeContainersSample/entry/build-profile.json5 +++ b/code/ArkTS1.2/ThreadSafeContainersSample/entry/build-profile.json5 @@ -1,3 +1,29 @@ +/** + * + * Copyright (c) 2025 Huawei Device Co., Ltd. + * + * All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice,this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS, + * + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + { "apiType": "stageMode", "arkTSVersion": "1.2", diff --git a/code/ArkTS1.2/ThreadSafeContainersSample/entry/hvigorfile.ts b/code/ArkTS1.2/ThreadSafeContainersSample/entry/hvigorfile.ts index c6edcd90486dd5a853cf7d34c8647f08414ca7a3..8f830cdfd5bfdd8bde92ebfc27fc0562423dca3d 100644 --- a/code/ArkTS1.2/ThreadSafeContainersSample/entry/hvigorfile.ts +++ b/code/ArkTS1.2/ThreadSafeContainersSample/entry/hvigorfile.ts @@ -1,3 +1,29 @@ +/** + * + * Copyright (c) 2025 Huawei Device Co., Ltd. + * + * All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice,this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS, + * + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + import { hapTasks } from '@ohos/hvigor-ohos-plugin'; export default { diff --git a/code/ArkTS1.2/ThreadSafeContainersSample/entry/oh-package.json5 b/code/ArkTS1.2/ThreadSafeContainersSample/entry/oh-package.json5 index 248c3b7541a589682a250f86a6d3ecf7414d2d6a..39ab3f901494a87d5ab258dfaa9e6882de1f5166 100644 --- a/code/ArkTS1.2/ThreadSafeContainersSample/entry/oh-package.json5 +++ b/code/ArkTS1.2/ThreadSafeContainersSample/entry/oh-package.json5 @@ -1,3 +1,29 @@ +/** + * + * Copyright (c) 2025 Huawei Device Co., Ltd. + * + * All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice,this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS, + * + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + { "name": "entry", "version": "1.0.0", diff --git a/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/ets/entryability/EntryAbility.ets b/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/ets/entryability/EntryAbility.ets index b892d18a00d879e20ee20840d94a2e06f10ae1c5..6342206113a10a2d7eaa67874e6f84d3fec379fa 100644 --- a/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/ets/entryability/EntryAbility.ets +++ b/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/ets/entryability/EntryAbility.ets @@ -1,3 +1,29 @@ +/** + * + * Copyright (c) 2025 Huawei Device Co., Ltd. + * + * All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice,this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS, + * + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + import UIAbility from '@ohos.app.ability.UIAbility'; import AbilityConstant from '@ohos.app.ability.AbilityConstant'; import Want from '@ohos.app.ability.Want'; diff --git a/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/ets/models/MyCallback.ets b/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/ets/models/MyCallback.ets index 5827b846dc824ef26c25e0a8612717cb7b448a3a..430e22de4fa5bfa011d66e22f32031967ca99119 100644 --- a/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/ets/models/MyCallback.ets +++ b/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/ets/models/MyCallback.ets @@ -1,3 +1,29 @@ +/** + * + * Copyright (c) 2025 Huawei Device Co., Ltd. + * + * All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice,this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS, + * + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + import { AsyncCallback, BusinessError, Callback, ErrorCallback } from '@ohos.base'; import hilog from '@ohos.hilog'; diff --git a/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/ets/pages/Index.ets b/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/ets/pages/Index.ets index 98546ebff6b8c8c2ae054256ce39fff76667f922..a51520e10e01d239f45f6d230ab11c3e6dcca899 100644 --- a/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/ets/pages/Index.ets +++ b/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/ets/pages/Index.ets @@ -1,77 +1,218 @@ -import { - memo, - __memo_context_type, - __memo_id_type -} from '@ohos.arkui.stateManagement' // should be insert by ui-plugins -import { - Text, - TextAttribute, - Column, - Component, - Button, - ButtonAttribute, - ClickEvent, - UserView -} from '@ohos.arkui.component' // TextAttribute should be insert by ui-plugins -import { - State, - StateDecoratedVariable, - MutableState, - stateOf, - observableProxy -} from '@ohos.arkui.stateManagement' // should be insert by ui-plugins +/** + * + * Copyright (c) 2025 Huawei Device Co., Ltd. + * + * All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice,this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS, + * + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +import { memo, __memo_context_type, __memo_id_type } from '@ohos.arkui.stateManagement' // should be insert by ui-plugins +import { Entry, Row, Text, TextAttribute, Column, Component, Button, ButtonAttribute, ClickEvent, UserView, TextArea, Scroll } from '@ohos.arkui.component' // TextAttribute should be insert by ui-plugins +import { State, MutableState, stateOf, observableProxy } from '@ohos.arkui.stateManagement' // should be insert by ui-plugins import hilog from '@ohos.hilog' import { ArrayBlockingQueueTest } from './containers/ArrayBlockingQueueTest'; import { ConcurrentHashMapTest } from './containers/ConcurrentHashMapTest'; import { ConcurrentSetTest } from './containers/ConcurrentSetTest'; import { LinkedBlockingQueueTest } from './containers/LinkedBlockingQueueTest'; - @Component struct MyStateSample { - aboutToAppear() { - let arrayQueue = new ArrayBlockingQueueTest(); - arrayQueue.basicScene(); - arrayQueue.concurrentInQueue(); - arrayQueue.concurrentOutQueue(); - arrayQueue.nullBlocking(); - let map = new ConcurrentHashMapTest(); - map.basicScene(); - map.concurrentInQueue(); - map.concurrentOutQueue(); - map.hashMapBenchmark(); - let set = new ConcurrentSetTest(); - set.basicScene(); - set.concurrentInSet(); - set.concurrentOutSet(); - let linkedQueue = new LinkedBlockingQueueTest(); - linkedQueue.basicScene(); - linkedQueue.concurrentInQueue(); - linkedQueue.concurrentOutQueue(); - linkedQueue.fullBlocking(); - linkedQueue.nullBlocking(); - } + @State logs: Array = new Array(); + @State logsString1: string = ''; + @State logsString2: string = ''; + @State logsString3: string = ''; + @State logsString4: string = ''; - @State stateVar: string = 'state var'; - message: string = 'var'; + @State container1: string = 'ArrayBlockingQueue'; + @State container2: string = 'ConcurrentSet'; + @State container3: string = 'ConcurrentHashMap'; + @State container4: string = 'LinkedBlockingQueue'; build() { - Column(undefined) { - Button(this.message).backgroundColor('#FFFF00FF') - .onClick((e: ClickEvent) => { - hilog.info(0x0000, 'testTag', 'On Click'); - }) - Text(this.stateVar).fontSize(20) + Scroll() { + Column(undefined) { + Text(this.container1) + .fontSize(22) + Button('场景一:接口调用') + .fontSize(18) + .margin(5) + .onClick((e: ClickEvent) => { + let arrayQueue = new ArrayBlockingQueueTest(); + this.logsString1 = arrayQueue.basicScene().join(`\n`); + }) + Button('场景二:并发入队') + .fontSize(18) + .margin(5) + .onClick((e: ClickEvent) => { + let arrayQueue = new ArrayBlockingQueueTest(); + this.logsString1 = arrayQueue.concurrentInQueue().join(`\n`); + }) + Button('场景三:并发出队') + .fontSize(18) + .margin(5) + .onClick((e: ClickEvent) => { + let arrayQueue = new ArrayBlockingQueueTest(); + this.logsString1 = arrayQueue.concurrentOutQueue().join(`\n`); + }) + Button('场景四:队列为空时阻塞') + .fontSize(18) + .margin(5) + .onClick((e: ClickEvent) => { + let arrayQueue = new ArrayBlockingQueueTest(); + this.logsString1 = arrayQueue.nullBlocking().join(`\n`); + }) + TextArea({ text: this.logsString1 }) + .fontSize(16) + .width('80%') + .height(180) + Text(this.container2) + .fontSize(22) + .margin(30) + Button('场景一:接口调用') + .fontSize(18) + .margin(5) + .onClick((e: ClickEvent) => { + let set = new ConcurrentSetTest(); + this.logsString2 = set.basicScene().join(`\n`); + hilog.info(0x0000, 'testTag', 'ConcurrentSetTest this.logsString2 ' + this.logsString2); + }) + Button('场景二:并列入队') + .fontSize(18) + .margin(5) + .onClick((e: ClickEvent) => { + let set = new ConcurrentSetTest(); + this.logsString2 = set.concurrentInSet().join(`\n`); + hilog.info(0x0000, 'testTag', 'ConcurrentSetTest this.logsString2 ' + this.logsString2); + }) + Button('场景三:并列出队') + .fontSize(18) + .margin(5) + .onClick((e: ClickEvent) => { + let set = new ConcurrentSetTest(); + this.logsString2 = set.concurrentOutSet().join(`\n`); + hilog.info(0x0000, 'testTag', 'ConcurrentSetTest this.logsString2 ' + this.logsString2); + }) + TextArea({ text: this.logsString2 }) + .fontSize(16) + .width('80%') + .height(180) + Text(this.container3) + .fontSize(22) + .margin(30) + Button('场景一:接口调用') + .fontSize(18) + .margin(5) + .onClick((e: ClickEvent) => { + let map = new ConcurrentHashMapTest(); + this.logsString3 = map.basicScene().join(`\n`); + }) + Button('场景二:并列入队') + .fontSize(18) + .margin(5) + .onClick((e: ClickEvent) => { + let map = new ConcurrentHashMapTest(); + this.logsString3 = map.concurrentInQueue().join(`\n`); + }) + Button('场景三:并列出队') + .fontSize(18) + .margin(5) + .onClick((e: ClickEvent) => { + let map = new ConcurrentHashMapTest(); + this.logsString3 = map.concurrentOutQueue().join(`\n`); + }) + Button('场景四:Benchmark') + .fontSize(18) + .margin(5) + .onClick((e: ClickEvent) => { + let map = new ConcurrentHashMapTest(); + this.logsString3 = map.hashMapBenchmark(); + }) + TextArea({ text: this.logsString3 }) + .fontSize(16) + .width('80%') + .height(180) + Text(this.container4) + .fontSize(22) + .margin(30) + Button('场景一:接口调用') + .fontSize(18) + .margin(5) + .onClick((e: ClickEvent) => { + this.logsString4 = ''; + let linkedQueue = new LinkedBlockingQueueTest(); + this.logsString4 = linkedQueue.basicScene().join(`\n`); + hilog.info(0x0000, 'testTag', 'LinkedBlockingQueueTest this.logsString4 ' + this.logsString4); + }) + Button('场景二:并列入队') + .fontSize(18) + .margin(5) + .onClick((e: ClickEvent) => { + this.logsString4 = ''; + let linkedQueue = new LinkedBlockingQueueTest(); + this.logsString4 = linkedQueue.concurrentInQueue().join(`\n`); + hilog.info(0x0000, 'testTag', 'LinkedBlockingQueueTest this.logsString4 ' + this.logsString4); + + }) + Button('场景三:并列出队') + .fontSize(18) + .margin(5) + .onClick((e: ClickEvent) => { + this.logsString4 = ''; + let linkedQueue = new LinkedBlockingQueueTest(); + this.logsString4 = linkedQueue.concurrentOutQueue().join(`\n`); + hilog.info(0x0000, 'testTag', 'LinkedBlockingQueueTest this.logsString4 ' + this.logsString4); + + }) + Button('场景四:队列满时阻塞') + .fontSize(18) + .margin(5) + .onClick((e: ClickEvent) => { + this.logsString4 = ''; + let linkedQueue = new LinkedBlockingQueueTest(); + this.logsString4 = linkedQueue.fullBlocking().join(`\n`); + hilog.info(0x0000, 'testTag', 'LinkedBlockingQueueTest this.logsString4 ' + this.logsString4); + + }) + Button('场景五:队列空时阻塞') + .fontSize(18) + .margin(5) + .onClick((e: ClickEvent) => { + this.logsString4 = ''; + let linkedQueue = new LinkedBlockingQueueTest(); + this.logsString4 = linkedQueue.nullBlocking().join(`\n`); + hilog.info(0x0000, 'testTag', 'LinkedBlockingQueueTest this.logsString4 ' + this.logsString4); + + }) + TextArea({ text: this.logsString4 }) + .fontSize(16) + .width('80%') + .height(180) + }.width('100%') } } } - export class ComExampleTrivialApplication extends UserView { getBuilder() { hilog.info(0x0000, 'testTag', 'getBuilder'); - let wrapper = @ - memo() => - { + let wrapper = @memo () => { hilog.info(0x0000, 'testTag', 'MyStateSample'); MyStateSample(undefined) } diff --git a/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/ets/pages/containers/ArrayBlockingQueueTest.ets b/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/ets/pages/containers/ArrayBlockingQueueTest.ets index 83802c8a2e54b3357090d5e571c5504c9537a617..6ae29a1536d9580bee35db7cfa838c6e87878a32 100644 --- a/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/ets/pages/containers/ArrayBlockingQueueTest.ets +++ b/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/ets/pages/containers/ArrayBlockingQueueTest.ets @@ -1,3 +1,29 @@ +/** + * + * Copyright (c) 2025 Huawei Device Co., Ltd. + * + * All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice,this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS, + * + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + import hilog from '@ohos.hilog'; function pushData1(queue: ArrayBlockingQueue, logs: Array) { @@ -45,7 +71,7 @@ function nullBlockingPush(queue: ArrayBlockingQueue, logs: Array) { } export class ArrayBlockingQueueTest { - basicScene() { + basicScene(): Array { const queueCapacity: int = 10; let queueInt: ArrayBlockingQueue = new ArrayBlockingQueue(queueCapacity); let logs: Array = new Array(); @@ -64,55 +90,59 @@ export class ArrayBlockingQueueTest { const isEmpty = queueInt.isEmpty(); logs.push(`isEmpty: ${isEmpty}`); hilog.info(0x0000, 'testTag', 'ArrayBlockingQueue basicScene ' + logs); + return logs; } - concurrentInQueue() { + concurrentInQueue(): Array { const queueCapacity: int = 200; let queueInt: ArrayBlockingQueue = new ArrayBlockingQueue(queueCapacity); let logs: Array = new Array(); - let p1 = launch - pushData1(queueInt, logs); - let p2 = launch - pushData2(queueInt, logs); - await p1; - await p2; + let task1: taskpool.Task = new taskpool.Task(pushData1, queueInt, logs); + let task2: taskpool.Task = new taskpool.Task(pushData2, queueInt, logs); + let t1 = taskpool.execute(task1); + let t2 = taskpool.execute(task2); + await t1; + await t2; const queueSize = queueInt.size; if (queueCapacity === queueSize) { logs.push('测试成功'); } hilog.info(0x0000, 'testTag', 'ArrayBlockingQueue concurrentInQueue ' + logs); + return logs; } - concurrentOutQueue() { + concurrentOutQueue(): Array { const queueCapacity = 200; let queueInt: ArrayBlockingQueue = new ArrayBlockingQueue(queueCapacity); let logs: Array = new Array(); for (let i: int = 0; i < queueCapacity; i++) { queueInt.push(i); } - let p1 = launch - popData1(queueInt, logs); - let p2 = launch - popData2(queueInt, logs); - await p1; - await p2; + let task1: taskpool.Task = new taskpool.Task(popData1, queueInt, logs); + let task2: taskpool.Task = new taskpool.Task(popData2, queueInt, logs); + let t1 = taskpool.execute(task1); + let t2 = taskpool.execute(task2); + await t1; + await t2; const isEmpty = queueInt.isEmpty(); if (isEmpty) { logs.push('测试成功'); } hilog.info(0x0000, 'testTag', 'ArrayBlockingQueue concurrentOutQueue ' + logs); + return logs; } - nullBlocking() { + nullBlocking(): Array { const queueCapacity = 3; let queueInt: ArrayBlockingQueue = new ArrayBlockingQueue(queueCapacity); let logs: Array = new Array(); - let p1 = launch - nullBlockingPop(queueInt, logs); - let p2 = launch - nullBlockingPush(queueInt, logs); - await p1; - await p2; + let task1: taskpool.Task = new taskpool.Task(nullBlockingPop, queueInt, logs); + let task2: taskpool.Task = new taskpool.Task(nullBlockingPush, queueInt, logs); + let t1 = taskpool.execute(task1); + let t2 = taskpool.execute(task2); + await t1; + await t2; hilog.info(0x0000, 'testTag', 'ArrayBlockingQueue nullBlocking ' + logs); + return logs; } } \ No newline at end of file diff --git a/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/ets/pages/containers/ConcurrentHashMapTest.ets b/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/ets/pages/containers/ConcurrentHashMapTest.ets index e0c8c69ae6109c61187b27fd15ce4e5906d42f2c..1ff620905c1f4425bf85146d4263c9cdfdb89f6d 100644 --- a/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/ets/pages/containers/ConcurrentHashMapTest.ets +++ b/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/ets/pages/containers/ConcurrentHashMapTest.ets @@ -1,3 +1,50 @@ +/** + * + * Copyright (c) 2025 Huawei Device Co., Ltd. + * + * All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice,this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS, + * + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +import { + memo, + __memo_context_type, + __memo_id_type +} from '@ohos.arkui.stateManagement'; // should be insert by ui-plugins +import { + Text, + TextAttribute, + Column, + Component, + Button, + ButtonAttribute, + ClickEvent, + UserView +} from '@ohos.arkui.component'; // TextAttribute should be insert by ui-plugins +import { + State, + MutableState, + stateOf, + observableProxy +} from '@ohos.arkui.stateManagement'; // should be insert by ui-plugins import hilog from '@ohos.hilog'; const MAP_CAPACITY: number = 100; @@ -34,22 +81,50 @@ function deleteData2(map: ConcurrentHashMap, logs: Array } } -function setLargeData(map: ConcurrentHashMap, logs: ArrayBlockingQueue, quantity: number) { - for (let i = quantity; i < quantity * 2; i++) { +function setLargeData1(map: ConcurrentHashMap, logs: ArrayBlockingQueue) { + for (let i = 200; i < 400; i++) { + map.set(i, `v${i}`); + logs.push(`set key: ${i} value: v${i}`); + } +} + +function setLargeData2(map: ConcurrentHashMap, logs: ArrayBlockingQueue) { + for (let i = 400; i < 800; i++) { + map.set(i, `v${i}`); + logs.push(`set key: ${i} value: v${i}`); + } +} + +function setLargeData3(map: ConcurrentHashMap, logs: ArrayBlockingQueue) { + for (let i = 800; i < 1600; i++) { map.set(i, `v${i}`); logs.push(`set key: ${i} value: v${i}`); } } -function getLargeData(map: ConcurrentHashMap, logs: ArrayBlockingQueue, quantity: number) { - for (let i = quantity; i < quantity * 2; i++) { +function getLargeData1(map: ConcurrentHashMap, logs: ArrayBlockingQueue) { + for (let i = 200; i < 400; i++) { + let getData = map.get(i); + logs.push(`get key: ${i} value: ${getData}`); + } +} + +function getLargeData2(map: ConcurrentHashMap, logs: ArrayBlockingQueue) { + for (let i = 200; i < 400; i++) { + let getData = map.get(i); + logs.push(`get key: ${i} value: ${getData}`); + } +} + +function getLargeData3(map: ConcurrentHashMap, logs: ArrayBlockingQueue) { + for (let i = 200; i < 400; i++) { let getData = map.get(i); logs.push(`get key: ${i} value: ${getData}`); } } export class ConcurrentHashMapTest { - basicScene() { + basicScene(): Array { let hashMapInt: ConcurrentHashMap = new ConcurrentHashMap(); let logs: Array = new Array(); for (let i = 0; i < 10; i++) { @@ -72,8 +147,7 @@ export class ConcurrentHashMapTest { } let value = v.value; keysStr += value + ' '; - } - ; + }; logs.push(`keys: ${keysStr}`); const values = hashMapInt.values(); let valuesStr = ''; @@ -84,8 +158,7 @@ export class ConcurrentHashMapTest { } let value = v.value; valuesStr += value + ' '; - } - ; + }; logs.push(`values: ${valuesStr}`); const entries = hashMapInt.entries(); let entriesStr = ''; @@ -96,8 +169,7 @@ export class ConcurrentHashMapTest { } let value = v.value; entriesStr += value + ' '; - } - ; + }; logs.push(`entries: ${entriesStr}`); const deleteKey = 3; const isDelete = hashMapInt.delete(3); @@ -127,70 +199,74 @@ export class ConcurrentHashMapTest { logs.push(`hashMapInt通过clear方法清空,hashMapInt.size = ${mapSize} `); } hilog.info(0x0000, 'testTag', 'ConcurrentHashMap basicScene ' + logs); + return logs; } - concurrentInQueue() { + concurrentInQueue(): Array { let hashMapInt: ConcurrentHashMap = new ConcurrentHashMap(); let logs: Array = new Array(); - let p1 = launch - setData1(hashMapInt, logs); - let p2 = launch - setData2(hashMapInt, logs); - await p1; - await p2; + let task1: taskpool.Task = new taskpool.Task(setData1, hashMapInt, logs); + let task2: taskpool.Task = new taskpool.Task(setData2, hashMapInt, logs); + let t1 = taskpool.execute(task1); + let t2 = taskpool.execute(task2); + await t1; + await t2; const mapSize = hashMapInt.size; if (MAP_CAPACITY === mapSize) { logs.push('测试成功'); } hilog.info(0x0000, 'testTag', 'ConcurrentHashMap concurrentInQueue ' + logs); + return logs; } - concurrentOutQueue() { + concurrentOutQueue(): Array { let hashMapInt: ConcurrentHashMap = new ConcurrentHashMap(); let logs: Array = new Array(); for (let i = 0; i < MAP_CAPACITY; i++) { hashMapInt.set(i, `v${i}`); } - let p1 = launch - deleteData1(hashMapInt, logs); - let p2 = launch - deleteData2(hashMapInt, logs); - await p1; - await p2; + let task1: taskpool.Task = new taskpool.Task(deleteData1, hashMapInt, logs); + let task2: taskpool.Task = new taskpool.Task(deleteData2, hashMapInt, logs); + let t1 = taskpool.execute(task1); + let t2 = taskpool.execute(task2); + await t1; + await t2; const isEmpty = hashMapInt.isEmpty(); if (isEmpty) { logs.push('测试成功'); } hilog.info(0x0000, 'testTag', 'ConcurrentHashMap concurrentOutQueue ' + logs); + return logs; } - hashMapBenchmark() { + hashMapBenchmark(): string { const startTime = new Date().getTime(); let hashMapInt: ConcurrentHashMap = new ConcurrentHashMap(); let logs: ArrayBlockingQueue = new ArrayBlockingQueue(2801); - let p1 = launch - setLargeData(hashMapInt, logs, 200); - let p2 = launch - setLargeData(hashMapInt, logs, 400); - let p3 = launch - setLargeData(hashMapInt, logs, 800); - let p4 = launch - getLargeData(hashMapInt, logs, 200); - let p5 = launch - getLargeData(hashMapInt, logs, 400); - let p6 = launch - getLargeData(hashMapInt, logs, 800); - await p1; - await p2; - await p3; - await p4; - await p5; - await p6; - + let task1: taskpool.Task = new taskpool.Task(setLargeData1, hashMapInt, logs); + let task2: taskpool.Task = new taskpool.Task(setLargeData2, hashMapInt, logs); + let task3: taskpool.Task = new taskpool.Task(setLargeData3, hashMapInt, logs); + let task4: taskpool.Task = new taskpool.Task(getLargeData1, hashMapInt, logs); + let task5: taskpool.Task = new taskpool.Task(getLargeData2, hashMapInt, logs); + let task6: taskpool.Task = new taskpool.Task(getLargeData3, hashMapInt, logs); + let t1 = taskpool.execute(task1); + let t2 = taskpool.execute(task2); + let t3 = taskpool.execute(task3); + let t4 = taskpool.execute(task4); + let t5 = taskpool.execute(task5); + let t6 = taskpool.execute(task6); + await t1; + await t2; + await t3; + await t4; + await t5; + await t6; const duration = new Date().getTime() - startTime; if (hashMapInt.size === 1400) { logs.push(`测试成功 吞吐量: 6 tasks in ${duration}ms`); } - hilog.info(0x0000, 'testTag', 'ConcurrentHashMap hashMapBenchmark ' + logs.getEnd()); + hilog.info(0x0000, 'testTag', 'ConcurrentHashMap hashMapBenchmark ' + logs); + return String(logs.getEnd()); } -} \ No newline at end of file +} + diff --git a/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/ets/pages/containers/ConcurrentSetTest.ets b/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/ets/pages/containers/ConcurrentSetTest.ets index 4f067084d2ef3191fafa8ac691423bf84189a488..b64cfe4a0b61108158504eb07f494f2e789d6efd 100644 --- a/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/ets/pages/containers/ConcurrentSetTest.ets +++ b/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/ets/pages/containers/ConcurrentSetTest.ets @@ -1,3 +1,29 @@ +/** + * + * Copyright (c) 2025 Huawei Device Co., Ltd. + * + * All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice,this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS, + * + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + import hilog from '@ohos.hilog'; function addData1(setInt: ConcurrentSet, logs: Array) { @@ -37,7 +63,7 @@ function deleteData2(setInt: ConcurrentSet, logs: Array) { } export class ConcurrentSetTest { - basicScene() { + basicScene(): Array { let setInt: ConcurrentSet = new ConcurrentSet(); let logs: Array = new Array(); for (let i = 0; i < 10; i++) { @@ -76,35 +102,36 @@ export class ConcurrentSetTest { } let value = v.value; valuesStr += value + ' '; - } - ; + }; logs.push(`values: ${valuesStr}`); logs.push(`当前集合: ${setInt}`); setInt.clear(); const setSize = setInt.size; logs.push(`清空集合后size: ${setSize}`); hilog.info(0x0000, 'testTag', 'ConcurrentSet basicScene ' + logs); + return logs; } - concurrentInSet() { + concurrentInSet(): Array { const setCapacity = 200; let set: Set = new Set(); let setInt: ConcurrentSet = new ConcurrentSet(set); let logs: Array = new Array(); - let p1 = launch - addData1(setInt, logs); - let p2 = launch - addData2(setInt, logs); - await p1; - await p2; + let task1: taskpool.Task = new taskpool.Task(addData1, setInt, logs); + let task2: taskpool.Task = new taskpool.Task(addData2, setInt, logs); + let t1 = taskpool.execute(task1); + let t2 = taskpool.execute(task2); + await t1; + await t2; const setSize = setInt.size; if (setCapacity === setSize) { logs.push('测试成功'); } hilog.info(0x0000, 'testTag', 'ConcurrentSet concurrentInSet ' + logs); + return logs; } - concurrentOutSet() { + concurrentOutSet(): Array { const setCapacity = 200; let set: Set = new Set(); for (let i: number = 0; i < setCapacity; i++) { @@ -112,15 +139,16 @@ export class ConcurrentSetTest { } let setInt: ConcurrentSet = new ConcurrentSet(set); let logs: Array = new Array(); - let p1 = launch - deleteData1(setInt, logs); - let p2 = launch - deleteData2(setInt, logs); - await p1; - await p2; + let task1: taskpool.Task = new taskpool.Task(deleteData1, setInt, logs); + let task2: taskpool.Task = new taskpool.Task(deleteData2, setInt, logs); + let t1 = taskpool.execute(task1); + let t2 = taskpool.execute(task2); + await t1; + await t2; if (setInt.size === 0) { logs.push('测试成功'); } hilog.info(0x0000, 'testTag', 'ConcurrentSet concurrentOutSet ' + logs); + return logs } } \ No newline at end of file diff --git a/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/ets/pages/containers/LinkedBlockingQueueTest.ets b/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/ets/pages/containers/LinkedBlockingQueueTest.ets index 67588c246b3dd6d4afd598bd68dc8ec08891c17a..cb09974057f8c41779e89745e8297d80e9214fea 100644 --- a/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/ets/pages/containers/LinkedBlockingQueueTest.ets +++ b/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/ets/pages/containers/LinkedBlockingQueueTest.ets @@ -1,3 +1,29 @@ +/** + * + * Copyright (c) 2025 Huawei Device Co., Ltd. + * + * All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice,this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS, + * + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + import hilog from '@ohos.hilog'; function pushData1(queue: LinkedBlockingQueue, logs: Array) { @@ -61,7 +87,7 @@ function nullBlockingPush(queue: LinkedBlockingQueue, logs: Array) } export class LinkedBlockingQueueTest { - basicScene() { + basicScene(): Array { const queue = 10; let queueInt: LinkedBlockingQueue = new LinkedBlockingQueue(queue); let logs: Array = new Array(); @@ -84,71 +110,76 @@ export class LinkedBlockingQueueTest { logs.push(`remainingCapacity: ${remainingCapacity}`); hilog.info(0x0000, 'testTag', 'LinkedBlockingQueueTest basicScene ' + logs); + return logs; } - concurrentInQueue() { + concurrentInQueue(): Array { const queueCapacity = 200; let queueInt: LinkedBlockingQueue = new LinkedBlockingQueue(queueCapacity); let logs: Array = new Array(); - let p1 = launch - pushData1(queueInt, logs); - let p2 = launch - pushData2(queueInt, logs); - await p1; - await p2; + let task1: taskpool.Task = new taskpool.Task(pushData1, queueInt, logs); + let task2: taskpool.Task = new taskpool.Task(pushData2, queueInt, logs); + let t1 = taskpool.execute(task1); + let t2 = taskpool.execute(task2); + await t1; + await t2; const queueSize = queueInt.size; if (queueCapacity === queueSize) { logs.push('测试成功'); } hilog.info(0x0000, 'testTag', 'LinkedBlockingQueueTest concurrentInQueue ' + logs); + return logs; } - concurrentOutQueue() { + concurrentOutQueue(): Array { const queueCapacity = 200; let queueInt: LinkedBlockingQueue = new LinkedBlockingQueue(queueCapacity); let logs: Array = new Array(); for (let i = 0; i < queueCapacity; i++) { queueInt.push(i); } - let p1 = launch - popData1(queueInt, logs); - let p2 = launch - popData2(queueInt, logs); - await p1; - await p2; + let task1: taskpool.Task = new taskpool.Task(popData1, queueInt, logs); + let task2: taskpool.Task = new taskpool.Task(popData2, queueInt, logs); + let t1 = taskpool.execute(task1); + let t2 = taskpool.execute(task2); + await t1; + await t2; const isEmpty = queueInt.isEmpty(); if (isEmpty) { logs.push('测试成功'); } hilog.info(0x0000, 'testTag', 'LinkedBlockingQueueTest concurrentOutQueue ' + logs); + return logs; } - fullBlocking() { + fullBlocking(): Array { const queueCapacity = 3; let queueInt: LinkedBlockingQueue = new LinkedBlockingQueue(queueCapacity); let logs: Array = new Array(); for (let i = 0; i < queueCapacity; i++) { queueInt.push(i); } - let p1 = launch - fullBlockingPush(queueInt, logs); - let p2 = launch - fullBlockingPop(queueInt, logs); - await p1; - await p2; + let task1: taskpool.Task = new taskpool.Task(fullBlockingPush, queueInt, logs); + let task2: taskpool.Task = new taskpool.Task(fullBlockingPop, queueInt, logs); + let t1 = taskpool.execute(task1); + let t2 = taskpool.execute(task2); + await t1; + await t2; hilog.info(0x0000, 'testTag', 'LinkedBlockingQueueTest fullBlocking ' + logs); + return logs; } - nullBlocking() { + nullBlocking(): Array { const queueCapacity = 3; let queueInt: LinkedBlockingQueue = new LinkedBlockingQueue(queueCapacity); let logs: Array = new Array(); - let p1 = launch - nullBlockingPop(queueInt, logs); - let p2 = launch - nullBlockingPush(queueInt, logs); - await p1; - await p2; + let task1: taskpool.Task = new taskpool.Task(nullBlockingPop, queueInt, logs); + let task2: taskpool.Task = new taskpool.Task(nullBlockingPush, queueInt, logs); + let t1 = taskpool.execute(task1); + let t2 = taskpool.execute(task2); + await t1; + await t2; hilog.info(0x0000, 'testTag', 'LinkedBlockingQueueTest nullBlocking ' + logs); + return logs; } } \ No newline at end of file diff --git a/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/module.json5 b/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/module.json5 index de78b5cae3b2ba99788048311e5d0cd34d075039..0c6512e21da8c15fbc98b90bea5267574b08a053 100644 --- a/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/module.json5 +++ b/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/module.json5 @@ -1,3 +1,29 @@ +/** + * + * Copyright (c) 2025 Huawei Device Co., Ltd. + * + * All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice,this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS, + * + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + { "module": { "name": "entry", @@ -5,9 +31,8 @@ "description": "$string:module_desc", "mainElement": "EntryAbility", "deviceTypes": [ - "phone", + "default", "tablet", - "2in1" ], "deliveryWithInstall": true, "installationFree": false, diff --git a/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/resources/base/element/string.json b/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/resources/base/element/string.json index 7f6fcd10cfc441a3a6922a518a957647fd6c89dd..eda978bbe3af3418597f64e76b72c76d0bdaada1 100644 --- a/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/resources/base/element/string.json +++ b/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/resources/base/element/string.json @@ -10,7 +10,7 @@ }, { "name": "EntryAbility_label", - "value": "线程安全容器" + "value": "ThreadSafeContainersSample" } ] } \ No newline at end of file diff --git a/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/resources/base/media/threadSafeContainers_1.png b/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/resources/base/media/threadSafeContainers_1.png new file mode 100644 index 0000000000000000000000000000000000000000..1c72f8e270453da7aed0510c0b63ba3df5231684 Binary files /dev/null and b/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/resources/base/media/threadSafeContainers_1.png differ diff --git a/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/resources/base/media/threadSafeContainers_2.png b/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/resources/base/media/threadSafeContainers_2.png new file mode 100644 index 0000000000000000000000000000000000000000..b1e3bf4b9b081e6128fb099376a9fcb5ad596218 Binary files /dev/null and b/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/resources/base/media/threadSafeContainers_2.png differ diff --git a/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/resources/base/media/threadSafeContainers_3.png b/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/resources/base/media/threadSafeContainers_3.png new file mode 100644 index 0000000000000000000000000000000000000000..cbf13912286a46ab276c84dad7351d42e1ca4975 Binary files /dev/null and b/code/ArkTS1.2/ThreadSafeContainersSample/entry/src/main/resources/base/media/threadSafeContainers_3.png differ diff --git a/code/ArkTS1.2/ThreadSafeContainersSample/hvigor/hvigor-config.json5 b/code/ArkTS1.2/ThreadSafeContainersSample/hvigor/hvigor-config.json5 index 85e8d4b2175fc4747650344f025e7d145bc3d361..fe83c3e838abfb3629eed84f6c7092f306d818f9 100644 --- a/code/ArkTS1.2/ThreadSafeContainersSample/hvigor/hvigor-config.json5 +++ b/code/ArkTS1.2/ThreadSafeContainersSample/hvigor/hvigor-config.json5 @@ -1,5 +1,5 @@ { - "modelVersion": "5.1.0", + "modelVersion": "5.0.2", "dependencies": { }, "execution": { diff --git a/code/ArkTS1.2/ThreadSafeContainersSample/hvigorfile.ts b/code/ArkTS1.2/ThreadSafeContainersSample/hvigorfile.ts index f3cb9f1a87a81687554a76283af8df27d8bda775..f435606b5766719f7203539113d84a2b281b0353 100644 --- a/code/ArkTS1.2/ThreadSafeContainersSample/hvigorfile.ts +++ b/code/ArkTS1.2/ThreadSafeContainersSample/hvigorfile.ts @@ -1,3 +1,29 @@ +/** + * + * Copyright (c) 2025 Huawei Device Co., Ltd. + * + * All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice,this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS, + * + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + import { appTasks } from '@ohos/hvigor-ohos-plugin'; export default { diff --git a/code/ArkTS1.2/ThreadSafeContainersSample/oh-package.json5 b/code/ArkTS1.2/ThreadSafeContainersSample/oh-package.json5 index 7f512c9632708e85a7ea34e739c01b0b8f1ad92e..b6f7090658c0e69d5582af5d236fde4eab954ed6 100644 --- a/code/ArkTS1.2/ThreadSafeContainersSample/oh-package.json5 +++ b/code/ArkTS1.2/ThreadSafeContainersSample/oh-package.json5 @@ -1,5 +1,31 @@ +/** + * + * Copyright (c) 2025 Huawei Device Co., Ltd. + * + * All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice,this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS, + * + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + { - "modelVersion": "5.1.0", + "modelVersion": "5.0.2", "description": "Please describe the basic information.", "dependencies": { }, diff --git a/code/ArkTS1.2/ThreadSafeContainersSample/ohosTest.md b/code/ArkTS1.2/ThreadSafeContainersSample/ohosTest.md new file mode 100644 index 0000000000000000000000000000000000000000..cf67d7311ecff0d3dc28fdb70f2a5d53329ffaa7 --- /dev/null +++ b/code/ArkTS1.2/ThreadSafeContainersSample/ohosTest.md @@ -0,0 +1,22 @@ +# 测试用例 + +## 用例表 + +| 测试功能 | 预置条件 | 输入 | 预期输出 | 是否自动 | 测试结果 | +|-----------------------------------|-------------------------------------|----------------------|------------------------------------|------|------| +| ArrayBlockingQueue容器接口调用验证 | 1. 需在真机测试
2. 构建并安装测试hap
3.启动测试hap| 1、点击ArrayBlockingQueue,场景一:接口调用 | 1、打印显示以下日志:
ArrayBlockingQueue basicScene push: 0,push: 1,push: 2,push: 3,push: 4,push: 5,push: 6,push: 7,push: 8,push: 9,getFirst: 0,getEnd: 9,pop: 0,size: 9,isEmpty: false | 否 | Pass | +| ArrayBlockingQueue容器并发入队验证 | 1. 需在真机测试
2. 构建并安装测试hap
3.启动测试hap| 1、点击ArrayBlockingQueue,场景二:并发入队 | 1、打印显示以下日志:
ArrayBlockingQueue concurrentInQueue push: 0,push: 1,push: 2,push: 3,push: 4,push: 5,push: 6,push: 7,push: 8,push: 9,push: 10,push: 11,push: 12,push: 13,push: 14,push: 15,push: 16,push: 17,push: 18,push: 19,push: 20,push: 21,push: 22,push: 23,push: 24,push: 25,push: 26,push: 27,push: 28,push: 29,push: 30,push: 31,push: 32,push: 33,push: 34,push: 35,push: 36,push: 37,push: 100,push: 38,push: 39,push: 40,push: 101,push: 41,push: 102,push: 42,push: 103,push: 43,push: 104,push: 44,push: 105,push: 45,push: 106,push: 46,push: 107,push: 47,push: 108,push: 48,push: 109,push: 49,push: 110,push: 50,push: 111,push: 51,push: 112,push: 52,push: 113,push: 53,push: 114,push: 54,push: 115,push: 116,push: 55,push: 117,push: 56,push: 57,push: 58,push: 119,push: 59,push: 120,push: 60,push: 121,push: 61,push: 122,push: 62,push: 123,push: 63,push: 124,push: 64,push: 125,push: 65,push: 126,push: 66,undefined,push: 128,push: 67,push: 129,push: 68,push: 130,push: 69,push: 131,push: 70,push: 132,push: 71,push: 133,push: 72,push: 134,push: 73,push: 135,push: 136,push: 74,push: 137,push: 75,push: 138,push: 76,push: 139,push: 77,push: 140,push: 78,push: 141,push: 79,push: 142,push: 80,push: 143,push: 81,push: 144,push: 82,push: 145,push: 83,push: 146,push: 84,push: 147,push: 85,push: 148,push: 86,push: 149,push: 87,push: 150,push: 151,push: 88,push: 152,push: 153,push: 154,push: 89,push: 155,push: 156,push: 90,push: 157,push: 91,undefined,push: 159,push: 92,push: 160,push: 161,push: 93,push: 162,push: 163,push: 94,push: 164,push: 165,push: 95,push: 166,push: 167,push: 96,push: 168,push: 169,push: 170,push: 171,push: 172,push: 173,push: 174,push: 175,push: 176,push: 177,push: 178,push: 179,push: 180,push: 181,push: 182,push: 183,push: 184,push: 185,push: 186,push: 187,push: 188,push: 189,push: 190,push: 191,push: 192,push: 193,push: 194,push: 195,push: 196,push: 197,push: 198,push: 199,push: 97,push: 98,push: 99,测试成功 | 否 | Pass | +| ArrayBlockingQueue容器并发出队验证 | 1. 需在真机测试
2. 构建并安装测试hap
3.启动测试hap| 1、点击ArrayBlockingQueue,场景三:并发出队 | 1、打印显示以下日志:
ArrayBlockingQueue concurrentOutQueue pop: 100,pop: 101,pop: 102,pop: 103,pop: 104,pop: 105,pop: 106,pop: 107,pop: 108,pop: 109,pop: 110,pop: 111,pop: 112,pop: 113,pop: 114,pop: 115,pop: 116,pop: 117,pop: 118,pop: 119,pop: 120,pop: 121,pop: 122,pop: 123,pop: 124,pop: 125,pop: 126,pop: 127,pop: 128,pop: 129,pop: 130,pop: 131,pop: 132,pop: 133,pop: 134,pop: 135,pop: 136,pop: 137,pop: 138,pop: 139,pop: 140,pop: 141,pop: 0,pop: 142,pop: 143,pop: 1,pop: 144,pop: 2,pop: 145,pop: 3,pop: 146,pop: 4,pop: 147,pop: 148,pop: 149,pop: 150,pop: 151,pop: 152,pop: 153,pop: 154,pop: 155,pop: 156,pop: 157,pop: 158,pop: 159,pop: 160,pop: 161,pop: 162,pop: 163,pop: 164,pop: 165,pop: 166,pop: 167,pop: 168,pop: 169,pop: 170,pop: 171,pop: 172,pop: 173,pop: 174,pop: 175,pop: 176,pop: 177,pop: 178,pop: 179,pop: 180,pop: 181,pop: 182,pop: 183,pop: 184,pop: 185,pop: 186,pop: 187,pop: 188,pop: 189,pop: 190,pop: 191,pop: 192,pop: 193,pop: 194,pop: 195,pop: 196,pop: 197,pop: 198,pop: 199,pop: 5,pop: 6,pop: 7,pop: 8,pop: 9,pop: 10,pop: 11,pop: 12,pop: 13,pop: 14,pop: 15,pop: 16,pop: 17,pop: 18,pop: 19,pop: 20,pop: 21,pop: 22,pop: 23,pop: 24,pop: 25,pop: 26,pop: 27,pop: 28,pop: 29,pop: 30,pop: 31,pop: 32,pop: 33,pop: 34,pop: 35,pop: 36,pop: 37,pop: 38,pop: 39,pop: 40,pop: 41,pop: 42,pop: 43,pop: 44,pop: 45,pop: 46,pop: 47,pop: 48,pop: 49,pop: 50,pop: 51,pop: 52,pop: 53,pop: 54,pop: 55,pop: 56,pop: 57,pop: 58,pop: 59,pop: 60,pop: 61,pop: 62,pop: 63,pop: 64,pop: 65,pop: 66,pop: 67,pop: 68,pop: 69,pop: 70,pop: 71,pop: 72,pop: 73,pop: 74,pop: 75,pop: 76,pop: 77,pop: 78,pop: 79,pop: 80,pop: 81,pop: 82,pop: 83,pop: 84,pop: 85,pop: 86,pop: 87,pop: 88,pop: 89,pop: 90,pop: 91,pop: 92,pop: 93,pop: 94,pop: 95,pop: 96,pop: 97,pop: 98,pop: 99,测试成功 | 否 | Pass | +| ArrayBlockingQueue容器队列为空时阻塞验证 | 1. 需在真机测试
2. 构建并安装测试hap
3.启动测试hap| 1、点击ArrayBlockingQueue,场景四:队列为空时阻塞 | 1、打印显示以下日志:
ArrayBlockingQueue nullBlocking pop: 0,push: 0,添加0成功 ,删除0成功 ,push: 1,pop: 1,添加1成功 ,push: 2,添加2成功 ,删除1成功 ,pop: 2,删除2成功 | 否 | Pass | +| ConcurrentSet容器接口调用验证 | 1. 需在真机测试
2. 构建并安装测试hap
3.启动测试hap| 1、点击ConcurrentSet容器,场景一:接口调用 | 1、打印显示以下日志:
add: 0 add: 1 add: 2 add: 3 add: 4 add: 5 add: 6 add: 7 add: 8 add: 9 当前集合: ConcurrentSet[0,1,2,3,4,5,6,7,8,9] 集合存在数据1 size: 10 delete: 2 删除是否成功: true 当前集合: ConcurrentSet[0,1,3,4,5,6,7,8,9] keys: 0 1 3 4 5 6 7 8 9 values: 0 1 3 4 5 6 7 8 9 当前集合: ConcurrentSet[0,1,3,4,5,6,7,8,9] 清空集合后size: 0 | 否 | Pass | +| ConcurrentSet容器并列入队验证 | 1. 需在真机测试
2. 构建并安装测试hap
3.启动测试hap| 1、点击ConcurrentSet容器,场景二:并列入队 | 1、打印显示以下日志:
add: 1 add: 2 add: 3 add: 4 add: 5 add: 6 add: 7 add: 8 add: 9 add: 10 add: 11 add: 12 add: 13 add: 14 add: 15 add: 16 add: 17 add: 18 add: 19 add: 20 add: 21 add: 22 add: 23 add: 24 add: 25 add: 26 add: 27 add: 28 add: 29 add: 30 add: 31 add: 32 add: 33 add: 34 add: 35 add: 36 add: 37 add: 38 add: 39 add: 40 add: 41 add: 42 add: 43 add: 44 add: 45 add: 46 add: 47 add: 48 add: 49 add: 50 add: 51 add: 52 add: 53 add: 54 add: 55 add: 56 add: 57 add: 58 add: 59 add: 60 add: 61 add: 62 add: 63 add: 64 add: 65 add: 66 add: 67 add: 68 add: 69 add: 70 add: 71 add: 72 add: 73 add: 74 add: 75 add: 76 add: 77 add: 78 add: 79 add: 80 add: 81 add: 82 add: 83 add: 84 add: 85 add: 86 add: 87 add: 88 add: 89 add: 90 add: 91 add: 92 add: 93 add: 94 add: 95 add: 96 add: 97 add: 98 add: 99 add: 100 add: 101 add: 102 add: 103 add: 104 add: 105 add: 106 add: 107 add: 108 add: 109 add: 110 add: 111 add: 112 add: 113 add: 114 add: 115 add: 116 add: 117 add: 118 add: 119 add: 120 add: 121 add: 122 add: 123 add: 124 add: 125 add: 126 add: 127 add: 128 add: 129 add: 130 add: 131 add: 132 add: 133 add: 134 add: 135 add: 136 add: 137 add: 138 add: 139 add: 140 add: 141 add: 142 add: 143 add: 144 add: 145 add: 146 add: 147 add: 148 add: 149 add: 150 add: 151 add: 152 add: 153 add: 154 add: 155 add: 156 add: 157 add: 158 add: 159 add: 160 add: 161 add: 162 add: 163 add: 164 add: 165 add: 166 add: 167 add: 168 add: 169 add: 170 add: 171 add: 172 add: 173 add: 174 add: 175 add: 176 add: 177 add: 178 add: 179 add: 180 add: 181 add: 182 add: 183 add: 184 add: 185 add: 186 add: 187 add: 188 add: 189 add: 190 add: 191 add: 192 add: 193 add: 194 add: 195 add: 196 add: 197 add: 198 add: 199 测试成功 | 否 | Pass | +| ConcurrentSet容器并列出队验证 | 1. 需在真机测试
2. 构建并安装测试hap
3.启动测试hap| 1、点击ConcurrentSet容器,场景三:并列出队 | 1、打印显示以下日志:
delete: 1 delete: 2 delete: 3 delete: 4 delete: 5 delete: 6 delete: 7 delete: 8 delete: 9 delete: 10 delete: 11 delete: 12 delete: 13 delete: 14 delete: 15 delete: 16 delete: 17 delete: 18 delete: 19 delete: 20 delete: 21 delete: 22 delete: 23 delete: 24 delete: 25 delete: 26 delete: 27 delete: 28 delete: 29 delete: 30 delete: 31 delete: 32 delete: 33 delete: 34 delete: 35 delete: 36 delete: 37 delete: 38 delete: 39 delete: 40 delete: 41 delete: 42 delete: 43 delete: 44 delete: 45 delete: 46 delete: 47 delete: 48 delete: 49 delete: 50 delete: 51 delete: 52 delete: 53 delete: 54 delete: 55 delete: 56 delete: 57 delete: 58 delete: 59 delete: 60 delete: 61 delete: 62 delete: 63 delete: 64 delete: 65 delete: 66 delete: 67 delete: 68 delete: 69 delete: 70 delete: 71 delete: 72 delete: 73 delete: 74 delete: 75 delete: 76 delete: 77 delete: 78 delete: 79 delete: 80 delete: 81 delete: 82 delete: 83 delete: 84 delete: 85 delete: 86 delete: 87 delete: 88 delete: 89 delete: 90 delete: 91 delete: 92 delete: 93 delete: 94 delete: 95 delete: 96 delete: 97 delete: 98 delete: 99 delete: 100 delete: 101 delete: 102 delete: 103 delete: 104 delete: 105 delete: 106 delete: 107 delete: 108 delete: 109 delete: 110 delete: 111 delete: 112 delete: 113 delete: 114 delete: 115 delete: 116 delete: 117 delete: 118 delete: 119 delete: 120 delete: 121 delete: 122 delete: 123 delete: 124 delete: 125 delete: 126 delete: 127 delete: 128 delete: 129 delete: 130 delete: 131 delete: 132 delete: 133 delete: 134 delete: 135 delete: 136 delete: 137 delete: 138 delete: 139 delete: 140 delete: 141 delete: 142 delete: 143 delete: 144 delete: 145 delete: 146 delete: 147 delete: 148 delete: 149 delete: 150 delete: 151 delete: 152 delete: 153 delete: 154 delete: 155 delete: 156 delete: 157 delete: 158 delete: 159 delete: 160 delete: 161 delete: 162 delete: 163 delete: 164 delete: 165 delete: 166 delete: 167 delete: 168 delete: 169 delete: 170 delete: 171 delete: 172 delete: 173 delete: 174 delete: 175 delete: 176 delete: 177 delete: 178 delete: 179 delete: 180 delete: 181 delete: 182 delete: 183 delete: 184 delete: 185 delete: 186 delete: 187 delete: 188 delete: 189 delete: 190 delete: 191 delete: 192 delete: 193 delete: 194 delete: 195 delete: 196 delete: 197 delete: 198 delete: 199 测试成功 | 否 | Pass | +| ConcurrentHashMap容器接口调用验证 | 1. 需在真机测试
2. 构建并安装测试hap
3.启动测试hap| 1、点击ConcurrentHashMap容器,场景一:接口调用 | 1、打印显示以下日志:
ConcurrentHashMap basicScene set: k0, v0,set: k1, v1,set: k2, v2,set: k3, v3,set: k4, v4,set: k5, v5,set: k6, v6,set: k7, v7,set: k8, v 8,set: k9, v9,当前map: 0,v0,1,v1,2,v2,3,v3,4,v4,5,v5,6,v6,7,v7,8,v 8,9,v9,get key=: 1 value=: v1,key: 2 是否存在: true,keys: 0 1 2 3 4 5 6 7 8 9 ,values: v0 v1 v2 v3 v4 v5 v6 v7 v 8 v9 ,entries: 0,v0 1,v1 2,v2 3,v3 4,v4 5,v5 6,v6 7,v7 8,v 8 9,v9 ,delete key= 3,删除成功,remove key= 4,移除成功,replace key= 5,替换成功,hashMapInt通过clear方法清空,hashMapInt.size = 0 | 否 | Pass | +| ConcurrentHashMap容器并列入队验证 | 1. 需在真机测试
2. 构建并安装测试hap
3.启动测试hap| 1、点击ConcurrentHashMap容器,场景二:并列入队 | 1、打印显示以下日志:
ConcurrentHashMap concurrentInQueue set key: 0 value: v0,set key: 1 value: v1,set key: 2 value: v2,set key: 3 value: v3,set key: 4 value: v4,set key: 5 value: v5,set key: 50 value: v50,set key: 6 value: v6,set key: 51 value: v51,set key: 7 value: v7,set key: 52 value: v52,set key: 8 value: v 8,set key: 53 value: v53,set key: 9 value: v9,set key: 54 value: v54,set key: 10 value: v10,set key: 11 value: v11,set key: 12 value: v12,set key: 13 value: v13,set key: 14 value: v14,set key: 15 value: v15,set key: 16 value: v16,set key: 17 value: v17,set key: 18 value: v18,set key: 19 value: v19,set key: 20 value: v20,set key: 21 value: v21,set key: 22 value: v22,set key: 23 value: v23,set key: 24 value: v24,set key: 25 value: v25,set key: 26 value: v26,set key: 27 value: v27,set key: 28 value: v28,set key: 55 value: v55,set key: 29 value: v29,set key: 56 value: v56,set key: 30 value: v30,set key: 57 value: v57,set key: 31 value: v31,set key: 58 value: v58,set key: 32 value: v32,set key: 59 value: v59,set key: 33 value: v33,set key: 60 value: v60,set key: 34 value: v34,set key: 61 value: v61,set key: 35 value: v35,set key: 62 value: v62,set key: 36 value: v36,set key: 63 value: v63,set key: 37 value: v37,set key: 64 value: v64,set key: 38 value: v38,set key: 65 value: v65,set key: 39 value: v39,set key: 66 value: v66,set key: 40 value: v40,set key: 67 value: v67,set key: 41 value: v41,set key: 68 value: v68,set key: 69 value: v69,set key: 42 value: v42,set key: 43 value: v43,set key: 44 value: v44,set key: 45 value: v45,set key: 46 value: v46,set key: 47 value: v47,set key: 48 value: v48,set key: 49 value: v49,set key: 70 value: v70,set key: 71 value: v71,set key: 72 value: v72,set key: 73 value: v73,set key: 74 value: v74,set key: 75 value: v75,set key: 76 value: v76,set key: 77 value: v77,set key: 78 value: v78,set key: 79 value: v79,set key: 80 value: v 80,set key: 81 value: v 81,set key: 82 value: v 82,set key: 83 value: v 83,set key: 84 value: v 84,set key: 85 value: v 85,set key: 86 value: v 86,set key: 87 value: v 87,set key: 88 value: v 88,set key: 89 value: v 89,set key: 90 value: v90,set key: 91 value: v91,set key: 92 value: v92,set key: 93 value: v93,set key: 94 value: v94,set key: 95 value: v95,set key: 96 value: v96,set key: 97 value: v97,set key: 98 value: v98,set key: 99 value: v99,测试成功 | 否 | Pass | +| ConcurrentHashMap容器并列出队验证 | 1. 需在真机测试
2. 构建并安装测试hap
3.启动测试hap| 1、点击ConcurrentHashMap容器,场景三:并列出队 | 1、打印显示以下日志:
ConcurrentHashMap concurrentOutQueue delete key: 0 成功,delete key: 1 成功,delete key: 2 成功,delete key: 3 成功,delete key: 4 成功,delete key: 5 成功,delete key: 6 成功,delete key: 7 成功,delete key: 8 成功,delete key: 9 成功,delete key: 10 成功,delete key: 50 成功,delete key: 11 成功,delete key: 51 成功,delete key: 12 成功,delete key: 52 成功,delete key: 13 成功,delete key: 53 成功,delete key: 14 成功,delete key: 54 成功,delete key: 15 成功,delete key: 55 成功,delete key: 16 成功,delete key: 56 成功,delete key: 57 成功,delete key: 17 成功,delete key: 18 成功,delete key: 19 成功,delete key: 58 成功,delete key: 20 成功,delete key: 21 成功,delete key: 59 成功,delete key: 22 成功,delete key: 60 成功,delete key: 23 成功,delete key: 61 成功,delete key: 24 成功,delete key: 62 成功,delete key: 25 成功,undefined,delete key: 26 成功,delete key: 64 成功,delete key: 27 成功,delete key: 65 成功,delete key: 66 成功,delete key: 28 成功,delete key: 67 成功,delete key: 29 成功,delete key: 68 成功,delete key: 30 成功,delete key: 69 成功,delete key: 31 成功,delete key: 70 成功,delete key: 32 成功,delete key: 71 成功,delete key: 33 成功,delete key: 72 成功,delete key: 34 成功,delete key: 73 成功,delete key: 35 成功,delete key: 74 成功,delete key: 36 成功,delete key: 75 成功,delete key: 37 成功,delete key: 76 成功,delete key: 38 成功,delete key: 77 成功,delete key: 39 成功,delete key: 78 成功,delete key: 40 成功,delete key: 79 成功,delete key: 41 成功,delete key: 80 成功,delete key: 42 成功,delete key: 81 成功,delete key: 43 成功,delete key: 82 成功,delete key: 44 成功,delete key: 83 成功,undefined,delete key: 84 成功,delete key: 46 成功,delete key: 47 成功,delete key: 48 成功,delete key: 49 成功,delete key: 85 成功,delete key: 86 成功,delete key: 87 成功,delete key: 88 成功,delete key: 89 成功,delete key: 90 成功,delete key: 91 成功,delete key: 92 成功,delete key: 93 成功,delete key: 94 成功,delete key: 95 成功,delete key: 96 成功,delete key: 97 成功,delete key: 98 成功,delete key: 99 成功,测试成功 | 否 | Pass | +| ConcurrentHashMap容器Benchmark验证 | 1. 需在真机测试
2. 构建并安装测试hap
3.启动测试hap| 1、点击ConcurrentHashMap容器,场景四:Benchmark | 1、打印显示以下日志:
ConcurrentHashMap hashMapBenchmark 测试成功 吞吐量: 6 tasks in 247ms | 否 | Pass | +| LinkedBlockingQueue容器接口调用验证 | 1. 需在真机测试
2. 构建并安装测试hap
3.启动测试hap| 1、点击LinkedBlockingQueue容器,场景一:接口调用 | 1、打印显示以下日志:
LinkedBlockingQueueTest basicScene 队列capacity: 10,push: 0,push: 1,push: 2,push: 3,push: 4,push: 5,push: 6,push: 7,push: 8,push: 9,pop: 0,add: 10,poll: 1,size: 9,remainingCapacity: 1 | 否 | Pass | +| LinkedBlockingQueue容器并列入队验证 | 1. 需在真机测试
2. 构建并安装测试hap
3.启动测试hap| 1、点击LinkedBlockingQueue容器,场景二:并列入队 | 1、打印显示以下日志:
LinkedBlockingQueueTest concurrentInQueue push: 0,push: 1,push: 2,push: 3,push: 4,push: 5,push: 6,push: 7,push: 8,push: 9,push: 10,push: 11,push: 100,push: 12,push: 101,push: 13,push: 102,push: 14,push: 103,push: 15,push: 104,push: 16,push: 105,push: 17,push: 106,push: 18,push: 107,push: 19,push: 108,push: 20,push: 109,push: 21,push: 110,push: 22,push: 111,push: 23,push: 112,push: 24,push: 113,push: 25,push: 114,push: 26,push: 115,push: 27,push: 116,push: 28,push: 117,push: 29,push: 118,push: 30,push: 119,push: 31,push: 120,push: 32,push: 121,push: 33,push: 122,push: 34,push: 123,push: 35,push: 124,push: 36,push: 125,push: 37,push: 126,push: 38,push: 127,push: 39,push: 128,push: 40,push: 129,push: 41,push: 130,push: 42,push: 131,push: 43,push: 132,push: 44,push: 133,undefined,push: 134,push: 46,push: 135,push: 47,push: 136,push: 137,push: 138,push: 48,push: 49,push: 50,push: 140,push: 51,push: 141,push: 142,push: 143,push: 144,push: 145,push: 146,push: 147,push: 148,push: 149,push: 150,push: 151,push: 152,push: 153,push: 154,push: 155,push: 156,push: 157,push: 158,push: 159,push: 160,push: 161,push: 162,push: 163,push: 164,push: 165,push: 166,push: 167,push: 168,push: 169,push: 52,push: 170,push: 171,push: 53,push: 172,push: 54,push: 173,push: 55,push: 174,push: 56,push: 175,push: 57,push: 176,push: 58,push: 177,push: 59,push: 178,push: 60,push: 61,push: 179,push: 62,push: 180,push: 63,push: 181,push: 64,push: 182,push: 65,push: 183,push: 66,push: 184,push: 67,push: 185,push: 68,push: 186,push: 69,push: 187,push: 70,push: 71,undefined,push: 189,push: 73,push: 190,push: 74,push: 191,push: 75,push: 192,push: 76,push: 193,push: 77,push: 194,push: 78,push: 79,push: 195,push: 80,push: 196,push: 81,push: 197,push: 82,push: 198,push: 83,push: 199,push: 84,push: 85,push: 86,push: 87,push: 88,push: 89,push: 90,push: 91,push: 92,push: 93,push: 94,push: 95,push: 96,push: 97,push: 98,push: 99,测试成功 | 否 | Pass | +| LinkedBlockingQueue容器并列出队验证 | 1. 需在真机测试
2. 构建并安装测试hap
3.启动测试hap| 1、点击LinkedBlockingQueue容器,场景三:并列出队 | 1、打印显示以下日志:
LinkedBlockingQueueTest concurrentOutQueuepop: 0,pop: 1,pop: 100,pop: 101,pop: 102,pop: 103,pop: 104,pop: 105,pop: 106,pop: 107,pop: 108,pop: 109,pop: 110,pop: 111,pop: 112,pop: 113,pop: 114,pop: 115,pop: 116,pop: 117,pop: 118,pop: 119,pop: 120,pop: 121,pop: 122,pop: 123,pop: 124,pop: 125,pop: 126,pop: 127,pop: 128,pop: 129,pop: 130,pop: 131,pop: 132,pop: 133,pop: 134,pop: 135,pop: 136,pop: 137,pop: 138,pop: 139,pop: 2,pop: 140,pop: 3,pop: 141,pop: 4,pop: 142,pop: 5,pop: 143,pop: 6,pop: 144,pop: 7,pop: 145,pop: 8,pop: 146,pop: 9,pop: 147,pop: 10,pop: 148,pop: 11,pop: 12,pop: 149,pop: 13,pop: 150,pop: 14,pop: 151,pop: 15,pop: 152,pop: 16,pop: 153,pop: 17,pop: 154,pop: 18,pop: 155,pop: 19,pop: 156,pop: 20,pop: 157,undefined,pop: 158,pop: 22,pop: 159,pop: 23,pop: 160,pop: 24,pop: 161,pop: 25,pop: 162,pop: 26,pop: 163,pop: 27,pop: 164,pop: 28,pop: 165,pop: 29,pop: 166,pop: 30,pop: 167,pop: 31,pop: 168,pop: 32,pop: 169,pop: 33,pop: 170,pop: 34,pop: 171,pop: 35,pop: 172,pop: 36,pop: 173,pop: 37,pop: 174,pop: 38,pop: 175,pop: 39,pop: 176,pop: 40,pop: 177,pop: 41,pop: 178,pop: 42,pop: 179,pop: 43,pop: 180,pop: 44,undefined,pop: 182,pop: 45,pop: 183,pop: 46,pop: 184,pop: 47,pop: 185,pop: 48,pop: 186,pop: 49,pop: 187,pop: 50,pop: 188,pop: 51,pop: 189,pop: 52,pop: 190,pop: 53,pop: 191,pop: 54,pop: 192,pop: 55,pop: 193,pop: 56,pop: 194,pop: 57,pop: 195,pop: 58,pop: 196,pop: 59,pop: 197,pop: 60,undefined,pop: 61,pop: 199,pop: 62,pop: 63,pop: 64,pop: 65,pop: 66,pop: 67,pop: 68,pop: 69,pop: 70,pop: 71,pop: 72,pop: 73,pop: 74,pop: 75,pop: 76,pop: 77,pop: 78,pop: 79,pop: 80,pop: 81,pop: 82,pop: 83,pop: 84,pop: 85,pop: 86,pop: 87,pop: 88,pop: 89,pop: 90,pop: 91,pop: 92,pop: 93,pop: 94,pop: 95,pop: 96,pop: 97,pop: 98,pop: 99,测试成功 | 否 | Pass | +| LinkedBlockingQueue容器队列为满时阻塞验证 | 1. 需在真机测试
2. 构建并安装测试hap
3.启动测试hap| 1、点击LinkedBlockingQueue容器,场景四:队列为满时阻塞 | 1、打印显示以下日志:
LinkedBlockingQueueTest fullBlocking push: 0,pop: 0,删除成功,pop: 1,删除成功,pop: 2,删除成功,pop: 3,添加成功,push: 1,添加成功,push: 2,添加成功,删除成功,pop: 4,删除成功,pop: 5,删除成功 | 否 | Pass | +| LinkedBlockingQueue容器队列为空时阻塞验证 | 1. 需在真机测试
2. 构建并安装测试hap
3.启动测试hap| 1、点击LinkedBlockingQueue容器,场景五:队列为空时阻塞 | 1、打印显示以下日志:
LinkedBlockingQueueTest nullBlocking pop: 0,push: 0,添加成功,push: 1,添加成功,push: 2,添加成功,删除成功,pop: 1,删除成功,pop: 2,删除成功 | 否 | Pass | \ No newline at end of file